Advertisement
Guest User

Untitled

a guest
Jul 28th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.63 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import sys
  3. import os
  4.  
  5. version = 1.2
  6.  
  7. def colorToFloat(input):
  8.     if (input < 0 or input > 256):
  9.         print("Warning, invalid input: {}".format(input))
  10.  
  11.     red = (input % 224) / 32
  12.     green = (input % 28) / 4
  13.     blue = input % 3
  14.    
  15.     red = red / 7.0
  16.     green = green / 7.0
  17.     blue = blue / 3.0
  18.    
  19.    
  20.     return "{} {} {}".format(red, green, blue)
  21.  
  22. def main():
  23.     # Manages group
  24.     group_set = set()
  25.  
  26.     file_name = os.path.splitext(sys.argv[1])[0];
  27.     file_ext = os.path.splitext(sys.argv[1])[1];
  28.    
  29.     # Might change this later. output
  30.     file_out_name = file_name
  31.  
  32.     if(len(sys.argv) < 2):
  33.         print("convert.py <file.in> ")
  34.         return 0
  35.        
  36.     autoColor = True;
  37.    
  38.     # You can color yourself.
  39.     if len(sys.argv) > 2:
  40.         if sys.argv[2] == "-c":
  41.             print("Manual coloring enabled")
  42.             autoColor = False
  43.    
  44.     f = open(sys.argv[1], "r")
  45.     mode = "header"
  46.     type = "model"
  47.     if file_ext == ".anm":
  48.         type = "animation"
  49.        
  50.     print("file kind " + file_ext)
  51.     print("Converting " + type)
  52.    
  53.     num_verts = 0
  54.     num_frames = 0
  55.     cur_frame = 0
  56.     cur_verts = 0
  57.     # frame, vertex.
  58.     verts = []
  59.     faces = []
  60.     for line in f:
  61.         if mode == "header":
  62.             print("Header: " + line.strip())
  63.             mode = "vertex"
  64.  
  65.         elif mode == "vertex":
  66.             num_verts = int(line.strip())
  67.            
  68.             if type == "animation":
  69.                 mode = "frame"
  70.             else:
  71.                 num_frames = 1
  72.                 mode = "read_frames"
  73.                 verts.append([])
  74.            
  75.             print("vertex count: " + str(num_verts))
  76.         elif mode == "frame":
  77.             num_frames = int(line.strip())
  78.                
  79.             print("frame count: " + str(num_frames))
  80.                
  81.             for i in range(0, num_frames):
  82.                 verts.append([])
  83.            
  84.             mode = "read_frames"
  85.            
  86.         elif mode == "read_frames":
  87.            
  88.             vert = line.strip().split()
  89.             vert[0] = float(vert[0])
  90.             vert[1] = float(vert[1])
  91.             vert[2] = float(vert[2])
  92.            
  93.             verts[cur_frame].append(vert[0:3])
  94.            
  95.             cur_verts += 1
  96.             if cur_verts == num_verts:
  97.                 cur_verts = 0
  98.                 cur_frame += 1
  99.                 if cur_frame == num_frames:
  100.                     mode = "read_faces"
  101.                
  102.         elif mode == "read_faces":
  103.             face = line.strip().split()
  104.             for i in range(len(face)):
  105.                 try:
  106.                     face[i] = int(face[i])
  107.                 except ValueError:
  108.                     print("Done")
  109.                    
  110.             faces.append(face) 
  111.  
  112.     f.close()
  113.  
  114.     # Create output
  115.     for fr in range(0, int(num_frames)):
  116.    
  117.        
  118.         if type == "animation":
  119.             f = open(file_out_name +"_frame" + str(fr) + ".obj", "w")
  120.         else:
  121.             f = open(file_out_name + ".obj", "w")
  122.        
  123.        
  124.         #Material library
  125.         f.write("mtllib " + file_name + type + ".mtl\n")
  126.        
  127.         for v in verts[fr]:
  128.             f.write("v {} {} {} 1\n".format(v[0], v[1], v[2]))
  129.    
  130.        
  131.        
  132.    
  133.         cur_group = -1
  134.    
  135.         for face in faces:
  136.        
  137.             if len(face) > 1:
  138.            
  139.                 group_set.add(face[-1])
  140.                 # New group encountered!
  141.                 if face[-1] != cur_group:
  142.                     # Change group here.
  143.                     cur_group = face[-1]
  144.                     f.write("g gr" + str(cur_group) + "\n")
  145.                     f.write("usemtl mtl" + str(cur_group) + "\n")
  146.                     f.write("s 0\n")
  147.                    
  148.                    
  149.                     f.write("")
  150.            
  151.                 f.write("f")
  152.                
  153.                 for ff in face[1:-1]:
  154.                     f.write(" {}".format(ff+1))
  155.                 f.write("\n")
  156.    
  157.         f.close()
  158.        
  159.        
  160.     # Create Material library
  161.     f = open(file_name + type + ".mtl", "w")
  162.     print("Creating material library " + file_name + type + ".mtl")
  163.     for group in group_set:
  164.         col = colorToFloat(int(group))
  165.         if autoColor == False:
  166.             col = input("Input color for group " + str(group) + ">")
  167.         #material name is mtl{Group}
  168.         f.write("newmtl mtl" + str(group) + "\n")
  169.         f.write("Ka " + col + "\n")
  170.         f.write("Kd " + col + "\n")
  171.         f.write("Ks 0.000 0.000 0.000\n")
  172.         f.write("Ns 10.000\n\n")
  173.    
  174.     f.close()
  175.    
  176.     print("All done!")
  177.    
  178.     return 0
  179.  
  180. if __name__ == "__main__":
  181.     sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement