Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import sys
- import os
- version = 1.2
- def colorToFloat(input):
- if (input < 0 or input > 256):
- print("Warning, invalid input: {}".format(input))
- red = (input % 224) / 32
- green = (input % 28) / 4
- blue = input % 3
- red = red / 7.0
- green = green / 7.0
- blue = blue / 3.0
- return "{} {} {}".format(red, green, blue)
- def main():
- # Manages group
- group_set = set()
- file_name = os.path.splitext(sys.argv[1])[0];
- file_ext = os.path.splitext(sys.argv[1])[1];
- # Might change this later. output
- file_out_name = file_name
- if(len(sys.argv) < 2):
- print("convert.py <file.in> ")
- return 0
- autoColor = True;
- # You can color yourself.
- if len(sys.argv) > 2:
- if sys.argv[2] == "-c":
- print("Manual coloring enabled")
- autoColor = False
- f = open(sys.argv[1], "r")
- mode = "header"
- type = "model"
- if file_ext == ".anm":
- type = "animation"
- print("file kind " + file_ext)
- print("Converting " + type)
- num_verts = 0
- num_frames = 0
- cur_frame = 0
- cur_verts = 0
- # frame, vertex.
- verts = []
- faces = []
- for line in f:
- if mode == "header":
- print("Header: " + line.strip())
- mode = "vertex"
- elif mode == "vertex":
- num_verts = int(line.strip())
- if type == "animation":
- mode = "frame"
- else:
- num_frames = 1
- mode = "read_frames"
- verts.append([])
- print("vertex count: " + str(num_verts))
- elif mode == "frame":
- num_frames = int(line.strip())
- print("frame count: " + str(num_frames))
- for i in range(0, num_frames):
- verts.append([])
- mode = "read_frames"
- elif mode == "read_frames":
- vert = line.strip().split()
- vert[0] = float(vert[0])
- vert[1] = float(vert[1])
- vert[2] = float(vert[2])
- verts[cur_frame].append(vert[0:3])
- cur_verts += 1
- if cur_verts == num_verts:
- cur_verts = 0
- cur_frame += 1
- if cur_frame == num_frames:
- mode = "read_faces"
- elif mode == "read_faces":
- face = line.strip().split()
- for i in range(len(face)):
- try:
- face[i] = int(face[i])
- except ValueError:
- print("Done")
- faces.append(face)
- f.close()
- # Create output
- for fr in range(0, int(num_frames)):
- if type == "animation":
- f = open(file_out_name +"_frame" + str(fr) + ".obj", "w")
- else:
- f = open(file_out_name + ".obj", "w")
- #Material library
- f.write("mtllib " + file_name + type + ".mtl\n")
- for v in verts[fr]:
- f.write("v {} {} {} 1\n".format(v[0], v[1], v[2]))
- cur_group = -1
- for face in faces:
- if len(face) > 1:
- group_set.add(face[-1])
- # New group encountered!
- if face[-1] != cur_group:
- # Change group here.
- cur_group = face[-1]
- f.write("g gr" + str(cur_group) + "\n")
- f.write("usemtl mtl" + str(cur_group) + "\n")
- f.write("s 0\n")
- f.write("")
- f.write("f")
- for ff in face[1:-1]:
- f.write(" {}".format(ff+1))
- f.write("\n")
- f.close()
- # Create Material library
- f = open(file_name + type + ".mtl", "w")
- print("Creating material library " + file_name + type + ".mtl")
- for group in group_set:
- col = colorToFloat(int(group))
- if autoColor == False:
- col = input("Input color for group " + str(group) + ">")
- #material name is mtl{Group}
- f.write("newmtl mtl" + str(group) + "\n")
- f.write("Ka " + col + "\n")
- f.write("Kd " + col + "\n")
- f.write("Ks 0.000 0.000 0.000\n")
- f.write("Ns 10.000\n\n")
- f.close()
- print("All done!")
- return 0
- if __name__ == "__main__":
- sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement