فهرست منبع

Added auto-detection of numpy

If numpy is not installed, pyassimp returns lists to allow the user to manage the data type.
Tcll 10 سال پیش
والد
کامیت
2e725c0dc5
1فایلهای تغییر یافته به همراه54 افزوده شده و 13 حذف شده
  1. 54 13
      port/PyAssimp/pyassimp/core.py

+ 54 - 13
port/PyAssimp/pyassimp/core.py

@@ -10,7 +10,9 @@ if sys.version_info < (2,6):
 
 import ctypes
 import os
-import numpy
+
+try: import numpy
+except: numpy = None
 
 import logging
 logger = logging.getLogger("pyassimp")
@@ -33,12 +35,30 @@ _assimp_lib = AssimpLib()
 
 def make_tuple(ai_obj, type = None):
     res = None
+
+    #notes:
+    # ai_obj._fields_ = [ ("attr", c_type), ... ]
+    # getattr(ai_obj, e[0]).__class__ == float
+
     if isinstance(ai_obj, structs.Matrix4x4):
-        res = numpy.array([getattr(ai_obj, e[0]) for e in ai_obj._fields_]).reshape((4,4))
+        if numpy:
+            res = numpy.array([getattr(ai_obj, e[0]) for e in ai_obj._fields_]).reshape((4,4))
+            #import pdb;pdb.set_trace()
+        else:
+            res = [getattr(ai_obj, e[0]) for e in ai_obj._fields_]
+            res = [res[i:i+4] for i in xrange(0,16,4)]
     elif isinstance(ai_obj, structs.Matrix3x3):
-        res = numpy.array([getattr(ai_obj, e[0]) for e in ai_obj._fields_]).reshape((3,3))
+        if numpy:
+            res = numpy.array([getattr(ai_obj, e[0]) for e in ai_obj._fields_]).reshape((3,3))
+        else:
+            res = [getattr(ai_obj, e[0]) for e in ai_obj._fields_]
+            res = [res[i:i+3] for i in xrange(0,9,3)]
     else:
-        res = numpy.array([getattr(ai_obj, e[0]) for e in ai_obj._fields_])
+        if numpy:
+            res = numpy.array([getattr(ai_obj, e[0]) for e in ai_obj._fields_])
+        else:
+            res = [getattr(ai_obj, e[0]) for e in ai_obj._fields_]
+
     return res
 
 # It is faster and more correct to have an init function for each assimp class
@@ -135,9 +155,14 @@ def _init(self, target = None, parent = None):
 
                 try:
                     if obj._type_ in structs.assimp_structs_as_tuple:
-                        setattr(target, name, numpy.array([make_tuple(obj[i]) for i in range(length)], dtype=numpy.float32))
+                        if numpy:
+                            setattr(target, name, numpy.array([make_tuple(obj[i]) for i in range(length)], dtype=numpy.float32))
 
-                        logger.debug(str(self) + ": Added an array of numpy arrays (type "+ str(type(obj)) + ") as self." + name)
+                            logger.debug(str(self) + ": Added an array of numpy arrays (type "+ str(type(obj)) + ") as self." + name)
+                        else:
+                            setattr(target, name, [make_tuple(obj[i]) for i in range(length)])
+                            
+                            logger.debug(str(self) + ": Added a list of lists (type "+ str(type(obj)) + ") as self." + name)
 
                     else:
                         setattr(target, name, [obj[i] for i in range(length)]) #TODO: maybe not necessary to recreate an array?
@@ -292,7 +317,10 @@ def release(scene):
 
 def _finalize_texture(tex, target):
     setattr(target, "achformathint", tex.achFormatHint)
-    data = numpy.array([make_tuple(getattr(tex, "pcData")[i]) for i in range(tex.mWidth * tex.mHeight)])
+    if numpy:
+        data = numpy.array([make_tuple(getattr(tex, "pcData")[i]) for i in range(tex.mWidth * tex.mHeight)])
+    else:
+        data = [make_tuple(getattr(tex, "pcData")[i]) for i in range(tex.mWidth * tex.mHeight)]
     setattr(target, "data", data)
 
 def _finalize_mesh(mesh, target):
@@ -308,11 +336,18 @@ def _finalize_mesh(mesh, target):
 
     def fill(name):
         mAttr = getattr(mesh, name)
-        if mAttr:
-            data = numpy.array([make_tuple(getattr(mesh, name)[i]) for i in range(nb_vertices)], dtype=numpy.float32)
-            setattr(target, name[1:].lower(), data)
+        if numpy:
+            if mAttr:
+                data = numpy.array([make_tuple(getattr(mesh, name)[i]) for i in range(nb_vertices)], dtype=numpy.float32)
+                setattr(target, name[1:].lower(), data)
+            else:
+                setattr(target, name[1:].lower(), numpy.array([], dtype="float32"))
         else:
-            setattr(target, name[1:].lower(), numpy.array([], dtype="float32"))
+            if mAttr:
+                data = [make_tuple(getattr(mesh, name)[i]) for i in range(nb_vertices)]
+                setattr(target, name[1:].lower(), data)
+            else:
+                setattr(target, name[1:].lower(), [])
 
     def fillarray(name):
         mAttr = getattr(mesh, name)
@@ -322,7 +357,10 @@ def _finalize_mesh(mesh, target):
             if mSubAttr:
                 data.append([make_tuple(getattr(mesh, name)[index][i]) for i in range(nb_vertices)])
 
-        setattr(target, name[1:].lower(), numpy.array(data, dtype=numpy.float32))
+        if numpy:
+            setattr(target, name[1:].lower(), numpy.array(data, dtype=numpy.float32))
+        else:
+            setattr(target, name[1:].lower(), data)
 
     fill("mNormals")
     fill("mTangents")
@@ -332,7 +370,10 @@ def _finalize_mesh(mesh, target):
     fillarray("mTextureCoords")
     
     # prepare faces
-    faces = numpy.array([f.indices for f in target.faces], dtype=numpy.int32)
+    if numpy:
+        faces = numpy.array([f.indices for f in target.faces], dtype=numpy.int32)
+    else:
+        faces = [f.indices for f in target.faces]
     setattr(target, 'faces', faces)