From 7f9dc205640d05a857e98465425a77b8d9649f95 Mon Sep 17 00:00:00 2001 From: Yusarina Date: Tue, 3 Dec 2024 17:40:31 +0000 Subject: [PATCH] Fixes --- core/common.py | 11 ++++++---- functions/armature_modifying.py | 38 ++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/core/common.py b/core/common.py index 2b4c702..719166b 100644 --- a/core/common.py +++ b/core/common.py @@ -469,7 +469,7 @@ def transfer_vertex_weights(context: Context, obj: bpy.types.Object, source_grou modifier = obj.modifiers.new(name="merge_weights", type="VERTEX_WEIGHT_MIX") modifier.show_viewport = True modifier.show_render = True - modifier.mix_set = 'B' # Replace weights in A with weights from B + modifier.mix_set = 'B' modifier.vertex_group_a = target_group modifier.vertex_group_b = source_group modifier.mask_constant = 1.0 @@ -482,12 +482,13 @@ def transfer_vertex_weights(context: Context, obj: bpy.types.Object, source_grou obj.select_set(True) context.view_layer.objects.active = obj - # Move modifier to the top of the stack if necessary + # Move modifier to the top of the stack if len(obj.modifiers) > 1: obj.modifiers.move(obj.modifiers.find(modifier.name), 0) - # Apply modifier - bpy.ops.object.modifier_apply(modifier=modifier.name) + # Apply modifier with correct syntax + with context.temp_override(active_object=obj): + bpy.ops.object.modifier_apply(modifier=modifier.name) # Clean up if delete_source_group and source_group in obj.vertex_groups: @@ -495,3 +496,5 @@ def transfer_vertex_weights(context: Context, obj: bpy.types.Object, source_grou return True + + diff --git a/functions/armature_modifying.py b/functions/armature_modifying.py index 7edaf97..e43b35b 100644 --- a/functions/armature_modifying.py +++ b/functions/armature_modifying.py @@ -145,7 +145,7 @@ class AvatarToolkit_OT_RemoveZeroWeightBones(Operator): bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.select_all(action='DESELECT') - # Store initial transforms + # Modify the initial transforms collection section to include all bones: initial_transforms = {} bpy.ops.object.mode_set(mode='EDIT') for bone in armature.data.edit_bones: @@ -156,14 +156,14 @@ class AvatarToolkit_OT_RemoveZeroWeightBones(Operator): 'matrix': bone.matrix.copy(), 'parent': bone.parent.name if bone.parent else None } - # Add end bones to transforms - if bone.name.endswith('_end'): - initial_transforms[bone.name] = { - 'head': bone.head.copy(), - 'tail': bone.tail.copy(), - 'roll': bone.roll, - 'matrix': bone.matrix.copy(), - 'parent': bone.parent.name if bone.parent else None + # Handle any child bones including _end bones + for child in bone.children: + initial_transforms[child.name] = { + 'head': child.head.copy(), + 'tail': child.tail.copy(), + 'roll': child.roll, + 'matrix': child.matrix.copy(), + 'parent': child.parent.name if child.parent else None } # Get weighted bones @@ -300,14 +300,19 @@ class AvatarToolkit_OT_MergeBonesToParents(Operator): def execute(self, context: Context) -> set[str]: prev_mode = context.mode - + armature = common.get_selected_armature(context) + # Map 'EDIT_ARMATURE' to 'EDIT' for bpy.ops.object.mode_set if prev_mode == 'EDIT_ARMATURE': prev_mode = 'EDIT' - # Switch to Edit Mode + # Set active object and mode + context.view_layer.objects.active = armature + bpy.ops.object.select_all(action='DESELECT') + armature.select_set(True) bpy.ops.object.mode_set(mode='EDIT') - armature_data: Armature = context.view_layer.objects.active.data + + armature_data: Armature = armature.data # Get selected bones in Edit Mode selected_bones = context.selected_editable_bones @@ -322,13 +327,16 @@ class AvatarToolkit_OT_MergeBonesToParents(Operator): bone = armature_data.edit_bones.get(bone_name) if bone and bone.parent: # Transfer weights from bone to its parent + context.view_layer.objects.active = obj common.transfer_vertex_weights( context=context, obj=obj, source_group=bone_name, target_group=bone.parent.name ) - # Ensure we're in Edit Mode after transfer + # Return to armature edit mode + context.view_layer.objects.active = armature + armature.select_set(True) bpy.ops.object.mode_set(mode='EDIT') else: self.report({'WARNING'}, f"Bone '{bone_name}' has no parent or not found; skipping") @@ -347,10 +355,11 @@ class AvatarToolkit_OT_MergeBonesToParents(Operator): self.report({'WARNING'}, f"Bone '{bone_name}' not found in armature; cannot delete") # Return to previous mode + context.view_layer.objects.active = armature + armature.select_set(True) bpy.ops.object.mode_set(mode=prev_mode) return {'FINISHED'} - class AvatarToolkit_OT_MergeArmatures(Operator): bl_idname = "avatar_toolkit.merge_armatures" bl_label = t("MergeArmature.merge_armatures.label") @@ -362,7 +371,6 @@ class AvatarToolkit_OT_MergeArmatures(Operator): return (common.get_selected_armature(context) is not None) and (common.get_merge_armature_source(context) is not None) def make_active(self, obj: bpy.types.Object, context: Context): - context.view_layer.objects.active = obj bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.select_all(action='DESELECT') context.view_layer.objects.active = obj