Merge remote-tracking branch 'refs/remotes/elrond/sql/media_data'
[mediagoblin.git] / mediagoblin / media_types / stl / model_loader.py
index a417b04c84f84b4b5b74f4a108d83ab20fcb30ff..60fa4851567eae5c62f95e0ae9c28a84b2a7bf81 100644 (file)
@@ -40,7 +40,7 @@ class ThreeDee():
 
         self.load(fileob)
         if not len(self.verts):
-            raise ThreeDeeParseError("Empyt model.")
+            raise ThreeDeeParseError("Empty model.")
 
         for vector in self.verts:
             for i in range(3):
@@ -90,35 +90,14 @@ class BinaryStlModel(ThreeDee):
     http://en.wikipedia.org/wiki/STL_%28file_format%29#Binary_STL
     """
 
-    def __num(self, fileob, hint):
-        assert hint == "uint" or hint == "real" or hint == "short"
-        form = None
-        bits = 0
-        if hint == "uint":
-            form = "<I" # little-endian unsigned int
-            bits = 32
-        elif hint == "real":
-            form = "<i" # little-endian signed int
-            bits = 32
-        elif hint == "short":
-            form = "<H" # little-endian unsigned short
-            bits = 16
-        return struct.unpack(form, fileob.read(bits/8))[0]
-
-    def __vector(self, fileob):
-        return tuple([self.__num(fileob, "real") for n in range(3)])
-
     def load(self, fileob):
         fileob.seek(80) # skip the header
-        triangle_count = self.__num(fileob, "uint")
-        for i in range(triangle_count):
-            self.__vector(fileob) # skip the normal vector
+        count = struct.unpack("<I", fileob.read(4))[0]
+        for i in range(count):
+            fileob.read(12) # skip the normal vector
             for v in range(3):
-                # - FIXME - traingle_count IS reporting the correct
-                # number, but the vertex information appears to be
-                # total nonsense :(
-                self.verts.append(self.__vector(fileob))
-            self.__num(fileob, "short") # skip the attribute byte count
+                self.verts.append(struct.unpack("<3f", fileob.read(12)))
+            fileob.read(2) # skip the attribute bytes
 
 
 def auto_detect(fileob, hint):