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_joint_parent(self): with open('tests/test_mocapbank.bvh') as f: mocap = Bvh(f.read()) self.assertEqual(mocap.joint_parent('Chest').name, 'Hips')
#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 print(len(joints))