[Bf-extensions-cvs] [5371d6ea] master: glTF exporter: support empty images

Julien Duroure noreply at git.blender.org
Sat Mar 23 10:02:32 CET 2019


Commit: 5371d6eadeafa35be8bc4f81057c3c4bfaf6d0d9
Author: Julien Duroure
Date:   Sat Mar 23 10:01:09 2019 +0100
Branches: master
https://developer.blender.org/rBA5371d6eadeafa35be8bc4f81057c3c4bfaf6d0d9

glTF exporter: support empty images

===================================================================

M	io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_texture.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_texture_info.py
M	io_scene_gltf2/io/com/gltf2_io.py

===================================================================

diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py b/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py
index 63cd257b..b570e616 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py
@@ -23,6 +23,7 @@ from io_scene_gltf2.io.com import gltf2_io
 from io_scene_gltf2.blender.exp import gltf2_blender_search_node_tree
 from io_scene_gltf2.io.exp import gltf2_io_binary_data
 from io_scene_gltf2.io.exp import gltf2_io_image_data
+from io_scene_gltf2.io.com import gltf2_io_debug
 
 
 def gather_image(
@@ -33,10 +34,15 @@ def gather_image(
         return None
 
     uri = __gather_uri(blender_shader_sockets_or_texture_slots, export_settings)
+    buffer_view = __gather_buffer_view(blender_shader_sockets_or_texture_slots, export_settings)
+    if not (uri is not None or buffer_view is not None):
+        # The blender image has no data
+        return None
+
     mime_type = __gather_mime_type(uri.filepath if uri is not None else "")
 
     image = gltf2_io.Image(
-        buffer_view=__gather_buffer_view(blender_shader_sockets_or_texture_slots, export_settings),
+        buffer_view=buffer_view,
         extensions=__gather_extensions(blender_shader_sockets_or_texture_slots, export_settings),
         extras=__gather_extras(blender_shader_sockets_or_texture_slots, export_settings),
         mime_type=mime_type,
@@ -55,6 +61,8 @@ def __filter_image(sockets_or_slots, export_settings):
 def __gather_buffer_view(sockets_or_slots, export_settings):
     if export_settings[gltf2_blender_export_keys.FORMAT] != 'GLTF_SEPARATE':
         image = __get_image_data(sockets_or_slots, export_settings)
+        if image is None:
+            return None
         return gltf2_io_binary_data.BinaryData(
             data=image.to_image_data(__gather_mime_type()))
     return None
@@ -106,9 +114,7 @@ def __get_image_data(sockets_or_slots, export_settings):
     # For shared ressources, such as images, we just store the portion of data that is needed in the glTF property
     # in a helper class. During generation of the glTF in the exporter these will then be combined to actual binary
     # ressources.
-    def split_pixels_by_channels(image: bpy.types.Image, export_settings) -> typing.List[typing.List[float]]:
-        assert image.channels > 0, "Image '{}' has no color channels and cannot be exported.".format(image.name)
-
+    def split_pixels_by_channels(image: bpy.types.Image, export_settings) -> typing.Optional[typing.List[typing.List[float]]]:
         channelcache = export_settings['gltf_channelcache']
         if image.name in channelcache:
             return channelcache[image.name]
@@ -125,6 +131,12 @@ def __get_image_data(sockets_or_slots, export_settings):
         results = [__get_tex_from_socket(socket) for socket in sockets_or_slots]
         image = None
         for result, socket in zip(results, sockets_or_slots):
+            if result.shader_node.image.channels == 0:
+                gltf2_io_debug.print_console("WARNING",
+                                             "Image '{}' has no color channels and cannot be exported.".format(
+                                                 result.shader_node.image))
+                continue
+
             # rudimentarily try follow the node tree to find the correct image data.
             source_channel = None
             target_channel = None
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gather_texture.py b/io_scene_gltf2/blender/exp/gltf2_blender_gather_texture.py
index 93db33f9..8ada0f77 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_gather_texture.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather_texture.py
@@ -39,7 +39,7 @@ def gather_texture(
     if not __filter_texture(blender_shader_sockets_or_texture_slots, export_settings):
         return None
 
-    return gltf2_io.Texture(
+    texture = gltf2_io.Texture(
         extensions=__gather_extensions(blender_shader_sockets_or_texture_slots, export_settings),
         extras=__gather_extras(blender_shader_sockets_or_texture_slots, export_settings),
         name=__gather_name(blender_shader_sockets_or_texture_slots, export_settings),
@@ -47,6 +47,12 @@ def gather_texture(
         source=__gather_source(blender_shader_sockets_or_texture_slots, export_settings)
     )
 
+    # although valid, most viewers cant handle missing source properties
+    if texture.source is None:
+        return None
+
+    return texture
+
 
 def __filter_texture(blender_shader_sockets_or_texture_slots, export_settings):
     return True
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gather_texture_info.py b/io_scene_gltf2/blender/exp/gltf2_blender_gather_texture_info.py
index 4f579800..48f133d8 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_gather_texture_info.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather_texture_info.py
@@ -37,6 +37,9 @@ def gather_texture_info(blender_shader_sockets_or_texture_slots: typing.Union[
         tex_coord=__gather_tex_coord(blender_shader_sockets_or_texture_slots, export_settings)
     )
 
+    if texture_info.index is None:
+        return None
+
     return texture_info
 
 
diff --git a/io_scene_gltf2/io/com/gltf2_io.py b/io_scene_gltf2/io/com/gltf2_io.py
index 1332adf6..1208c06a 100755
--- a/io_scene_gltf2/io/com/gltf2_io.py
+++ b/io_scene_gltf2/io/com/gltf2_io.py
@@ -1104,7 +1104,7 @@ class Texture:
         result["extras"] = self.extras
         result["name"] = from_union([from_str, from_none], self.name)
         result["sampler"] = from_union([from_int, from_none], self.sampler)
-        result["source"] = from_int(self.source)
+        result["source"] = from_int(self.source)  # most viewers can't handle missing sources
         return result



More information about the Bf-extensions-cvs mailing list