diff options
author | Julien Duroure <julien.duroure@gmail.com> | 2021-12-18 14:05:05 +0300 |
---|---|---|
committer | Julien Duroure <julien.duroure@gmail.com> | 2021-12-18 14:05:05 +0300 |
commit | c2eeb9bb4732602c324a011f7f913a33a5e8b1f2 (patch) | |
tree | e8d9e7bc7508c05d9555ebd0fc6a580d801fae85 /io_scene_gltf2 | |
parent | f35aff344ff651364a54cc6c9fa75bf2699ad2a8 (diff) |
glTF importer: performance enhancement when images are packed
Diffstat (limited to 'io_scene_gltf2')
-rwxr-xr-x | io_scene_gltf2/__init__.py | 2 | ||||
-rwxr-xr-x | io_scene_gltf2/blender/imp/gltf2_blender_image.py | 83 |
2 files changed, 37 insertions, 48 deletions
diff --git a/io_scene_gltf2/__init__.py b/io_scene_gltf2/__init__.py index 954098eb..3fbc9239 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, 8, 5), + "version": (1, 8, 6), 'blender': (3, 0, 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 7f523922..082e5665 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_image.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_image.py @@ -38,54 +38,54 @@ class BlenderImage(): # Image is already used somewhere return - tmp_dir = None + is_binary = False is_placeholder = False + + 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) + 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 = img_name or 'Image_%d' % img_idx - tmp_dir = tempfile.TemporaryDirectory(prefix='gltfimg-') - filename = _filenamify(img_name) or 'Image_%d' % img_idx - filename += _img_extension(img) - path = join(tmp_dir.name, filename) - with open(path, 'wb') as f: - f.write(img_data) - - num_images = len(bpy.data.images) - - 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 - - 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 - - finally: - if tmp_dir is not None: - tmp_dir.cleanup() + 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") + def _placeholder_image(name, path): image = bpy.data.images.new(name, 128, 128) @@ -97,14 +97,3 @@ def _placeholder_image(name, path): def _uri_to_path(uri): uri = urllib.parse.unquote(uri) return normpath(uri) - -def _img_extension(img): - if img.mime_type == 'image/png': - return '.png' - if img.mime_type == 'image/jpeg': - return '.jpg' - return '' - -def _filenamify(s): - s = s.strip().replace(' ', '_') - return re.sub(r'(?u)[^-\w.]', '', s) |