From 025605c8e6f40b750ca528fa23426774633c3941 Mon Sep 17 00:00:00 2001 From: Rupert Rawnsley Date: Mon, 4 Oct 2021 13:22:33 +0100 Subject: [PATCH 1/5] Export support for OpenEXR images through the MOZ_texture_exr extension --- gather_properties.py | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/gather_properties.py b/gather_properties.py index 7fa17422..bd26ddc2 100644 --- a/gather_properties.py +++ b/gather_properties.py @@ -185,7 +185,7 @@ def gather_lightmap_texture_info(blender_material, export_settings): } -# gather_texture/image with HDR support via MOZ_texture_rgbe +# gather_texture/image with HDR support via MOZ_texture_rgbe and OPEN_EXR support via MOZ_texture_exr from io_scene_gltf2.io.com.gltf2_io_extensions import Extension from io_scene_gltf2.io.com.gltf2_io import Texture, Image @@ -199,6 +199,8 @@ class HubsImageData(ImageData): def file_extension(self): if self._mime_type == "image/vnd.radiance": return ".hdr" + if self._mime_type == "image/x-exr": + return ".exr" return super().file_extension() class HubsExportImage(ExportImage): @@ -212,10 +214,11 @@ def from_blender_image(image: bpy.types.Image): def encode(self, mime_type: Optional[str]) -> bytes: if mime_type == "image/vnd.radiance": return self.encode_from_image_hdr(self.blender_image()) + if mime_type == "image/x-exr": + return self.encode_from_image_exr(self.blender_image()) return super().encode(mime_type) - # TODO this should allow conversion from other HDR formats (namely EXR), - # in memory images, and combining separate channels like SDR images + # TODO this should allow in memory images, and combining separate channels like SDR images def encode_from_image_hdr(self, image: bpy.types.Image) -> bytes: if image.file_format == "HDR" and image.source == 'FILE' and not image.is_dirty: if image.packed_file is not None: @@ -228,6 +231,19 @@ def encode_from_image_hdr(self, image: bpy.types.Image) -> bytes: raise Exception("HDR images must be saved as a .hdr file before exporting") + # TODO this should allow in memory images, and combining separate channels like SDR images + def encode_from_image_exr(self, image: bpy.types.Image) -> bytes: + if image.file_format == "OPEN_EXR" and image.source == 'FILE' and not image.is_dirty: + if image.packed_file is not None: + return image.packed_file.data + else: + src_path = bpy.path.abspath(image.filepath_raw) + if os.path.isfile(src_path): + with open(src_path, 'rb') as f: + return f.read() + + raise Exception("EXR images must be saved as a .exr file before exporting") + @cached def gather_image(blender_image, export_settings): if not blender_image: @@ -238,6 +254,8 @@ def gather_image(blender_image, export_settings): if export_settings["gltf_image_format"] == "AUTO": if blender_image.file_format == "HDR": mime_type = "image/vnd.radiance" + elif blender_image.file_format == "OPEN_EXR": + mime_type = "image/x-exr" else: mime_type = "image/png" else: @@ -274,7 +292,6 @@ def gather_texture(blender_image, export_settings): texture_extensions = {} is_hdr = blender_image and blender_image.file_format == "HDR" - if is_hdr: ext_name = "MOZ_texture_rgbe" texture_extensions[ext_name] = Extension( @@ -284,6 +301,16 @@ def gather_texture(blender_image, export_settings): }, required=False ) + is_exr = blender_image and blender_image.file_format == "OPEN_EXR" + if is_exr: + ext_name = "MOZ_texture_exr" + texture_extensions[ext_name] = Extension( + name=ext_name, + extension={ + "source": image + }, + required=False + ) # export_user_extensions('gather_texture_hook', export_settings, texture, blender_shader_sockets) @@ -292,5 +319,5 @@ def gather_texture(blender_image, export_settings): extras=None, name=None, sampler=None, - source=None if is_hdr else image + source=None if is_hdr or is_exr else image ) From 6d2869503e6546f68759703f7c1d509e82bdc429 Mon Sep 17 00:00:00 2001 From: Rupert Rawnsley Date: Fri, 7 Jul 2023 17:10:48 +0100 Subject: [PATCH 2/5] Fix for gtlf exporter python library path changes --- addons/io_hubs_addon/io/utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/addons/io_hubs_addon/io/utils.py b/addons/io_hubs_addon/io/utils.py index b4d4b237..b1d81513 100644 --- a/addons/io_hubs_addon/io/utils.py +++ b/addons/io_hubs_addon/io/utils.py @@ -1,14 +1,14 @@ import os import bpy from io_scene_gltf2.blender.com import gltf2_blender_extras -from io_scene_gltf2.blender.exp import gltf2_blender_gather_materials, gltf2_blender_gather_nodes, gltf2_blender_gather_joints -from io_scene_gltf2.blender.exp import gltf2_blender_gather_texture_info, gltf2_blender_export_keys +from io_scene_gltf2.blender.exp import gltf2_blender_gather_nodes, gltf2_blender_gather_joints +from io_scene_gltf2.blender.exp.material import gltf2_blender_gather_materials, gltf2_blender_gather_texture_info +from io_scene_gltf2.blender.exp.material.extensions import gltf2_blender_image from io_scene_gltf2.blender.exp.gltf2_blender_gather_cache import cached from io_scene_gltf2.io.com import gltf2_io_extensions from io_scene_gltf2.io.com import gltf2_io from io_scene_gltf2.io.exp import gltf2_io_binary_data from io_scene_gltf2.io.exp import gltf2_io_image_data -from io_scene_gltf2.blender.exp import gltf2_blender_image from typing import Optional, Tuple, Union from ..nodes.lightmap import MozLightmapNode @@ -76,7 +76,7 @@ def gather_image(blender_image, export_settings): if type(data) == tuple: data = data[0] - if export_settings[gltf2_blender_export_keys.FORMAT] == 'GLTF_SEPARATE': + if export_settings['gltf_format'] == 'GLTF_SEPARATE': uri = HubsImageData(data=data, mime_type=mime_type, name=name) buffer_view = None else: From de43def2e1bf2f457f71cea1f685156225c46f5d Mon Sep 17 00:00:00 2001 From: Rupert Rawnsley Date: Mon, 10 Jul 2023 19:17:02 +0100 Subject: [PATCH 3/5] Refactor to preserve compatibility with previous versions of Blender --- addons/io_hubs_addon/io/utils.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/addons/io_hubs_addon/io/utils.py b/addons/io_hubs_addon/io/utils.py index b1d81513..20a32162 100644 --- a/addons/io_hubs_addon/io/utils.py +++ b/addons/io_hubs_addon/io/utils.py @@ -1,9 +1,14 @@ import os import bpy from io_scene_gltf2.blender.com import gltf2_blender_extras -from io_scene_gltf2.blender.exp import gltf2_blender_gather_nodes, gltf2_blender_gather_joints -from io_scene_gltf2.blender.exp.material import gltf2_blender_gather_materials, gltf2_blender_gather_texture_info -from io_scene_gltf2.blender.exp.material.extensions import gltf2_blender_image +if bpy.app.version >= (3, 6, 0): + from io_scene_gltf2.blender.exp import gltf2_blender_gather_nodes, gltf2_blender_gather_joints + from io_scene_gltf2.blender.exp.material import gltf2_blender_gather_materials, gltf2_blender_gather_texture_info + from io_scene_gltf2.blender.exp.material.extensions import gltf2_blender_image +else: + from io_scene_gltf2.blender.exp import gltf2_blender_gather_materials, gltf2_blender_gather_nodes, gltf2_blender_gather_joints + from io_scene_gltf2.blender.exp import gltf2_blender_gather_texture_info, gltf2_blender_export_keys + from io_scene_gltf2.blender.exp import gltf2_blender_image from io_scene_gltf2.blender.exp.gltf2_blender_gather_cache import cached from io_scene_gltf2.io.com import gltf2_io_extensions from io_scene_gltf2.io.com import gltf2_io @@ -12,6 +17,11 @@ from typing import Optional, Tuple, Union from ..nodes.lightmap import MozLightmapNode +if bpy.app.version >= (3, 6, 0): + gltf2_blender_export_keys_format = 'gltf_format' +else: + gltf2_blender_export_keys_format = gltf2_blender_export_keys.FORMAT + # gather_texture/image with HDR support via MOZ_texture_rgbe @@ -76,7 +86,7 @@ def gather_image(blender_image, export_settings): if type(data) == tuple: data = data[0] - if export_settings['gltf_format'] == 'GLTF_SEPARATE': + if export_settings[gltf2_blender_export_keys_format] == 'GLTF_SEPARATE': uri = HubsImageData(data=data, mime_type=mime_type, name=name) buffer_view = None else: From 9133e3c92420ed8c62f76b9fd6668ef68420ffc0 Mon Sep 17 00:00:00 2001 From: Rupert Rawnsley Date: Tue, 11 Jul 2023 07:56:55 +0100 Subject: [PATCH 4/5] Simplified export key change as it is already backwards compatible --- addons/io_hubs_addon/io/utils.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/addons/io_hubs_addon/io/utils.py b/addons/io_hubs_addon/io/utils.py index 20a32162..12ba170e 100644 --- a/addons/io_hubs_addon/io/utils.py +++ b/addons/io_hubs_addon/io/utils.py @@ -17,11 +17,6 @@ from typing import Optional, Tuple, Union from ..nodes.lightmap import MozLightmapNode -if bpy.app.version >= (3, 6, 0): - gltf2_blender_export_keys_format = 'gltf_format' -else: - gltf2_blender_export_keys_format = gltf2_blender_export_keys.FORMAT - # gather_texture/image with HDR support via MOZ_texture_rgbe @@ -86,7 +81,7 @@ def gather_image(blender_image, export_settings): if type(data) == tuple: data = data[0] - if export_settings[gltf2_blender_export_keys_format] == 'GLTF_SEPARATE': + if export_settings['gltf_format'] == 'GLTF_SEPARATE': uri = HubsImageData(data=data, mime_type=mime_type, name=name) buffer_view = None else: From e759e5e92cd1e52e6d064e0d95cf5ae4e430262e Mon Sep 17 00:00:00 2001 From: Rupert Rawnsley Date: Tue, 11 Jul 2023 08:40:04 +0100 Subject: [PATCH 5/5] Lint suggestions --- addons/io_hubs_addon/io/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/addons/io_hubs_addon/io/utils.py b/addons/io_hubs_addon/io/utils.py index 620c52ac..e824f84a 100644 --- a/addons/io_hubs_addon/io/utils.py +++ b/addons/io_hubs_addon/io/utils.py @@ -75,6 +75,7 @@ def encode_from_image_exr(self, image: bpy.types.Image) -> bytes: raise Exception("EXR images must be saved as a .exr file before exporting") + @cached def gather_image(blender_image, export_settings): if not blender_image: