Advertisement
Guest User

Inverse bone poser 0.2 (blender 2.93)

a guest
Nov 26th, 2023
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.57 KB | Source Code | 0 0
  1. import bpy
  2. import math
  3. import mathutils
  4.  
  5. active_object = bpy.context.active_object
  6. other_object = None
  7. all_armatures = True
  8. sel_obj_count = 0
  9. for o in bpy.context.selected_objects:
  10.     sel_obj_count += 1
  11.     if o.type != "ARMATURE":
  12.         all_armatures = False
  13.     else:
  14.         if o != active_object:
  15.             other_object = o
  16.  
  17. if sel_obj_count == 2 and all_armatures:
  18.    
  19.     other_object_rest_bone_new_matrices = {}
  20.     active_object_rest_bone_new_matrices = {}
  21.    
  22.     bpy.ops.object.posemode_toggle()
  23.    
  24.     for active_object_pose_bone in active_object.pose.bones:
  25.         other_object_rest_bone_new_matrices[active_object_pose_bone.name] = None
  26.         active_object_rest_bone_new_matrices[active_object_pose_bone.name] = None
  27.    
  28.     bpy.ops.object.posemode_toggle()
  29.     bpy.ops.object.editmode_toggle()
  30.    
  31.     active_object_bone_parents = {}
  32.    
  33.    
  34.     for active_object_rest_bone_name in active_object_rest_bone_new_matrices:
  35.         for rest_b in active_object.data.edit_bones:
  36.             if rest_b.name == active_object_rest_bone_name:
  37.                
  38.                 active_object_rest_parent_matrix = mathutils.Matrix.Identity(3)
  39.                 if rest_b.parent:
  40.                     active_object_rest_parent_matrix = rest_b.parent.matrix.to_3x3()
  41.                     active_object_bone_parents[rest_b.name] = rest_b.parent.name
  42.                 active_object_rest_matrix = rest_b.matrix.to_3x3()
  43.                
  44.                 active_object_rest_bone_new_matrices[active_object_rest_bone_name] = (active_object_rest_parent_matrix.transposed() @ active_object_rest_matrix).to_4x4()
  45.                 break
  46.    
  47.     for other_object_rest_bone_name in other_object_rest_bone_new_matrices:
  48.         for rest_b in other_object.data.edit_bones:
  49.             if rest_b.name == other_object_rest_bone_name:
  50.                
  51.                 other_object_rest_parent_matrix = mathutils.Matrix.Identity(3)
  52.                 if rest_b.name in active_object_bone_parents:
  53.                     active_parent_name = active_object_bone_parents[rest_b.name]
  54.                     if active_parent_name in other_object.data.edit_bones:
  55.                         other_object_rest_parent_matrix = other_object.data.edit_bones[active_parent_name].matrix.to_3x3()
  56.                     elif rest_b.parent:
  57.                         other_object_rest_parent_matrix = rest_b.parent.matrix.to_3x3()
  58.                 elif rest_b.parent:
  59.                     other_object_rest_parent_matrix = rest_b.parent.matrix.to_3x3()
  60.                 other_object_rest_matrix = rest_b.matrix.to_3x3()
  61.                
  62.                 other_object_rest_bone_new_matrices[other_object_rest_bone_name] = (other_object_rest_parent_matrix.transposed() @ other_object_rest_matrix).to_4x4()
  63.                 break
  64.    
  65.     bpy.ops.object.editmode_toggle()
  66.     bpy.ops.object.posemode_toggle()
  67.    
  68.     for active_object_pose_bone in active_object.pose.bones:
  69.         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]:
  70.             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]:
  71.                 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]
  72.                 active_object_pose_bone.matrix_basis = active_object_pose_bone.matrix_basis.transposed()
  73.    
  74.     bpy.ops.object.posemode_toggle()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement