浏览代码

Moved exceptions and logger to the root of the package scope. This is to make these modules more accessible to the upcoming importer.
Added some error logging where exceptions are also raised.
Logger no longer assumes the file name. Sub-packages (exporter, importer) will define this name upon init.
Performed some code cleanup in exporter/geometry.py.

repsac 10 年之前
父节点
当前提交
af040867af

+ 0 - 0
utils/exporters/blender/addons/io_three/exporter/exceptions.py → utils/exporters/blender/addons/io_three/exceptions.py


+ 8 - 12
utils/exporters/blender/addons/io_three/exporter/__init__.py

@@ -1,22 +1,15 @@
 import os
 import os
 import sys
 import sys
 import traceback
 import traceback
-from .. import constants
-from . import (
-    scene, 
-    geometry, 
-    api, 
-    exceptions, 
-    logger, 
-    base_classes
-)
+from .. import constants, logger, exceptions
+from . import scene, geometry, api, base_classes
 
 
 
 
 def _error_handler(func):
 def _error_handler(func):
     
     
     def inner(filepath, options, *args, **kwargs):
     def inner(filepath, options, *args, **kwargs):
         level = options.get(constants.LOGGING, constants.DEBUG)
         level = options.get(constants.LOGGING, constants.DEBUG)
-        logger.init(level=level)
+        logger.init('io_three.export.log', level=level)
         api.init()
         api.init()
         
         
         try:
         try:
@@ -65,9 +58,12 @@ def export_geometry(filepath, options, node=None):
     if node is None:
     if node is None:
         node = api.active_object()
         node = api.active_object()
         if node is None:
         if node is None:
-            raise exceptions.SelectionError('Nothing selected')
+            msg = 'Nothing selected'
+            logger.error(msg)
+            raise exceptions.SelectionError(msg)
         if node.type != 'MESH':
         if node.type != 'MESH':
-            raise exceptions.GeometryError('Not a valid mesh object')
+            msg = 'Not a valid mesh object'
+            raise exceptions.GeometryError(msg)
     
     
     mesh = api.object.mesh(node, options)
     mesh = api.object.mesh(node, options)
     parent = base_classes.BaseScene(filepath, options)
     parent = base_classes.BaseScene(filepath, options)

+ 2 - 3
utils/exporters/blender/addons/io_three/exporter/base_classes.py

@@ -1,6 +1,5 @@
 import uuid
 import uuid
-from .. import constants 
-from .exceptions import ThreeValueError
+from .. import constants, exceptions 
 
 
 
 
 class BaseClass(constants.BASE_DICT):
 class BaseClass(constants.BASE_DICT):
@@ -18,7 +17,7 @@ class BaseClass(constants.BASE_DICT):
     def __setitem__(self, key, value):
     def __setitem__(self, key, value):
         if not isinstance(value, constants.VALID_DATA_TYPES):
         if not isinstance(value, constants.VALID_DATA_TYPES):
             msg = 'Value is an invalid data type: %s' % type(value)
             msg = 'Value is an invalid data type: %s' % type(value)
-            raise ThreeValueError(msg) 
+            raise exceptions.ThreeValueError(msg) 
         constants.BASE_DICT.__setitem__(self, key, value)
         constants.BASE_DICT.__setitem__(self, key, value)
 
 
     @property
     @property

+ 16 - 17
utils/exporters/blender/addons/io_three/exporter/geometry.py

@@ -1,6 +1,6 @@
 import os
 import os
-from .. import constants
-from . import base_classes, io, logger, api
+from .. import constants, logger
+from . import base_classes, io, api
 
 
 
 
 FORMAT_VERSION = 3
 FORMAT_VERSION = 3
@@ -23,7 +23,8 @@ class Geometry(base_classes.BaseNode):
         logger.info('Setting %s to "%s"', node, geo_type)
         logger.info('Setting %s to "%s"', node, geo_type)
 
 
         self._defaults[constants.TYPE] = geo_type
         self._defaults[constants.TYPE] = geo_type
-        base_classes.BaseNode.__init__(self, node, parent=parent,
+        base_classes.BaseNode.__init__(self, node, 
+            parent=parent,
             type=geo_type)
             type=geo_type)
 
 
     @property
     @property
@@ -59,27 +60,25 @@ class Geometry(base_classes.BaseNode):
         bitset = lambda x,y: x & ( 1 << y )
         bitset = lambda x,y: x & ( 1 << y )
         face_count = 0
         face_count = 0
 
 
+        masks = (constants.MASK[constants.UVS],
+            constants.MASK[constants.NORMALS],
+            constants.MASK[constants.COLORS])
+
         while offset < length:
         while offset < length:
             bit = faces[offset]
             bit = faces[offset]
             offset += 1
             offset += 1
             face_count += 1
             face_count += 1
-            is_quad = bitset(bit, constants.MASK[constants.QUAD])
-            has_material = bitset(bit, constants.MASK[constants.MATERIALS])
-            has_uv = bitset(bit, constants.MASK[constants.UVS])
-            has_normal = bitset(bit, constants.MASK[constants.NORMALS])
-            has_color = bitset(bit, constants.MASK[constants.COLORS])
 
 
+            is_quad = bitset(bit, constants.MASK[constants.QUAD])
             vector = 4 if is_quad else 3
             vector = 4 if is_quad else 3
             offset += vector
             offset += vector
 
 
-            if has_material:
+            if bitset(bit, constants.MASK[constants.MATERIALS]):
                 offset += 1
                 offset += 1
-            if has_uv:
-                offset += vector
-            if has_normal:
-                offset += vector
-            if has_color:
-                offset += vector
+
+            for mask in masks:
+                if bitset(bit, mask):
+                    offset += vector
 
 
         return face_count
         return face_count
 
 
@@ -109,7 +108,6 @@ class Geometry(base_classes.BaseNode):
             data[constants.MATERIALS] = self[constants.MATERIALS].copy()
             data[constants.MATERIALS] = self[constants.MATERIALS].copy()
         except KeyError:
         except KeyError:
             logger.debug('No materials to copy')
             logger.debug('No materials to copy')
-            pass
 
 
         return data
         return data
 
 
@@ -323,7 +321,8 @@ class Geometry(base_classes.BaseNode):
 
 
         if self.options.get(constants.COLORS):
         if self.options.get(constants.COLORS):
             logger.info('Parsing %s', constants.COLORS)
             logger.info('Parsing %s', constants.COLORS)
-            self[constants.COLORS] = api.mesh.vertex_colors(self.node)
+            self[constants.COLORS] = api.mesh.vertex_colors(
+                self.node)
         
         
         if self.options.get(constants.FACE_MATERIALS):
         if self.options.get(constants.FACE_MATERIALS):
             logger.info('Parsing %s', constants.FACE_MATERIALS)
             logger.info('Parsing %s', constants.FACE_MATERIALS)

+ 2 - 2
utils/exporters/blender/addons/io_three/exporter/image.py

@@ -1,6 +1,6 @@
 import os
 import os
-from .. import constants
-from . import base_classes, io, api, logger
+from .. import constants, logger
+from . import base_classes, io, api
 
 
 
 
 class Image(base_classes.BaseNode):
 class Image(base_classes.BaseNode):

+ 2 - 2
utils/exporters/blender/addons/io_three/exporter/io.py

@@ -1,6 +1,6 @@
 import shutil
 import shutil
-from .. import constants
-from . import _json, logger
+from .. import constants, logger
+from . import _json
 
 
 
 
 def copy_registered_textures(dest, registration):
 def copy_registered_textures(dest, registration):

+ 2 - 2
utils/exporters/blender/addons/io_three/exporter/material.py

@@ -1,5 +1,5 @@
-from .. import constants
-from . import base_classes, utilities, logger, api
+from .. import constants, logger
+from . import base_classes, utilities, api
 
 
 
 
 class Material(base_classes.BaseNode):
 class Material(base_classes.BaseNode):

+ 2 - 2
utils/exporters/blender/addons/io_three/exporter/object.py

@@ -1,5 +1,5 @@
-from .. import constants
-from . import base_classes, logger, api
+from .. import constants, logger
+from . import base_classes, api
 
 
 
 
 class Object(base_classes.BaseNode):
 class Object(base_classes.BaseNode):

+ 1 - 2
utils/exporters/blender/addons/io_three/exporter/scene.py

@@ -1,12 +1,11 @@
 import os
 import os
-from .. import constants
+from .. import constants, logger
 from . import (
 from . import (
     base_classes,
     base_classes,
     texture,
     texture,
     material,
     material,
     geometry, 
     geometry, 
     object,
     object,
-    logger,
     io,
     io,
     api
     api
 )
 )

+ 2 - 2
utils/exporters/blender/addons/io_three/exporter/texture.py

@@ -1,5 +1,5 @@
-from .. import constants
-from . import base_classes, image, api, logger
+from .. import constants, logger
+from . import base_classes, image, api
 
 
 
 
 class Texture(base_classes.BaseNode):
 class Texture(base_classes.BaseNode):

+ 3 - 4
utils/exporters/blender/addons/io_three/exporter/logger.py → utils/exporters/blender/addons/io_three/logger.py

@@ -2,11 +2,10 @@ import os
 import logging
 import logging
 import tempfile
 import tempfile
 
 
-from .. import constants
+from . import constants
 
 
 LOG_FILE = None
 LOG_FILE = None
 LOGGER = None
 LOGGER = None
-FILE_NAME = 'io_three.export.log'
 
 
 LEVELS = {
 LEVELS = {
     constants.DEBUG: logging.DEBUG,
     constants.DEBUG: logging.DEBUG,
@@ -16,9 +15,9 @@ LEVELS = {
     constants.CRITICAL: logging.CRITICAL
     constants.CRITICAL: logging.CRITICAL
 }
 }
 
 
-def init(level=constants.DEBUG):
+def init(filename, level=constants.DEBUG):
     global LOG_FILE
     global LOG_FILE
-    LOG_FILE = os.path.join(tempfile.gettempdir(), FILE_NAME)
+    LOG_FILE = os.path.join(tempfile.gettempdir(), filename)
     with open(LOG_FILE, 'w'):
     with open(LOG_FILE, 'w'):
         pass
         pass