Browse Source

Add feature to blender exporter to embed textures in .json file as base64 string, much like three.js editor

Matt Hirsch 9 years ago
parent
commit
8c02a182a6

+ 19 - 0
utils/exporters/blender/addons/io_three/__init__.py

@@ -378,6 +378,10 @@ def restore_export_settings(properties, settings):
         constants.COPY_TEXTURES,
         constants.EXPORT_OPTIONS[constants.COPY_TEXTURES])
 
+    properties.option_embed_textures = settings.get(
+        constants.EMBED_TEXTURES,
+        constants.EXPORT_OPTIONS[constants.EMBED_TEXTURES])
+
     properties.option_texture_folder = settings.get(
         constants.TEXTURE_FOLDER,
         constants.EXPORT_OPTIONS[constants.TEXTURE_FOLDER])
@@ -468,6 +472,7 @@ def set_settings(properties):
         constants.COMPRESSION: properties.option_compression,
         constants.INDENT: properties.option_indent,
         constants.COPY_TEXTURES: properties.option_copy_textures,
+        constants.EMBED_TEXTURES: properties.option_embed_textures,
         constants.TEXTURE_FOLDER: properties.option_texture_folder,
 
         constants.SCENE: properties.option_export_scene,
@@ -521,6 +526,10 @@ def animation_options():
 
     return anim
 
+def resolve_conflicts(self, context):
+    if(self.option_embed_textures):
+        self.option_copy_textures = False;
+
 class ExportThree(bpy.types.Operator, ExportHelper):
     """Class that handles the export properties"""
 
@@ -670,6 +679,12 @@ class ExportThree(bpy.types.Operator, ExportHelper):
         description="Copy textures",
         default=constants.EXPORT_OPTIONS[constants.COPY_TEXTURES])
 
+    option_embed_textures = BoolProperty(
+        name="Embed textures",
+        description="Embed base64 textures in .json",
+        default=constants.EXPORT_OPTIONS[constants.EMBED_TEXTURES],
+        update=resolve_conflicts)
+
     option_texture_folder = StringProperty(
         name="Texture folder",
         description="add this folder to textures path",
@@ -915,6 +930,10 @@ class ExportThree(bpy.types.Operator, ExportHelper):
 
         row = layout.row()
         row.prop(self.properties, 'option_copy_textures')
+        row.enabled = not self.properties.option_embed_textures
+
+        row = layout.row()
+        row.prop(self.properties, 'option_embed_textures')
 
         row = layout.row()
         row.prop(self.properties, 'option_texture_folder')

+ 2 - 0
utils/exporters/blender/addons/io_three/constants.py

@@ -94,6 +94,7 @@ HIERARCHY = 'hierarchy'
 FACE_MATERIALS = 'faceMaterials'
 SKINNING = 'skinning'
 COPY_TEXTURES = 'copyTextures'
+EMBED_TEXTURES = 'embedTextures'
 TEXTURE_FOLDER = 'textureFolder'
 ENABLE_PRECISION = 'enablePrecision'
 PRECISION = 'precision'
@@ -154,6 +155,7 @@ EXPORT_OPTIONS = {
     LIGHTS: False,
     HIERARCHY: False,
     COPY_TEXTURES: True,
+    EMBED_TEXTURES: False,
     TEXTURE_FOLDER: '',
     LOGGING: DEBUG,
     ENABLE_PRECISION: True,

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

@@ -1,4 +1,5 @@
 import os
+import base64
 from .. import constants, logger
 from . import base_classes, io, api
 
@@ -11,8 +12,17 @@ class Image(base_classes.BaseNode):
         logger.debug("Image().__init__(%s)", node)
         base_classes.BaseNode.__init__(self, node, parent, constants.IMAGE)
 
-        texture_folder = self.scene.options.get(constants.TEXTURE_FOLDER, "")
-        self[constants.URL] = os.path.join(texture_folder, api.image.file_name(self.node))
+        if(self.scene.options.get(constants.EMBED_TEXTURES, False)):
+            texturefile = open(api.image.file_path(self.node),"rb")
+            extension = os.path.splitext(api.image.file_path(self.node))[1][1:].strip().lower()
+            if(extension == 'jpg') :
+                extension = 'jpeg'
+            self[constants.URL] = "data:image/" + extension + ";base64," + base64.b64encode(texturefile.read()).decode("utf-8")
+            texturefile.close();
+        else:
+            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):