diff options
author | Julien Duroure <julien.duroure@gmail.com> | 2018-11-24 18:28:33 +0300 |
---|---|---|
committer | Julien Duroure <julien.duroure@gmail.com> | 2018-11-24 18:28:33 +0300 |
commit | b1f2133fa2849da272e9d8404f371220226ddaf1 (patch) | |
tree | 25db56e0f2211bd1059fe0e04e78430a6156e021 /io_scene_gltf2/blender/imp/gltf2_blender_image.py | |
parent | 8959f1798cfc86924493347304118c61bd5c7f7a (diff) |
Initial commit of glTF 2.0 importer/exporter
Official Khronos Group Blender glTF 2.0 importer and exporter.
glTF specification: https://github.com/KhronosGroup/glTF
The upstream repository can be found here:
https://github.com/KhronosGroup/glTF-Blender-IO
Reviewed By: Bastien, Campbell
Differential Revision: https://developer.blender.org/D3929
Diffstat (limited to 'io_scene_gltf2/blender/imp/gltf2_blender_image.py')
-rwxr-xr-x | io_scene_gltf2/blender/imp/gltf2_blender_image.py | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_image.py b/io_scene_gltf2/blender/imp/gltf2_blender_image.py new file mode 100755 index 00000000..ca1eb626 --- /dev/null +++ b/io_scene_gltf2/blender/imp/gltf2_blender_image.py @@ -0,0 +1,101 @@ +# Copyright 2018 The glTF-Blender-IO authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import bpy +import os +import tempfile +from os.path import dirname, join, isfile, basename + +from ...io.imp.gltf2_io_binary import BinaryData + + +# Note that Image is not a glTF2.0 object +class BlenderImage(): + """Manage Image.""" + def __new__(cls, *args, **kwargs): + raise RuntimeError("%s should not be instantiated" % cls) + + @staticmethod + def get_image_path(gltf, img_idx): + """Return image path.""" + pyimage = gltf.data.images[img_idx] + + image_name = "Image_" + str(img_idx) + + if pyimage.uri: + sep = ';base64,' + if pyimage.uri[:5] == 'data:': + idx = pyimage.uri.find(sep) + if idx != -1: + return False, None, None + + if isfile(join(dirname(gltf.filename), pyimage.uri)): + return True, join(dirname(gltf.filename), pyimage.uri), basename(join(dirname(gltf.filename), pyimage.uri)) + else: + pyimage.gltf.log.error("Missing file (index " + str(img_idx) + "): " + pyimage.uri) + return False, None, None + + if pyimage.buffer_view is None: + return False, None, None + + return False, None, None + + @staticmethod + def create(gltf, img_idx): + """Image creation.""" + img = gltf.data.images[img_idx] + + img.blender_image_name = None + + if gltf.import_settings['import_pack_images'] is False: + + # Images are not packed (if image is a real file) + real, path, img_name = BlenderImage.get_image_path(gltf, img_idx) + + if real is True: + + # Check if image is already loaded + for img_ in bpy.data.images: + if img_.filepath == path: + # Already loaded, not needed to reload it + img.blender_image_name = img_.name + return + + blender_image = bpy.data.images.load(path) + blender_image.name = img_name + img.blender_image_name = blender_image.name + return + + # Check if the file is already loaded (packed file) + file_creation_needed = True + for img_ in bpy.data.images: + if hasattr(img_, "gltf_index") and img_['gltf_index'] == img_idx: + file_creation_needed = False + img.blender_image_name = img_.name + break + + if file_creation_needed is True: + # Create a temp image, pack, and delete image + tmp_image = tempfile.NamedTemporaryFile(delete=False) + img_data, img_name = BinaryData.get_image_data(gltf, img_idx) + tmp_image.write(img_data) + tmp_image.close() + + blender_image = bpy.data.images.load(tmp_image.name) + blender_image.pack() + blender_image.name = img_name + img.blender_image_name = blender_image.name + blender_image['gltf_index'] = img_idx + os.remove(tmp_image.name) + |