def get_empty_bone_anim(cls, name, values=[1.0, 1.0, 1.0, 0, 0, 0, 0, 0, 0], angle_scale=0): new_anim = bone_anim() new_anim.name = name new_anim.add_scale("X", j3d.AnimComponent(0, values[0])) new_anim.add_scale("Y", j3d.AnimComponent(0, values[1])) new_anim.add_scale("Z", j3d.AnimComponent(0, values[2])) new_anim.add_rotation("X", j3d.AnimComponent(0, values[3])) new_anim.add_rotation("Y", j3d.AnimComponent(0, values[4])) new_anim.add_rotation("Z", j3d.AnimComponent(0, values[5])) new_anim.add_translation("X", j3d.AnimComponent(0, values[6])) new_anim.add_translation("Y", j3d.AnimComponent(0, values[7])) new_anim.add_translation("Z", j3d.AnimComponent(0, values[8])) return new_anim
def from_table(cls, f, info): blk = cls(int(info[0][1]), int(info[0][3])) keyframes = [] print("filename " + f) frame_offset = 1 if f == "" or info[1][1] == "Duration": frame_offset = 2 blk.tan_type = 1 else: blk.tan_type = int(info[0][5]) for i in range(frame_offset, len(info[1])): if info[1][i] != "": text = info[1][i][6:] text = int(text) keyframes.append(text) print("keyframes") print(keyframes) for i in range(2, len(info)): #for each cluster current_anim = cluster_anim() for k in range(frame_offset, len(info[i])): #for each keyframe if info[i][k] != "": comp = j3d.AnimComponent(keyframes[k - frame_offset], float(info[i][k])) current_anim.seq.append(comp) current_anim.seq = j3d.make_tangents(current_anim.seq) blk.animations.append(current_anim) if f == "": print("no saving") return blk else: with open(f, "wb") as f: blk.write_blk(f) f.close()
def from_table(cls, f, info): brk = cls(int(info[0][0]), int(info[0][1]), int(info[0][2])) keyframes = [] for i in range(3, len( info[1] ) ): if info[1][i] != "": text = info[1][i][6:] text = int(text) keyframes.append(text) print(keyframes) for i in range(0, int( len(info) / 4) ): curr_line = 4 * i + 2 color_anim = ColorAnimation(i, info[curr_line][0], int( info[curr_line][1] ) ) for j in range(0, 4): rgba = "RGBA" rgba = rgba[j: j+1] for k in range(3, len( info[curr_line + j] ) ): if info[curr_line + j][k] != "": anim_comp = j3d.AnimComponent(keyframes[k - 3], int(info[curr_line + j][k]) ) color_anim.add_component(rgba, anim_comp) color_anim.component[rgba] = j3d.make_tangents(color_anim.component[rgba]) if info[curr_line + 1][0].startswith("Reg"): brk.register_animations.append(color_anim) else: brk.constant_animations.append(color_anim) if f == "": print("no saving") return brk else: with open(f, "wb") as f: brk.write_brk(f) f.close()
def from_table(cls, f, info): bck = cls(int(info[0][1]), int(info[0][3]), int(info[0][5])) if len(info[0]) >= 7 and info[0][7] != "": bck.tan_type = int( info[0][7] ) keyframes = [] for i in range(2, len( info[1] ) ): if info[1][i] != "": text = info[1][i][6:] text = int(text) keyframes.append(text) print("keyframes") print (keyframes) for i in range( int( len(info) / 9 ) ): #for each bone line = 9 * i + 2 current_anim = bone_anim() current_anim.name = info[line][0] if info[line + 1][0].startswith("S"): current_anim.tan_inter = 1 for j in range(9): #for each of thing in scale/rot/trans x/y/z/ xyz = "XYZ" xyz = xyz[j%3: j%3 + 1] for k in range(2, len(info[line + j])): #for each keyframe if info[line + j][k] != "": try: comp = j3d.AnimComponent( keyframes[k-2], float(info[line + j][k])) except: comp = j3d.AnimComponent( bck.duration, float(info[line + j][k]) ) if j < 3: current_anim.add_scale(xyz, comp) #print("scale " + xyz + " " + str(keyframes[k-2]) + ", " + str( float(info[line + j][k]))) elif j < 6: """if comp.value < -180 * bck.anglescale: comp.value = comp.value + 360 * bck.anglescale elif comp.value > 180 * bck.anglescale: comp.value = comp.value - 360 * bck.anglescale""" current_anim.add_rotation(xyz, comp) #print("rot " + xyz + " " + str(keyframes[k-2]) + ", " + str( float(info[line + j][k]))) else: current_anim.add_translation(xyz, comp) #print("trans " + xyz + " " + str(keyframes[k-2]) + ", " + str( float(info[line + j][k]))) #calculate tangents for j in range(9): xyz = "XYZ" xyz = xyz[j%3: j%3 + 1] if j < 3: current_anim.scale[xyz] = j3d.make_tangents(current_anim.scale[xyz], current_anim.tan_inter) if j < 6: current_anim.rotation[xyz] = j3d.make_tangents(current_anim.rotation[xyz], current_anim.tan_inter) else: current_anim.translation[xyz] = j3d.make_tangents(current_anim.translation[xyz], current_anim.tan_inter) bck.animations.append(current_anim) if f == "": print("no saving") return bck else: with open(f, "wb") as f: bck.write_bck(f) f.close()
def from_maya_anim(cls, filepath): lines = filepath.readlines() duration = int( lines[6][lines[6].find(" "): lines[6].find(";")] ) #duration -= int( lines[5][lines[5].find(" "): lines[5].find(";")] ) max_angle = 0 print (duration) i = 7 bck = cls(1, 1, duration) current_bone = lines[i].split()[3] #iterate through all the lines while ( i < len(lines) ): # make a new bone new_bone_name = lines[i].split()[3] current_bone = new_bone_name new_bone = bone_anim() new_bone.name = new_bone_name bck.animations.append(new_bone) # while it is the same bone animation while(new_bone_name == current_bone): current_bone = new_bone_name #jump to keys for the entry values = lines[i].split() thing = values[2] i += 8 #read the keyframes while( not "}" in lines[i]): values = lines[i].split() new_entry = j3d.AnimComponent(int(values[0]), float(values[1])) if len(thing) == 6: new_bone.add_scale( thing[-1], new_entry ) elif len(thing) == 7: max_angle = max( abs(max_angle), abs(new_entry.value) ) new_bone.add_rotation( thing[-1], new_entry ) elif thing.startswith("translate"): new_bone.add_translation( thing[-1], new_entry ) i += 1 i += 2 if( i < len(lines) ): new_bone_name = lines[i].split()[3] else: new_bone_name = current_bone + "asdf" bck.anglescale = int( max_angle / 180) ; for anim in bck.animations : for axis in {"X", "Y", "Z"} : if len( anim.scale[axis] ) == 0: new_entry = j3d.AnimComponent(0, 1.0) anim.add_scale(axis, new_entry) if len( anim.rotation[axis] ) == 0: new_entry = j3d.AnimComponent(0, 0) anim.add_rotation(axis, new_entry) if len( anim.translation[axis] ) == 0: new_entry = j3d.AnimComponent(0, 0) anim.add_translation(axis, new_entry) return bck
def from_table(cls, f, info): btk = cls(int(info[0][0]), 0, int(info[0][1]), 0, int(info[0][2])) keyframes = [] for i in range(3, len(info[1])): if info[1][i] != "": text = info[1][i][6:] text = int(text) keyframes.append(text) print("keyframes") print(keyframes) num_of_mats = int((len(info) - 2) / 9) #read all the values for i in range(num_of_mats): #for each material line = 9 * i + 2 centrum = info[line + 4][0] if not isinstance(centrum, tuple): print("convert centrum to float tuple") centrum = centrum.strip("()") centrum = eval(centrum) #centrum = tuple(filter(float, centrum.split(",") ) ) assert isinstance(centrum, tuple) current_anim = MatrixAnimation(i, int(info[line + 2][0]), info[line][0], centrum) for j in range(9): #for each of thing in scale/rot/trans u/v/w/ uvw = "UVW" uvw = uvw[j % 3:j % 3 + 1] for k in range(3, len(info[line + j])): #for each keyframe if info[line + j][k] != "": comp = j3d.AnimComponent(keyframes[k - 3], float(info[line + j][k])) if info[line + j][1].startswith("S"): current_anim.tan_inter[j] = 1 else: current_anim.tan_inter[j] = 0 if j < 3: current_anim.add_scale(uvw, comp) #print("scale " + uvw + " " + str(keyframes[k-4]) + ", " + str( float(info[line + j][k]))) elif j < 6: current_anim.add_rotation(uvw, comp) # print("rot " + uvw + " " + str(keyframes[k-4]) + ", " + str( float(info[line + j][k]))) else: current_anim.add_translation(uvw, comp) # print("trans " + uvw + " " + str(keyframes[k-4]) + ", " + str( float(info[line + j][k]))) #calculate tangents for j in range(9): uvw = "UVW" uvw = uvw[j % 3:j % 3 + 1] if j < 3: current_anim.scale[uvw] = j3d.make_tangents( current_anim.scale[uvw], current_anim.tan_inter[j]) if j < 6: current_anim.rotation[uvw] = j3d.make_tangents( current_anim.rotation[uvw], current_anim.tan_inter[j]) else: current_anim.translation[uvw] = j3d.make_tangents( current_anim.translation[uvw], current_anim.tan_inter[j]) btk.animations.append(current_anim) if f == "": print("no saving") return btk else: with open(f, "wb") as f: btk.write_btk(f) f.close()
def from_table(cls, f, info, sound_data=None): #print("loop mode " + str( info[0][1] ) ) #loop mode, anglescale, duration, tantype bck = cls(int(info[0][0]), 0, int(info[0][1]), int(info[0][2])) max_angle = 0 #print(sound_data) bck.sound = sound_data if len(info[0]) >= 7 and info[0][7] != "": bck.tan_type = int(info[0][7]) keyframes = [] for i in range(3, len(info[1])): if info[1][i] != "": text = info[1][i][6:] text = int(text) keyframes.append(text) #print("keyframes") #print (keyframes) for i in range(int(len(info) / 9)): #for each bone line = 9 * i + 2 current_anim = bone_anim() current_anim.name = info[line][0] """ if info[line + 1][0].startswith("S"): current_anim.tan_inter = 1 """ for j in range(9): #for each of thing in scale/rot/trans x/y/z/ xyz = "XYZ" xyz = xyz[j % 3:j % 3 + 1] for k in range(3, len(info[line + j])): #for each keyframe if info[line + j][k] != "": try: comp = j3d.AnimComponent(keyframes[k - 3], float(info[line + j][k])) except: comp = j3d.AnimComponent(bck.duration, float(info[line + j][k])) if info[line + j][1].startswith("S"): current_anim.tan_inter[j] = 1 else: current_anim.tan_inter[j] = 0 if j < 3: current_anim.add_scale(xyz, comp) #print("scale " + xyz + " " + str(keyframes[k-2]) + ", " + str( float(info[line + j][k]))) elif j < 6: """if comp.value < -180 * bck.anglescale: comp.value = comp.value + 360 * bck.anglescale elif comp.value > 180 * bck.anglescale: comp.value = comp.value - 360 * bck.anglescale""" max_angle = max(max_angle, comp.value) current_anim.add_rotation(xyz, comp) #print("rot " + xyz + " " + str(keyframes[k-2]) + ", " + str( float(info[line + j][k]))) else: current_anim.add_translation(xyz, comp) #print("trans " + xyz + " " + str(keyframes[k-2]) + ", " + str( float(info[line + j][k]))) #calculate tangents bck.anglescale = math.ceil(abs(max_angle) / 180) #print("anglescale: ", bck.anglescale) for j in range(9): xyz = "XYZ" xyz = xyz[j % 3:j % 3 + 1] if j < 3: current_anim.scale[xyz] = j3d.make_tangents( current_anim.scale[xyz], current_anim.tan_inter[j]) if j < 6: current_anim.rotation[xyz] = j3d.make_tangents( current_anim.rotation[xyz], current_anim.tan_inter[j]) else: current_anim.translation[xyz] = j3d.make_tangents( current_anim.translation[xyz], current_anim.tan_inter[j]) bck.animations.append(current_anim) if f == "": #print("no saving") return bck else: with open(f, "wb") as f: bck.write_bck(f) f.close()
def from_blender_bvh_old(cls, filepath): import re lines = filepath.read().splitlines() lines = [line for line in lines if line != ""] #process motion try: motion_index = lines.index("MOTION") motion_line = motion_index except: return motion_index += 1 #print( lines[motion_index] ) duration_regex = "^Frames: (\d+)$" m = re.match(duration_regex, lines[motion_index]) if m is None: return bck = cls(duration=int(m.group(1))) motion_index += 2 all_values = [] while motion_index < len(lines): curr_values = lines[motion_index].split() #print( curr_values) all_values.append(curr_values) motion_index += 1 all_values = [[row[i] for row in all_values] for i in range(len(all_values[0]))] #turn the array into a bunch of animcomponents for i in range(len(all_values)): for j in range(len(all_values[i])): all_values[i][j] = j3d.AnimComponent(j, all_values[i][j]) #print( all_values) #process the bones assert (lines[0] == "HIERARCHY") hierachy_index = 1 values_index = 0 max_rotation = 0 while hierachy_index < motion_line: #print(hierachy_index) bone_regex = "^\s*(ROOT|JOINT) (\S*)$" m = re.match(bone_regex, lines[hierachy_index]) if m is None: return bck curr_bone = bone_anim() curr_bone.name = m.group(2) #print(curr_bone.name) curr_bone.add_scale("X", j3d.AnimComponent(0, 1.0)) curr_bone.add_scale("Y", j3d.AnimComponent(0, 1.0)) curr_bone.add_scale("Z", j3d.AnimComponent(0, 1.0)) hierachy_index += 2 #to get to offset offsets = lines[hierachy_index].split() hierachy_index += 1 channels = lines[hierachy_index] for axis in [ "Xposition", "Yposition", "Zposition", "Xrotation", "Yrotation", "Zrotation" ]: if channels.find(axis) != -1: #the particular channel is in there if axis.find("position") != -1: #if the axis is a position for comp in all_values[values_index]: curr_bone.add_translation(axis[0], comp) values_index += 1 else: #if the axis is a rotation for comp in all_values[values_index]: curr_bone.add_rotation(axis[0], comp) max_rotation = max(float(comp.value), max_rotation) values_index += 1 else: axises = "XYZ" if axis.find("position") != -1: curr_bone.add_translation( axis[0], j3d.AnimComponent( 0, offsets[axises.find(axis[0]) + 1])) else: curr_bone.add_rotation( axis[0], j3d.AnimComponent( 0, offsets[axises.find(axis[0]) + 1])) hierachy_index += 1 if lines[hierachy_index].strip() == "End Site": hierachy_index += 3 while lines[hierachy_index].strip() == "}": hierachy_index += 1 bck.animations.append(curr_bone) bck.anglescale = int(max_rotation / 180) return bck
def from_maya_anim(cls, filepath): lines = filepath.readlines() duration = lines[6][lines[6].find(" "):lines[6].find(";")].strip() print(duration) duration = int(float(duration)) #duration -= int( lines[5][lines[5].find(" "): lines[5].find(";")] ) max_angle = 0 print(duration) i = 7 bck = cls(1, 1, duration) current_bone = lines[i].split()[3] #iterate through all the lines while (i < len(lines)): if not lines[i + 1].startswith("animData"): print(lines[i], lines[i + 1]) i = i + 1 continue # make a new bone new_bone_name = lines[i].split()[3] current_bone = new_bone_name new_bone = bone_anim() new_bone.name = new_bone_name bck.animations.append(new_bone) # while it is the same bone animation while (new_bone_name == current_bone): current_bone = new_bone_name #jump to keys for the entry values = lines[i].split() thing = values[2] i += 8 tan_inter_index = 0 tan_inter_type = 1 if thing.startswith("r"): tan_inter_index += 3 elif thing.startswith("t"): tan_inter_index += 6 if thing.endswith("Y"): tan_inter_index += 1 elif thing.endswith("Z"): tan_inter_index += 2 #read the keyframes while (not "}" in lines[i]): values = lines[i].split() new_entry = j3d.AnimComponent(int(float(values[0])), float(values[1])) if values[2].lower() in ["linear", "fixed"]: tan_inter_type = 0 if len(thing) == 6: new_bone.add_scale(thing[-1], new_entry) elif len(thing) == 7: max_angle = max(abs(max_angle), abs(new_entry.value)) new_bone.add_rotation(thing[-1], new_entry) elif thing.startswith("translate"): new_bone.add_translation(thing[-1], new_entry) i += 1 i += 2 if (i < len(lines)): new_bone_name = lines[i].split()[3] else: new_bone_name = current_bone + "asdf" new_bone.tan_inter[tan_inter_index] = tan_inter_type bck.anglescale = int(max_angle / 180) for anim in bck.animations: for axis in {"X", "Y", "Z"}: if len(anim.scale[axis]) == 0: new_entry = j3d.AnimComponent(0, 1.0) anim.add_scale(axis, new_entry) if len(anim.rotation[axis]) == 0: new_entry = j3d.AnimComponent(0, 0) anim.add_rotation(axis, new_entry) if len(anim.translation[axis]) == 0: new_entry = j3d.AnimComponent(0, 0) anim.add_translation(axis, new_entry) line_pos = anim.name.rfind("|") if line_pos > -1: anim.name = anim.name[line_pos + 1:] return bck
def from_table(cls, f, info): brk = cls(int(info[0][1]), int(info[0][3]), int(info[0][5])) constant_line = 0 for i in range(len(info)): if info[i][0].startswith("C"): constant_line = i if constant_line != 0: break print("constant line " + str(constant_line)) if constant_line > 2: #there are register animations print("there are register animations") keyframes = [] for i in range(3, len(info[2])): if info[2][i] != "": text = info[2][i][6:] text = int(text) keyframes.append(text) print(keyframes) for i in range(0, int(constant_line / 4)): curr_line = 4 * i + 3 color_anim = ColorAnimation(i, info[curr_line][0], int(info[curr_line][1])) for j in range(0, 4): rgba = "RGBA" rgba = rgba[j:j + 1] for k in range(3, len(info[curr_line + j])): if info[curr_line + j][k] != "": anim_comp = j3d.AnimComponent( keyframes[k - 3], int(info[curr_line + j][k])) color_anim.add_component(rgba, anim_comp) color_anim.component[rgba] = j3d.make_tangents( color_anim.component[rgba]) brk.register_animations.append(color_anim) #print(brk.register_animations) if constant_line + 1 < len(info): print("there are constant animations") keyframes = [] for i in range(3, len(info[constant_line + 1])): if info[constant_line + 1][i] != "": text = info[constant_line + 1][i][6:] text = int(text) keyframes.append(text) for i in range(0, int((len(info) - constant_line) / 4)): curr_line = constant_line + 4 * i + 2 color_anim = ColorAnimation(i, info[curr_line][0], int(info[curr_line][1])) for j in range(0, 4): rgba = "RGBA" rgba = rgba[j:j + 1] for k in range(3, len(info[curr_line + j])): if info[curr_line + j][k] != "": anim_comp = j3d.AnimComponent( keyframes[k - 3], int(info[curr_line + j][k])) color_anim.add_component(rgba, anim_comp) color_anim.component[rgba] = j3d.make_tangents( color_anim.component[rgba]) brk.constant_animations.append(color_anim) if f == "": print("no saving") return brk else: with open(f, "wb") as f: brk.write_brk(f) f.close()