-
Notifications
You must be signed in to change notification settings - Fork 323
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
(WIP) More flexible image export handling for extensions. #1410
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,14 +27,16 @@ | |
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions | ||
|
||
|
||
@cached | ||
# @cached | ||
def gather_image( | ||
blender_shader_sockets: typing.Tuple[bpy.types.NodeSocket], | ||
export_settings): | ||
export_settings, | ||
export_image_class = ExportImage | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic here is that all of the extensions related to image formats right now actually start at the |
||
): | ||
if not __filter_image(blender_shader_sockets, export_settings): | ||
return None | ||
|
||
image_data = __get_image_data(blender_shader_sockets, export_settings) | ||
image_data = __get_image_data(blender_shader_sockets, export_image_class, export_settings) | ||
if image_data.empty(): | ||
# The export image has no data | ||
return None | ||
|
@@ -93,20 +95,14 @@ def __gather_extras(sockets, export_settings): | |
|
||
|
||
def __gather_mime_type(sockets, export_image, export_settings): | ||
# force png if Alpha contained so we can export alpha | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the user specifically sets their export mode to jpeg, I think things should always export as a jpeg. When set to auto the new |
||
for socket in sockets: | ||
if socket.name == "Alpha": | ||
return "image/png" | ||
|
||
if export_settings["gltf_image_format"] == "AUTO": | ||
image = export_image.blender_image() | ||
if image is not None and __is_blender_image_a_jpeg(image): | ||
return "image/jpeg" | ||
return "image/png" | ||
preferred = export_image.preferred_mime_type() | ||
if preferred: return preferred | ||
|
||
elif export_settings["gltf_image_format"] == "JPEG": | ||
return "image/jpeg" | ||
|
||
return "image/png" | ||
|
||
def __gather_name(export_image, export_settings): | ||
# Find all Blender images used in the ExportImage | ||
|
@@ -122,9 +118,7 @@ def __gather_name(export_image, export_settings): | |
if len(filepaths) == 1: | ||
filename = os.path.basename(list(filepaths)[0]) | ||
name, extension = os.path.splitext(filename) | ||
if extension.lower() in ['.png', '.jpg', '.jpeg']: | ||
if name: | ||
return name | ||
return name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Attempting to remove as much as possible that is assuming a particular filetype. This check seems superfluous anyway. |
||
|
||
# Combine the image names: img1-img2-img3 | ||
names = [] | ||
|
@@ -148,12 +142,12 @@ def __gather_uri(image_data, mime_type, name, export_settings): | |
return None | ||
|
||
|
||
def __get_image_data(sockets, export_settings) -> ExportImage: | ||
def __get_image_data(sockets, export_image_class, export_settings) -> ExportImage: | ||
# For shared resources, such as images, we just store the portion of data that is needed in the glTF property | ||
# in a helper class. During generation of the glTF in the exporter these will then be combined to actual binary | ||
# resources. | ||
results = [__get_tex_from_socket(socket, export_settings) for socket in sockets] | ||
composed_image = ExportImage() | ||
composed_image = export_image_class() | ||
for result, socket in zip(results, sockets): | ||
if result.shader_node.image.channels == 0: | ||
gltf2_io_debug.print_console("WARNING", | ||
|
@@ -200,7 +194,7 @@ def __get_image_data(sockets, export_settings) -> ExportImage: | |
composed_image.fill_white(Channel.B) | ||
else: | ||
# copy full image...eventually following sockets might overwrite things | ||
composed_image = ExportImage.from_blender_image(result.shader_node.image) | ||
composed_image = export_image_class.from_blender_image(result.shader_node.image) | ||
|
||
return composed_image | ||
|
||
|
@@ -213,10 +207,3 @@ def __get_tex_from_socket(blender_shader_socket: bpy.types.NodeSocket, export_se | |
if not result: | ||
return None | ||
return result[0] | ||
|
||
|
||
def __is_blender_image_a_jpeg(image: bpy.types.Image) -> bool: | ||
if image.source != 'FILE': | ||
return False | ||
path = image.filepath_raw.lower() | ||
return path.endswith('.jpg') or path.endswith('.jpeg') or path.endswith('.jpe') |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,13 @@ class ImageData: | |
# FUTURE_WORK: as a method to allow the node graph to be better supported, we could model some of | ||
# the node graph elements with numpy functions | ||
|
||
extension_for_mime = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We end up with multiple spots with very similar mappings... should be centralized somehow |
||
"image/jpeg": ".jpg", | ||
"image/png": ".png", | ||
"image/x-exr": ".exr", | ||
"image/vnd.radiance": ".hdr" | ||
} | ||
|
||
def __init__(self, data: bytes, mime_type: str, name: str): | ||
self._data = data | ||
self._mime_type = mime_type | ||
|
@@ -46,9 +53,7 @@ def name(self): | |
|
||
@property | ||
def file_extension(self): | ||
if self._mime_type == "image/jpeg": | ||
return ".jpg" | ||
return ".png" | ||
return ImageData.extension_for_mime.get(self._mime_type) | ||
|
||
@property | ||
def byte_length(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export_image_class appears to not be hashable which causes this to fail. Not ideal.