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 #####
22 Import and export STL files
24 Used as a blender script, it load all the stl files in the scene:
26 blender --python stl_utils.py -- file1.stl file2.stl file3.stl ...
34 Set struct with order.
37 - insert data into without doubles
38 - get the list of data in insertion order with self.list
40 Like collections.OrderedDict, but quicker, can be replaced if
51 Add a value to the Set, return its position in it.
53 value
= self
.setdefault(item
, self
._len
)
54 if value
== self
._len
:
55 self
.list.append(item
)
61 # an stl binary file is
62 # - 80 bytes of description
63 # - 4 bytes of size (unsigned int)
66 # - 12 bytes of normal
67 # - 9 * 4 bytes of coordinate (3*3 floats)
68 # - 2 bytes of garbage (usually 0)
70 BINARY_STRIDE
= 12 * 4 + 2
73 def _header_version():
75 return "Exported from Blender-" + bpy
.app
.version_string
78 def _is_ascii_file(data
):
80 This function returns True if the data represents an ASCII file.
82 Please note that a False value does not necessary means that the data
83 represents a binary file. It can be a (very *RARE* in real life, but
84 can easily be forged) ascii file.
91 data
.seek(BINARY_HEADER
)
92 size
= struct
.unpack('<I', data
.read(4))[0]
93 # Use seek() method to get size of the file.
94 data
.seek(0, os
.SEEK_END
)
95 file_size
= data
.tell()
96 # Reset to the start of the file.
99 if size
== 0: # Odds to get that result from an ASCII file are null...
100 print("WARNING! Reported size (facet number) is 0, assuming invalid binary STL file.")
101 return False # Assume binary in this case.
103 return (file_size
!= BINARY_HEADER
+ 4 + BINARY_STRIDE
* size
)
106 def _binary_read(data
):
112 data
.seek(BINARY_HEADER
)
113 size
= struct
.unpack('<I', data
.read(4))[0]
116 # Workaround invalid crap.
117 data
.seek(0, os
.SEEK_END
)
118 file_size
= data
.tell()
119 # Reset to after-the-size in the file.
120 data
.seek(BINARY_HEADER
+ 4)
122 file_size
-= BINARY_HEADER
+ 4
123 size
= file_size
// BINARY_STRIDE
124 print("WARNING! Reported size (facet number) is 0, inferring %d facets from file size." % size
)
126 # We read 4096 elements at once, avoids too much calls to read()!
128 chunks
= [CHUNK_LEN
] * (size
// CHUNK_LEN
)
129 chunks
.append(size
% CHUNK_LEN
)
131 unpack
= struct
.Struct('<12f').unpack_from
132 for chunk_len
in chunks
:
135 buf
= data
.read(BINARY_STRIDE
* chunk_len
)
136 for i
in range(chunk_len
):
137 # read the normal and points coordinates of each triangle
138 pt
= unpack(buf
, BINARY_STRIDE
* i
)
139 yield pt
[:3], (pt
[3:6], pt
[6:9], pt
[9:])
142 def _ascii_read(data
):
143 # an stl ascii file is like
144 # HEADER: solid some name
162 if l
.startswith(b
'facet'):
163 curr_nor
= tuple(map(float, l
.split()[2:]))
164 # if we encounter a vertex, read next 2
165 if l
.startswith(b
'vertex'):
166 yield curr_nor
, [tuple(map(float, l_item
.split()[1:])) for l_item
in (l
, data
.readline(), data
.readline())]
169 def _binary_write(filepath
, faces
):
172 from mathutils
.geometry
import normal
174 with
open(filepath
, 'wb') as data
:
177 # we write padding at header beginning to avoid to
178 # call len(list(faces)) which may be expensive
179 fw(struct
.calcsize('<80sI') * b
'\0')
182 pack
= struct
.Struct('<9f').pack
184 # number of vertices written
188 # calculate face normal
189 # write normal + vertexes + pad as attributes
190 fw(struct
.pack('<3f', *normal(*face
)) + pack(*itertools
.chain
.from_iterable(face
)))
191 # attribute byte count (unused)
195 # header, with correct value now
197 fw(struct
.pack('<80sI', _header_version().encode('ascii'), nb
))
200 def _ascii_write(filepath
, faces
):
201 from mathutils
.geometry
import normal
203 with
open(filepath
, 'w') as data
:
205 header
= _header_version()
206 fw('solid %s\n' % header
)
209 # calculate face normal
210 fw('facet normal %f %f %f\nouter loop\n' % normal(*face
)[:])
212 fw('vertex %f %f %f\n' % vert
[:])
213 fw('endloop\nendfacet\n')
215 fw('endsolid %s\n' % header
)
218 def write_stl(filepath
="", faces
=(), ascii
=False):
220 Write a stl file from faces,
226 iterable of tuple of 3 vertex, vertex is tuple of 3 coordinates as float
229 save the file in ascii format (very huge)
231 (_ascii_write
if ascii
else _binary_write
)(filepath
, faces
)
234 def read_stl(filepath
):
236 Return the triangles and points of an stl binary file.
238 Please note that this process can take lot of time if the file is
239 huge (~1m30 for a 1 Go stl file on an quad core i7).
241 - returns a tuple(triangles, triangles' normals, points).
244 A list of triangles, each triangle as a tuple of 3 index of
248 A list of vectors3 (tuples, xyz).
251 An indexed list of points, each point is a tuple of 3 float
256 >>> tris, tri_nors, pts = read_stl(filepath)
259 >>> # print the coordinate of the triangle n
260 >>> print(pts[i] for i in tris[n])
263 start_time
= time
.process_time()
265 tris
, tri_nors
, pts
= [], [], ListDict()
267 with
open(filepath
, 'rb') as data
:
268 # check for ascii or binary
269 gen
= _ascii_read
if _is_ascii_file(data
) else _binary_read
271 for nor
, pt
in gen(data
):
272 # Add the triangle and the point.
273 # If the point is already in the list of points, the
274 # index returned by pts.add() will be the one from the
275 # first equal point inserted.
276 tris
.append([pts
.add(p
) for p
in pt
])
279 print('Import finished in %.4f sec.' % (time
.process_time() - start_time
))
281 return tris
, tri_nors
, pts
.list
284 if __name__
== '__main__':
287 from io_mesh_stl
import blender_utils
289 filepaths
= sys
.argv
[sys
.argv
.index('--') + 1:]
291 for filepath
in filepaths
:
292 objName
= bpy
.path
.display_name(filepath
)
293 tris
, pts
= read_stl(filepath
)
295 blender_utils
.create_and_link_mesh(objName
, tris
, pts
)