コード例 #1
0
    def execute(self, context):
        def close_rigify(meta):
            """Close Rigify Layers panel by default"""
            text = meta.data.rigify_rig_ui
            for (index, line) in enumerate(text.lines):
                if (line.body == "class RigLayers(bpy.types.Panel):"):
                    text.cursor_set(index + 6)  # bl_category = 'Item'
                    text.write("    bl_options = {'DEFAULT_CLOSED'}\n")
                    return True

        for daz in context.selected_objects:
            (meta, daz) = self.poll_parse(context, daz)
            if None in (meta, daz):
                continue

            layers_extra = get_layers_extra(meta)

            Set.active(context, meta)
            bpy.ops.pose.rigify_generate()
            close_rigify(meta)

            rig = context.active_object
            meta_to_rigify(daz, rig)

            adjust_rigify(rig)
            set_layers_extra(rig, layers_extra)

        return {'FINISHED'}
コード例 #2
0
    def execute(self, context):
        active = Get.active(context)
        active_select = Is.selected(active)
        selected = Get.selected(context)
        is_pose = bool(context.mode == 'POSE')
        arg = dict(only_selected=bool(selected))

        if is_pose:
            clear = bpy.ops.pose.paths_clear

            # Operator only runs on active rig, so repeat for all in pose mode
            if selected:
                obs = {b.id_data for b in selected}
            else:
                obs = {ob for ob in Get.objects(context) if ob.mode == 'POSE'}

            for ob in obs:
                Set.active(context, ob)
                clear(**arg)
            else:
                Set.active(context, active)
                Set.select(active, active_select)
        elif bpy.ops.object.paths_clear.poll(context.copy()):
            bpy.ops.object.paths_clear(**arg)

        return {'FINISHED'}
コード例 #3
0
    def execute(self, context):
        (rig, mesh) = self.get_rig_mesh(context)

        # Switch to object mode first, to prevent sending rig to empty mode
        # happens when switching from Armature Edit to Weight Paint (warning but no error)
        # from there, trying to change the armature mode gives error after fixing the mode
        bpy.ops.object.mode_set(mode='OBJECT')

        if self.mode == 'POSE':
            Set.active(context, rig)
            return bpy.ops.object.mode_set(mode='POSE')
        elif self.mode == 'EDIT_ARMATURE':
            Set.active(context, rig)
            return bpy.ops.object.mode_set(mode='EDIT')
        elif self.mode == 'WEIGHT_PAINT':
            Set.active(context, mesh)
            return bpy.ops.object.mode_set(mode='WEIGHT_PAINT')
        elif self.mode == 'EDIT_MESH':
            Set.active(context, mesh)
            return bpy.ops.object.mode_set(mode='EDIT')
        elif self.mode == 'SCULPT':
            Set.active(context, mesh)
            return bpy.ops.object.mode_set(mode='SCULPT')

        return {'PASS_THROUGH'}
コード例 #4
0
ファイル: mainFunctions.py プロジェクト: Irmitya/zpy__mods
def returnToArmature(context, widget):
    bone = fromWidgetFindBone(widget)
    armature = bone.id_data

    # Unhide collection if it was hidden in a previous run
    if widget.users_collection:  # Try to use the widget's collection
        collection = widget.users_collection[0]
    else:  # otherwise use default
        collection = get_collection(context)
    Set.visible(context, collection)

    Set.mode(context, 'OBJECT', widget)

    # collection = get_collection(context)
    if [x for x in armature.users_collection if x != collection]:
        # Don't hide the active collection
        collection.hide_viewport = True
    "New version doesn't have this"
    # get_collection_view_layer(collection).hide_viewport = True

    if getattr(context.space_data, 'local_view', None):
        bpy.ops.view3d.localview()

    Set.active(context, armature)
    Set.select(armature, True)

    Set.mode(context, 'POSE', armature)
    # Set.select(bone, True)
    # Set.active(context, bone)
    armature.data.bones[bone.name].select = True
    armature.data.bones.active = armature.data.bones[bone.name]
