MCPcopy Create free account
hub / github.com/TencentARC/BrushNet / export_to_ply

Function export_to_ply

src/diffusers/utils/export_utils.py:46–95  ·  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

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