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_from_bvh(self, fname, exclude_bones=None, spec_channels=None): if exclude_bones is None: exclude_bones = {} if spec_channels is None: spec_channels = dict() with open(fname) as f: mocap = Bvh(f.read()) joint_names = list( filter(lambda x: all([t not in x for t in exclude_bones]), mocap.get_joints_names())) dof_ind = {'x': 0, 'y': 1, 'z': 2} self.len_scale = 0.0254 self.root = Bone() self.root.id = 0 self.root.name = joint_names[0] self.root.channels = mocap.joint_channels(self.root.name) self.name2bone[self.root.name] = self.root self.bones.append(self.root) for i, joint in enumerate(joint_names[1:]): bone = Bone() bone.id = i + 1 bone.name = joint bone.channels = spec_channels[joint] if joint in spec_channels.keys( ) else mocap.joint_channels(joint) bone.dof_index = [dof_ind[x[0].lower()] for x in bone.channels] bone.offset = np.array(mocap.joint_offset(joint)) * self.len_scale bone.lb = [-180.0] * 3 bone.ub = [180.0] * 3 self.bones.append(bone) self.name2bone[joint] = bone for bone in self.bones[1:]: parent_name = mocap.joint_parent(bone.name).name if parent_name in self.name2bone.keys(): bone_p = self.name2bone[parent_name] bone_p.child.append(bone) bone.parent = bone_p self.forward_bvh(self.root) for bone in self.bones: if len(bone.child) == 0: bone.end = bone.pos + np.array([ float(x) for x in mocap.get_joint(bone.name).children[-1]['OFFSET'] ]) * self.len_scale else: bone.end = sum([bone_c.pos for bone_c in bone.child]) / len(bone.child)
def test_unknown_joint(self): with open('tests/test_mocapbank.bvh') as f: mocap = Bvh(f.read()) with self.assertRaises(LookupError): mocap.joint_offset('FooBar')
def test_joint_offset(self): with open('tests/test_mocapbank.bvh') as f: mocap = Bvh(f.read()) self.assertEqual(mocap.joint_offset('RightElbow'), (-2.6865, -25.0857, 1.2959))
with open('../Data/BVH/Vasso_Miserable_0.bvh') as data: mocap = Bvh(data.read()) #Get Mocap Tree tree = [str(item) for item in mocap.root] #['HIERARCHY', 'ROOT Hips', 'MOTION', 'Frames: 1068', 'Frame Time: 0.0333333'] #Get ROOT OFFSET root = next(mocap.root.filter('ROOT'))['OFFSET'] #['3.93885', '96.9818', '-23.2913'] #Get all JOINT names joints = mocap.get_joints_names() #54 mocap.get_joints_names()[17] #All Names mocap.joint_offset('Head') #Offset mocap.joint_channels('Neck') #Channels mocap.get_joint_channels_index('Spine') # mocap.joint_parent_index('Neck') #Parent Index mocap.joint_parent('Head').name # Parent Name #mocap.joint_direct_children('Hips') #Direct Children #Get Frames mocap.nframes mocap.frame_time mocap.frame_joint_channel(22, 'Spine', 'Xrotation') #SEARCH [str(node) for node in mocap.search('JOINT', 'LeftShoulder')] #Single Item [str(node) for node in mocap.search('JOINT')] #All Items