[Bf-extensions-cvs] [74df87c] master: Fix T44292: Work around invalid binary STL files on import.

Bastien Montagne noreply at git.blender.org
Wed Apr 8 12:40:55 CEST 2015


Commit: 74df87c3589053cc567493657bef20160a5e67df
Author: Bastien Montagne
Date:   Wed Apr 8 12:39:30 2015 +0200
Branches: master
https://developer.blender.org/rBA74df87c3589053cc567493657bef20160a5e67df

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... :'(

===================================================================

M	io_mesh_stl/__init__.py
M	io_mesh_stl/stl_utils.py

===================================================================

diff --git a/io_mesh_stl/__init__.py b/io_mesh_stl/__init__.py
index 8a0b108..9e69327 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 d1dea5e..0ab24e3 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)



More information about the Bf-extensions-cvs mailing list