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'}
def mesh(self, context): layout = self.layout obj = context.active_object if not (Is.mesh(obj) and Is.armature(obj.parent)): return False scn = context.scene if hasattr(scn, 'DazShowRigging'): layout.prop(scn, 'DazMannequinGroup', text="") idname = 'daz.add_mannequin_macro' # op = layout.operator(idname, text="Full Macro") layout.operator_menu_hold(idname, menu='MACRO_MT_add_mannequin_full', text="Add Mannequin (Full Macro)" + mannequin.head).macro = True layout.operator_menu_hold(idname, menu='MACRO_MT_add_mannequin_only', text="Add Mannequin" + mannequin.head) # op.head = 'SOLID' # op.macro = True # layout.operator(idname, text="Solid").head = 'SOLID' # layout.operator(idname, text="Jaw").head = 'JAW' # layout.operator(idname, text="Full").head = 'FULL' layout.operator('object.data_transfer_mannequin_preset', icon='OUTLINER_DATA_MESH') layout.operator('object.data_transfer_materials', icon='MATERIAL') return True
def get_objs(self, context): if context.mode == 'PAINT_WEIGHT': objs = [context.object] else: objs = list() rig = context.active_pose_bone.id_data for ob in context.selected_objects: if Is.mesh(ob): for mod in ob.modifiers: if (mod.type == 'ARMATURE') and (mod.object == rig): objs.append(ob) if not objs: for b in context.selected_pose_bones: for mesh in bpy.data.objects: if mesh.type != 'MESH': continue for modifier in mesh.modifiers: if modifier.type == 'ARMATURE' and modifier.object == b.id_data: if mesh not in objs: objs.append(mesh) return objs
def execute(self, context): mute = None for obj in bpy.data.objects: if (not Is.mesh(obj)) or (obj.data.shape_keys is None): continue muted = bool('mute_shapekeys' in obj) if mute is None: mute = not muted if mute and not muted: # Storing shapekey settings obj['mute_shapekeys'] = (obj.active_shape_key_index, obj.show_only_shape_key) (obj.active_shape_key_index, obj.show_only_shape_key) = (0, True) elif not mute and muted: # Resetting shapekey settings (obj.active_shape_key_index, obj.show_only_shape_key) = obj['mute_shapekeys'] del (obj['mute_shapekeys']) if mute is True: self.report({'INFO'}, "Locked all mesh shapekeys") elif mute is False: self.report({'INFO'}, "Unlocked all mesh shapekeys") return {'FINISHED'}
def get_rig_mesh(context): ob = context.object if Is.armature(ob): for ob in context.selected_objects: if Is.mesh(ob): for mod in ob.modifiers: if (mod.type == 'ARMATURE') and (mod.object == context.object): return (context.object, ob) elif Is.mesh(ob): for mod in ob.modifiers: if (mod.type == 'ARMATURE') and (mod.object in context.selected_objects): return (mod.object, ob) return (None, None)
def get_meshes(self, context, rig): meshes = set() for ob in context.selected_objects: if Is.mesh(ob): for mod in ob.modifiers: if (mod.type == 'ARMATURE') and (mod.object == rig): meshes.add(ob) if not meshes: for ob in bpy.data.objects: if not Is.mesh(ob): continue for modifier in ob.modifiers: if (modifier.type == 'ARMATURE') and (modifier.object == rig): meshes.add(ob) return meshes
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'}
def rigify_to_meta(rigify, metarig): """Retarget Rigify meshes to Metarig""" pose = (metarig.data.pose_position, rigify.data.pose_position) (metarig.data.pose_position, rigify.data.pose_position) = ('REST', 'REST') utils.update(bpy.context) for obj in bpy.data.objects: if Is.curve(obj) and obj.name.startswith(rigify.name + '-MCH-'): # Splines from angavrilov's spline rig continue for mod in obj.modifiers: if hasattr(mod, 'object') and mod.object == rigify: mod.object = metarig metafy_vgroups(rigify, obj, metarig) if (obj.parent == rigify): rigify_bone = obj.parent_bone if rigify_bone: if rigify_bone.startswith('DEF-'): meta_bone = rigify_bone[4:] else: meta_bone = rigify_bone if meta_bone in metarig.data.bones: mat = Get.matrix(obj) # pmat = obj.matrix_parent_inverse.copy() obj.parent = metarig obj.parent_bone = meta_bone # obj.matrix_parent_inverse = pmat Set.matrix(obj, mat) else: obj.parent = metarig if Is.mesh(obj): meshes = {obj.data} if hasattr(obj, 'variants'): # The LoDs store the mesh datas and drivers without an object for layer in obj.variants.layers: if layer.mesh: meshes.add(layer.mesh) for lod in layer.lods: meshes.add(lod.mesh) for mesh in meshes: if mesh: rigify_drivers(rigify, metarig, mesh.shape_keys) for mat in bpy.data.materials: rigify_drivers(rigify, metarig, mat) rigify_drivers(rigify, metarig, mat.node_tree) (metarig.data.pose_position, rigify.data.pose_position) = pose
def poll(cls, context): ob = context.object if Is.armature(ob) and ob.mode in ('POSE', 'EDIT'): return Get.selected(context, ob) if not bpy.ops.paint.weight_from_bones.poll(context.copy()): return if Is.mesh(ob) and (ob.mode == 'WEIGHT_PAINT'): for mod in ob.modifiers: if (mod.type == 'ARMATURE') and (mod.object in Get.objects(context)[:]): if mod.object.mode == 'POSE': return True
def execute(self, context): mannequin.head = f" ({self.head.title()})" objects = list() for obj in context.selected_objects: if Is.mesh(obj) and Is.armature(obj.parent): inst = self.convert(context, obj) objects.append(inst) for obj in reversed(objects): Set.active_select(context, obj, isolate=False) return {'FINISHED'}
def meta_to_rigify(metarig, rigify): """Retarget Metarig meshes to Rigify rig""" pose = (metarig.data.pose_position, rigify.data.pose_position) (metarig.data.pose_position, rigify.data.pose_position) = ('REST', 'REST') utils.update(bpy.context) for obj in bpy.data.objects: for mod in obj.modifiers: if hasattr(mod, 'object') and mod.object == metarig: mod.object = rigify rigify_vgroups(metarig, rigify, obj) if obj.parent == metarig: if obj.parent_bone: rigify_bone = vg_names.get(obj.parent_bone, False) if rigify_bone is False: rigify_bone = 'DEF-' + obj.parent_bone if rigify_bone and rigify_bone in rigify.data.bones: mat = Get.matrix(obj) # pmat = obj.matrix_parent_inverse.copy() obj.parent = rigify obj.parent_bone = rigify_bone # obj.matrix_parent_inverse = pmat Set.matrix(obj, mat) else: obj.parent = rigify if Is.mesh(obj): meshes = {obj.data} if hasattr(obj, 'variants'): # The LoDs store the mesh datas and drivers without an object for layer in obj.variants.layers: if layer.mesh: meshes.add(layer.mesh) for lod in layer.lods: meshes.add(lod.mesh) for mesh in meshes: if mesh: rigify_drivers(metarig, rigify, mesh.shape_keys) for mat in bpy.data.materials: rigify_drivers(metarig, rigify, mat) rigify_drivers(metarig, rigify, mat.node_tree) (metarig.data.pose_position, rigify.data.pose_position) = pose
def draw(self, context): layout = self.layout pie = layout.menu_pie() pie.operator_enum("object.mode_set", "mode") def op(text, icon, mode): pie.operator('zpy.mode_set_armature_mesh', text=text, icon=icon).mode = mode pie.operator_context = 'EXEC_DEFAULT' if Is.mesh(context.object): op("Pose", 'POSE_HLT', 'POSE') op("Edit Armature", 'EDITMODE_HLT', 'EDIT_ARMATURE') elif Is.armature(context.object): pie.separator() op("Weight Paint", 'WPAINT_HLT', 'WEIGHT_PAINT') pie.separator() op("Sculpt", 'SCULPTMODE_HLT', 'SCULPT') op("Edit Mesh", 'EDITMODE_HLT', 'EDIT_MESH')
def get_rig_meshes(self, context): """ Find the selected rigs and meshes, attached together. If only a rig is selected, find all the meshes that use it. """ active = context.object if context.mode == 'PAINT_WEIGHT': meshes = {active} rig = None for mod in active.modifiers: if (mod.type == 'ARMATURE') and (mod.object in Get.objects(context)[:]): rig = mod.object if rig.mode == 'POSE': break if not rig: self.report({'ERROR'}, "Can't find rig using the active mesh") else: rig = active meshes = set() for ob in context.selected_objects: if Is.mesh(ob): for mod in ob.modifiers: if (mod.type == 'ARMATURE') and (mod.object == rig): meshes.add(ob) if not meshes: for ob in Get.objects(context): if not Is.visible(context, ob): continue for mod in ob.modifiers: if (mod.type == 'ARMATURE') and (mod.object == rig): meshes.add(ob) if not meshes: self.report({'ERROR'}, "Can't find mesh using the active rig") return (rig, meshes)
def poll(cls, context): return Is.mesh(context.object) and (context.object.data.shape_keys)
def poll(cls, context): ob = context.object return (Is.mesh(ob) and getattr(ob.active_shape_key, 'vertex_group', None))
def poll(self, context): for obj in context.selected_objects: if Is.mesh(obj) and Is.armature(obj.parent): return True
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'}
def convert(self, context, obj): scn = context.scene Set.active_select(context, obj, isolate=True) coll_instanced = False # Whether or not to instance collection in full macro head = self.head group = obj.name + '-Mannequin' coll = bpy.data.collections.get(group) if coll: # Regenerating mannequin coll_instanced = True for ob in coll.objects.values(): if Is.mesh(ob) and ob.DazMannequin: bpy.data.objects.remove(ob) else: coll = bpy.data.collections.new(group) scn.collection.children.link(coll) # "temporarily" unhide collection if hidden in_view = Is.in_view(context, coll) if not in_view: Set.visible(context, coll, view_layer=True) visible = Is.visible(context, coll) if not visible: Set.visible(context, coll) # Add mannequin objects for current mesh self.generate(context, obj, obj.parent, coll) if self.macro: has_mann = False for ob in coll.objects.values(): if Is.mesh(ob): Set.select(ob) has_mann = True if has_mann: bpy.ops.object.data_transfer_mannequin_preset() # if obj.data.materials: # bpy.ops.object.data_transfer_materials() # Hide the collection and create an instancer of it # if coll: # # Set.visible(context, obj, value=False) # Set.visible(context, coll, value=False, view_layer=True) # if not coll_instanced: # inst = New.object(context, name=coll.name) # inst.instance_type = 'COLLECTION' # inst.instance_collection = coll # Set.empty_size(inst, 0) # return inst for ob in coll.objects.values(): Set.select(ob, value=False) if not visible: Set.visible(context, coll, False) if not in_view: Set.visible(context, coll, False, view_layer=True) return obj