# Mesh2 so mesh interpolation format file converter # version .1b # Currently supports mesh2 files exported from # Wings3d 0.98.08e. Support is planned for files # exported by Art of Illusion , but this is not # yet implimented. # Script writen by Kitsune_e (Ed Blake) 2003 # Some neat string operations, probably faster then I could come up with too! from string import * # once the script is finished, and has most of the bugs worked out # I plan to use a TKinter UI for this part... but until then: base_name = raw_input("What is the base name of your files? ") extension = raw_input("What is the file extension of your files? ") number = input("How many files are there? ") # for files named base_01.ext to base_nn.ext for i in range(1,number+1): if i < 10: Snumber = "0" + str(i) else: Snumber = str(i) # read the file into a list, and setup some variables name = base_name + "_" + Snumber + "." + extension print "\n\n Opening " + name + "!" old_file = open(name,"r") file = old_file.readlines() old_file.close() length = len(file)-1 tmp = 0 done = 0 print "\n\n Getting vectors ", while not done: if tmp <= length: #if we're not at the EOF print ".", place = find(file[tmp],"vertex_vectors") #does this line have the vector data if place > -1: vertices = split(file[tmp][find(file[tmp],"{")+1:],",") # If so convert string to list vert_number = vertices.pop(0) # The first value is the number of verts print "\n\nFound " + vert_number + " vertices!" done = 1 else: # If not then we've run out of file, with verts this should end the program with an error message done = 1 tmp = tmp + 1 tmp = 0 done = 0 # Same stuff as in vert vectors, different names print "\n\n Getting normals ", while not done: if tmp <= length: print ".", place = find(file[tmp],"normal_vectors") if place > -1: normals = split(file[tmp][find(file[tmp],"{")+1:],",") norm_number = normals.pop(0) print "\n\nFound " + norm_number + " normals!" done = 1 else: # It is possable not to have any normal vectors, so let this pass. done = 1 tmp = tmp + 1 tmp = 0 done = 0 # More simple lists of vectors print "\n\n Getting UV vectors ", while not done: print ".", if tmp <= length: place = find(file[tmp],"uv_vectors") if place > -1: UVs = split(file[tmp][find(file[tmp],"{")+1:],",") UV_number = UVs.pop(0) print "\n\nFound " + UV_number + " UV vectors!" done = 1 else: # Like in normals UVs may be absent. done = 1 tmp = tmp + 1 tmp = 0 done = 0 # This gets tricky, I only need to get face indices for the first # file, as the same data is repeated in all the rest. if i == 1: tmp = 0 done = 0 face_indices = [] print "\n\n Getting face indices ", while not done: print ".", if tmp <= length: place = find(file[tmp],"face_indices") if place > -1: face_list = split(file[tmp][find(file[tmp],"{")+1:],",") # Get face data face_number = face_list.pop(0) # Save the length for index in range(0,len(face_list)): # Scan all each index if "<" in face_list[index]: # If it has a "<" in it it is a vector face_indices.append(face_list[index]) # Grab it, , print "\n\nFound " + face_number + " faces!" done = 1 else: # Again this one should be an error done = 1 tmp = tmp + 1 tmp = 0 done = 0 # UV was taken, so UVi seemed good for uv_indices # This is the same as with the vectors, but only needs to be done once "\n\n Getting UV indices ", while not done: print ".", if tmp <= length: place = find(file[tmp],"uv_indices") if place > -1: UVis = split(file[tmp][find(file[tmp],"{")+1:],",") UVi_number = UVis.pop(0) print "'\n\nFound " + UVi_number + " UV faces!" done = 1 else: done = 1 tmp = tmp + 1 tmp = 0 done = 0 # I think that normal indices are always the same as faces but... "\n\n Getting normal indices ", while not done: print ".", if tmp <= length: place = find(file[tmp],"normal_indices") if place > -1: normalis = split(file[tmp][find(file[tmp],"{")+1:],",") normalis_number = normalis.pop(0) print "'\n\nFound " + normalis_number + " normal faces!" done = 1 else: done = 1 tmp = tmp + 1 print "\n\n Writing files..." # Write shared data to a file that all the rest will use # only do this on the first file new_name = base_name + "_data.inc" output = open(new_name,"w") output.write("// Shared data for Mesh2 interpolation macro\n\n// Number of vertices:\n") output.write("#declare Number_of_Vertices=" + vert_number + ";\n\n") output.write("#declare Number_of_Normals=" + norm_number + ";\n\n") output.write("#declare Number_of_normalis=" + normalis_number + ";\n\n") output.write("#declare KeyFrame_normalis=array[Number_of_normalis]{\n\n") output.write(join(normalis,",") + "\n\n") output.write("#declare Number_of_UVs=" + UV_number + ";\n\n") output.write("#declare Number_of_UVis=" + UVi_number + ";\n\n") output.write("#declare KeyFrame_UVis=array[Number_of_UVis]{\n\n") output.write(join(UVis,",") + "\n\n") output.write("#declare Number_of_Faces=" + face_number + ";\n\n") output.write("#declare KeyFrame_Faces=array[Number_of_Faces]{\n\n") output.write(join(face_indices,",") + "\n}\n") output.close() print "\n\n Writing files..." new_name = base_name + "_" + Snumber + ".m2i" # .m2i for mesh2 interpolation file output = open(new_name,"w") output.write("// Vertex vectors\n") output.write("#declare KeyFrame" + Snumber + "_Vertices=array[Number_of_Vertices]{\n") output.write(" " + join(vertices,",") + "\n\n") output.write("// Normal vectors\n") output.write("#declare KeyFrame" + Snumber + "_Normals=array[Number_of_Normals]{\n") output.write(" " + join(normals,",") + "\n\n") output.write("// UV vectors\n") output.write("#declare KeyFrame" + Snumber + "_UVs=array[Number_of_UVs]{\n") output.write(" " + join(UVs,",") + "\n\n") output.close()