-
Notifications
You must be signed in to change notification settings - Fork 21
/
gltf2_get.py
597 lines (439 loc) · 15.3 KB
/
gltf2_get.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# Copyright (c) 2017 The Khronos Group Inc.
# Copyright (c) 2017-2024 Soft8Soft
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import bpy
import os
import pluginUtils.gltf as gltf
from .utils import *
def getUsedMaterials():
"""
Gathers and returns all unfiltered, valid Blender materials.
"""
materials = []
for bl_mat in bpy.data.materials:
materials.append(bl_mat)
return materials
def getImageIndex(exportSettings, uri):
"""
Return the image index in the glTF array.
"""
if exportSettings['uri_cache'] is None:
return -1
if uri in exportSettings['uri_cache']['uri']:
return exportSettings['uri_cache']['uri'].index(uri)
return -1
def getTextureIndexByTexture(exportSettings, glTF, bl_texture):
"""
Return the texture index in the glTF array by a given texture. Safer than
"getTextureIndex" in case of different textures with the same image or linked textures with
the same name but with different images.
"""
if (exportSettings['uri_cache'] is None or glTF.get('textures') is None
or bl_texture is None):
return -1
bl_image = getTexImage(bl_texture)
if bl_image is None or bl_image.filepath is None:
return -1
uri = getImageExportedURI(exportSettings, bl_image)
image_uri = exportSettings['uri_cache']['uri']
tex_name = getTextureName(bl_texture)
index = 0
for texture in glTF['textures']:
source = getTextureSource(texture)
if source is not None and 'name' in texture:
current_image_uri = image_uri[source]
if current_image_uri == uri and texture['name'] == tex_name:
return index
index += 1
return -1
def getTextureSource(tex):
"""texture source can also be in extension"""
if 'source' in tex:
return tex['source']
else:
ext = gltf.getAssetExtension(tex, 'KHR_texture_basisu')
if ext:
return ext['source']
else:
return None
def getTextureIndexNode(exportSettings, glTF, name, shaderNode):
"""
Return the texture index in the glTF array.
"""
if shaderNode is None:
return -1
if not isinstance(shaderNode, (bpy.types.ShaderNodeBsdfPrincipled,
bpy.types.ShaderNodeMixShader,
bpy.types.ShaderNodeGroup)):
return -1
if shaderNode.inputs.get(name) is None:
return -1
if len(shaderNode.inputs[name].links) == 0:
return -1
fromNode = shaderNode.inputs[name].links[0].from_node
if isinstance(fromNode, bpy.types.ShaderNodeNormalMap):
if len(fromNode.inputs['Color'].links) > 0:
fromNode = fromNode.inputs['Color'].links[0].from_node
else:
return -1
# COMPAT: Blender < 3.3, checking for type
if hasattr(bpy.types, 'ShaderNodeSeparateColor') and isinstance(fromNode, bpy.types.ShaderNodeSeparateColor):
if len(fromNode.inputs['Color'].links) > 0:
fromNode = fromNode.inputs['Color'].links[0].from_node
else:
return -1
# COMPAT: Blender < 3.3
if isinstance(fromNode, bpy.types.ShaderNodeSeparateRGB):
if len(fromNode.inputs['Image'].links) > 0:
fromNode = fromNode.inputs['Image'].links[0].from_node
else:
return -1
# color factor
if isinstance(fromNode, bpy.types.ShaderNodeMixRGB) and fromNode.blend_type == 'MULTIPLY':
if len(fromNode.inputs['Color1'].links) > 0:
fromNode = fromNode.inputs['Color1'].links[0].from_node
elif len(fromNode.inputs['Color2'].links) > 0:
fromNode = fromNode.inputs['Color2'].links[0].from_node
else:
return -1
if not isinstance(fromNode, bpy.types.ShaderNodeTexImage):
return -1
if getTexImage(fromNode) is None or getTexImage(fromNode).size[0] == 0 or getTexImage(fromNode).size[1] == 0:
return -1
return getTextureIndexByTexture(exportSettings, glTF, fromNode)
def getTexcoordIndex(glTF, name, shaderNode):
"""
Return the texture coordinate index, if assigend and used.
"""
if shaderNode is None:
return 0
if not isinstance(shaderNode, (bpy.types.ShaderNodeBsdfPrincipled,
bpy.types.ShaderNodeMixShader,
bpy.types.ShaderNodeGroup)):
return 0
if shaderNode.inputs.get(name) is None:
return 0
if len(shaderNode.inputs[name].links) == 0:
return 0
fromNode = shaderNode.inputs[name].links[0].from_node
if isinstance(fromNode, bpy.types.ShaderNodeNormalMap):
fromNode = fromNode.inputs['Color'].links[0].from_node
# COMPAT: Blender < 3.3, checking for type
if hasattr(bpy.types, 'ShaderNodeSeparateColor') and isinstance(fromNode, bpy.types.ShaderNodeSeparateColor):
fromNode = fromNode.inputs['Color'].links[0].from_node
# COMPAT: Blender < 3.3
if isinstance(fromNode, bpy.types.ShaderNodeSeparateRGB):
fromNode = fromNode.inputs['Image'].links[0].from_node
if isinstance(fromNode, bpy.types.ShaderNodeMixRGB) and fromNode.blend_type == 'MULTIPLY':
if len(fromNode.inputs['Color1'].links) > 0:
fromNode = fromNode.inputs['Color1'].links[0].from_node
elif len(fromNode.inputs['Color2'].links) > 0:
fromNode = fromNode.inputs['Color2'].links[0].from_node
if not isinstance(fromNode, bpy.types.ShaderNodeTexImage):
return 0
if len(fromNode.inputs['Vector'].links) == 0:
return 0
inputNode = fromNode.inputs['Vector'].links[0].from_node
if not isinstance(inputNode, bpy.types.ShaderNodeUVMap):
return 0
if inputNode.uv_map == '':
return 0
# try to gather map index.
for bl_mesh in bpy.data.meshes:
texCoordIndex = bl_mesh.uv_layers.find(inputNode.uv_map)
if texCoordIndex >= 0:
return texCoordIndex
return 0
def getMaterialType(bl_mat):
"""
get blender material type: PBR, EEVEE, BASIC
"""
if not bl_mat.use_nodes or bl_mat.node_tree == None:
return 'BASIC'
if bl_mat.v3d.gltf_compat:
return 'PBR'
return 'EEVEE'
def getSkinIndex(glTF, name, index_offset):
"""
Return the skin index in the glTF array.
"""
if glTF.get('skins') is None:
return -1
skeleton = gltf.getNodeIndex(glTF, name)
index = 0
for skin in glTF['skins']:
if skin['skeleton'] == skeleton:
return index + index_offset
index += 1
return -1
def getCameraIndex(glTF, name):
"""
Return the camera index in the glTF array.
"""
if glTF.get('cameras') is None:
return -1
index = 0
for camera in glTF['cameras']:
if camera['name'] == name:
return index
index += 1
return -1
def getCurveIndex(glTF, name):
"""
Return the curve index in the glTF array.
"""
v3dExt = gltf.getAssetExtension(glTF, 'S8S_v3d_curves')
if v3dExt == None:
return -1
if v3dExt.get('curves') == None:
return -1
curves = v3dExt['curves']
index = 0
for curve in curves:
if curve['name'] == name:
return index
index += 1
return -1
def getNodeGraphIndex(glTF, name):
"""
Return the node graph index in the glTF array.
"""
v3dExt = gltf.getAssetExtension(glTF, 'S8S_v3d_materials')
if v3dExt == None:
return -1
if v3dExt.get('nodeGraphs') == None:
return -1
index = 0
for graph in v3dExt['nodeGraphs']:
if graph['name'] == name:
return index
index += 1
return -1
def getImageExportedURI(exportSettings, bl_image):
"""
Return exported URI for a blender image.
"""
name, ext = os.path.splitext(bpy.path.basename(bl_image.filepath))
name = name if name != '' else 'v3d_exported_image_' + bl_image.name.lower().replace(' ', '_')
if imgNeedsCompression(bl_image, exportSettings):
if bl_image.file_format == 'HDR':
ext = '.hdr.xz'
else:
ext = '.ktx2'
elif (bl_image.file_format == 'JPEG'
or bl_image.file_format == 'WEBP'
or bl_image.file_format == 'BMP'
or bl_image.file_format == 'HDR'
or bl_image.file_format == 'PNG'):
if ext == '':
ext = '.' + bl_image.file_format.lower()
else:
ext = '.png'
unique_uri = name + ext
uri_cache = exportSettings['uri_cache']
i = 0
while unique_uri in uri_cache['uri']:
index = uri_cache['uri'].index(unique_uri)
if uri_cache['bl_datablocks'][index] == bl_image:
break
i += 1
unique_uri = name + '_' + integerToBlSuffix(i) + ext
return unique_uri
def getImageExportedMimeType(bl_image, exportSettings):
if imgNeedsCompression(bl_image, exportSettings):
if bl_image.file_format == 'HDR':
return 'application/x-xz'
else:
return 'image/ktx2'
elif bl_image.file_format == 'JPEG':
return 'image/jpeg'
elif bl_image.file_format == 'WEBP':
return 'image/webp'
elif bl_image.file_format == 'BMP':
return 'image/bmp'
elif bl_image.file_format == 'HDR':
return 'image/vnd.radiance'
else:
return 'image/png'
def getNameInBrackets(data_path):
"""
Return Blender node on a given Blender data path.
"""
if data_path is None:
return None
index = data_path.find("[\"")
if (index == -1):
return None
node_name = data_path[(index + 2):]
index = node_name.find("\"")
if (index == -1):
return None
return node_name[:(index)]
def getAnimParamDim(fcurves, node_name):
dim = 0
for fcurve in fcurves:
if getNameInBrackets(fcurve.data_path) == node_name:
dim = max(dim, fcurve.array_index+1)
return dim
def getAnimParam(data_path):
"""
return animated param in data path:
nodes['name'].outputs[0].default_value -> default_value
"""
index = data_path.rfind('.')
if index == -1:
return data_path
return data_path[(index + 1):]
def getScalar(default_value, init_value = 0.0):
"""
Return scalar with a given default/fallback value.
"""
return_value = init_value
if default_value is None:
return return_value
return_value = default_value
return return_value
def getVec3(default_value, init_value = [0.0, 0.0, 0.0]):
"""
Return vec3 with a given default/fallback value.
"""
return_value = init_value.copy()
if default_value is None or len(default_value) < 3:
return return_value
index = 0
for number in default_value:
return_value[index] = number
index += 1
if index == 3:
return return_value
return return_value
def getVec4(default_value, init_value = [0.0, 0.0, 0.0, 1.0]):
"""
Return vec4 with a given default/fallback value.
"""
return_value = init_value.copy()
if default_value is None or len(default_value) < 4:
return return_value
index = 0
for number in default_value:
return_value[index] = number
index += 1
if index == 4:
return return_value
return return_value
def getIndex(list, name):
"""
Return index of a glTF element by a given name.
"""
if list is None or name is None:
return -1
index = 0
for element in list:
if element.get('name') is None:
continue
if element['name'] == name:
return index
index += 1
return -1
def getByName(list, name):
"""
Return element by a given name.
"""
if list is None or name is None:
return None
for element in list:
if element.get('name') is None:
continue
if element['name'] == name:
return element
return None
def getOrCreateDefaultMatIndex(glTF):
def_idx = gltf.getMaterialIndex(glTF, DEFAULT_MAT_NAME)
if def_idx == -1:
if 'materials' not in glTF:
glTF['materials'] = []
glTF['materials'].append(createDefaultMaterial())
def_idx = len(glTF['materials']) - 1
return def_idx
def createDefaultMaterial():
return {
"extensions" : {
"S8S_v3d_materials" : {
"nodeGraph" : {
"edges" : [
{
"fromNode" : 1,
"fromOutput" : 0,
"toInput" : 0,
"toNode" : 0
}
],
"nodes" : [
{
"inputs" : [
[ 0, 0, 0, 0 ],
[ 0, 0, 0, 0 ],
[ 0, 0, 0 ]
],
"is_active_output" : True,
"name" : "Material Output",
"outputs" : [],
"type" : "OUTPUT_MATERIAL_BL"
},
{
"inputs" : [
[ 0.800000011920929, 0.800000011920929, 0.800000011920929, 1.0 ],
0.0,
[ 0.0, 0.0, 0.0 ]
],
"is_active_output" : False,
"name" : "Diffuse BSDF",
"outputs" : [
[ 0, 0, 0, 0 ]
],
"type" : "BSDF_DIFFUSE_BL"
}
]
},
"useCastShadows" : True,
"useShadows" : True
}
},
"name" : DEFAULT_MAT_NAME
}
def getFontPath(bl_font):
path = bl_font.filepath
abspath = bpy.path.abspath(bl_font.filepath)
# handle missing font as well
if path == '<builtin>' or not os.path.isfile(abspath):
return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'fonts', 'bfont.woff')
else:
return abspath
def getFontExportedURI(bl_font):
path = bl_font.filepath
abspath = bpy.path.abspath(bl_font.filepath)
# handle missing font as well
if path == '<builtin>' or not os.path.isfile(abspath):
return 'bfont.woff'
else:
return bpy.path.basename(path)
def getFontExportedMimeType(bl_font):
path = bl_font.filepath
abspath = bpy.path.abspath(bl_font.filepath)
# handle missing font as well
if path == '<builtin>' or not os.path.isfile(abspath):
return 'font/woff'
else:
return 'font/ttf'