Want more features on Pastebin? Sign Up, it's FREE!
Guest

obj vert and face extractor

By: a guest on Aug 28th, 2012  |  syntax: C++  |  size: 1.30 KB  |  views: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import random
  2. f=open("Ezreal.obj","r")
  3. lines=f.readlines()
  4. f.close()
  5. verticies=[]
  6. faces=[]
  7. texture=(0,0)
  8. colors=["1,0,0,1","0,1,0,1","0,0,1,1"]
  9. vertcount=0
  10. elementcount=0
  11. facecheck1=0
  12. facecheck2=0
  13. facecount=0
  14. for line in lines:
  15.     if line.startswith("v "):
  16.         verts=line.split(" ")
  17.         verticies.append((verts[1],verts[2],verts[3].split("\n")[0]))
  18.         facecheck1+=1
  19.         vertcount+=3
  20.     if line.startswith("f "):
  21.         face=line.split(" ")
  22.         del face[0]
  23.        
  24.         faces.append((face[0].split("/")[0],face[1].split("/")[0],face[2].split("/")[0]))
  25.         elementcount+=3
  26.         facecheck2+=1
  27.         facecount+=1
  28.         print face[0].split("/")[0],face[1].split("/")[0],face[2].split("/")[0]
  29.  
  30. f=open("Ezrealarray.txt","w")
  31. f.write("float tri[]={")
  32.  
  33. for vert in verticies:
  34.     f.write(vert[0]+","+vert[1]+","+vert[2]+","+random.choice(colors)+",0,0,"+"\n")
  35. f.write("};")
  36. f.close()
  37.  
  38. f=open("Ezrealelementarray.txt","w")
  39. f.write("unsigned int ind[]={")
  40.  
  41. for face in faces:
  42.     f.write(face[0]+","+face[1]+","+face[2]+",\n")
  43. f.write("};")
  44. f.close()
  45. print "This object has:"
  46. print vertcount,"verticies"
  47. print elementcount,"elements"
  48.  
  49. if facecheck1==facecheck2:
  50.     print "faces number match in verticies and elements."
  51. print facecount,"faces"
clone this paste RAW Paste Data