Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Duroure <julien.duroure@gmail.com>2022-01-20 20:37:59 +0300
committerJulien Duroure <julien.duroure@gmail.com>2022-01-20 20:37:59 +0300
commit4d2d118f9add7f634d6c2d7428f4c4d307c21368 (patch)
treebaf9c9db3a354a3b2a16437ee84f10be7cfdc86e /io_scene_gltf2/blender/imp/gltf2_blender_image.py
parent38be61ebb868c0b56c140286af8720b2e7d0de11 (diff)
glTF importer: use relative path when possible
Diffstat (limited to 'io_scene_gltf2/blender/imp/gltf2_blender_image.py')
-rwxr-xr-xio_scene_gltf2/blender/imp/gltf2_blender_image.py87
1 files changed, 45 insertions, 42 deletions
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_image.py b/io_scene_gltf2/blender/imp/gltf2_blender_image.py
index 082e5665..3acf1786 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_image.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_image.py
@@ -43,49 +43,52 @@ class BlenderImage():
num_images = len(bpy.data.images)
- try:
-
- if img.uri is not None and not img.uri.startswith('data:'):
- # Image stored in a file
- path = join(dirname(gltf.filename), _uri_to_path(img.uri))
- img_name = img_name or basename(path)
-
+ if img.uri is not None and not img.uri.startswith('data:'):
+ # Image stored in a file
+ path = join(dirname(gltf.filename), _uri_to_path(img.uri))
+ path = os.path.abspath(path)
+ if bpy.data.is_saved and bpy.context.preferences.filepaths.use_relative_paths:
try:
- blender_image = bpy.data.images.load(
- os.path.abspath(path),
- check_existing=True,
- )
- 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
-
- else:
- # Image stored as data => create a tempfile, pack, and delete file
- is_binary = True
- img_data = BinaryData.get_image_data(gltf, img_idx)
- if img_data is None:
- return
- img_name = 'Image_%d' % img_idx
-
- # Create image, width and height are dummy values
- img_pack = bpy.data.images.new(img_name, 8, 8)
- # Set packed file data
- img_pack.pack(data=img_data.tobytes(), data_len=len(img_data))
- img_pack.source = 'FILE'
- img.blender_image_name = img_pack.name
-
- if is_binary is False:
- if len(bpy.data.images) != num_images: # If created a new image
- blender_image.name = img_name
- img.blender_image_name = img_name
-
- needs_pack = gltf.import_settings['import_pack_images']
- if not is_placeholder and needs_pack:
- blender_image.pack()
- except:
- print("Unknown error loading texture")
-
+ path = bpy.path.relpath(path)
+ except:
+ # May happen on Windows if on different drives, eg. C:\ and D:\
+ pass
+
+ img_name = img_name or basename(path)
+
+ try:
+ blender_image = bpy.data.images.load(
+ path,
+ check_existing=True,
+ )
+ 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
+
+ else:
+ # Image stored as data => pack
+ is_binary = True
+ img_data = BinaryData.get_image_data(gltf, img_idx)
+ if img_data is None:
+ return
+ img_name = 'Image_%d' % img_idx
+
+ # Create image, width and height are dummy values
+ img_pack = bpy.data.images.new(img_name, 8, 8)
+ # Set packed file data
+ img_pack.pack(data=img_data.tobytes(), data_len=len(img_data))
+ img_pack.source = 'FILE'
+ img.blender_image_name = img_pack.name
+
+ if is_binary is False:
+ if len(bpy.data.images) != num_images: # If created a new image
+ blender_image.name = img_name
+ img.blender_image_name = img_name
+
+ needs_pack = gltf.import_settings['import_pack_images']
+ if not is_placeholder and needs_pack:
+ blender_image.pack()
def _placeholder_image(name, path):
image = bpy.data.images.new(name, 128, 128)