smooth all objects (so we see their normals)
[blender-addons.git] / io_mesh_ply / export_ply.py
blobd4db57107f13d2faec59c180b68617ffcc1a7aeb
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 # <pep8-80 compliant>
21 """
22 This script exports Stanford PLY files from Blender. It supports normals,
23 colors, and texture coordinates per face or per vertex.
24 Only one mesh can be exported at a time.
25 """
27 import bpy
28 import os
31 def save_mesh(filepath,
32 mesh,
33 use_normals=True,
34 use_uv_coords=True,
35 use_colors=True,
38 def rvec3d(v):
39 return round(v[0], 6), round(v[1], 6), round(v[2], 6)
41 def rvec2d(v):
42 return round(v[0], 6), round(v[1], 6)
44 file = open(filepath, "w", encoding="utf8", newline="\n")
45 fw = file.write
47 # Be sure tessface & co are available!
48 if not mesh.tessfaces and mesh.polygons:
49 mesh.calc_tessface()
51 has_uv = bool(mesh.tessface_uv_textures)
52 has_vcol = bool(mesh.tessface_vertex_colors)
54 if not has_uv:
55 use_uv_coords = False
56 if not has_vcol:
57 use_colors = False
59 if not use_uv_coords:
60 has_uv = False
61 if not use_colors:
62 has_vcol = False
64 if has_uv:
65 active_uv_layer = mesh.tessface_uv_textures.active
66 if not active_uv_layer:
67 use_uv_coords = False
68 has_uv = False
69 else:
70 active_uv_layer = active_uv_layer.data
72 if has_vcol:
73 active_col_layer = mesh.tessface_vertex_colors.active
74 if not active_col_layer:
75 use_colors = False
76 has_vcol = False
77 else:
78 active_col_layer = active_col_layer.data
80 # in case
81 color = uvcoord = uvcoord_key = normal = normal_key = None
83 mesh_verts = mesh.vertices # save a lookup
84 ply_verts = [] # list of dictionaries
85 # vdict = {} # (index, normal, uv) -> new index
86 vdict = [{} for i in range(len(mesh_verts))]
87 ply_faces = [[] for f in range(len(mesh.tessfaces))]
88 vert_count = 0
89 for i, f in enumerate(mesh.tessfaces):
91 smooth = not use_normals or f.use_smooth
92 if not smooth:
93 normal = f.normal[:]
94 normal_key = rvec3d(normal)
96 if has_uv:
97 uv = active_uv_layer[i]
98 uv = uv.uv1, uv.uv2, uv.uv3, uv.uv4
99 if has_vcol:
100 col = active_col_layer[i]
101 col = col.color1[:], col.color2[:], col.color3[:], col.color4[:]
103 f_verts = f.vertices
105 pf = ply_faces[i]
106 for j, vidx in enumerate(f_verts):
107 v = mesh_verts[vidx]
109 if smooth:
110 normal = v.normal[:]
111 normal_key = rvec3d(normal)
113 if has_uv:
114 uvcoord = uv[j][0], uv[j][1]
115 uvcoord_key = rvec2d(uvcoord)
117 if has_vcol:
118 color = col[j]
119 color = (int(color[0] * 255.0),
120 int(color[1] * 255.0),
121 int(color[2] * 255.0),
123 key = normal_key, uvcoord_key, color
125 vdict_local = vdict[vidx]
126 pf_vidx = vdict_local.get(key) # Will be None initially
128 if pf_vidx is None: # same as vdict_local.has_key(key)
129 pf_vidx = vdict_local[key] = vert_count
130 ply_verts.append((vidx, normal, uvcoord, color))
131 vert_count += 1
133 pf.append(pf_vidx)
135 fw("ply\n")
136 fw("format ascii 1.0\n")
137 fw("comment Created by Blender %s - "
138 "www.blender.org, source file: %r\n" %
139 (bpy.app.version_string, os.path.basename(bpy.data.filepath)))
141 fw("element vertex %d\n" % len(ply_verts))
143 fw("property float x\n"
144 "property float y\n"
145 "property float z\n")
147 if use_normals:
148 fw("property float nx\n"
149 "property float ny\n"
150 "property float nz\n")
151 if use_uv_coords:
152 fw("property float s\n"
153 "property float t\n")
154 if use_colors:
155 fw("property uchar red\n"
156 "property uchar green\n"
157 "property uchar blue\n")
159 fw("element face %d\n" % len(mesh.tessfaces))
160 fw("property list uchar uint vertex_indices\n")
161 fw("end_header\n")
163 for i, v in enumerate(ply_verts):
164 fw("%.6f %.6f %.6f" % mesh_verts[v[0]].co[:]) # co
165 if use_normals:
166 fw(" %.6f %.6f %.6f" % v[1]) # no
167 if use_uv_coords:
168 fw(" %.6f %.6f" % v[2]) # uv
169 if use_colors:
170 fw(" %u %u %u" % v[3]) # col
171 fw("\n")
173 for pf in ply_faces:
174 if len(pf) == 3:
175 fw("3 %d %d %d\n" % tuple(pf))
176 else:
177 fw("4 %d %d %d %d\n" % tuple(pf))
179 file.close()
180 print("writing %r done" % filepath)
182 return {'FINISHED'}
185 def save(operator,
186 context,
187 filepath="",
188 use_mesh_modifiers=True,
189 use_normals=True,
190 use_uv_coords=True,
191 use_colors=True,
192 global_matrix=None
195 scene = context.scene
196 obj = context.active_object
198 if global_matrix is None:
199 from mathutils import Matrix
200 global_matrix = Matrix()
202 if bpy.ops.object.mode_set.poll():
203 bpy.ops.object.mode_set(mode='OBJECT')
205 if use_mesh_modifiers and obj.modifiers:
206 mesh = obj.to_mesh(scene, True, 'PREVIEW')
207 else:
208 mesh = obj.data.copy()
210 if not mesh:
211 raise Exception("Error, could not get mesh data from active object")
213 mesh.transform(global_matrix * obj.matrix_world)
214 if use_normals:
215 mesh.calc_normals()
217 ret = save_mesh(filepath, mesh,
218 use_normals=use_normals,
219 use_uv_coords=use_uv_coords,
220 use_colors=use_colors,
223 if use_mesh_modifiers:
224 bpy.data.meshes.remove(mesh)
226 return ret