Преглед изворни кода

add support for texture folder

mese79 пре 10 година
родитељ
комит
932ee2d443

+ 14 - 3
utils/exporters/blender/addons/io_three/__init__.py

@@ -26,7 +26,8 @@ from bpy.props import (
     EnumProperty,
     BoolProperty,
     FloatProperty,
-    IntProperty
+    IntProperty,
+    StringProperty
 )
 
 from . import constants
@@ -41,7 +42,7 @@ SETTINGS_FILE_EXPORT = 'three_settings_export.js'
 bl_info = {
     'name': "Three.js Format",
     'author': "repsac, mrdoob, yomotsu, mpk, jpweeks",
-    'version': (1, 2, 3),
+    'version': (1, 2, 2),
     'blender': (2, 7, 3),
     'location': "File > Export",
     'description': "Export Three.js formatted JSON files.",
@@ -295,6 +296,7 @@ def save_settings_export(properties):
         constants.COMPRESSION: properties.option_compression,
         constants.INDENT: properties.option_indent,
         constants.COPY_TEXTURES: properties.option_copy_textures,
+        constants.TEXTURE_FOLDER: properties.option_texture_folder,
 
         constants.SCENE: properties.option_export_scene,
         #constants.EMBED_GEOMETRY: properties.option_embed_geometry,
@@ -420,6 +422,10 @@ def restore_settings_export(properties):
         constants.COPY_TEXTURES,
         constants.EXPORT_OPTIONS[constants.COPY_TEXTURES])
 
+    properties.option_texture_folder = settings.get(
+        constants.TEXTURE_FOLDER,
+        constants.EXPORT_OPTIONS[constants.TEXTURE_FOLDER])
+
     properties.option_embed_animation = settings.get(
         constants.EMBED_ANIMATION,
         constants.EXPORT_OPTIONS[constants.EMBED_ANIMATION])
@@ -617,6 +623,9 @@ class ExportThree(bpy.types.Operator, ExportHelper):
         description="Copy textures",
         default=constants.EXPORT_OPTIONS[constants.COPY_TEXTURES])
 
+    option_texture_folder = StringProperty(name="Texture folder",
+        description="add this folder to textures path", default="")
+
     option_lights = BoolProperty(
         name="Lights",
         description="Export default scene lights",
@@ -693,7 +702,6 @@ class ExportThree(bpy.types.Operator, ExportHelper):
             raise Exception("filename not set")
 
         settings = save_settings_export(self.properties)
-        settings['addon_version'] = bl_info['version']
 
         filepath = self.filepath
         if settings[constants.COMPRESSION] == constants.MSGPACK:
@@ -812,6 +820,9 @@ class ExportThree(bpy.types.Operator, ExportHelper):
         row = layout.row()
         row.prop(self.properties, 'option_copy_textures')
 
+        row = layout.row()
+        row.prop(self.properties, "option_texture_folder")
+
         row = layout.row()
         row.prop(self.properties, 'option_scale')
 

+ 7 - 5
utils/exporters/blender/addons/io_three/constants.py

@@ -63,6 +63,7 @@ LIGHTS = 'lights'
 FACE_MATERIALS = 'faceMaterials'
 SKINNING = 'skinning'
 COPY_TEXTURES = 'copyTextures'
+TEXTURE_FOLDER = "texture_folder"
 ENABLE_PRECISION = 'enablePrecision'
 PRECISION = 'precision'
 DEFAULT_PRECISION = 6
@@ -91,18 +92,18 @@ INFLUENCES_PER_VERTEX = 'influencesPerVertex'
 EXPORT_OPTIONS = {
     FACES: True,
     VERTICES: True,
-    NORMALS: False,
-    UVS: False,
+    NORMALS: True,
+    UVS: True,
     COLORS: False,
-    MATERIALS: False,
+    MATERIALS: True,
     FACE_MATERIALS: False,
     SCALE: 1,
     FRAME_STEP: 1,
     FRAME_INDEX_AS_TIME: False,
-    SCENE: False,
+    SCENE: True,
     MIX_COLORS: False,
     COMPRESSION: None,
-    MAPS: False,
+    MAPS: True,
     ANIMATION: OFF,
     BONES: False,
     SKINNING: False,
@@ -110,6 +111,7 @@ EXPORT_OPTIONS = {
     CAMERAS: False,
     LIGHTS: False,
     COPY_TEXTURES: True,
+    TEXTURE_FOLDER: "",
     LOGGING: DEBUG,
     ENABLE_PRECISION: True,
     PRECISION: DEFAULT_PRECISION,

+ 5 - 18
utils/exporters/blender/addons/io_three/exporter/__init__.py

@@ -1,7 +1,7 @@
 import os
 import sys
 import traceback
-from .. import constants, logger, exceptions, dialogs
+from .. import constants, logger, exceptions
 from . import scene, geometry, api, base_classes
 
 
@@ -9,10 +9,7 @@ def _error_handler(func):
     
     def inner(filepath, options, *args, **kwargs):
         level = options.get(constants.LOGGING, constants.DEBUG)
-        version = options.get('addon_version')
         logger.init('io_three.export.log', level=level)
-        if version is not None:
-            logger.debug("Addon Version %s", version)
         api.init()
         
         try:
@@ -58,26 +55,16 @@ def export_scene(filepath, options):
 
 @_error_handler
 def export_geometry(filepath, options, node=None):
-    msg = ""
-    exception = None
     if node is None:
         node = api.active_object()
         if node is None:
-            msg = "Nothing selected"
+            msg = 'Nothing selected'
             logger.error(msg)
-            exception = exceptions.SelectionError
+            raise exceptions.SelectionError(msg)
         if node.type != 'MESH':
-            msg = "%s is not a valid mesh object" % node.name
-            logger.error(msg)
-            exception = exceptions.GeometryError
+            msg = 'Not a valid mesh object'
+            raise exceptions.GeometryError(msg)
     
-    if exception is not None:
-        if api.batch_mode():
-            raise exception(msg)
-        else:
-            dialogs.error(msg)
-            return
-
     mesh = api.object.mesh(node, options)
     parent = base_classes.BaseScene(filepath, options)
     geo = geometry.Geometry(mesh, parent)

+ 0 - 10
utils/exporters/blender/addons/io_three/exporter/api/__init__.py

@@ -13,16 +13,6 @@ def active_object():
     return bpy.context.scene.objects.active
 
 
-def batch_mode():
-    """
-
-    :return: Whether or not the session is interactive
-    :rtype: bool
-
-    """
-    return bpy.context.area is None
-
-
 def init():
     """Initializing the api module. Required first step before
     initializing the actual export process.

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

@@ -380,7 +380,7 @@ def _valid_textures(material):
     for texture in material.texture_slots:
         if not texture:
             continue
-        if texture.texture.type != IMAGE:
+        if texture.texture.type != IMAGE or not texture.use:
             continue
         logger.debug("Valid texture found %s", texture)
         yield texture

+ 6 - 3
utils/exporters/blender/addons/io_three/exporter/api/texture.py

@@ -170,6 +170,9 @@ def textures():
 
     """
     logger.debug("texture.textures()")
-    for texture in data.textures:
-        if texture.type == IMAGE:
-            yield texture.name
+    for mat in data.materials:
+        if mat.users == 0:
+            continue
+        for slot in mat.texture_slots:
+            if slot and slot.use and slot.texture.type == IMAGE:
+                yield slot.texture.name

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

@@ -135,7 +135,7 @@ class Geometry(base_classes.BaseNode):
 
         return data
 
-    def copy_textures(self):
+    def copy_textures(self, texture_folder=""):
         """Copy the textures to the destination directory."""
         logger.debug("Geometry().copy_textures()")
         if self.options.get(constants.COPY_TEXTURES):
@@ -143,7 +143,7 @@ class Geometry(base_classes.BaseNode):
             if texture_registration:
                 logger.info("%s has registered textures", self.node)
                 io.copy_registered_textures(
-                    os.path.dirname(self.scene.filepath),
+                    os.path.join(os.path.dirname(self.scene.filepath), texture_folder),
                     texture_registration)
 
     def parse(self):

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

@@ -11,7 +11,8 @@ class Image(base_classes.BaseNode):
         logger.debug("Image().__init__(%s)", node)
         base_classes.BaseNode.__init__(self, node, parent, constants.IMAGE)
 
-        self[constants.URL] = api.image.file_name(self.node)
+        texture_folder = self.scene.options.get(constants.TEXTURE_FOLDER, "")
+        self[constants.URL] = os.path.join(texture_folder, api.image.file_name(self.node))
 
     @property
     def destination(self):

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

@@ -14,6 +14,7 @@ def copy_registered_textures(dest, registration):
 
     """
     logger.debug("io.copy_registered_textures(%s, %s)", dest, registration)
+    os.makedirs(dest, exist_ok=True)
     for value in registration.values():
         copy(value['file_path'], dest)
 

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

@@ -159,9 +159,10 @@ class Scene(base_classes.BaseScene):
         io.dump(self.filepath, data, options=self.options)
 
         if self.options.get(constants.COPY_TEXTURES):
+            texture_folder = self.options.get(constants.TEXTURE_FOLDER)
             for geo in self[constants.GEOMETRIES]:
                 logger.info("Copying textures from %s", geo.node)
-                geo.copy_textures()
+                geo.copy_textures(texture_folder)
 
     def _parse_geometries(self):
         """Locate all geometry nodes and parse them"""