diff --git a/core/common.py b/core/common.py index a58d1f2..cb9562c 100644 --- a/core/common.py +++ b/core/common.py @@ -262,3 +262,25 @@ def update_progress(self, context, message): def finish_progress(context): context.window_manager.progress_end() context.area.header_text_set(None) + +def transfer_vertex_weights(context: Context, obj: bpy.types.Object, source_group: str, target_group: str, delete_source_group: bool = True) -> bool: + + modifier: bpy.types.VertexWeightMixModifier = obj.modifiers.new(name="merge_weights",type="VERTEX_WEIGHT_MIX") + + modifier.mix_set = 'B' + modifier.vertex_group_a = target_group + modifier.vertex_group_b = source_group + modifier.mask_constant = 1.0 + + bpy.ops.object.mode_set(mode='OBJECT') + prev_obj: bpy.types.Object = context.view_layer.objects.active + context.view_layer.objects.active = obj + bpy.ops.object.modifier_apply(modifier=modifier.name) + if delete_source_group: + obj.vertex_groups.remove(obj.vertex_groups.get(source_group)) + bpy.ops.object.mode_set(mode='EDIT') + bpy.ops.object.mode_set(mode='OBJECT') + context.view_layer.objects.active = prev_obj + + return True + diff --git a/functions/armature_modifying.py b/functions/armature_modifying.py new file mode 100644 index 0000000..e48408b --- /dev/null +++ b/functions/armature_modifying.py @@ -0,0 +1,158 @@ +import bpy +from ..core import common +from bpy.types import Operator, Context, Mesh, Armature, EditBone +from ..core.register import register_wrap +from .translations import t + +@register_wrap +class AvatarToolkit_OT_RemoveZeroWeightBones(Operator): + bl_idname = "avatar_toolkit.remove_zero_weight_bones" + bl_label = t("Tools.remove_zero_weight_bones.label") + bl_description = t("Tools.remove_zero_weight_bones.desc") + bl_options = {'REGISTER', 'UNDO'} + + threshold: bpy.props.FloatProperty( + default=0.01, + name=t("Tools.remove_zero_weight_bones.threshold.label"), + description=t("Tools.remove_zero_weight_bones.threshold.desc"), + min=0.0000001, + max=0.9999999) + + @classmethod + def poll(cls, context: Context) -> bool: + return common.get_selected_armature(context) is not None + + def execute(self, context: Context) -> set[str]: + armature = common.get_selected_armature(context) + if not common.is_valid_armature(armature): + self.report({'ERROR'}, t("Tools.apply_transforms.invalid_armature")) + return {'CANCELLED'} + + weighted_bones: list[str] = [] + + + bpy.ops.object.mode_set(mode='OBJECT') + bpy.ops.object.select_all(action='DESELECT') + + armature.select_set(True) + context.view_layer.objects.active = armature + + meshes = common.get_all_meshes(context) + for mesh in meshes: + mesh_data: Mesh = mesh.data + for vertex in mesh_data.vertices: + for group in vertex.groups: + if group.weight > self.threshold: + weighted_bones.append(mesh.vertex_groups[group.group].name) #add bone name to list of bones that are greater than the weight threshold + + bpy.ops.object.mode_set(mode='EDIT') + amature_data: Armature = armature.data + unweighted_bones: list[str] = [] + + #doing 2 loops to prevent modification of array during iteration + for bone in amature_data.edit_bones: + if bone.name not in weighted_bones: + unweighted_bones.append(bone.name) #add bones that arent in the list of bones that have weight into the list of bones that don't + + for bone_name in unweighted_bones: + for edit_bone in amature_data.edit_bones[bone_name].children: + edit_bone.use_connect = False #to fix randomly moving bones + edit_bone.parent = amature_data.edit_bones[bone_name].parent #to fix unparented bones. + amature_data.edit_bones.remove(amature_data.edit_bones[bone_name]) #delete list of unweighted bones from the armature + + self.report({'INFO'}, t("Tools.remove_zero_weight_bones.success")) + return {'FINISHED'} + +@register_wrap +class AvatarToolkit_OT_MergeBonesToActive(Operator): + bl_idname = "avatar_toolkit.merge_bones_to_active" + bl_label = t("Tools.merge_bones_to_active.label") + bl_description = t("Tools.merge_bones_to_active.desc") + bl_options = {'REGISTER', 'UNDO'} + + delete_old: bpy.props.BoolProperty(name=t("Tools.merge_bones_to_active.delete_old.label"), description=t("Tools.merge_bones_to_active.delete_old.desc"), default=False) + + @classmethod + def poll(cls, context: Context) -> bool: + if common.get_selected_armature(context) is not None: + if common.get_selected_armature(context) == context.view_layer.objects.active: + if context.mode == "POSE": + return len(context.selected_pose_bones) > 1 + elif context.mode == "EDIT_ARMATURE": + return len(context.selected_bones) > 1 + return False + + def execute(cls, context: Context) -> set[str]: + + prev_mode: str = "EDIT" + if context.mode == "POSE": + prev_mode = "POSE" + + #get active bone and a list of all other selected bones + bpy.ops.object.mode_set(mode='EDIT') + target_bone: str = context.active_bone.name + + armature_data: Armature = context.view_layer.objects.active.data + + + bones: list[str] = [i.name for i in context.selected_bones] + bones.remove(target_bone) + + for obj in common.get_all_meshes(context): + for bone in bones: + bone_name: str = armature_data.edit_bones[bone].name + common.transfer_vertex_weights(context=context,obj=obj,source_group=bone_name,target_group=armature_data.edit_bones[target_bone].name) + bpy.ops.object.mode_set(mode='EDIT') + for bone in bones: + if cls.delete_old: + for bone_child in armature_data.edit_bones[bone].children: + bone_child.parent = armature_data.edit_bones[bone].parent + armature_data.edit_bones.remove(armature_data.edit_bones[bone]) + + bpy.ops.object.mode_set(mode=prev_mode) + return {'FINISHED'} + +@register_wrap +class AvatarToolkit_OT_MergeBonesToParents(Operator): + bl_idname = "avatar_toolkit.merge_bones_to_parents" + bl_label = t("Tools.merge_bones_to_parents.label") + bl_description = t("Tools.merge_bones_to_parents.desc") + bl_options = {'REGISTER', 'UNDO'} + + delete_old: bpy.props.BoolProperty(name=t("Tools.merge_bones_to_parents.delete_old.label"), description=t("Tools.merge_bones_to_parents.delete_old.desc"), default=False) + + @classmethod + def poll(cls, context: Context) -> bool: + if common.get_selected_armature(context) is not None: + if common.get_selected_armature(context) == context.view_layer.objects.active: + if context.mode == "POSE": + return len(context.selected_pose_bones) > 0 + elif context.mode == "EDIT_ARMATURE": + return len(context.selected_bones) > 0 + return False + + def execute(cls, context: Context) -> set[str]: + + prev_mode: str = "EDIT" + if context.mode == "POSE": + prev_mode = "POSE" + #get active bone and a list of all other selected bones + bpy.ops.object.mode_set(mode='EDIT') + armature_data: Armature = context.view_layer.objects.active.data + + for obj in common.get_all_meshes(context): + for bone in [i.name for i in context.selected_bones]: + if armature_data.edit_bones[bone].parent != None: + bone_name: str = armature_data.edit_bones[bone].name + common.transfer_vertex_weights(context=context,obj=obj,source_group=bone_name,target_group=armature_data.edit_bones[bone].parent.name) + bpy.ops.object.mode_set(mode='EDIT') + + for bone in [i.name for i in context.selected_bones]: + if cls.delete_old: + for bone_child in armature_data.edit_bones[bone].children: + bone_child.parent = armature_data.edit_bones[bone].parent + armature_data.edit_bones.remove(armature_data.edit_bones[bone]) + + + bpy.ops.object.mode_set(mode=prev_mode) + return {'FINISHED'} \ No newline at end of file diff --git a/resources/translations/en_US.json b/resources/translations/en_US.json index 21d7428..b433d0e 100644 --- a/resources/translations/en_US.json +++ b/resources/translations/en_US.json @@ -141,6 +141,19 @@ "Tools.apply_transforms.desc": "Apply position, rotation, and scale to the armature and its meshes", "Tools.apply_transforms.invalid_armature": "Invalid armature selected", "Tools.apply_transforms.success": "Transforms applied successfully to armature and meshes", + "Tools.remove_zero_weight_bones.success": "Zero weight bones removed successfully", + "Tools.remove_zero_weight_bones.label": "Remove Zero Weight Bones", + "Tools.remove_zero_weight_bones.desc": "Remove bones from the armature that have weights less than threshold.", + "Tools.merge_bones_to_active.delete_old.desc": "Remove old bones when merging.", + "Tools.merge_bones_to_active.delete_old.label": "Remove Old Bones", + "Tools.merge_bones_to_active.desc": "Merge selected bones to active bone (selected in bright blue or orange).", + "Tools.merge_bones_to_active.label": "Merge Bones to Active", + "Tools.merge_bones_to_parents.delete_old.desc": "Remove old bones when merging.", + "Tools.merge_bones_to_parents.delete_old.label": "Remove Old Bones", + "Tools.merge_bones_to_parents.desc": "Merges every bone in the selection to each of their parents.", + "Tools.merge_bones_to_parents.label": "Merge Bones to Individual Parents", + "Tools.remove_zero_weight_bones.threshold.label": "Weight Threshold", + "Tools.remove_zero_weight_bones.threshold.desc": "If a bone is not weighted to any part of any mesh under the armature with a threshold greater than this, it is removed", "VisemePanel.create_visemes": "Create Visemes", "VisemePanel.creating_viseme": "Creating viseme: {viseme_name}", "VisemePanel.creating_viseme_detail": "Creating viseme: {viseme_name}", diff --git a/ui/tools.py b/ui/tools.py index 0acfc80..caec2c2 100644 --- a/ui/tools.py +++ b/ui/tools.py @@ -8,6 +8,7 @@ from ..functions.translations import t from ..core.common import get_selected_armature from ..functions.seperate_by import AvatarToolKit_OT_SeparateByMaterials, AvatarToolKit_OT_SeparateByLooseParts from ..functions.additional_tools import AvatarToolKit_OT_ApplyTransforms +from ..functions.armature_modifying import AvatarToolkit_OT_RemoveZeroWeightBones, AvatarToolkit_OT_MergeBonesToActive, AvatarToolkit_OT_MergeBonesToParents @register_wrap class AvatarToolkit_PT_ToolsPanel(bpy.types.Panel): @@ -39,5 +40,10 @@ class AvatarToolkit_PT_ToolsPanel(bpy.types.Panel): row.operator(AvatarToolKit_OT_SeparateByLooseParts.bl_idname, text=t("Tools.separate_by_loose_parts.label"), icon='OUTLINER_OB_MESH') row = layout.row(align=True) row.operator(AvatarToolKit_OT_ApplyTransforms.bl_idname, text=t("Tools.apply_transforms.label"), icon='OBJECT_ORIGIN') + row = layout.row(align=True) + row.operator(AvatarToolkit_OT_RemoveZeroWeightBones.bl_idname, text=t("Tools.remove_zero_weight_bones.label"), icon='BONE_DATA') + row = layout.row(align=True) + row.operator(AvatarToolkit_OT_MergeBonesToActive.bl_idname, text=t("Tools.merge_bones_to_active.label"), icon='BONE_DATA') + row.operator(AvatarToolkit_OT_MergeBonesToParents.bl_idname, text=t("Tools.merge_bones_to_parents.label"), icon='BONE_DATA') else: layout.label(text=t("Tools.select_armature"), icon='ERROR')