Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. def write_ply(mesh, filename, binary=True):
  2. """
  3. Output the mesh object as an ASCII .ply file, or as a little endian binary file.
  4. """
  5. header = "comment This file was generated by the SurfacePy libraryn"
  6. header += "element vertex {0}n".format(len(mesh.vertices))
  7. header += "property float xnproperty float ynproperty float zn"
  8. header += "element face {0}n".format(len(mesh.triangles))
  9. header += "property list uchar int vertex_indicesn"
  10. header += "end_headern"
  11. with open(filename, 'wb') as file:
  12. if binary==True:
  13. file.write(__pack_string("plynformat binary_little_endian 1.0n"))
  14. else:
  15. file.write(__pack_string("plynformat ascii 1.0n"))
  16. file.write(__pack_string(header))
  17. if binary==True:
  18. for vert in mesh.vertices:
  19. file.write(struct.pack('<fff', vert.x, vert.y, vert.z))
  20. for tri in mesh.triangles:
  21. file.write(struct.pack('<Biii', ord('3'), tri.i1, tri.i3, tri.i2))
  22. else:
  23. for vert in mesh.vertices:
  24. file.write(__pack_string("{0}n".format(vert)))
  25. for tri in mesh.triangles:
  26. file.write(__pack_string("3 {0} {1} {2}n".format(tri.i1, tri.i3, tri.i2)))
  27.  
  28.  
  29. def __pack_string(str):
  30. """
  31. Returns a bytes object of a string, in little-endian format.
  32. """
  33. chars = [c.encode('utf-8') for c in str]
  34. fmt = "<{0}".format(''.join(['c' for i in range(0, len(chars))]))
  35. return __pack(fmt, chars)
  36.  
  37.  
  38.  
  39. def __pack(fmt, args):
  40. return struct.pack(fmt, *args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement