Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def write_ply(mesh, filename, binary=True):
- """
- Output the mesh object as an ASCII .ply file, or as a little endian binary file.
- """
- header = "comment This file was generated by the SurfacePy libraryn"
- header += "element vertex {0}n".format(len(mesh.vertices))
- header += "property float xnproperty float ynproperty float zn"
- header += "element face {0}n".format(len(mesh.triangles))
- header += "property list uchar int vertex_indicesn"
- header += "end_headern"
- with open(filename, 'wb') as file:
- if binary==True:
- file.write(__pack_string("plynformat binary_little_endian 1.0n"))
- else:
- file.write(__pack_string("plynformat ascii 1.0n"))
- file.write(__pack_string(header))
- if binary==True:
- for vert in mesh.vertices:
- file.write(struct.pack('<fff', vert.x, vert.y, vert.z))
- for tri in mesh.triangles:
- file.write(struct.pack('<Biii', ord('3'), tri.i1, tri.i3, tri.i2))
- else:
- for vert in mesh.vertices:
- file.write(__pack_string("{0}n".format(vert)))
- for tri in mesh.triangles:
- file.write(__pack_string("3 {0} {1} {2}n".format(tri.i1, tri.i3, tri.i2)))
- def __pack_string(str):
- """
- Returns a bytes object of a string, in little-endian format.
- """
- chars = [c.encode('utf-8') for c in str]
- fmt = "<{0}".format(''.join(['c' for i in range(0, len(chars))]))
- return __pack(fmt, chars)
- def __pack(fmt, args):
- return struct.pack(fmt, *args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement