diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2015-04-08 13:39:30 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2015-04-08 13:39:30 +0300 |
commit | 74df87c3589053cc567493657bef20160a5e67df (patch) | |
tree | 8c6257b5bb95ea3d04faa6ddf71fc35a4758cfe3 /io_mesh_stl | |
parent | 8ec82c7a976fc13bdb9dae3c84ac58e13e01a8b9 (diff) |
Fix T44292: Work around invalid binary STL files on import.
Some report a size (facet number) of zero... To think even something as
simple as STL format cannot be respected universally... :'(
Diffstat (limited to 'io_mesh_stl')
-rw-r--r-- | io_mesh_stl/__init__.py | 2 | ||||
-rw-r--r-- | io_mesh_stl/stl_utils.py | 19 |
2 files changed, 18 insertions, 3 deletions
diff --git a/io_mesh_stl/__init__.py b/io_mesh_stl/__init__.py index 8a0b1086..9e693271 100644 --- a/io_mesh_stl/__init__.py +++ b/io_mesh_stl/__init__.py @@ -21,7 +21,7 @@ bl_info = { "name": "STL format", "author": "Guillaume Bouchard (Guillaum)", - "version": (1, 1, 1), + "version": (1, 1, 2), "blender": (2, 74, 0), "location": "File > Import-Export > Stl", "description": "Import-Export STL files", diff --git a/io_mesh_stl/stl_utils.py b/io_mesh_stl/stl_utils.py index d1dea5e9..0ab24e3f 100644 --- a/io_mesh_stl/stl_utils.py +++ b/io_mesh_stl/stl_utils.py @@ -79,7 +79,7 @@ def _is_ascii_file(data): represents a binary file. It can be a (very *RARE* in real life, but can easily be forged) ascii file. """ - # Skip header... + # Skip header... data.seek(BINARY_HEADER) size = struct.unpack('<I', data.read(4))[0] # Use seek() method to get size of the file. @@ -88,6 +88,10 @@ def _is_ascii_file(data): # Reset to the start of the file. data.seek(0) + if size == 0: # Odds to get that result from an ASCII file are null... + print("WARNING! Reported size (facet number) is 0, assuming invalid binary STL file.") + return False # Assume binary in this case. + return (file_size != BINARY_HEADER + 4 + BINARY_STRIDE * size) @@ -105,10 +109,21 @@ def _binary_read(data): # STRIDE between each triangle (first normal + coordinates + garbage) OFFSET = 12 - # Skip header... + # Skip header... data.seek(BINARY_HEADER) size = struct.unpack('<I', data.read(4))[0] + if size == 0: + # Workaround invalid crap. + data.seek(0, os.SEEK_END) + file_size = data.tell() + # Reset to after-the-size in the file. + data.seek(BINARY_HEADER + 4) + + file_size -= BINARY_HEADER + 4 + size = file_size // BINARY_STRIDE + print("WARNING! Reported size (facet number) is 0, inferring %d facets from file size." % size) + # We read 4096 elements at once, avoids too much calls to read()! CHUNK_LEN = 4096 chunks = [CHUNK_LEN] * (size // CHUNK_LEN) |