コード例 #5
0
ファイル: mainFunctions.py プロジェクト: Irmitya/zpy__mods
def editWidget(context, active_bone):
    widget = active_bone.custom_shape

    armature = active_bone.id_data
    Set.mode(context, 'OBJECT', armature)
    Set.select(armature, False)
    '''  # 2.7
    if context.space_data.lock_camera_and_layers == False :
        visibleLayers = numpy.array(bpy.context.space_data.layers)+widget.layers-armature.layers
        bpy.context.space_data.layers = visibleLayers.tolist()

    else :
        visibleLayers = numpy.array(bpy.context.scene.layers)+widget.layers-armature.layers
        bpy.context.scene.layers = visibleLayers.tolist()
    '''

    "Commented because the new version only uses get_collection"
    if widget.users_collection:
        collection = widget.users_collection[0]
    else:
        collection = get_collection(context)
        collection.objects.link(widget)
    Set.in_scene(context, collection)
    Set.visible(context, collection)
    "Commented because new version uses operator"
    # get_collection_view_layer(collection).hide_viewport = False
    if getattr(context.space_data, 'local_view', None):
        bpy.ops.view3d.localview()

    # select object and make it active
    Set.select(widget, True)
    Set.active(context, widget)
    Set.mode(context, 'EDIT', widget)
コード例 #6
0
 def invoke(self, context, event):
     if event.alt:
         active = context.object
         for ob in context.selected_objects:
             if Is.mesh(ob) and (ob.data.shape_keys):
                 Set.active(context, ob)
                 self.purge(ob)
         Set.active(context, active)
     else:
         self.purge(context.object)
     return {'FINISHED'}
コード例 #7
0
    def execute(self, context):
        rig = context.active_object
        active = None

        if context.mode == 'PAINT_WEIGHT':
            for obj in context.selected_objects:
                if obj.type == 'ARMATURE':
                    active = rig
                    rig = obj
                    Set.active(context, rig)

        # if rig.type =='ARMATURE' and context.mode != 'EDIT_ARMATURE':

        meshes = self.get_meshes(context, rig)

        bone1 = context.active_pose_bone
        # bone2 = context.selected_pose_bones[1]
        for bone2 in context.selected_pose_bones:
            if bone2 == bone1:
                continue
            found = None

            for mesh in meshes:
                utils.merge_vertex_groups(
                    mesh,
                    bone1.name,
                    bone2.name,
                    # If remove is False, and the bone stayed, it would be equivalent to transferring only partial weights
                    remove=(self.mode == 'remove'))
                found = True

            # if found:
            # if self.mode == 'hide':
            #     bone2.hide = True
            # elif self.mode == 'show':
            #     bone2.hide = False
            # elif self.mode == 'remove':
            if (found and self.mode == 'remove'):
                for ch in bone2.id_data.children:
                    if (ch.parent_type == 'BONE') and (ch.parent_bone
                                                       == bone2.name):
                        mat = Get.matrix(ch)
                        ch.parent_bone = bone1.name
                        Set.matrix(ch, mat)
                name2 = bone2.name
                Set.mode(context, mode='EDIT')
                bone2 = rig.data.edit_bones[name2]
                rig.data.edit_bones.remove(bone2)
                Set.mode(context, mode='POSE')

        if active:
            Set.active(context, active)

        return {'FINISHED'}
コード例 #8
0
    def execute(self, context):
        active = obj = context.object
        transfer = bpy.ops.object.data_transfer

        if (not Is.mesh(obj)) or getattr(obj, 'DazMannequin', False):
            # redesignate obj
            obj = None

            for o in context.selected_objects:
                if Is.mesh(o) and (not getattr(o, 'DazMannequin', False)):
                    obj = o
                    Set.active(context, o)
                    break

        if not all((
                obj,
                obj.data.polygons,
                transfer.poll(context.copy()),
        )):
            self.report({'INFO'}, "Only Mannequins Selected")
            return {'CANCELLED'}

        mesh = obj.data

        if mesh.polygons[0].use_smooth:
            # only check one face, rather than all
            bpy.ops.object.shade_smooth()

        if mesh.use_auto_smooth:
            transfer(data_type='CUSTOM_NORMAL')
            for o in context.selected_objects:
                if Is.mesh(o):
                    o.data.use_auto_smooth = True

        if obj.vertex_groups:
            # Vertex groups add to file size, so don't keep them for everything
            transfer(data_type='VGROUP_WEIGHTS',
                     layers_select_src='ALL',
                     layers_select_dst='NAME')

        if mesh.vertex_colors:
            transfer(data_type='VCOL',
                     layers_select_src='ALL',
                     layers_select_dst='NAME')

        if mesh.uv_layers:
            transfer(data_type='UV',
                     layers_select_src='ALL',
                     layers_select_dst='NAME')

        Set.active(context, active)

        return {'FINISHED'}
コード例 #9
0
ファイル: camera_to_view.py プロジェクト: Irmitya/zpy_general
    def execute(self, context):
        area = context.area
        view = area.spaces.active
        reg = view.region_3d

        # Create new camera object, add to scene, and select it
        camo = New.camera(context, name="Viewport", size=0.5)
        cam = camo.data
        Set.active(context, camo)
        # Set.active_select(camo)

        # Set Lock viewport to the camera
        view.camera = camo
        view.use_local_camera = True
        view.lock_camera = True

        # Send camera object to the current viewport rotation/location
        Set.matrix(camo, reg.view_matrix.inverted())

        # Switch to camera view
        if reg.view_perspective != 'CAMERA':
            bpy.ops.view3d.view_camera()
            # run op to prevent changing the (actual) viewport's position
        # reg.view_perspective = 'CAMERA'

        # Mirror viewport properties to the camera
        cam.lens = view.lens / 2
        cam.clip_start = view.clip_start
        cam.clip_end = view.clip_end

        # Re-center the camera view window if it was changed
        bpy.ops.view3d.view_center_camera()
        # reg.view_camera_offset = [0.0, 0.0]
        # reg.view_camera_zoom = 28

        # Setup Deph of Field
        cam.dof.use_dof = True
        # cam.dof.aperture_fstop = 0.5  # Blur amount
        # cam.dof.aperture_fstop = 2.8
        # cam.dof.focus_distance = reg.view_distance
        bpy.ops.ui.eyedropper_depth('INVOKE_DEFAULT')

        return {'FINISHED'}
コード例 #10
0
    def execute(self, context):
        active = Get.active(context)
        mode = context.mode
        pose = list()

        for rig in context.selected_objects:
            (rig, meta) = self.poll_parse(context, rig)
            if None in (rig, meta):
                continue

            rigify_to_meta(rig, meta)
            pose.append((meta, rig))
        else:
            if mode == 'POSE':
                Set.mode(context, 'OBJECT')
            for (meta, rig) in pose:
                Set.select(rig, False)
                Set.select(meta, True)
                if rig == active:
                    Set.active(context, meta)
            if mode == 'POSE':
                Set.mode(context, 'POSE')

        return {'FINISHED'}
