Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenEXR export #44

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
50 changes: 41 additions & 9 deletions addons/io_hubs_addon/io/utils.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
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
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
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

# 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


class HubsImageData(gltf2_io_image_data.ImageData):
@property
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


Expand All @@ -34,13 +41,14 @@ def from_blender_image(image: bpy.types.Image):
def encode(self, mime_type: Optional[str], export_settings) -> Union[Tuple[bytes, bool], 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())
if bpy.app.version < (3, 5, 0):
return super().encode(mime_type)
else:
return super().encode(mime_type, export_settings)

# 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) -> Union[Tuple[bytes, bool], bytes]:
if image.file_format == "HDR" and image.source == 'FILE' and not image.is_dirty:
if image.packed_file is not None:
Expand All @@ -54,6 +62,19 @@ def encode_from_image_hdr(self, image: bpy.types.Image) -> Union[Tuple[bytes, bo
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):
Expand All @@ -66,6 +87,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:
Expand All @@ -76,7 +99,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:
Expand Down Expand Up @@ -106,7 +129,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] = gltf2_io_extensions.Extension(
Expand All @@ -116,6 +138,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] = gltf2_io_extensions.Extension(
name=ext_name,
extension={
"source": image
},
required=False
)

# export_user_extensions('gather_texture_hook', export_settings, texture, blender_shader_sockets)

Expand All @@ -124,7 +156,7 @@ 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
)


Expand Down