Guest User

Inverse bone poser (blender 2.93)

a guest
Nov 26th, 2023
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 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.     bpy.ops.object.posemode_toggle()
  20.     other_object_rest_bone_new_matrices = {}
  21.     active_object_rest_bone_new_matrices = {}
  22.    
  23.     for active_object_pose_bone in active_object.pose.bones:
  24.         other_object_rest_bone_new_matrices[active_object_pose_bone.name] = None
  25.         active_object_rest_bone_new_matrices[active_object_pose_bone.name] = None
  26.    
  27.     bpy.ops.object.posemode_toggle()
  28.     bpy.ops.object.editmode_toggle()
  29.    
  30.     for other_object_rest_bone_name in other_object_rest_bone_new_matrices:
  31.         for rest_b in other_object.data.edit_bones:
  32.             if rest_b.name == other_object_rest_bone_name:
  33.                
  34.                 other_object_rest_parent_matrix = mathutils.Matrix.Identity(3)
  35.                 if rest_b.parent:
  36.                     other_object_rest_parent_matrix = rest_b.parent.matrix.to_3x3()
  37.                 other_object_rest_matrix = rest_b.matrix.to_3x3()
  38.                
  39.                 other_object_rest_bone_new_matrices[other_object_rest_bone_name] = (other_object_rest_parent_matrix.transposed() @ other_object_rest_matrix).to_4x4()
  40.                 break
  41.    
  42.     for active_object_rest_bone_name in active_object_rest_bone_new_matrices:
  43.         for rest_b in active_object.data.edit_bones:
  44.             if rest_b.name == active_object_rest_bone_name:
  45.                
  46.                 active_object_rest_parent_matrix = mathutils.Matrix.Identity(3)
  47.                 if rest_b.parent:
  48.                     active_object_rest_parent_matrix = rest_b.parent.matrix.to_3x3()
  49.                 active_object_rest_matrix = rest_b.matrix.to_3x3()
  50.                
  51.                 active_object_rest_bone_new_matrices[active_object_rest_bone_name] = (active_object_rest_parent_matrix.transposed() @ active_object_rest_matrix).to_4x4()
  52.                 break
  53.    
  54.     bpy.ops.object.editmode_toggle()
  55.     bpy.ops.object.posemode_toggle()
  56.    
  57.     for active_object_pose_bone in active_object.pose.bones:
  58.         if active_object_pose_bone.name in active_object_rest_bone_new_matrices and active_object_pose_bone.parent:
  59.             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]
  60.             active_object_pose_bone.matrix_basis = active_object_pose_bone.matrix_basis.transposed()
  61.    
  62.     bpy.ops.object.posemode_toggle()
Advertisement
Add Comment
Please, Sign In to add comment