Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bpy
- import math
- import mathutils
- active_object = bpy.context.active_object
- other_object = None
- all_armatures = True
- sel_obj_count = 0
- for o in bpy.context.selected_objects:
- sel_obj_count += 1
- if o.type != "ARMATURE":
- all_armatures = False
- else:
- if o != active_object:
- other_object = o
- if sel_obj_count == 2 and all_armatures:
- other_object_rest_bone_new_matrices = {}
- active_object_rest_bone_new_matrices = {}
- bpy.ops.object.posemode_toggle()
- for active_object_pose_bone in active_object.pose.bones:
- other_object_rest_bone_new_matrices[active_object_pose_bone.name] = None
- active_object_rest_bone_new_matrices[active_object_pose_bone.name] = None
- bpy.ops.object.posemode_toggle()
- bpy.ops.object.editmode_toggle()
- active_object_bone_parents = {}
- for active_object_rest_bone_name in active_object_rest_bone_new_matrices:
- for rest_b in active_object.data.edit_bones:
- if rest_b.name == active_object_rest_bone_name:
- active_object_rest_parent_matrix = mathutils.Matrix.Identity(3)
- if rest_b.parent:
- active_object_rest_parent_matrix = rest_b.parent.matrix.to_3x3()
- active_object_bone_parents[rest_b.name] = rest_b.parent.name
- active_object_rest_matrix = rest_b.matrix.to_3x3()
- active_object_rest_bone_new_matrices[active_object_rest_bone_name] = (active_object_rest_parent_matrix.transposed() @ active_object_rest_matrix).to_4x4()
- break
- for other_object_rest_bone_name in other_object_rest_bone_new_matrices:
- for rest_b in other_object.data.edit_bones:
- if rest_b.name == other_object_rest_bone_name:
- other_object_rest_parent_matrix = mathutils.Matrix.Identity(3)
- if rest_b.name in active_object_bone_parents:
- active_parent_name = active_object_bone_parents[rest_b.name]
- if active_parent_name in other_object.data.edit_bones:
- other_object_rest_parent_matrix = other_object.data.edit_bones[active_parent_name].matrix.to_3x3()
- elif rest_b.parent:
- other_object_rest_parent_matrix = rest_b.parent.matrix.to_3x3()
- elif rest_b.parent:
- other_object_rest_parent_matrix = rest_b.parent.matrix.to_3x3()
- other_object_rest_matrix = rest_b.matrix.to_3x3()
- other_object_rest_bone_new_matrices[other_object_rest_bone_name] = (other_object_rest_parent_matrix.transposed() @ other_object_rest_matrix).to_4x4()
- break
- bpy.ops.object.editmode_toggle()
- bpy.ops.object.posemode_toggle()
- for active_object_pose_bone in active_object.pose.bones:
- if active_object_pose_bone.name in active_object_rest_bone_new_matrices and active_object_rest_bone_new_matrices[active_object_pose_bone.name]:
- if active_object_pose_bone.name in other_object_rest_bone_new_matrices and other_object_rest_bone_new_matrices[active_object_pose_bone.name]:
- active_object_pose_bone.matrix_basis = other_object_rest_bone_new_matrices[active_object_pose_bone.name].transposed() @ active_object_rest_bone_new_matrices[active_object_pose_bone.name]
- active_object_pose_bone.matrix_basis = active_object_pose_bone.matrix_basis.transposed()
- bpy.ops.object.posemode_toggle()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement