def get_bvh_offsets_and_animation(file_name, frame_number): with open(file_name) as f: mocap = Bvh(f.read()) mocap_joint_names = mocap.get_joints_names() mocap_joint_prefix = mocap_joint_names[0].split('Hips')[0] rotation_matrices = np.empty((1, 3, 3), dtype=float) offsets = np.empty((1, 3), dtype=float) root_position = mocap.frame_joint_channels( frame_number, mocap_joint_names[0], ['Xposition', 'Yposition', 'Zposition']) for name in joint_names: # predefined joint_names mocap_joint_name = mocap_joint_prefix + name euler = mocap.frame_joint_channels( frame_number, mocap_joint_name, ['Xrotation', 'Yrotation', 'Zrotation']) # print(name, euler) # rot = R.from_euler('xyz', euler, degrees=False) # Previous models are trained for wrong rotations rot = R.from_euler('XYZ', euler, degrees=True) # should we use 'XYZ', degrees=True? rot = np.array(rot.as_matrix()) rot = rot.reshape(1, 3, 3) rotation_matrices = np.append(rotation_matrices, rot, axis=0) offset = np.array(mocap.joint_offset(mocap_joint_name)) offsets = np.append(offsets, offset.reshape(1, 3), axis=0) # print('{}:\nRotationMatrix:\n'.format(name), rot[0], '\nOffset: ', offset.reshape(3, )) offsets = np.delete(offsets, [0, 0], axis=0) rotation_matrices = np.delete(rotation_matrices, [0, 0, 0], axis=0) return offsets, rotation_matrices, root_position #[3], [J, 3], [J, 3]
def load_bvh_file(fname, skeleton): with open(fname) as f: mocap = Bvh(f.read()) # build bone_addr bone_addr = dict() start_ind = 0 for bone in skeleton.bones: end_ind = start_ind + len(bone.channels) bone_addr[bone.name] = (start_ind, end_ind) start_ind = end_ind dof_num = start_ind poses = np.zeros((mocap.nframes, dof_num)) for i in range(mocap.nframes): for bone in skeleton.bones: trans = np.array( mocap.frame_joint_channels(i, bone.name, bone.channels)) if bone == skeleton.root: trans[:3] *= skeleton.len_scale trans[3:6] = np.deg2rad(trans[3:6]) else: trans = np.deg2rad(trans) start_ind, end_ind = bone_addr[bone.name] poses[i, start_ind:end_ind] = trans return poses, bone_addr
def test_frame_joint_multi_channels(self): with open('tests/test_mocapbank.bvh') as f: mocap = Bvh(f.read()) rotation = mocap.frame_joint_channels(30, 'Head', ['Xrotation', 'Yrotation', 'Zrotation']) self.assertEqual(rotation, [1.77, 13.94, -7.42])
from bvh import Bvh with open('Male2_A1_Stand.bvh') as f: mocap = Bvh(f.read()) for name in mocap.get_joints_names(): print(name + " : ", mocap.frame_joint_channels(0, name, mocap.joint_channels(name)))