Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. import bpy
  2. from math import degrees
  3.  
  4.  
  5. def get_local_orientation(pose_bone):
  6. local_orientation = pose_bone.matrix_channel.to_euler()
  7.  
  8. if pose_bone.parent is None:
  9. return (local_orientation.x, local_orientation.y, local_orientation.z)
  10. else:
  11. my_orientation = pose_bone.matrix_channel.copy()
  12. parent_orientation = pose_bone.parent.matrix_channel.copy()
  13.  
  14. my_orientation.invert()
  15. orientation = (my_orientation * parent_orientation).to_euler()
  16.  
  17. return (orientation.x, orientation.y, orientation.z)
  18.  
  19. def get_joint_angles(context):
  20. arm = context.scene.objects['Armature']
  21. pose = arm.pose
  22. bones = pose.bones
  23. joint_names = ['Base','Shoulder','Elbow','Wrist1','Wrist2','Wrist3']
  24. axis_index = {
  25. 'Base': 2,
  26. 'Shoulder': 1,
  27. 'Elbow': 1,
  28. 'Wrist1': 1,
  29. 'Wrist2': 2,
  30. 'Wrist3': 1,
  31. }
  32. axis_correction = {
  33. 'Base': (1, 0),
  34. 'Shoulder': (-1, -math.pi/2),
  35. 'Elbow': (-1, 0),
  36. 'Wrist1': (-1, -math.pi/2),
  37. 'Wrist2': (-1, 0),
  38. 'Wrist3': (-1, 0),
  39. }
  40. joint_angles_by_name = {}
  41. for bone in bones:
  42. joint_angles_by_name[bone.name] = get_local_orientation(bone)
  43.  
  44. joint_angles = []
  45. for name in joint_names:
  46. bl_angle = joint_angles_by_name[name][axis_index[name]]
  47. direction, offset = axis_correction[name]
  48. joint_angle = direction * bl_angle + offset
  49. joint_angles.append(joint_angle)
  50.  
  51. return joint_angles
  52.  
  53.  
  54. class SimpleBoneAnglesPanel(bpy.types.Panel):
  55. bl_label = "Joint Angles"
  56. bl_space_type = 'VIEW_3D'
  57. bl_region_type = 'UI'
  58.  
  59. def draw(self, context):
  60. joint_names = ['Base','Shoulder','Elbow','Wrist1','Wrist2','Wrist3']
  61. joint_angles = joint_angles(context)
  62. row = self.layout.row()
  63. row.label(text='test')
  64. #for z in range(len(joint_angles)):
  65. #row = self.layout.row()
  66. #row.label(text = joint_names[z] + ': {:.3}'.format(degrees(joint_angles[z])))
  67. bpy.utils.register_class(SimpleBoneAnglesPanel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement