Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. import bpy
  2.  
  3. target = bpy.data.objects['Target']
  4. source = bpy.data.objects['Source']
  5.  
  6.  
  7. for pb in source.pose.bones:
  8. pb.rotation_mode = 'YXZ'
  9.  
  10. for pb in target.pose.bones:
  11. pb.rotation_mode = 'YXZ'
  12.  
  13. sourceroll = {}
  14. bpy.ops.object.select_all(action='DESELECT')
  15. source.select = True
  16. bpy.context.scene.objects.active = source
  17. bpy.ops.object.mode_set(mode='EDIT')
  18. bpy.ops.armature.select_all(action='SELECT')
  19.  
  20. for b in source.data.edit_bones:
  21. sourceroll.update({b.name:b.roll})
  22.  
  23. bpy.ops.object.mode_set(mode='OBJECT')
  24.  
  25. bpy.ops.object.select_all(action='DESELECT')
  26. target.select = True
  27. bpy.context.scene.objects.active = target
  28. bpy.ops.object.mode_set(mode='EDIT')
  29. bpy.ops.armature.select_all(action='SELECT')
  30.  
  31. for b in target.data.edit_bones:
  32. b.roll = sourceroll[b.name]
  33.  
  34. bpy.ops.object.mode_set(mode='OBJECT')
  35.  
  36. import bpy
  37. import math
  38.  
  39. def align_bone_x_axis(edit_bone, new_x_axis):
  40. """ new_x_axis is a 3D Vector the edit_bone's x-axis will point towards.
  41. """
  42. new_x_axis = new_x_axis.cross(edit_bone.y_axis)
  43. new_x_axis.normalize()
  44. dot = max(-1.0, min(1.0, edit_bone.z_axis.dot(new_x_axis)))
  45. angle = math.acos(dot)
  46. edit_bone.roll += angle
  47. dot1 = edit_bone.z_axis.dot(new_x_axis)
  48. edit_bone.roll -= angle * 2.0
  49. dot2 = edit_bone.z_axis.dot(new_x_axis)
  50. if dot1 > dot2:
  51. edit_bone.roll += angle * 2.0
  52.  
  53. source = bpy.data.objects['Source']
  54. target = bpy.data.objects['Target']
  55. source_to_world_matrix = source.matrix_world.to_3x3()
  56. world_to_target_matrix = target.matrix_world.inverted().to_3x3()
  57.  
  58. source_roll = {}
  59. source_x_axis = {}
  60. bpy.ops.object.select_all(action='DESELECT')
  61. source.select = True
  62. bpy.context.scene.objects.active = source
  63. bpy.ops.object.mode_set(mode='EDIT')
  64. bpy.ops.armature.select_all(action='SELECT')
  65.  
  66. for b in source.data.edit_bones:
  67. source_roll[b.name] = b.roll
  68. source_x_axis[b.name] = source_to_world_matrix * b.x_axis
  69.  
  70. bpy.ops.object.mode_set(mode='OBJECT')
  71.  
  72. bpy.ops.object.select_all(action='DESELECT')
  73. target.select = True
  74. bpy.context.scene.objects.active = target
  75. bpy.ops.object.mode_set(mode='EDIT')
  76. bpy.ops.armature.select_all(action='SELECT')
  77.  
  78. for b in target.data.edit_bones:
  79. b.roll = source_roll[b.name]
  80. align_bone_x_axis(b, world_to_target_matrix * source_x_axis[b.name])
  81.  
  82. bpy.ops.object.mode_set(mode='OBJECT')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement