Connect Bones

- Connect bones button added.
- Added slider so the user can choose the distance, cats was set at a hard coded amount which could sometimes cause some issues for some modals.
This commit is contained in:
Yusarina
2024-09-20 12:03:36 +01:00
parent 218628c053
commit 5086e753c1
3 changed files with 63 additions and 1 deletions
+54
View File
@@ -1,4 +1,5 @@
import bpy import bpy
import math
from bpy.types import Context, Operator from bpy.types import Context, Operator
from ..core.register import register_wrap from ..core.register import register_wrap
from ..core.common import get_selected_armature, is_valid_armature, get_all_meshes from ..core.common import get_selected_armature, is_valid_armature, get_all_meshes
@@ -35,3 +36,56 @@ class AvatarToolKit_OT_ApplyTransforms(Operator):
self.report({'INFO'}, t("Tools.apply_transforms.success")) self.report({'INFO'}, t("Tools.apply_transforms.success"))
return {'FINISHED'} return {'FINISHED'}
@register_wrap
class AvatarToolKit_OT_ConnectBones(Operator):
bl_idname = "avatar_toolkit.connect_bones"
bl_label = t("Tools.connect_bones.label")
bl_description = t("Tools.connect_bones.desc")
bl_options = {'REGISTER', 'UNDO'}
min_distance: bpy.props.FloatProperty(
name=t("Tools.connect_bones.min_distance.label"),
description=t("Tools.connect_bones.min_distance.desc"),
default=0.005,
min=0.001,
max=0.1
)
@classmethod
def poll(cls, context: Context) -> bool:
return get_selected_armature(context) is not None
def execute(self, context: Context) -> set[str]:
armature = get_selected_armature(context)
if not is_valid_armature(armature):
self.report({'ERROR'}, t("Tools.connect_bones.invalid_armature"))
return {'CANCELLED'}
bpy.ops.object.mode_set(mode='EDIT')
edit_bones = armature.data.edit_bones
bones_connected = 0
for bone in edit_bones:
if len(bone.children) == 1 and bone.name not in ['LeftEye', 'RightEye', 'Head', 'Hips']:
child = bone.children[0]
distance = math.dist(bone.head, child.head)
if distance > self.min_distance:
bone.tail = child.head
if bone.parent and len(bone.parent.children) == 1:
bone.use_connect = True
bones_connected += 1
bpy.ops.object.mode_set(mode='OBJECT')
self.report({'INFO'}, t("Tools.connect_bones.success").format(bones_connected=bones_connected))
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
layout = self.layout
layout.prop(self, "min_distance")
+6
View File
@@ -165,6 +165,12 @@
"Tools.merge_bones_to_parents.label": "Merge Bones to Individual 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.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", "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",
"Tools.connect_bones.label": "Connect Bones",
"Tools.connect_bones.desc": "Connect bones with their respective children",
"Tools.connect_bones.invalid_armature": "Invalid armature selected",
"Tools.connect_bones.min_distance.label": "Minimum Distance",
"Tools.connect_bones.min_distance.desc": "Minimum distance between bones to connect them",
"Tools.connect_bones.success": "Connected {bones_connected} bones successfully",
"MergeArmatures.select_armature": "Please select an armature", "MergeArmatures.select_armature": "Please select an armature",
"MergeArmatures.title.label": "Merge Armatures:", "MergeArmatures.title.label": "Merge Armatures:",
"MergeArmatures.label": "Merge Armatures", "MergeArmatures.label": "Merge Armatures",
+3 -1
View File
@@ -8,7 +8,7 @@ from ..functions.translations import t
from ..core.common import get_selected_armature from ..core.common import get_selected_armature
from ..functions.mesh_tools import AvatarToolkit_OT_RemoveUnusedShapekeys from ..functions.mesh_tools import AvatarToolkit_OT_RemoveUnusedShapekeys
from ..functions.seperate_by import AvatarToolKit_OT_SeparateByMaterials, AvatarToolKit_OT_SeparateByLooseParts from ..functions.seperate_by import AvatarToolKit_OT_SeparateByMaterials, AvatarToolKit_OT_SeparateByLooseParts
from ..functions.additional_tools import AvatarToolKit_OT_ApplyTransforms from ..functions.additional_tools import AvatarToolKit_OT_ApplyTransforms, AvatarToolKit_OT_ConnectBones
from ..functions.armature_modifying import AvatarToolkit_OT_RemoveZeroWeightBones, AvatarToolkit_OT_MergeBonesToActive, AvatarToolkit_OT_MergeBonesToParents from ..functions.armature_modifying import AvatarToolkit_OT_RemoveZeroWeightBones, AvatarToolkit_OT_MergeBonesToActive, AvatarToolkit_OT_MergeBonesToParents
@register_wrap @register_wrap
@@ -47,5 +47,7 @@ class AvatarToolkit_PT_ToolsPanel(bpy.types.Panel):
row = layout.row(align=True) 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_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') row.operator(AvatarToolkit_OT_MergeBonesToParents.bl_idname, text=t("Tools.merge_bones_to_parents.label"), icon='BONE_DATA')
row = layout.row(align=True)
row.operator(AvatarToolKit_OT_ConnectBones.bl_idname, text=t("Tools.connect_bones.label"), icon='BONE_DATA')
else: else:
layout.label(text=t("Tools.select_armature"), icon='ERROR') layout.label(text=t("Tools.select_armature"), icon='ERROR')