コード例 #1
0
    def execute(self, context):
        obj = context.object

        for m in obj.modifiers:
            if m.type == 'ARMATURE':
                rig = m.object
                if rig in context.selected_objects:
                    bone_name = rig.data.bones.active.name
                    bone = rig.pose.bones[bone_name]
                    break

        keys = obj.data.shape_keys.key_blocks

        if self.active:
            key = obj.active_shape_key
            if key == keys[0]:
                self.report({'WARNING'}, "Active shape key is the base shape")
                return {'CANCELLED'}

            if Get.driver(key, 'value'):
                key.driver_remove('value')
            # if key.name in bone:
            # del bone[key.name]

            self.add_driver(key, bone)
        else:
            for key in keys[1:]:
                if (self.filter and (self.filter not in key.name)):
                    continue
                if not (self.overwrite or (not Get.driver(key, 'value'))):
                    continue

                self.add_driver(key, bone)

        return {'FINISHED'}
コード例 #2
0
    def execute(self, context):
        obj = context.object

        in_edit = (obj.mode == 'EDIT')
        if in_edit:
            Set.mode(context, 'OBJECT')

        active = obj.active_shape_key
        vg = active.vertex_group
        index = obj.active_shape_key_index
        pin = obj.show_only_shape_key

        obj.show_only_shape_key = True
        active.vertex_group = ''
        bpy.ops.object.shape_key_add(from_mix=True)
        while obj.active_shape_key_index > (index + 1):
            bpy.ops.object.shape_key_move(type='UP')
        active.vertex_group = vg

        shape = obj.active_shape_key

        if self.mirror:
            shape.name = utils.flip_name(active.name)
            shape.vertex_group = utils.flip_name(vg)
        else:
            shape.vertex_group = vg
            shape.name = active.name

        for var in ('interpolation', 'mute', 'relative_key', 'slider_max',
                    'slider_min', 'value'):
            setattr(shape, var, getattr(active, var))
            driver = Get.driver(active, var)
            if not driver:
                continue
            newdriver = utils.copy_driver(driver, shape, var)

            if self.mirror:
                for v in newdriver.driver.variables:
                    for t in v.targets:
                        t.bone_target = utils.flip_name(t.bone_target)

        # obj.active_shape_key_index = index
        obj.show_only_shape_key = pin

        if in_edit:
            Set.mode(context, 'EDIT')

        return {'FINISHED'}
コード例 #3
0
ファイル: shapenonsense.py プロジェクト: Irmitya/zpy__mods
def transfer_by_pos2(context, obj, selobj, ob_key, precision, scale):
    if (selobj.data.shape_keys is None):
        selobj.shape_key_add(from_mix=False).name = "Basis"
        selobj.active_shape_key_index = 0

    selobj_keys = selobj.data.shape_keys.key_blocks

    index = 0
    for (del_index, del_key) in enumerate(selobj_keys):
        if ob_key.name == del_key.name:
            index = del_index
            selobj.shape_key_remove(del_key)
            break
    selob_key = selobj.shape_key_add(name=ob_key.name, from_mix=False)

    if index:
        active = selobj.active_shape_key_index
        selobj.active_shape_key_index = len(selobj_keys) - 1
        while selobj_keys[index] != selob_key:
            bpy.ops.object.shape_key_move((dict(object=selobj)), type='UP')
        selobj.active_shape_key_index = active

    # find matches using a rounding precision of 5 and a scale of 1

    ddict = find_doubles_with_other(obj, selobj)
    nomatch = ddict[1]
    ddict = ddict[0]
    copydoublesco(ob_key, selob_key, ddict)

    # set precision and scale to use for the second pass

    # generates a dictionary which archives verts in active obj:matching vert in selected obj
    pass2 = generate_rounded_pos_dict(obj, precision, scale)
    round_pass(obj, selobj, ob_key, selob_key, nomatch, scale, precision, pass2)

    selob_key.vertex_group = ob_key.vertex_group
    selob_key.relative_key = ob_key.relative_key
    selob_key.slider_min = ob_key.slider_min
    selob_key.slider_max = ob_key.slider_max
    selob_key.value = ob_key.value

    driver = Get.driver(ob_key, 'value')
    if driver:
        utils.copy_driver(driver, selob_key, 'value')
コード例 #4
0
ファイル: apply_shapekey.py プロジェクト: Irmitya/zpy_general
    def execute(self, context):
        obj = context.object

        in_edit = (obj.mode == 'EDIT')
        if in_edit:
            Set.mode(context, 'OBJECT')

        active = obj.active_shape_key
        index = obj.active_shape_key_index
        pin = obj.show_only_shape_key

        obj.show_only_shape_key = True
        bpy.ops.object.shape_key_add(from_mix=True)
        while obj.active_shape_key_index > (index + 1):
            bpy.ops.object.shape_key_move(type='UP')

        shape = obj.active_shape_key
        for var in ('interpolation', 'mute', 'relative_key', 'slider_max', 'slider_min', 'value'):
            setattr(shape, var, getattr(active, var))
        # if self.keep_vertex_group:
            # shape.vertex_group = active.vertex_group

        # Redirect drivers, so that they auto update after operation
        for var in ('slider_max', 'slider_min', 'value'):
            driver = Get.driver(active, var)
            if driver:
                driver.data_path = f'key_blocks["{shape.name}"].{var}'
                # driver.data_path = driver.data_path.replace(active.name, shape.name, 1)

        name = active.name
        active.name += ".masked"
        shape.name = name

        obj.active_shape_key_index = index
        bpy.ops.object.shape_key_remove(all=False)
        obj.active_shape_key_index = index
        obj.show_only_shape_key = pin

        if in_edit:
            Set.mode(context, 'EDIT')

        return {'FINISHED'}
コード例 #5
0
ファイル: Set.py プロジェクト: Irmitya/zpy
    def set_influence():
        def get_src(constraint):
            for src in srcs:
                for con in src.constraints:
                    if con == constraint:
                        return src

        for constraint in constraints:
            driver = Get.driver(constraint, 'influence')
            if driver:
                (constraint, prop) = Get.controls_from_driver(driver)[0]
                src = constraint
            else:
                prop = 'influence'
                src = get_src(constraint)

                if src is None:
                    continue

            if influence is None:
                # mode = 'apply'
                pass
            elif influence is False:
                # mode = 'invert'
                setattr(constraint, prop, not getattr(constraint, prop))
            else:
                # mode = 'set'
                setattr(constraint, prop, influence)

            # key_con = prefs.get("Keyframe Constraints Inf")
            # if key_con:
            if insert_key:
                keyframe.prop(
                    context,
                    constraint,
                    prop,
                    group=keyframe.group_name(src),
                    options={'INSERTKEY_NEEDED'},
                )
