diff options
author | Julien Duroure <julien.duroure@gmail.com> | 2019-05-31 08:25:56 +0300 |
---|---|---|
committer | Julien Duroure <julien.duroure@gmail.com> | 2019-05-31 08:25:56 +0300 |
commit | b2b97906fbe0f7930e1afc3d86bd64bc932b1d48 (patch) | |
tree | 8808231eba162359149b9e63508b699ef7b72945 /io_scene_gltf2/blender/exp/gltf2_blender_image.py | |
parent | 2bbd76ce994615b918de2866953ff50ade072261 (diff) |
glTF exporter: Fix T64760, T65234: preserve alpha when exists, and image exporting enhancements
Diffstat (limited to 'io_scene_gltf2/blender/exp/gltf2_blender_image.py')
-rw-r--r-- | io_scene_gltf2/blender/exp/gltf2_blender_image.py | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_image.py b/io_scene_gltf2/blender/exp/gltf2_blender_image.py index 080acd04..24abca62 100644 --- a/io_scene_gltf2/blender/exp/gltf2_blender_image.py +++ b/io_scene_gltf2/blender/exp/gltf2_blender_image.py @@ -13,6 +13,7 @@ # limitations under the License. import bpy +import os import typing import numpy as np import tempfile @@ -23,7 +24,8 @@ class ExportImage: # 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 - def __init__(self, img: typing.Union[np.ndarray, typing.List[np.ndarray]], max_channels: int = 4): + def __init__(self, img: typing.Union[np.ndarray, typing.List[np.ndarray]], max_channels: int = 4,\ + blender_image: bpy.types.Image = None, has_alpha: bool = False): if isinstance(img, list): np.stack(img, axis=2) @@ -36,12 +38,15 @@ class ExportImage: self._img = img self._max_channels = max_channels + self._blender_image = blender_image + self._has_alpha = has_alpha @classmethod def from_blender_image(cls, blender_image: bpy.types.Image): img = np.array(blender_image.pixels) img = img.reshape((blender_image.size[0], blender_image.size[1], blender_image.channels)) - return ExportImage(img=img) + has_alpha = blender_image.depth == 32 + return ExportImage(img=img, blender_image=blender_image, has_alpha=has_alpha) @classmethod def white_image(cls, width, height, num_channels: int = 4): @@ -90,22 +95,26 @@ class ExportImage: self._img = np.concatenate([self.img, other.img], axis=2) - def update(self, other): - self[:other.channels] = other[:other.channels] - def __add__(self, other): self.append(other) def encode(self, mime_type: typing.Optional[str]) -> bytes: - image = bpy.data.images.new("TmpImage", width=self.width, height=self.height) - pixels = self._img.flatten().tolist() - image.pixels = pixels - file_format = { "image/jpeg": "JPEG", "image/png": "PNG" }.get(mime_type, "PNG") + if self._blender_image is not None and file_format == self._blender_image.file_format: + src_path = bpy.path.abspath(self._blender_image.filepath_raw) + if os.path.isfile(src_path): + with open(src_path, "rb") as f: + encoded_image = f.read() + return encoded_image + + image = bpy.data.images.new("TmpImage", width=self.width, height=self.height, alpha=self._has_alpha) + pixels = self._img.flatten().tolist() + image.pixels = pixels + # we just use blenders built in save mechanism, this can be considered slightly dodgy but currently is the only # way to support with tempfile.TemporaryDirectory() as tmpdirname: |