-
Notifications
You must be signed in to change notification settings - Fork 329
/
Copy pathtexture.py
252 lines (197 loc) · 9.2 KB
/
texture.py
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# Copyright 2018-2021 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 typing
import bpy
from ....io.exp.user_extensions import export_user_extensions
from ....io.com.gltf2_io_extensions import Extension
from ....io.exp.image_data import ImageData
from ....io.exp.binary_data import BinaryData
from ....io.com import debug as gltf2_io_debug
from ....io.com import gltf2_io
from ..sampler import gather_sampler
from ..cache import cached
from .search_node_tree import get_texture_node_from_socket, NodeSocket
from . import image
@cached
def gather_texture(
blender_shader_sockets: typing.Tuple[bpy.types.NodeSocket],
use_tile: bool,
export_settings):
"""
Gather texture sampling information and image channels from a blender shader texture attached to a shader socket.
:param blender_shader_sockets: The sockets of the material which should contribute to the texture
:param export_settings: configuration of the export
:return: a glTF 2.0 texture with sampler and source embedded (will be converted to references by the exporter)
"""
if not __filter_texture(blender_shader_sockets, export_settings):
return None, None, None
source, webp_image, image_data, factor, udim_image = __gather_source(blender_shader_sockets, use_tile, export_settings)
exts, remove_source = __gather_extensions(blender_shader_sockets, source, webp_image, image_data, export_settings)
texture = gltf2_io.Texture(
extensions=exts,
extras=__gather_extras(blender_shader_sockets, export_settings),
name=__gather_name(blender_shader_sockets, export_settings),
sampler=__gather_sampler(blender_shader_sockets, export_settings),
source=source if remove_source is False else None
)
# although valid, most viewers can't handle missing source properties
# This can have None source for "keep original", when original can't be found
if texture.source is None and remove_source is False:
return None, None, udim_image
export_user_extensions('gather_texture_hook', export_settings, texture, blender_shader_sockets)
return texture, factor, udim_image
def __filter_texture(blender_shader_sockets, export_settings):
# User doesn't want to export textures
if export_settings['gltf_image_format'] == "NONE":
return None
return True
def __gather_extensions(blender_shader_sockets, source, webp_image, image_data, export_settings):
extensions = {}
remove_source = False
required = False
ext_webp = {}
# If user want to keep original textures, and these textures are WebP, we need to remove source from
# gltf2_io.Texture, and populate extension
if export_settings['gltf_keep_original_textures'] is True \
and source is not None \
and source.mime_type == "image/webp":
ext_webp["source"] = source
remove_source = True
required = True
# If user want to export in WebP format (so without fallback in png/jpg)
if export_settings['gltf_image_format'] == "WEBP":
# We create all image without fallback
ext_webp["source"] = source
remove_source = True
required = True
# If user doesn't want to export in WebP format, but want WebP too. Texture is not WebP
if export_settings['gltf_image_format'] != "WEBP" \
and export_settings['gltf_add_webp'] \
and source is not None \
and source.mime_type != "image/webp":
# We need here to create some WebP textures
new_mime_type = "image/webp"
new_data, _ = image_data.encode(new_mime_type, export_settings)
if export_settings['gltf_format'] == 'GLTF_SEPARATE':
uri = ImageData(
data=new_data,
mime_type=new_mime_type,
name=source.uri.name
)
buffer_view = None
name = source.uri.name
else:
buffer_view = BinaryData(data=new_data)
uri = None
name = source.name
webp_image = __make_webp_image(buffer_view, None, None, new_mime_type, name, uri, export_settings)
ext_webp["source"] = webp_image
# If user doesn't want to export in WebP format, but want WebP too. Texture is WebP
if export_settings['gltf_image_format'] != "WEBP" \
and source is not None \
and source.mime_type == "image/webp":
# User does not want fallback
if export_settings['gltf_webp_fallback'] is False:
ext_webp["source"] = source
remove_source = True
required = True
# If user doesn't want to export in webp format, but want WebP too as fallback. Texture is WebP
if export_settings['gltf_image_format'] != "WEBP" \
and webp_image is not None \
and export_settings['gltf_webp_fallback'] is True:
# Already managed in __gather_source, we only have to assign
ext_webp["source"] = webp_image
# Not needed in code, for for documentation:
# remove_source = False
# required = False
if len(ext_webp) > 0:
extensions["EXT_texture_webp"] = Extension('EXT_texture_webp', ext_webp, required)
return extensions, remove_source
else:
return None, False
@cached
def __make_webp_image(buffer_view, extensions, extras, mime_type, name, uri, export_settings):
return gltf2_io.Image(
buffer_view=buffer_view,
extensions=extensions,
extras=extras,
mime_type=mime_type,
name=name,
uri=uri
)
def __gather_extras(blender_shader_sockets, export_settings):
return None
def __gather_name(blender_shader_sockets, export_settings):
return None
def __gather_sampler(blender_shader_sockets, export_settings):
shader_nodes = [get_texture_node_from_socket(socket, export_settings) for socket in blender_shader_sockets]
if len(shader_nodes) > 1:
export_settings['log'].warning(
"More than one shader node tex image used for a texture. "
"The resulting glTF sampler will behave like the first shader node tex image."
)
first_valid_shader_node = next(filter(lambda x: x is not None, shader_nodes))
# group_path can't be a list, so transform it to str
sep_item = "##~~gltf-sep~~##"
sep_inside_item = "##~~gltf-inside-sep~~##"
group_path_str = ""
if len(first_valid_shader_node.group_path) > 0:
# Retrieving the blender material using this shader tree
for mat in bpy.data.materials:
if mat.use_nodes is True and id(mat.node_tree) == id(first_valid_shader_node.group_path[0].original):
group_path_str += mat.name #TODO if linked, we can have multiple materials with same name...
break
if len(first_valid_shader_node.group_path) > 1:
for idx, i in enumerate(first_valid_shader_node.group_path[1:]):
group_path_str += sep_item
if idx == 0:
group_path_str += first_valid_shader_node.group_path[0].name
else:
group_path_str += i.id_data.name
group_path_str += sep_inside_item
group_path_str += i.name
return gather_sampler(
first_valid_shader_node.shader_node,
group_path_str,
export_settings)
def __gather_source(blender_shader_sockets, use_tile, export_settings):
source, image_data, factor, udim_image = image.gather_image(blender_shader_sockets, use_tile, export_settings)
if export_settings['gltf_keep_original_textures'] is False \
and export_settings['gltf_image_format'] != "WEBP" \
and source is not None \
and source.mime_type == "image/webp":
if export_settings['gltf_webp_fallback'] is False:
# Already managed in __gather_extensions
return source, None, image_data, factor, udim_image
else:
# Need to create a PNG texture
new_mime_type = "image/png"
new_data, _ = image_data.encode(new_mime_type, export_settings)
if export_settings['gltf_format'] == 'GLTF_SEPARATE':
buffer_view = None
uri = ImageData(
data=new_data,
mime_type=new_mime_type,
name=source.uri.name
)
name = source.uri.name
else:
uri = None
buffer_view = BinaryData(data=new_data)
name = source.name
png_image = __make_webp_image(buffer_view, None, None, new_mime_type, name, uri, export_settings)
# We inverted the png & WebP image, to have the png as main source
return png_image, source, image_data, factor, udim_image
return source, None, image_data, factor, udim_image
# Helpers