1 # SPDX-License-Identifier: GPL-2.0-or-later
4 This script exports Stanford PLY files from Blender. It supports normals,
5 colors, and texture coordinates per face or per vertex.
11 def _write_binary(fw
, ply_verts
: list, ply_faces
: list) -> None:
12 from struct
import pack
15 # ---------------------------
17 for v
, normal
, uv
, color
in ply_verts
:
18 fw(pack("<3f", *v
.co
))
19 if normal
is not None:
20 fw(pack("<3f", *normal
))
24 fw(pack("<4B", *color
))
27 # ---------------------------
31 fw(pack(f
"<B{length}I", length
, *pf
))
34 def _write_ascii(fw
, ply_verts
: list, ply_faces
: list) -> None:
37 # ---------------------------
39 for v
, normal
, uv
, color
in ply_verts
:
40 fw(b
"%.6f %.6f %.6f" % v
.co
[:])
41 if normal
is not None:
42 fw(b
" %.6f %.6f %.6f" % normal
[:])
44 fw(b
" %.6f %.6f" % uv
)
46 fw(b
" %u %u %u %u" % color
)
50 # ---------------------------
59 def save_mesh(filepath
, bm
, use_ascii
, use_normals
, use_uv
, use_color
):
60 uv_lay
= bm
.loops
.layers
.uv
.active
61 col_lay
= bm
.loops
.layers
.color
.active
63 use_uv
= use_uv
and uv_lay
is not None
64 use_color
= use_color
and col_lay
is not None
65 normal
= uv
= color
= None
77 v
= map_id
= loop
.vert
80 uv
= loop
[uv_lay
].uv
[:]
83 # Identify vertex by pointer unless exporting UVs,
84 # in which case id by UV coordinate (will split edges by seams).
85 if (_id
:= ply_vert_map
.get(map_id
)) is not None:
92 color
= tuple(int(x
* 255.0) for x
in loop
[col_lay
])
94 ply_verts
.append((v
, normal
, uv
, color
))
95 ply_vert_map
[map_id
] = ply_vert_id
96 pf
.append(ply_vert_id
)
99 with
open(filepath
, "wb") as file:
101 file_format
= b
"ascii" if use_ascii
else b
"binary_little_endian"
104 # ---------------------------
107 fw(b
"format %s 1.0\n" % file_format
)
108 fw(b
"comment Created by Blender %s - www.blender.org\n" % bpy
.app
.version_string
.encode("utf-8"))
110 fw(b
"element vertex %d\n" % len(ply_verts
))
112 b
"property float x\n"
113 b
"property float y\n"
114 b
"property float z\n"
118 b
"property float nx\n"
119 b
"property float ny\n"
120 b
"property float nz\n"
124 b
"property float s\n"
125 b
"property float t\n"
129 b
"property uchar red\n"
130 b
"property uchar green\n"
131 b
"property uchar blue\n"
132 b
"property uchar alpha\n"
135 fw(b
"element face %d\n" % len(ply_faces
))
136 fw(b
"property list uchar uint vertex_indices\n")
140 # ---------------------------
143 _write_ascii(fw
, ply_verts
, ply_faces
)
145 _write_binary(fw
, ply_verts
, ply_faces
)
153 use_mesh_modifiers
=True,
164 if bpy
.ops
.object.mode_set
.poll():
165 bpy
.ops
.object.mode_set(mode
='OBJECT')
168 obs
= context
.selected_objects
170 obs
= context
.scene
.objects
172 depsgraph
= context
.evaluated_depsgraph_get()
176 if use_mesh_modifiers
:
177 ob_eval
= ob
.evaluated_get(depsgraph
)
182 me
= ob_eval
.to_mesh()
186 me
.transform(ob
.matrix_world
)
188 ob_eval
.to_mesh_clear()
190 # Workaround for hardcoded unsigned char limit in other DCCs PLY importers
191 if (ngons
:= [f
for f
in bm
.faces
if len(f
.verts
) > 255]):
192 bmesh
.ops
.triangulate(bm
, faces
=ngons
)
194 if global_matrix
is not None:
195 bm
.transform(global_matrix
)
211 t_delta
= time
.time() - t
212 print(f
"Export completed {filepath!r} in {t_delta:.3f}")