[Bf-extensions-cvs] [59f60554] master: glTF importer: create placeholder images for files that can't be loaded

Julien Duroure noreply at git.blender.org
Mon Jan 4 21:03:29 CET 2021


Commit: 59f60554f18c1634c77294b11260955c1a58b137
Author: Julien Duroure
Date:   Mon Jan 4 21:02:59 2021 +0100
Branches: master
https://developer.blender.org/rBA59f60554f18c1634c77294b11260955c1a58b137

glTF importer: create placeholder images for files that can't be loaded

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

M	io_scene_gltf2/__init__.py
M	io_scene_gltf2/blender/imp/gltf2_blender_image.py

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

diff --git a/io_scene_gltf2/__init__.py b/io_scene_gltf2/__init__.py
index b8afe70f..5cde29d2 100755
--- a/io_scene_gltf2/__init__.py
+++ b/io_scene_gltf2/__init__.py
@@ -15,7 +15,7 @@
 bl_info = {
     'name': 'glTF 2.0 format',
     'author': 'Julien Duroure, Scurest, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors',
-    "version": (1, 5, 12),
+    "version": (1, 5, 13),
     'blender': (2, 91, 0),
     'location': 'File > Import-Export',
     'description': 'Import-Export as glTF 2.0',
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_image.py b/io_scene_gltf2/blender/imp/gltf2_blender_image.py
index 63ef0a92..f0984399 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_image.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_image.py
@@ -39,18 +39,15 @@ class BlenderImage():
             return
 
         tmp_dir = None
+        is_placeholder = False
         try:
             if img.uri is not None and not img.uri.startswith('data:'):
                 # Image stored in a file
-                img_from_file = True
                 path = join(dirname(gltf.filename), _uri_to_path(img.uri))
                 img_name = img_name or basename(path)
-                if not isfile(path):
-                    gltf.log.error("Missing file (index " + str(img_idx) + "): " + img.uri)
-                    return
+
             else:
                 # Image stored as data => create a tempfile, pack, and delete file
-                img_from_file = False
                 img_data = BinaryData.get_image_data(gltf, img_idx)
                 if img_data is None:
                     return
@@ -63,10 +60,25 @@ class BlenderImage():
                     f.write(img_data)
 
             num_images = len(bpy.data.images)
-            blender_image = bpy.data.images.load(os.path.abspath(path), check_existing=img_from_file)
+
+            try:
+                blender_image = bpy.data.images.load(
+                    os.path.abspath(path),
+                    check_existing=tmp_dir is None,
+                )
+            except RuntimeError:
+                gltf.log.error("Missing image file (index %d): %s" % (img_idx, path))
+                blender_image = _placeholder_image(img_name, os.path.abspath(path))
+                is_placeholder = True
+
             if len(bpy.data.images) != num_images:  # If created a new image
                 blender_image.name = img_name
-                if gltf.import_settings['import_pack_images'] or not img_from_file:
+
+                needs_pack = (
+                    gltf.import_settings['import_pack_images'] or
+                    tmp_dir is not None
+                )
+                if not is_placeholder and needs_pack:
                     blender_image.pack()
 
             img.blender_image_name = blender_image.name
@@ -75,6 +87,13 @@ class BlenderImage():
             if tmp_dir is not None:
                 tmp_dir.cleanup()
 
+def _placeholder_image(name, path):
+    image = bpy.data.images.new(name, 128, 128)
+    # allow the path to be resolved later
+    image.filepath = path
+    image.source = 'FILE'
+    return image
+
 def _uri_to_path(uri):
     uri = urllib.parse.unquote(uri)
     return normpath(uri)



More information about the Bf-extensions-cvs mailing list