Skip to content

Commit

Permalink
Merge pull request #115 from fmnijk/main
Browse files Browse the repository at this point in the history
Solve bone id not unique issue when joining models
  • Loading branch information
UuuNyaa authored Nov 10, 2023
2 parents 4e538c7 + b0c3498 commit e2742c3
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions mmd_tools/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,50 @@ def join_models(parent_root_object: bpy.types.Object, child_root_objects: List[b
'selected_editable_objects': [parent_armature_object],
}, location=True, rotation=True, scale=True)

def _change_bone_id(bone, new_bone_id, root):
"""This function will also update the references of bone morphs and rotate+/move+."""
bone_id = bone.mmd_bone.bone_id

# Change Bone ID
bone.mmd_bone.bone_id = new_bone_id

# Update Relative Bone Morph # Update the reference of bone morph # 更新骨骼表情
for bone_morph in root.mmd_root.bone_morphs:
for data in bone_morph.data:
if data.bone_id == bone_id:
data.bone_id = new_bone_id

# Update Relative Additional Transform # Update the reference of rotate+/move+ # 更新付与親
armature = FnModel.find_armature(root)
pose_bones = armature.pose.bones
for pose_bone in pose_bones:
if pose_bone.is_mmd_shadow_bone:
continue
mmd_bone = pose_bone.mmd_bone
if mmd_bone.additional_transform_bone_id == bone_id:
mmd_bone.additional_transform_bone_id = new_bone_id

# Change the Bone ID if necessary to make sure that each ID is still unique after joining models.
max_bone_id = -1
armature = FnModel.find_armature(parent_root_object)
pose_bones = armature.pose.bones
for pose_bone in pose_bones:
if pose_bone.is_mmd_shadow_bone:
continue
mmd_bone = pose_bone.mmd_bone
if mmd_bone.bone_id > max_bone_id:
max_bone_id = mmd_bone.bone_id
child_root_object: bpy.types.Object
for child_root_object in child_root_objects:
armature = FnModel.find_armature(child_root_object)
pose_bones = armature.pose.bones
for pose_bone in pose_bones:
if pose_bone.is_mmd_shadow_bone:
continue
if pose_bone.mmd_bone.bone_id != -1:
max_bone_id += 1
_change_bone_id(pose_bone, max_bone_id, child_root_object)

child_root_object: bpy.types.Object
for child_root_object in child_root_objects:
child_model = Model(child_root_object)
Expand Down

0 comments on commit e2742c3

Please sign in to comment.