diff options
author | Julien Duroure <julien.duroure@gmail.com> | 2020-01-23 23:39:36 +0300 |
---|---|---|
committer | Julien Duroure <julien.duroure@gmail.com> | 2020-01-23 23:39:36 +0300 |
commit | 872e3e6afac97f1740d9334cb441af292938c43e (patch) | |
tree | 351c99f7179579ddd1631e9828ca16c31158a116 /io_scene_gltf2/blender/exp | |
parent | 3049742f7ff91beaa0a0c7b05d62bf2e418fd264 (diff) |
glTF exporter: image options change
Diffstat (limited to 'io_scene_gltf2/blender/exp')
-rwxr-xr-x | io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py | 39 | ||||
-rw-r--r-- | io_scene_gltf2/blender/exp/gltf2_blender_image.py | 22 |
2 files changed, 18 insertions, 43 deletions
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py b/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py index 81da3780..ee184cdc 100755 --- a/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py +++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py @@ -42,7 +42,7 @@ def gather_image( # The export image has no data return None - mime_type = __gather_mime_type(blender_shader_sockets_or_texture_slots, export_settings) + mime_type = __gather_mime_type(blender_shader_sockets_or_texture_slots, image_data, export_settings) name = __gather_name(blender_shader_sockets_or_texture_slots, export_settings) uri = __gather_uri(image_data, mime_type, name, export_settings) @@ -95,27 +95,20 @@ def __gather_extras(sockets_or_slots, export_settings): return None -def __gather_mime_type(sockets_or_slots, export_settings): +def __gather_mime_type(sockets_or_slots, export_image, export_settings): # force png if Alpha contained so we can export alpha for socket in sockets_or_slots: if socket.name == "Alpha": return "image/png" - if export_settings["gltf_image_format"] == "NAME": - extension = __get_extension_from_slot(sockets_or_slots, export_settings) - extension = extension.lower() - if extension in [".jpeg", ".jpg", ".png"]: - return { - ".jpeg": "image/jpeg", - ".jpg": "image/jpeg", - ".png": "image/png", - }[extension] + if export_settings["gltf_image_format"] == "AUTO": + image = export_image.blender_image() + if image is not None and image.file_format == 'JPEG': + return "image/jpeg" return "image/png" elif export_settings["gltf_image_format"] == "JPEG": return "image/jpeg" - else: - return "image/png" def __gather_name(sockets_or_slots, export_settings): @@ -246,23 +239,3 @@ def __get_texname_from_slot(sockets_or_slots, export_settings): elif isinstance(sockets_or_slots[0], bpy.types.MaterialTextureSlot): return sockets_or_slots[0].texture.image.name - -@cached -def __get_extension_from_slot(sockets_or_slots, export_settings): - if __is_socket(sockets_or_slots): - for socket in sockets_or_slots: - node = __get_tex_from_socket(socket, export_settings) - if node is not None: - image_name = node.shader_node.image.name - filepath = bpy.data.images[image_name].filepath - name, extension = os.path.splitext(filepath) - if extension: - return extension - return '.png' - - elif isinstance(sockets_or_slots[0], bpy.types.MaterialTextureSlot): - image_name = sockets_or_slots[0].texture.image.name - filepath = bpy.data.images[image_name].filepath - name, extension = os.path.splitext(filepath) - return extension - diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_image.py b/io_scene_gltf2/blender/exp/gltf2_blender_image.py index 4a7818fe..a21dcba5 100644 --- a/io_scene_gltf2/blender/exp/gltf2_blender_image.py +++ b/io_scene_gltf2/blender/exp/gltf2_blender_image.py @@ -86,15 +86,18 @@ class ExportImage: def empty(self) -> bool: return not self.fills + def blender_image(self) -> Optional[bpy.types.Image]: + """If there's an existing Blender image we can use, + returns it. Otherwise (if channels need packing), + returns None. + """ + if self.__on_happy_path(): + for fill in self.fills.values(): + return fill.image + return None + def __on_happy_path(self) -> bool: - # Whether there is an existing Blender image we can use for this - # ExportImage because all the channels come from the matching - # channel of that image, eg. - # - # self.fills = { - # Channel.R: FillImage(image=im, src_chan=Channel.R), - # Channel.G: FillImage(image=im, src_chan=Channel.G), - # } + # All src_chans match their dst_chan and come from the same image return ( all(isinstance(fill, FillImage) for fill in self.fills.values()) and all(dst_chan == fill.src_chan for dst_chan, fill in self.fills.items()) and @@ -115,8 +118,7 @@ class ExportImage: return self.__encode_unhappy() def __encode_happy(self) -> bytes: - for fill in self.fills.values(): - return self.__encode_from_image(fill.image) + return self.__encode_from_image(self.blender_image()) def __encode_unhappy(self) -> bytes: result = self.__encode_unhappy_with_compositor() |