MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / export_to_ply

Function export_to_ply

diffusers/src/diffusers/utils/export_utils.py:43–92  ·  view source on GitHub ↗

Write a PLY file for a mesh.

(mesh, output_ply_path: str = None)

Source from the content-addressed store, hash-verified

41
42
43def export_to_ply(mesh, output_ply_path: str = None):
44 """
45 Write a PLY file for a mesh.
46 """
47 if output_ply_path is None:
48 output_ply_path = tempfile.NamedTemporaryFile(suffix=".ply").name
49
50 coords = mesh.verts.detach().cpu().numpy()
51 faces = mesh.faces.cpu().numpy()
52 rgb = np.stack([mesh.vertex_channels[x].detach().cpu().numpy() for x in "RGB"], axis=1)
53
54 with buffered_writer(open(output_ply_path, "wb")) as f:
55 f.write(b"ply\n")
56 f.write(b"format binary_little_endian 1.0\n")
57 f.write(bytes(f"element vertex {len(coords)}\n", "ascii"))
58 f.write(b"property float x\n")
59 f.write(b"property float y\n")
60 f.write(b"property float z\n")
61 if rgb is not None:
62 f.write(b"property uchar red\n")
63 f.write(b"property uchar green\n")
64 f.write(b"property uchar blue\n")
65 if faces is not None:
66 f.write(bytes(f"element face {len(faces)}\n", "ascii"))
67 f.write(b"property list uchar int vertex_index\n")
68 f.write(b"end_header\n")
69
70 if rgb is not None:
71 rgb = (rgb * 255.499).round().astype(int)
72 vertices = [
73 (*coord, *rgb)
74 for coord, rgb in zip(
75 coords.tolist(),
76 rgb.tolist(),
77 )
78 ]
79 format = struct.Struct("<3f3B")
80 for item in vertices:
81 f.write(format.pack(*item))
82 else:
83 format = struct.Struct("<3f")
84 for vertex in coords.tolist():
85 f.write(format.pack(*vertex))
86
87 if faces is not None:
88 format = struct.Struct("<B3I")
89 for tri in faces.tolist():
90 f.write(format.pack(len(tri), *tri))
91
92 return output_ply_path
93
94
95def export_to_obj(mesh, output_obj_path: str = None):

Callers

nothing calls this directly

Calls 1

buffered_writerFunction · 0.70

Tested by

no test coverage detected