From daba04536497ef31c47971df52a27bb2c4523d76 Mon Sep 17 00:00:00 2001 From: 989onan Date: Mon, 19 Aug 2024 13:59:31 -0400 Subject: [PATCH 1/4] Add zero weight bone removal --- functions/armature_modifying.py | 64 +++++++++++++++++++++++++++++++ resources/translations/en_US.json | 5 +++ ui/tools.py | 4 +- 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 functions/armature_modifying.py diff --git a/functions/armature_modifying.py b/functions/armature_modifying.py new file mode 100644 index 0000000..ba82d05 --- /dev/null +++ b/functions/armature_modifying.py @@ -0,0 +1,64 @@ +import bpy +from ..core import common +from bpy.types import Operator, Context, Mesh, Armature, EditBone, PoseBone +from ..core.register import register_wrap +from .translations import t + +@register_wrap +class AvatarToolkit_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'} diff --git a/resources/translations/en_US.json b/resources/translations/en_US.json index a0ec7b9..e982db8 100644 --- a/resources/translations/en_US.json +++ b/resources/translations/en_US.json @@ -130,6 +130,11 @@ "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.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 7d8dc73..db6841a 100644 --- a/ui/tools.py +++ b/ui/tools.py @@ -7,6 +7,7 @@ from ..functions.translations import t from ..core.common import get_selected_armature from ..functions.seperate_by import SeparateByMaterials, SeparateByLooseParts from ..functions.additional_tools import ApplyTransforms +from ..functions.armature_modifying import AvatarToolkit_RemoveZeroWeightBones @register_wrap class AvatarToolkitToolsPanel(bpy.types.Panel): @@ -33,10 +34,11 @@ class AvatarToolkitToolsPanel(bpy.types.Panel): row.operator(CreateDigitigradeLegs.bl_idname, text=t("Tools.create_digitigrade_legs.label"), icon='BONE_DATA') layout.separator() row = layout.row(align=True) - layout.label(text=t("Tools.separate_by.label"), icon='MESH') + layout.label(text=t("Tools.separate_by.label"), icon='MESH_DATA') row.operator(SeparateByMaterials.bl_idname, text=t("Tools.separate_by_materials.label"), icon='MATERIAL') row.operator(SeparateByLooseParts.bl_idname, text=t("Tools.separate_by_loose_parts.label"), icon='OUTLINER_OB_MESH') row = layout.row(align=True) row.operator(ApplyTransforms.bl_idname, text=t("Tools.apply_transforms.label"), icon='OBJECT_ORIGIN') + row.operator(AvatarToolkit_RemoveZeroWeightBones.bl_idname, text=t("Tools.remove_zero_weight_bones.label"), icon='BONE_DATA') else: layout.label(text=t("Tools.select_armature"), icon='ERROR') From dc2b6e46ce1d6b71c8a65afec43ebff7f10316f5 Mon Sep 17 00:00:00 2001 From: 989onan Date: Fri, 6 Sep 2024 12:32:50 -0400 Subject: [PATCH 2/4] Add Bone Tools - Added merge to active to tools section for bones - Added merge to individual parents, which merges each selected bone to their parent bone --- core/common.py | 22 +++++++ functions/armature_modifying.py | 96 ++++++++++++++++++++++++++++++- resources/translations/en_US.json | 8 +++ ui/tools.py | 6 +- 4 files changed, 130 insertions(+), 2 deletions(-) diff --git a/core/common.py b/core/common.py index 0ea4ae0..2ce289c 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 index ba82d05..8982909 100644 --- a/functions/armature_modifying.py +++ b/functions/armature_modifying.py @@ -1,6 +1,6 @@ import bpy from ..core import common -from bpy.types import Operator, Context, Mesh, Armature, EditBone, PoseBone +from bpy.types import Operator, Context, Mesh, Armature, EditBone from ..core.register import register_wrap from .translations import t @@ -62,3 +62,97 @@ class AvatarToolkit_RemoveZeroWeightBones(Operator): 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 9ba83ec..be71d7a 100644 --- a/resources/translations/en_US.json +++ b/resources/translations/en_US.json @@ -135,6 +135,14 @@ "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", diff --git a/ui/tools.py b/ui/tools.py index db6841a..8b1b189 100644 --- a/ui/tools.py +++ b/ui/tools.py @@ -7,7 +7,7 @@ from ..functions.translations import t from ..core.common import get_selected_armature from ..functions.seperate_by import SeparateByMaterials, SeparateByLooseParts from ..functions.additional_tools import ApplyTransforms -from ..functions.armature_modifying import AvatarToolkit_RemoveZeroWeightBones +from ..functions.armature_modifying import AvatarToolkit_RemoveZeroWeightBones, AvatarToolkit_OT_MergeBonesToActive, AvatarToolkit_OT_MergeBonesToParents @register_wrap class AvatarToolkitToolsPanel(bpy.types.Panel): @@ -39,6 +39,10 @@ class AvatarToolkitToolsPanel(bpy.types.Panel): row.operator(SeparateByLooseParts.bl_idname, text=t("Tools.separate_by_loose_parts.label"), icon='OUTLINER_OB_MESH') row = layout.row(align=True) row.operator(ApplyTransforms.bl_idname, text=t("Tools.apply_transforms.label"), icon='OBJECT_ORIGIN') + row = layout.row(align=True) row.operator(AvatarToolkit_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') From 40a7f71f079ffabd717848d6e7191cf76dd133a9 Mon Sep 17 00:00:00 2001 From: 989onan Date: Fri, 6 Sep 2024 12:34:06 -0400 Subject: [PATCH 3/4] fix zero weight bones Class name --- functions/armature_modifying.py | 2 +- ui/tools.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/functions/armature_modifying.py b/functions/armature_modifying.py index 8982909..e48408b 100644 --- a/functions/armature_modifying.py +++ b/functions/armature_modifying.py @@ -5,7 +5,7 @@ from ..core.register import register_wrap from .translations import t @register_wrap -class AvatarToolkit_RemoveZeroWeightBones(Operator): +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") diff --git a/ui/tools.py b/ui/tools.py index 8b1b189..8ec1f7e 100644 --- a/ui/tools.py +++ b/ui/tools.py @@ -7,7 +7,7 @@ from ..functions.translations import t from ..core.common import get_selected_armature from ..functions.seperate_by import SeparateByMaterials, SeparateByLooseParts from ..functions.additional_tools import ApplyTransforms -from ..functions.armature_modifying import AvatarToolkit_RemoveZeroWeightBones, AvatarToolkit_OT_MergeBonesToActive, AvatarToolkit_OT_MergeBonesToParents +from ..functions.armature_modifying import AvatarToolkit_OT_RemoveZeroWeightBones, AvatarToolkit_OT_MergeBonesToActive, AvatarToolkit_OT_MergeBonesToParents @register_wrap class AvatarToolkitToolsPanel(bpy.types.Panel): @@ -40,7 +40,7 @@ class AvatarToolkitToolsPanel(bpy.types.Panel): row = layout.row(align=True) row.operator(ApplyTransforms.bl_idname, text=t("Tools.apply_transforms.label"), icon='OBJECT_ORIGIN') row = layout.row(align=True) - row.operator(AvatarToolkit_RemoveZeroWeightBones.bl_idname, text=t("Tools.remove_zero_weight_bones.label"), icon='BONE_DATA') + 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') From 337fb7c56119be9f6b05d207e0fec68f44f7f3fd Mon Sep 17 00:00:00 2001 From: Yusarina Date: Wed, 11 Sep 2024 01:39:35 +0100 Subject: [PATCH 4/4] Update tools.py --- ui/tools.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ui/tools.py b/ui/tools.py index eaa6c36..caec2c2 100644 --- a/ui/tools.py +++ b/ui/tools.py @@ -6,8 +6,8 @@ from ..functions.digitigrade_legs import AvatarToolKit_OT_CreateDigitigradeLegs from ..functions.resonite_functions import AvatarToolKit_OT_ConvertToResonite from ..functions.translations import t from ..core.common import get_selected_armature -from ..functions.seperate_by import SeparateByMaterials, SeparateByLooseParts -from ..functions.additional_tools import ApplyTransforms +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 @@ -36,10 +36,10 @@ class AvatarToolkit_PT_ToolsPanel(bpy.types.Panel): layout.separator() row = layout.row(align=True) layout.label(text=t("Tools.separate_by.label"), icon='MESH_DATA') - row.operator(SeparateByMaterials.bl_idname, text=t("Tools.separate_by_materials.label"), icon='MATERIAL') - row.operator(SeparateByLooseParts.bl_idname, text=t("Tools.separate_by_loose_parts.label"), icon='OUTLINER_OB_MESH') + row.operator(AvatarToolKit_OT_SeparateByMaterials.bl_idname, text=t("Tools.separate_by_materials.label"), icon='MATERIAL') + 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(ApplyTransforms.bl_idname, text=t("Tools.apply_transforms.label"), icon='OBJECT_ORIGIN') + 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)