Advertisement
flyingfences

.3mf to .ply

Sep 26th, 2015
1,530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1. # Python script to convert Microsoft's .3mf files into usable .ply files
  2. # written by flyingfences on 26 September 2015
  3. # released under the X11 License
  4.  
  5. # .3mf must first be extracted (7-zip works), and this script run on the .model within
  6. # written for Python 3.5, compatibilities with other versions not tested
  7. # make sure to use an x64 install of Python, or you'll likely run out of memory
  8.  
  9.  
  10. print("initializing")
  11. from xml.dom import minidom
  12.  
  13. # load the .model extracted from the .3mf file into memory and get it all sorted out
  14. print("parsing the file...", end='')
  15. msf = minidom.parse('3dmodel.model')
  16. print("done")
  17. print("extracting: ", end='')
  18. print("colors...", end='')
  19. colors = msf.getElementsByTagName('color')
  20. print("vertices...", end='')
  21. verts = msf.getElementsByTagName('vertex')
  22. print("triangles...", end='')
  23. tris = msf.getElementsByTagName('triangle')
  24. print("done")
  25.  
  26. # match colors to vertices
  27. print("matching colors to vertices...", end='')
  28. cols = []
  29. for n in range(0,len(verts)):
  30.     cols.append("#000000")
  31. for n in range(0,len(tris)):
  32.     cols[int(tris[n].attributes['v1'].value)] = colors[int(tris[n].attributes['colorid'].value.split(',')[0])].attributes['value'].value
  33.     cols[int(tris[n].attributes['v2'].value)] = colors[int(tris[n].attributes['colorid'].value.split(',')[1])].attributes['value'].value
  34.     cols[int(tris[n].attributes['v3'].value)] = colors[int(tris[n].attributes['colorid'].value.split(',')[2])].attributes['value'].value
  35. # all that was incredibly redundant, but it was faster to write this way than to figure out how to only do each assignment once
  36. print("done")
  37.  
  38. # write the header for the .ply file
  39. print("writing file header...", end='')
  40. ply = open('output.ply', 'w')
  41. ply.write("ply\nformat ascii 1.0\n")
  42. ply.write("element vertex %i\n" %len(verts))
  43. ply.write("property float x\nproperty float y\nproperty float z\nproperty uchar red\nproperty uchar green\nproperty uchar blue\nproperty uchar alpha\n")
  44. ply.write("element face %i\n" %len(tris))
  45. ply.write("property list uchar int vertex_indices\nend_header\n")
  46. print("done")
  47.  
  48. # writing the data to the output
  49. # write the vertices first
  50. # format: x y z r g b a
  51. print("writing vertices...", end='')
  52. for n in range(0,len(verts)):
  53.     ply.write("%f %f %f %i %i %i 255\n" % (
  54.       float(verts[n].attributes['x'].value),
  55.       float(verts[n].attributes['y'].value),
  56.       float(verts[n].attributes['z'].value),
  57.       int(cols[n][1:3],16),
  58.       int(cols[n][3:5],16),
  59.       int(cols[n][5:7],16)))
  60. print("done")
  61. # then writing the faces
  62. print("writing faces...", end='')
  63. for n in range(0,len(tris)):
  64.     ply.write("3 %i %i %i\n" % (
  65.       int(tris[n].attributes['v1'].value),
  66.       int(tris[n].attributes['v2'].value),
  67.       int(tris[n].attributes['v3'].value)))
  68. print("done")
  69.  
  70. # close the output and terminate the program
  71. ply.close()
  72. print("conversion completed")
  73. input("press return to exit")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement