def test_frame_channel(self): with open('tests/test_mocapbank.bvh') as f: mocap = Bvh(f.read()) self.assertEqual(mocap.frame_joint_channel(22, 'Hips', 'Xrotation'), -20.98) self.assertEqual(mocap.frame_joint_channel(22, 'Chest', 'Xrotation'), 17.65) self.assertEqual(mocap.frame_joint_channel(22, 'Neck', 'Xrotation'), -6.77) self.assertEqual(mocap.frame_joint_channel(22, 'Head', 'Yrotation'), 8.47)
def test_frame_iteration(self): with open('tests/test_mocapbank.bvh') as f: mocap = Bvh(f.read()) x_accumulator = 0.0 for i in range(0, mocap.nframes): x_accumulator += mocap.frame_joint_channel(i, 'Hips', 'Xposition') self.assertTrue(abs(-19735.902699999995 - x_accumulator) < 0.0001)
def test_frame_channel2(self): with open('tests/test_freebvh.bvh') as f: mocap = Bvh(f.read()) self.assertEqual(mocap.frame_joint_channel(22, 'mixamorig:Hips', 'Xposition'), 4.3314)
def test_frame_channel_fallback(self): with open('tests/test_mocapbank.bvh') as f: mocap = Bvh(f.read()) self.assertEqual(mocap.frame_joint_channel(22, 'Hips', 'Badrotation', 17), 17)
BVH.save(retargetpath, anim, rest_names, 1.0 / 25, order='xyz') ############################################################################################ with open(constraintpath, 'r') as f: content = f.readlines() content = [x.strip() for x in content] joints = [list(filter(None, c.split('\t'))) for c in content] from bvh import Bvh with open(retargetpath) as f: new_mocap = Bvh(f.read()) channel = ["Xrotation", "Yrotation", "Zrotation"] with open(c_retargetpath, 'w') as f: f.write("Frames: " + str(new_mocap.nframes) + "\n") f.write("Frame Time: " + str(new_mocap.frame_time) + "\n") for fr in range(new_mocap.nframes): out_list = [0, 0, 0] for j in joints: for x, ang in enumerate(j[1:]): if ang != "0": out_list.append( new_mocap.frame_joint_channel(fr, j[0], channel[x])) else: out_list.append(0) frame = [str(a) for a in out_list] f.write(" ".join(frame) + "\n")
class bvhModel(): def __init__( self ): self.Skeleton = [] self.mocapdata = None self.FileName = "" def Load( self, pathname, filename ): self.PathName = pathname self.FileName = filename tempFile = pathname + filename print(tempFile) with open( tempFile ) as f: self.mocapdata = Bvh( f.read() ) self.Skeleton = self.GetSkeleton() return tempFile def GetLength( self ): if self.mocapdata: return self.mocapdata.nframes return 0 def GetSkeleton( self ): self.Skeleton = [] def iterate_joints( joint ): self.Skeleton.append( str( joint )) for child in joint.filter( 'JOINT' ): iterate_joints( child ) iterate_joints( next( self.mocapdata.root.filter( 'ROOT' ))) return self.Skeleton def GetData( self ): return self.mocapdata def AsInputData( self, strType, globalGaugeRef, labelRef, gaugeRef ): ''' This seems to convert a bvh to the corresponding CSV in rogerio's paper, which can be used as input to his classifier. ''' data = [] nFrames = self.GetLength() pos = self.FileName.rfind( "\\" ) + 1 outputFile = "data_" + strType.lower() + "\\" + self.FileName[pos:-3] + "csv" #If an output file is not provided, process the bvh to get corresponding csv input data. #otherwise, load a previous version of the file that has been created and return if not os.path.isfile( outputFile ): gaugeRef.SetValue( 0 ) gaugeOffset = 0 if strType.lower() != "standardized": labelRef.SetLabel( "Analizing: " + self.FileName ) gaugeRef.SetRange( 2 * nFrames ) gaugeOffset = nFrames print( "File: " + self.FileName + " ... analyzing", end = ' ' ) # analyzing data to determine its minimum and maximum values minimumR = minimumP = [99999, 99999, 99999] maximumR = maximumP = [-99999, -99999, -99999] origin = [0,0,0] for frame in range( 1, nFrames ): globalGaugeRef.SetValue( globalGaugeRef.GetValue() + 1 ) for joint in self.Skeleton: jointName = joint.split(" ")[1] i = 0 for channel in self.mocapdata.joint_channels( jointName ): channelValue = self.mocapdata.frame_joint_channel( frame, jointName, channel ) if channel[1:] != "position": if minimumR[i] > channelValue: minimumR[i] = channelValue if maximumR[i] < channelValue: maximumR[i] = channelValue else: if frame == 1: origin[i] = channelValue if minimumP[i] > channelValue: minimumP[i] = channelValue if maximumP[i] < channelValue: maximumP[i] = channelValue i = (i+1) % 3 gaugeRef.SetValue( frame + 1 ) labelRef.SetLabel( "Normalizing: " + self.FileName ) print( "normalizing" ) else: gaugeRef.SetRange( nFrames ) labelRef.SetLabel( "Loading: " + self.FileName ) print( "File: " + self.FileName + " ... loading" ) header = "" structure = "" for frame in range( 1, nFrames ): globalGaugeRef.SetValue( globalGaugeRef.GetValue() + 1 ) frameData = [] for joint in self.Skeleton: jointName = joint.split(" ")[1] i = 0 for channel in self.mocapdata.joint_channels( jointName ): if frame == 1: if header == "": header = jointName + "_" + channel else: header += "," + jointName + "_" + channel structure += jointName + "_" + channel + "\n" channelValue = self.mocapdata.frame_joint_channel( frame, jointName, channel ) if channel[1:] != "position": if strType.lower() == "normalized": channelValue = ( channelValue - minimumR[i] ) / ( maximumR[i] - minimumR[i] ) elif strType.lower() == "rescaled": channelValue = 2 * (( channelValue - minimumR[i] ) / ( maximumR[i] - minimumR[i] )) - 1 else: if strType.lower() != "standardized": channelValue -= origin[i] if strType.lower() == "normalized": if i == 1: channelValue = ( channelValue - minimumP[i] ) / ( maximumP[i] - minimumP[i] ) else: channelValue = 0 elif strType.lower() == "rescaled": channelValue = 2 * (( channelValue - minimumP[i] ) / ( maximumP[i] - minimumP[i] )) - 1 frameData.append( channelValue ) i = (i+1) % 3 data.append( frameData ) gaugeRef.SetValue( frame + gaugeOffset + 1 ) self.SaveAsCSV( data, header, outputFile ) fileNode = open( "structure.txt","w" ) fileNode.write( structure ) fileNode.close() else: labelRef.SetLabel( "Loading previous version of file: " + self.FileName ) print( "Loading previous version of file: " + self.FileName ) gaugeRef.SetRange( 1 ) gaugeRef.SetValue( 1 ) globalGaugeRef.SetValue( globalGaugeRef.GetValue() + nFrames * 2 ) data = np.array( read_csv( outputFile ) ).tolist() #data = np.array( read_csv( outputFile, header = None ) ).tolist() return np.array( data ) def SaveAsCSV( self, data, header, outputFile ): fileNode = open( outputFile,"w" ) fileNode.write( header + "\n" ) for entry in data: entryStr = "" for value in entry: if entryStr != "": entryStr += "," entryStr += str( value ) fileNode.write( entryStr + "\n" ) fileNode.close()
class MotionClip(object): def __init__(self, mocap_file: str, constraints_file: str): with open(mocap_file) as f: self.mocap = Bvh(f.read()) with open(constraints_file) as f: content = f.readlines() content = [x.strip() for x in content] content = [x.split() for x in content] channel_mapping = ['Xrotation', 'Yrotation', 'Zrotation'] self.mapping = {} for joint in content: self.mapping[joint[0]] = [] for i in range(1, 4): if (joint[i] != '0'): self.mapping[joint[0]].append( (channel_mapping[i - 1], joint[i].lower())) def get_pose(self, time: float): pose = {} frame = math.floor(time / self.mocap.frame_time) if frame >= self.mocap.nframes: print("(get_pose) frame >= " + str(frame >= self.mocap.nframes)) return None # print("(get_pose) FN : ", time , frame) for joint in self.mocap.get_joints_names(): for channel in self.mapping[joint]: curr_angle = self.mocap.frame_joint_channel( frame, joint, channel[0]) pose[str(channel[1]).lower()] = curr_angle # pose["lle1"] = 0 # pose["rle1"] = 0 # pose["lle2"] = 0 # pose["rle2"] = 0 return pose def similarity(self, time, actual_pose, keys): target_pose = self.get_pose(time) if target_pose is not None: ret = self.get_pose(time + self.mocap.frame_time) for joint in ret: # if joint in [ # "he2", # "lae1", # "lle3", # "lle4", # "lle5", # "rae1", # "rle3", # "rle4", # "rle5", # ]: # ret[joint] *= -1 ret[joint] = np.clip(ret[joint], clip_limits[joint][0], clip_limits[joint][1]) # if joint in blacklist: # ret[joint] = 0 return ret, self.euclead_distance(actual_pose, target_pose, keys) def euclead_distance(self, a, b, keys): ans = 0 for x in b.keys(): if x in keys: # if x in [ # "he2", # "lae1", # "lle3", # "lle4", # "lle5", # "rae1", # "rle3", # "rle4", # "rle5", # ]: # b[x] *= -1 b[x] = np.clip(b[x], clip_limits[x][0], clip_limits[x][1]) # print("(Diff) ", x, a[x], b[x]) ans += (a[x] - b[x]) * (a[x] - b[x]) return ans
mocap = Bvh(f.read()) frame_num = list(range(mocap.nframes)) joint_position_x = list() joint_position_y = list() joint_position_z = list() item = 'Hips' #RightArm Hips Spine LeftArm print(item) for frame in range(0, mocap.nframes): #0,mocap.nframes #join_position_x if frame == 0: joint_position_x.append( mocap.frame_joint_channel(frame, item, 'Xrotation')) else: if (mocap.frame_joint_channel(frame, item, 'Xrotation') - joint_position_x[frame - 1]) > 70: joint_position_x.append( 180 - mocap.frame_joint_channel(frame, item, 'Xrotation')) elif (mocap.frame_joint_channel(frame, item, 'Xrotation') - joint_position_x[frame - 1]) < -70: joint_position_x.append( 180 + mocap.frame_joint_channel(frame, item, 'Xrotation')) else: joint_position_x.append( mocap.frame_joint_channel(frame, item, 'Xrotation')) if frame == 0: joint_position_y.append(
#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))
with open('take004_point_tired_chr00_frame90.bvh', 'r') as f: mocap = Bvh(f.read()) frame_num = list(range(mocap.nframes)) joint_position = list() joint_position_x = list() joint_position_y = list() joint_position_z = list() item = 'LeftForeArm' print(item) for frame in range(0, mocap.nframes): #0,mocap.nframes x = mocap.frame_joint_channel(frame, item, 'Xposition') y = mocap.frame_joint_channel(frame, item, 'Yposition') z = mocap.frame_joint_channel(frame, item, 'Zposition') joint_position_x.append(x) joint_position_y.append(y) joint_position_z.append(z) joint_position.append(((x**2) + (y**2) + (z**2))**(1 / 2)) #fig = plt.figure() routine_x = list() routine_y = list() routine_z = list() routine = list() routine_num = list(range(100))
# for all DeepMimicHumanoid Joints for p in range(0, len(deepMimicHumanoidJoints)): # Append Time if p == 0: keyFrame.append(mocap.frame_time) # Append root position elif p == 1: if posLocked: keyFrame.append(2) keyFrame.append(2) keyFrame.append(2) else: keyFrame.append( mocap.frame_joint_channel( i, bvhBoneName(deepMimicHumanoidJoints[p]), 'Xposition')) keyFrame.append( mocap.frame_joint_channel( i, bvhBoneName(deepMimicHumanoidJoints[p]), 'Yposition')) keyFrame.append( mocap.frame_joint_channel( i, bvhBoneName(deepMimicHumanoidJoints[p]), 'Zposition')) elif dimensions[p] == 1: keyFrame.append( math.radians( mocap.frame_joint_channel( i, bvhBoneName(deepMimicHumanoidJoints[p]),
with open('../processed/test.skl', 'w') as f: f.write(start_skill_str + "\n") for frame in range(0, min(mocap.nframes, 120), 3): # print(frame, mocap.frame_joint_channel(frame, "LeftThigh", "Xrotation"), mocap.frame_joint_channel(frame, "LeftThigh", "Yrotation"), mocap.frame_joint_channel(frame, "LeftThigh", "Zrotation")) if (frame == 0): frame_data_str = "settar " else: frame_data_str = "inctar " wait_frame_str = "wait " + str( round(k * mocap.frame_time * (frame - prev_frame), 1)) + " end\n" write = False for joint in mocap.get_joints_names(): for channel in mapping[joint]: this_frame_val = mocap.frame_joint_channel( frame, joint, channel[0]) if frame == 0: prev_frame_val = 0 else: prev_frame_val = mocap.frame_joint_channel( prev_frame, joint, channel[0]) print(frame, prev_frame, channel, this_frame_val, prev_frame_val) if "A" not in channel[1] and abs(this_frame_val - prev_frame_val) > theta: write = True frame_data_str += " " + str(channel[1]) + " " + str( round(this_frame_val - prev_frame_val, 1)) if (write): prev_frame = frame