Understood! You want to align the transformation of bones in the "Avatar" rig with those in the "Onigiri" rig based on matching bone names, without changing their hierarchical structure. In this case, instead of parenting, we can simply copy the world transform (location, rotation, and scale) from the "Onigiri" bones to the corresponding "Avatar" bones.
Here's a revised version of the Blender script that handles this:
import bpy
# Names of the armatures
avatar_armature_name = "Avatar"
onigiri_armature_name = "Onigiri"
# Get the armature objects
avatar_armature = bpy.data.objects.get(avatar_armature_name)
onigiri_armature = bpy.data.objects.get(onigiri_armature_name)
# Check if both armatures exist
if avatar_armature is None or onigiri_armature is None:
print(f"One or both armatures '{avatar_armature_name}' and '{onigiri_armature_name}' not found.")
else:
# Ensure both objects are of type armature
if avatar_armature.type == 'ARMATURE' and onigiri_armature.type == 'ARMATURE':
# Switch to pose mode to access bones
bpy.context.view_layer.objects.active = avatar_armature
bpy.ops.object.mode_set(mode='POSE')
avatar_pose_bones = avatar_armature.pose.bones
onigiri_pose_bones = onigiri_armature.pose.bones
# Loop through the Avatar armature bones and match with Onigiri bones
for avatar_bone in avatar_pose_bones:
if avatar_bone.name in onigiri_pose_bones:
onigiri_bone = onigiri_pose_bones[avatar_bone.name]
# Get the world matrix of the Onigiri bone
onigiri_world_matrix = onigiri_armature.matrix_world @ onigiri_bone.matrix
# Apply the world matrix to the Avatar bone
avatar_bone.matrix = onigiri_world_matrix
print(f"Copied transforms from {onigiri_bone.name} to {avatar_bone.name}.")
# Switch back to object mode
bpy.ops.object.mode_set(mode='OBJECT')
else:
print(f"One of the selected objects is not an armature.")
Changes Made:
-
Copy Transforms from World Space: Instead of parenting, the script computes the world transformation matrix of the "Onigiri" bones and applies it directly to the corresponding "Avatar" bones.
-
Using the matrix Property: The matrix property gives us a 4x4 transformation matrix, which is more encompassing because it considers location, rotation, and scale together.
Instructions to Run the Script:
- Open Blender.
- Switch to the Scripting workspace.
- Create a new text block and paste the updated script.
- Ensure the armature names are correct.
- Click the "Run Script" button.
This approach will ensure that even if the bone hierarchy or structure is different, as long as the bone names match, their world transforms will be copied accurately. Let me know if you need any more modifications!