export_animations.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import struct
  2. import numpy as np
  3. import bvh
  4. import quat
  5. bvh_files = [
  6. 'ground1_subject1.bvh',
  7. 'ground2_subject2.bvh',
  8. 'kthstreet_gPO_sFM_cAll_d02_mPO_ch01_atombounce_001.bvh',
  9. ]
  10. joints = [
  11. 'Hips',
  12. 'Spine',
  13. 'Spine1',
  14. 'Spine2',
  15. 'Spine3',
  16. 'Neck',
  17. 'Neck1',
  18. 'Head',
  19. 'HeadEnd',
  20. 'RightShoulder',
  21. 'RightArm',
  22. 'RightForeArm',
  23. 'RightHand',
  24. 'RightHandThumb1',
  25. 'RightHandThumb2',
  26. 'RightHandThumb3',
  27. 'RightHandThumb4',
  28. 'RightHandIndex1',
  29. 'RightHandIndex2',
  30. 'RightHandIndex3',
  31. 'RightHandIndex4',
  32. 'RightHandMiddle1',
  33. 'RightHandMiddle2',
  34. 'RightHandMiddle3',
  35. 'RightHandMiddle4',
  36. 'RightHandRing1',
  37. 'RightHandRing2',
  38. 'RightHandRing3',
  39. 'RightHandRing4',
  40. 'RightHandPinky1',
  41. 'RightHandPinky2',
  42. 'RightHandPinky3',
  43. 'RightHandPinky4',
  44. 'RightForeArmEnd',
  45. 'RightArmEnd',
  46. 'LeftShoulder',
  47. 'LeftArm',
  48. 'LeftForeArm',
  49. 'LeftHand',
  50. 'LeftHandThumb1',
  51. 'LeftHandThumb2',
  52. 'LeftHandThumb3',
  53. 'LeftHandThumb4',
  54. 'LeftHandIndex1',
  55. 'LeftHandIndex2',
  56. 'LeftHandIndex3',
  57. 'LeftHandIndex4',
  58. 'LeftHandMiddle1',
  59. 'LeftHandMiddle2',
  60. 'LeftHandMiddle3',
  61. 'LeftHandMiddle4',
  62. 'LeftHandRing1',
  63. 'LeftHandRing2',
  64. 'LeftHandRing3',
  65. 'LeftHandRing4',
  66. 'LeftHandPinky1',
  67. 'LeftHandPinky2',
  68. 'LeftHandPinky3',
  69. 'LeftHandPinky4',
  70. 'LeftForeArmEnd',
  71. 'LeftArmEnd',
  72. 'RightUpLeg',
  73. 'RightLeg',
  74. 'RightFoot',
  75. 'RightToeBase',
  76. 'RightToeBaseEnd',
  77. 'RightLegEnd',
  78. 'RightUpLegEnd',
  79. 'LeftUpLeg',
  80. 'LeftLeg',
  81. 'LeftFoot',
  82. 'LeftToeBase',
  83. 'LeftToeBaseEnd',
  84. 'LeftLegEnd',
  85. 'LeftUpLegEnd',
  86. ]
  87. for bvh_file in bvh_files:
  88. bvh_data = bvh.load(bvh_file)
  89. positions = bvh_data['positions'].copy()
  90. parents = bvh_data['parents'].copy()
  91. names = bvh_data['names'].copy()
  92. rotations = quat.unroll(quat.from_euler(np.radians(bvh_data['rotations']), order=bvh_data['order']))
  93. rotations, positions = quat.fk(rotations, positions, parents)
  94. assert names == joints
  95. # Swap order
  96. rotations = np.concatenate([
  97. rotations[...,1:2],
  98. rotations[...,2:3],
  99. rotations[...,3:4],
  100. rotations[...,0:1],
  101. ], axis=-1).astype(np.float32)
  102. # Convert from cm to m
  103. positions = (0.01 * positions).astype(np.float32)
  104. with open(bvh_file.replace('.bvh', '.bin'), 'wb') as f:
  105. nframes = positions.shape[0]
  106. nbones = positions.shape[1]
  107. f.write(struct.pack('ii', nframes, nbones))
  108. for i in range(nbones):
  109. f.write(struct.pack('32si', bytes(names[i], encoding='ascii'), parents[i]))
  110. for i in range(nframes):
  111. for j in range(nbones):
  112. f.write(struct.pack('ffffffffff',
  113. positions[i,j,0], positions[i,j,1], positions[i,j,2],
  114. rotations[i,j,0], rotations[i,j,1], rotations[i,j,2], rotations[i,j,3],
  115. 1.0, 1.0, 1.0
  116. ))
  117. f.write(struct.pack('32s', bytes(bvh_file.replace('.bvh',''), encoding='ascii')[:31]))