コード例 #11
0
    def execute(self, context):
        scn = context.scene
        active = Get.active(context)
        selected = Get.selected(context)
        is_pose = bool(context.mode == 'POSE')

        if not is_pose:
            self.use_tails = False
        mode = ('HEADS', 'TAILS')[self.use_tails]

        # Use the line thickness of the active item, across the selection
        if getattr(active, 'motion_path', None):
            line = active.motion_path.line_thickness
        else:
            line = 1

        colors = dict()
        types = dict()
        for src in Get.selected(context):
            mp = src.motion_path
            if not mp:
                continue

            if mp.use_custom_color:
                colors[src] = mp.color

            if Is.posebone(src):
                display = src.id_data.pose.animation_visualization.motion_path
            else:
                display = src.animation_visualization.motion_path

            types[src] = display.type

        # Get the frame range to bake motion paths in
        motion = utils.prefs(__package__).motion

        if self.use_start_end:
            start = self.start_frame
            end = self.end_frame
        elif (motion.use_relative_range) or (scn.use_preview_range):
            start = scn.frame_preview_start
            end = scn.frame_preview_end
            fc = scn.frame_current
            fb = motion.frame_before
            fa = motion.frame_after

            if not (scn.use_preview_range) or (abs(end - start) > 100 >
                                               (fb + fa)):
                # If the preview range is too high, just default to nearby
                start = fc - fb
                end = fc + fa
        else:
            # if (active):
            # # Use the active object's motion path's in_range distance
            # if (Is.posebone(active)):
            #     mp = active.id_data.pose.animation_visualization.motion_path
            # else:
            #     mp = active.animation_visualization.motion_path
            # fb = mp.frame_before
            # fa = mp.frame_after
            # if (fb < 25): fb = 25
            # if (fa < 25): fa = 25

            # start = scn.frame_current - fb
            # end = scn.frame_current + fa
            start = scn.frame_start
            end = scn.frame_end
            fc = scn.frame_current
            if 150 < abs(end - start):
                start = fc - 50
                end = fc + 50

        # Create the motion paths
        args = dict(start_frame=start, end_frame=end + 1)
        if is_pose:
            op = bpy.ops.pose
            args['bake_location'] = mode

            # Operator only runs on active rig, so repeat for all in pose mode
            obs = {b.id_data for b in selected}

            for ob in obs:
                Set.active(context, ob)
                op.paths_clear(only_selected=True)
                op.paths_calculate(**args)
            else:
                Set.active(context, active)
        else:
            op = bpy.ops.object
            op.paths_clear(only_selected=True)
            op.paths_calculate(**args)

        for src in selected:
            mp = src.motion_path
            if not mp: continue

            mp.line_thickness = line
            color = colors.get(src)
            if color:
                mp.color = color

            if Is.posebone(src):
                display = src.id_data.pose.animation_visualization.motion_path
            else:
                display = src.animation_visualization.motion_path
            display.type = types.get(src, 'CURRENT_FRAME')

        # Set to use the frame hider instead of ever displaying all points
        # if is_pose:
        #     src.id_data.pose.animation_visualization. \
        #         motion_path.type = 'CURRENT_FRAME'
        # else:
        #     src.animation_visualization. \
        #         motion_path.type = 'CURRENT_FRAME'
        # scn.frame_set(scn.frame_current)

        return {'FINISHED'}
コード例 #12
0
    def execute(self, context):
        (rig, meshes) = self.get_rig_meshes(context)
        if not (rig and meshes):
            return {'CANCELLED'}

        use_mask = bool((self.from_active or self.normalize_active))
        active = context.object
        active_bone = context.active_pose_bone.bone
        mode = active.mode
        pose = rig.data.pose_position
        scale = rig.scale.copy()

        if use_mask:
            active_bone.select = False

        Set.mode(context, 'OBJECT')
        rig.data.pose_position = 'REST'
        rig.scale *= 50

        for mesh in meshes:
            mscale = mesh.scale.copy()
            mp = mesh.parent
            while mp and mp != rig:
                # Sort through mesh parent hierarchy to find if the rig is a parent somewhere in there
                mp = mp.parent
            if mp != rig:
                # The rig/parent is already scaled, so don't scale mesh again
                mesh.scale *= 50
            Set.active(context, mesh)
            Set.mode(context, 'WEIGHT_PAINT')

            vg = mesh.vertex_groups
            bone_group = vg.get(active_bone.name)
            do_bone = bool((use_mask and bone_group))
            mindex = vg.active_index

            bpy.ops.paint.weight_from_bones(type='AUTOMATIC')

            if do_bone:
                vg.active_index = mindex
                bpy.ops.object.vertex_group_invert()
                for b in context.selected_pose_bones:
                    utils.subtract_vertex_groups(mesh, active_bone.name, b.name)
                bpy.ops.object.vertex_group_invert()
                if self.normalize_active:
                    for b in context.selected_pose_bones:
                        utils.subtract_vertex_groups(mesh, b.name, active_bone.name)

            if (mindex != -1):
                vg.active_index = mindex

            Set.mode(context, 'OBJECT')
            mesh.scale = mscale

        Set.active(context, active)
        rig.scale = scale
        rig.data.pose_position = pose
        Set.mode(context, mode)

        if use_mask:
            active_bone.select = True

        if len(meshes) > 1:
            self.report({'INFO'}, f"Assigned weights to {len(meshes)} meshes")
        else:
            self.report({'INFO'}, f"Assigned weights to {list(meshes)[0].name}")

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

        if (not Is.mesh(obj)) or getattr(obj, 'DazMannequin', False):
            obj = None

            for o in context.selected_objects:
                if Is.mesh(o) and (not getattr(o, 'DazMannequin', False)):
                    obj = o
                    Set.active(context, obj)
                    break

        if not (obj and obj.data.polygons):
            self.report({'INFO'}, "Only Mannequins Selected")
            return {'CANCELLED'}
        elif not obj.data.materials:
            self.report({'INFO'},
                        "Mesh doesn't have any materials to transfer")
            return {'CANCELLED'}

        def getface(f, o):
            r = self.Round
            area = round(f.area, r)
            center = utils.multiply_matrix(o.matrix_world, f.center)
            center = tuple([round(a, r) for a in center])
            return (area, center)

        mesh = obj.data
        # obj_faces = {
        # # getface(f, obj): obj.material_slots[f.material_index].material
        # getface(f, obj): mesh.materials[f.material_index]
        # for f in mesh.polygons
        # }

        obj_faces = dict()
        count = 0
        total = len(mesh.polygons)
        for (i, f) in enumerate(mesh.polygons):
            perc = round(((i + 1) / total * 100))
            log = f"\rGetting face ids ({obj.name}): {count}/{total} ({perc}%)"
            print(log + " [old_mats]", end="")

            obj_faces[getface(f, obj)] = mesh.materials[f.material_index]
            count += 1

        count_obj = 0
        total_obj = len(context.selected_objects) - 1
        for (i, o) in enumerate(context.selected_objects):
            if (o == obj) or (not Is.mesh(o)):
                continue
            omesh = o.data

            perc_obj = round(((i + 1) / total_obj * 100))
            log = f"\rTransferring materials ({o.name}): {count_obj}/{total_obj} ({perc_obj}%)"

            print(log + " [old_mats]", end="")
            old_mats = {i: m for i, m in enumerate(omesh.materials)}

            print(log + " [for face in mesh.polygons]", end="")
            # count = 0
            # total = len(omesh.polygons)
            for face in omesh.polygons:
                mat_i = obj_faces.get(getface(face, o))
                if mat_i is None:
                    # old_mats.pop(face.material_index)
                    continue

                if mat_i.name not in omesh.materials:
                    omesh.materials.append(mat_i)

                for (i, mat) in enumerate(o.material_slots):
                    if mat.material == mat_i:
                        face.material_index = i
                        if i in old_mats:
                            old_mats.pop(face.material_index)
                        break

            print(log + " [for i in old_mats]", end="")
            for i in reversed(list(old_mats)):
                o.data.materials.pop(index=i)

            count_obj += 1
        else:
            print(
                f"\rTransferred {count_obj}/{total_obj} materials from {obj.name}",
                " " * 20)

        Set.active(context, active)

        return {'FINISHED'}