コード例 #6
0
ファイル: New.py プロジェクト: Irmitya/zpy
def driver(src, path, **kargs):
    driver_type = kargs.get('driver_type', None)
    # 'AVERAGE', 'Sum Values', 'SCRIPTED', 'Minimum Value', 'Maximum Value
    expression = kargs.get('expression', None)
    frames = kargs.get('frames', list())  # keyframe.co for the driver's fcurve
    name = kargs.get('name', "var")  # Name of the variable added to the driver
    overwrite = kargs.get('overwrite', False)  # Delete the existing driver
    rotation_mode = kargs.get('rotation_mode', 'AUTO')
    target = kargs.get('target', None)
    target_path = kargs.get('target_path', '')
    transform_space = kargs.get('transform_space', 'LOCAL_SPACE')
    transform_type = kargs.get('transform_type', 'LOC_X')
    var_type = kargs.get('var_type', None)
    # 'SINGLE_PROP', 'TRANSFORMS', 'Rotational Difference', 'Distance'
    if var_type is None:
        if target and (not target_path):
            var_type = 'TRANSFORMS'
        else:
            var_type = 'SINGLE_PROP'

    Driver = Get.driver(src, path)

    if not Driver:
        Driver = src.driver_add(path)
        overwrite = True

    if overwrite:
        while Driver.keyframe_points:
            Driver.keyframe_points.remove(Driver.keyframe_points[0])

    if frames:
        if overwrite:
            Driver.extrapolation = 'LINEAR'
            while Driver.modifiers:
                Driver.modifiers.remove(Driver.modifiers[0])
        Driver.keyframe_points.add(len(frames))
        for key, co in zip(Driver.keyframe_points[:], frames):
            key.interpolation = 'LINEAR'
            key.co = co

    driver = Driver.driver

    if overwrite:
        if (expression is None):
            if (driver_type is None):
                driver_type = 'AVERAGE'
            elif (driver.type == 'SCRIPTED'):
                driver.expression = name

        while driver.variables:
            driver.variables.remove(driver.variables[0])

    if expression is not None:
        driver.expression = expression
    if driver_type:
        driver.type = driver_type

    var = driver.variables.new()
    var.name = name
    var.type = var_type
    var_target = var.targets[0]

    if target:
        is_pose = Is.posebone(target)
        is_bone = Is.bone(target) or Is.editbone(target)
        is_obj = Is.object(target)

        if is_obj:
            var_target.id = target
        elif (is_pose or is_bone):
            var_target.id = target.id_data
            var_target.bone_target = target.name
            if target_path and (not target_path.startswith(
                ('pose.bones', 'bones'))):
                if is_pose:
                    text = f'pose.bones["{target.name}"]'
                else:
                    text = f'bones["{target.name}"]'

                if (target_path[0] != '['):
                    text += '.'

                target_path = text + target_path
        else:
            try:
                var_target.id = target
            except:
                var_target.id = target.id_data

    var_target.data_path = target_path
    var_target.rotation_mode = rotation_mode
    var_target.transform_space = transform_space
    var_target.transform_type = transform_type

    return Driver
コード例 #7
0
    def pose_func(self, context, bone):
        rig = bone.id_data
        bones = rig.pose.bones
        bbones = ('bbone', 'bbone_start', 'bbone_end', 'bbone_head',
                  'bbone_in', 'bbone_out')

        def parse(bbone):
            self.bones[bbone] = rig

            # Prep the widget for removal
            wgt = bbone.custom_shape
            if wgt:
                self.widgets.add(wgt)

            # Remove stretch constraints
            for con in list(bone.constraints):
                if (getattr(con, 'target', None) == rig) and (con.subtarget
                                                              == bbone.name):
                    bone.constraints.remove(con)

            # Remove Custom Handle
            if bone.bbone_custom_handle_start == bbone:
                bone.bone.bbone_custom_handle_start = None
                bone.bone.bbone_handle_type_start = 'AUTO'
            if bone.bbone_custom_handle_end == bbone:
                bone.bone.bbone_custom_handle_end = None
                bone.bone.bbone_handle_type_end = 'AUTO'

        for bbone_name in bbones:
            bbone = bones.get(get_name(bone, bbone_name))
            if not bbone:
                continue

            parse(bbone)

            if bbone_name in ('bbone_start',
                              'bbone_end') and Get.mirror_bone(bbone):
                (prefix, replace, suffix,
                 number) = utils.flip_name(bone.name, only_split=True)
                center_name = prefix + suffix + number
                center = bones.get(center_name)
                if center:
                    parse(center)

            # Remove BBone drivers
            if bbone_name in ('bbone_in', 'bbone_out'):
                in_out = bbone_name.split('_')[1]
                in_outs = (
                    f'bbone_curve{in_out}x',
                    f'bbone_curve{in_out}y',
                    f'bbone_roll{in_out}',
                    f'bbone_scale{in_out}x',
                    f'bbone_scale{in_out}y',
                    f'bbone_ease{in_out}',
                )
                for bbone_in_out in in_outs:
                    Driver = Get.driver(bone, bbone_in_out)
                    try:
                        target = Driver.driver.variables[0].targets[0]
                    except:
                        continue
                    if (target.bone_target == bbone.name) and (target.id
                                                               == rig):
                        bone.driver_remove(bbone_in_out)