make merge armature use the new identify bones method
This commit is contained in:
@@ -2,7 +2,6 @@ import bpy
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from typing import List, Optional, Dict, Set, Tuple, Any
|
from typing import List, Optional, Dict, Set, Tuple, Any
|
||||||
from bpy.types import Context, Object, Operator, ArmatureModifier, EditBone, VertexGroup, Mesh, ShapeKey
|
from bpy.types import Context, Object, Operator, ArmatureModifier, EditBone, VertexGroup, Mesh, ShapeKey
|
||||||
from ...core.dictionaries import bone_names
|
|
||||||
from ...core.logging_setup import logger
|
from ...core.logging_setup import logger
|
||||||
from ...core.translations import t
|
from ...core.translations import t
|
||||||
from ...core.common import (
|
from ...core.common import (
|
||||||
@@ -11,6 +10,7 @@ from ...core.common import (
|
|||||||
clear_unused_data_blocks,
|
clear_unused_data_blocks,
|
||||||
join_mesh_objects,
|
join_mesh_objects,
|
||||||
remove_unused_shapekeys,
|
remove_unused_shapekeys,
|
||||||
|
identify_bones,
|
||||||
)
|
)
|
||||||
from ...core.dictionaries import simplify_bonename
|
from ...core.dictionaries import simplify_bonename
|
||||||
|
|
||||||
@@ -175,44 +175,22 @@ def merge_armatures(
|
|||||||
for bone in merge_armature_data.bones:
|
for bone in merge_armature_data.bones:
|
||||||
original_parents[bone.name] = bone.parent.name if bone.parent else None
|
original_parents[bone.name] = bone.parent.name if bone.parent else None
|
||||||
|
|
||||||
#create reverse lookup
|
|
||||||
reverse_bone_lookup = {}
|
|
||||||
for preferred_name, name_list in bone_names.items():
|
|
||||||
for name in name_list:
|
|
||||||
reverse_bone_lookup[name] = preferred_name
|
|
||||||
|
|
||||||
# Get base bone names
|
|
||||||
base_bone_names: Set[str] = {bone.name for bone in base_armature.data.bones}
|
|
||||||
|
|
||||||
base_armature_standards: Dict[str,Optional[str]] = {}
|
|
||||||
for bone in base_bone_names:
|
|
||||||
if simplify_bonename(bone) in reverse_bone_lookup:
|
|
||||||
base_armature_standards[reverse_bone_lookup[simplify_bonename(bone)]] = bone
|
|
||||||
|
|
||||||
# Switch to edit mode on merge armature and rename bones
|
# Switch to edit mode on merge armature and rename bones
|
||||||
bpy.context.view_layer.objects.active = merge_armature
|
bpy.context.view_layer.objects.active = merge_armature
|
||||||
bpy.ops.object.mode_set(mode='EDIT')
|
bpy.ops.object.mode_set(mode='EDIT')
|
||||||
|
|
||||||
# Handle bone renaming/removing to target armature.
|
# Identify our bones to what their standard name is like "hips" for source and target armature bones.
|
||||||
bone_names_source: list[str] = [bone.name for bone in merge_armature_data.edit_bones]
|
identifed_base_bone_names: Dict[str,str] = identify_bones(base_armature.data)
|
||||||
for bone in bone_names_source:
|
identified_bone_names_source: Dict[str,str] = identify_bones(merge_armature_data)
|
||||||
bone_name = bone
|
|
||||||
if bone_name not in base_bone_names: #not auto mergable to original
|
|
||||||
|
|
||||||
if simplify_bonename(bone_name) in reverse_bone_lookup: #if is a standard bone through standard translation.
|
for standard,bone_name in identified_bone_names_source.items():
|
||||||
if reverse_bone_lookup[simplify_bonename(bone_name)] in base_armature_standards: #if this bone equals for example, "hips", does a bone that should be "hips" exist on our target armature?
|
if standard in identifed_base_bone_names: #if the bone we are at on our merge armature has a standard name translation for the target armature
|
||||||
#if so, rename this bone to that one
|
merge_armature_data.edit_bones[bone_name].name = identifed_base_bone_names[standard] #change it's name to the one on the target merge to armature's coorisponding standard bone
|
||||||
merge_armature_data.edit_bones[bone_name].name = base_armature_standards[reverse_bone_lookup[simplify_bonename(bone_name)]]
|
bone_name = merge_armature_data.edit_bones[bone_name].name
|
||||||
bone_name = merge_armature_data.edit_bones[bone_name].name
|
#adjust original parents list to point to the new name.
|
||||||
#adjust original parents list to point to the new name.
|
for child_bone in merge_armature_data.edit_bones[bone_name]:
|
||||||
for child_bone in merge_armature_data.edit_bones[bone_name]:
|
original_parents[child_bone.name] = bone_name
|
||||||
original_parents[child_bone.name] = bone_name
|
#then remove so it doesn't clash when merged.
|
||||||
#then remove so it doesn't clash when merged.
|
|
||||||
merge_armature_data.edit_bones.remove(merge_armature_data.edit_bones[bone_name])
|
|
||||||
continue
|
|
||||||
|
|
||||||
#if it really doesn't have a counter part, just don't bother.
|
|
||||||
else:
|
|
||||||
merge_armature_data.edit_bones.remove(merge_armature_data.edit_bones[bone_name])
|
merge_armature_data.edit_bones.remove(merge_armature_data.edit_bones[bone_name])
|
||||||
|
|
||||||
# Return to object mode
|
# Return to object mode
|
||||||
|
|||||||
Reference in New Issue
Block a user