Cleanup: trailing space
[blender-addons.git] / io_scene_gltf2 / blender / imp / gltf2_blender_image.py
blob4f9af79915cb435af70f00723e27ae988d0a47e2
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2018-2021 The glTF-Blender-IO authors.
4 import bpy
5 import os
6 import tempfile
7 from os.path import dirname, join, isfile, basename, normpath
8 import urllib.parse
9 import re
11 from ...io.imp.gltf2_io_binary import BinaryData
12 from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions
15 # Note that Image is not a glTF2.0 object
16 class BlenderImage():
17 """Manage Image."""
18 def __new__(cls, *args, **kwargs):
19 raise RuntimeError("%s should not be instantiated" % cls)
21 @staticmethod
22 def create(gltf, img_idx):
23 """Image creation."""
24 img = gltf.data.images[img_idx]
26 if img.blender_image_name is not None:
27 # Image is already used somewhere
28 return
30 import_user_extensions('gather_import_image_before_hook', gltf, img)
32 if img.uri is not None and not img.uri.startswith('data:'):
33 blender_image = create_from_file(gltf, img_idx)
34 else:
35 blender_image = create_from_data(gltf, img_idx)
37 if blender_image:
38 img.blender_image_name = blender_image.name
40 import_user_extensions('gather_import_image_after_hook', gltf, img, blender_image)
43 def create_from_file(gltf, img_idx):
44 # Image stored in a file
46 num_images = len(bpy.data.images)
48 img = gltf.data.images[img_idx]
50 path = join(dirname(gltf.filename), _uri_to_path(img.uri))
51 path = os.path.abspath(path)
52 if bpy.data.is_saved and bpy.context.preferences.filepaths.use_relative_paths:
53 try:
54 path = bpy.path.relpath(path)
55 except:
56 # May happen on Windows if on different drives, eg. C:\ and D:\
57 pass
59 img_name = img.name or basename(path)
61 try:
62 blender_image = bpy.data.images.load(
63 path,
64 check_existing=True,
67 needs_pack = gltf.import_settings['import_pack_images']
68 if needs_pack:
69 blender_image.pack()
71 except RuntimeError:
72 gltf.log.error("Missing image file (index %d): %s" % (img_idx, path))
73 blender_image = _placeholder_image(img_name, os.path.abspath(path))
75 if len(bpy.data.images) != num_images: # If created a new image
76 blender_image.name = img_name
78 return blender_image
81 def create_from_data(gltf, img_idx):
82 # Image stored as data => pack
83 img_data = BinaryData.get_image_data(gltf, img_idx)
84 if img_data is None:
85 return
86 img_name = 'Image_%d' % img_idx
88 # Create image, width and height are dummy values
89 blender_image = bpy.data.images.new(img_name, 8, 8)
90 # Set packed file data
91 blender_image.pack(data=img_data.tobytes(), data_len=len(img_data))
92 blender_image.source = 'FILE'
94 return blender_image
96 def _placeholder_image(name, path):
97 image = bpy.data.images.new(name, 128, 128)
98 # allow the path to be resolved later
99 image.filepath = path
100 image.source = 'FILE'
101 return image
103 def _uri_to_path(uri):
104 uri = urllib.parse.unquote(uri)
105 return normpath(uri)