Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gltf2_blender_gather_image.py « exp « blender « io_scene_gltf2 - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b570e61678c81ffed4802a89492f03cce65d4c29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Copyright 2018 The glTF-Blender-IO authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re

import bpy
import typing
import os
import numpy as np

from . import gltf2_blender_export_keys
from io_scene_gltf2.io.com import gltf2_io
from io_scene_gltf2.blender.exp import gltf2_blender_search_node_tree
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.io.com import gltf2_io_debug


def gather_image(
        blender_shader_sockets_or_texture_slots: typing.Union[typing.Tuple[bpy.types.NodeSocket],
                                                              typing.Tuple[bpy.types.Texture]],
        export_settings):
    if not __filter_image(blender_shader_sockets_or_texture_slots, export_settings):
        return None

    uri = __gather_uri(blender_shader_sockets_or_texture_slots, export_settings)
    buffer_view = __gather_buffer_view(blender_shader_sockets_or_texture_slots, export_settings)
    if not (uri is not None or buffer_view is not None):
        # The blender image has no data
        return None

    mime_type = __gather_mime_type(uri.filepath if uri is not None else "")

    image = gltf2_io.Image(
        buffer_view=buffer_view,
        extensions=__gather_extensions(blender_shader_sockets_or_texture_slots, export_settings),
        extras=__gather_extras(blender_shader_sockets_or_texture_slots, export_settings),
        mime_type=mime_type,
        name=__gather_name(blender_shader_sockets_or_texture_slots, export_settings),
        uri=uri
    )
    return image


def __filter_image(sockets_or_slots, export_settings):
    if not sockets_or_slots:
        return False
    return True


def __gather_buffer_view(sockets_or_slots, export_settings):
    if export_settings[gltf2_blender_export_keys.FORMAT] != 'GLTF_SEPARATE':
        image = __get_image_data(sockets_or_slots, export_settings)
        if image is None:
            return None
        return gltf2_io_binary_data.BinaryData(
            data=image.to_image_data(__gather_mime_type()))
    return None


def __gather_extensions(sockets_or_slots, export_settings):
    return None


def __gather_extras(sockets_or_slots, export_settings):
    return None


def __gather_mime_type(filepath=""):
    extension_types = {'.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg'}
    default_extension = extension_types['.png']

    matches = re.findall(r'\.\w+$', filepath)
    extension = matches[0] if len(matches) > 0 else default_extension
    return extension_types[extension] if extension.lower() in extension_types.keys() else default_extension


def __gather_name(sockets_or_slots, export_settings):
    if __is_socket(sockets_or_slots):
        node = __get_tex_from_socket(sockets_or_slots[0])
        if node is not None:
            return node.shader_node.image.name
    elif isinstance(sockets_or_slots[0], bpy.types.MaterialTextureSlot):
        return sockets_or_slots[0].name
    return None


def __gather_uri(sockets_or_slots, export_settings):
    if export_settings[gltf2_blender_export_keys.FORMAT] == 'GLTF_SEPARATE':
        # as usual we just store the data in place instead of already resolving the references
        return __get_image_data(sockets_or_slots, export_settings)
    return None


def __is_socket(sockets_or_slots):
    return isinstance(sockets_or_slots[0], bpy.types.NodeSocket)


def __is_slot(sockets_or_slots):
    return isinstance(sockets_or_slots[0], bpy.types.MaterialTextureSlot)


def __get_image_data(sockets_or_slots, export_settings):
    # For shared ressources, 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
    # ressources.
    def split_pixels_by_channels(image: bpy.types.Image, export_settings) -> typing.Optional[typing.List[typing.List[float]]]:
        channelcache = export_settings['gltf_channelcache']
        if image.name in channelcache:
            return channelcache[image.name]

        pixels = np.array(image.pixels)
        pixels = pixels.reshape((pixels.shape[0] // image.channels, image.channels))
        channels = np.split(pixels, pixels.shape[1], axis=1)

        channelcache[image.name] = channels

        return channels

    if __is_socket(sockets_or_slots):
        results = [__get_tex_from_socket(socket) for socket in sockets_or_slots]
        image = None
        for result, socket in zip(results, sockets_or_slots):
            if result.shader_node.image.channels == 0:
                gltf2_io_debug.print_console("WARNING",
                                             "Image '{}' has no color channels and cannot be exported.".format(
                                                 result.shader_node.image))
                continue

            # rudimentarily try follow the node tree to find the correct image data.
            source_channel = None
            target_channel = None
            source_channels_length = None
            for elem in result.path:
                if isinstance(elem.from_node, bpy.types.ShaderNodeSeparateRGB):
                    source_channel = {
                        'R': 0,
                        'G': 1,
                        'B': 2
                    }[elem.from_socket.name]

            if source_channel is not None:
                pixels = [split_pixels_by_channels(result.shader_node.image, export_settings)[source_channel]]
                target_channel = source_channel
                source_channel = 0
                source_channels_length = 1
            else:
                pixels = split_pixels_by_channels(result.shader_node.image, export_settings)
                target_channel = 0
                source_channel = 0
                source_channels_length = len(pixels)

            # Change target channel for metallic and roughness.
            if elem.to_socket.name == 'Metallic':
                target_channel = 2
                source_channels_length = 1
            elif elem.to_socket.name == 'Roughness':
                target_channel = 1
                source_channels_length = 1

            file_name = os.path.splitext(result.shader_node.image.name)[0]
            if result.shader_node.image.packed_file is None:
                file_path = result.shader_node.image.filepath
            else:
                # empty path for packed textures, because they are converted to png anyway
                file_path = ""

            image_data = gltf2_io_image_data.ImageData(
                file_name,
                file_path,
                result.shader_node.image.size[0],
                result.shader_node.image.size[1],
                source_channel,
                target_channel,
                source_channels_length,
                pixels)

            if image is None:
                image = image_data
            else:
                image.add_to_image(target_channel, image_data)

        return image
    elif __is_slot(sockets_or_slots):
        texture = __get_tex_from_slot(sockets_or_slots[0])
        pixels = split_pixels_by_channels(texture.image, export_settings)

        image_data = gltf2_io_image_data.ImageData(
            texture.name,
            texture.image.filepath,
            texture.image.size[0],
            texture.image.size[1],
            0,
            0,
            len(pixels),
            pixels)
        return image_data
    else:
        # Texture slots
        raise NotImplementedError()


def __get_tex_from_socket(blender_shader_socket: bpy.types.NodeSocket):
    result = gltf2_blender_search_node_tree.from_socket(
        blender_shader_socket,
        gltf2_blender_search_node_tree.FilterByType(bpy.types.ShaderNodeTexImage))
    if not result:
        return None
    return result[0]


def __get_tex_from_slot(blender_texture_slot):
    return blender_texture_slot.texture