-
Notifications
You must be signed in to change notification settings - Fork 21
/
gltf2_generate.py
3385 lines (2399 loc) · 117 KB
/
gltf2_generate.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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 copy
import json
import math
import os.path
import shutil
join = os.path.join
norm = os.path.normpath
import pluginUtils
import pluginUtils.gltf as gltf
import pluginUtils.rawdata
log = pluginUtils.log.getLogger('V3D-BL')
from .gltf2_animate import *
from .gltf2_extract import *
from .gltf2_filter import *
from .gltf2_get import *
from .utils import *
from profilehooks import profile
VERSION = '4.8.0'
# Blender default grey color
PRIMITIVE_MODE_LINES = 1
PRIMITIVE_MODE_TRIANGLES = 4
PROXY_NODE_PREFIX = 'v3d_Proxy_Node_{idx}_{name}'
SPOT_SHADOW_MIN_NEAR = 0.01
CAM_ANGLE_EPSILON = math.pi / 180
# some small offset to be added to the alphaCutoff option, it's needed bcz
# Blender uses the "less or equal" condition for clipping values, but the engine
# uses just "less"
ALPHA_CUTOFF_EPS = 1e-4
SUN_DEFAULT_NEAR = 0.1
SUN_DEFAULT_FAR = 100
MAX_SHADOW_CAM_FAR = 10000
PMREM_SIZE_MIN = 256
PMREM_SIZE_MAX = 1024
ROTATION_NODE_TYPES = [
'NODE_X_90',
'NODE_INV_X_90',
'NODE_INV_X_90_X_90'
]
def generateAsset(operator, context, exportSettings, glTF):
"""
Generates the top level asset entry.
"""
asset = {}
asset['version'] = '2.0'
asset['generator'] = 'Verge3D for Blender v{}'.format(VERSION)
if exportSettings['copyright'] != "":
asset['copyright'] = exportSettings['copyright']
glTF['asset'] = asset
def generateAnimChannel(glTF, blObject, samplerName, path, nodeName, samplers, channels):
idx = getIndex(samplers, samplerName)
if idx > -1:
channel = gltf.createAnimChannel(idx, gltf.getNodeIndex(glTF, nodeName), path)
# HACK: avoid channel duplication (occurs when animating an armature object)
for ch in channels:
if ch['target'] == channel['target']:
return None
# to resolve default animation params
channel['bl_obj'] = blObject
channels.append(channel)
return channel
return None
def generateAnimationsParameter(operator,
context,
exportSettings,
glTF,
action,
channels,
samplers,
bl_obj,
bl_bone,
bl_mat_name,
bl_mat_node_name,
rotation_mode,
is_morph_data,
constraint_name=None):
"""
Helper function for storing animation parameters.
"""
bl_node_name = bl_obj.name
prefix = ""
postfix = ""
location = [None, None, None]
rotation_axis_angle = [None, None, None, None]
rotation_euler = [None, None, None]
rotation_quaternion = [None, None, None, None]
scale = [None, None, None]
value = []
eval_time = [] # follow path constraint animation
# for material node animation
default_value = [None]
energy = [None]
data = {
'location' : location,
'rotation_axis_angle' : rotation_axis_angle,
'rotation_euler' : rotation_euler,
'rotation_quaternion' : rotation_quaternion,
'scale' : scale,
'value' : value,
'default_value': default_value,
'energy': energy,
'eval_time': eval_time
}
node_type = 'NODE'
used_node_name = bl_node_name
# correcting rotation animation
if bl_obj.type == 'CAMERA' or bl_obj.type == 'LIGHT' or bl_obj.type == 'FONT':
node_type = 'NODE_X_90'
parent = bl_obj.parent
if parent:
if parent.type in ('CAMERA', 'LIGHT', 'FONT'):
if node_type == 'NODE_X_90':
node_type = 'NODE_INV_X_90_X_90'
else:
node_type = 'NODE_INV_X_90'
if bl_bone != None:
node_type = 'JOINT'
used_node_name = bl_bone.name
elif bl_mat_node_name != None:
node_type = 'MAT_NODE'
used_node_name = bl_mat_node_name
default_value *= getAnimParamDim(action.fcurves, used_node_name)
# gather fcurves in data dict
for bl_fcurve in action.fcurves:
node_name = getNameInBrackets(bl_fcurve.data_path)
if node_name != None and not is_morph_data:
if (node_type == 'JOINT' or node_type == 'MAT_NODE') and used_node_name != node_name:
continue
elif node_type == 'NODE' or node_type in ROTATION_NODE_TYPES:
continue
else:
prefix = node_name + "_"
postfix = "_" + node_name
data_path = getAnimParam(bl_fcurve.data_path)
if (data_path not in ['location', 'rotation_axis_angle',
'rotation_euler', 'rotation_quaternion', 'scale',
'value', 'default_value', 'energy', 'eval_time']):
continue
if data_path not in ['value', 'eval_time']:
data[data_path][bl_fcurve.array_index] = bl_fcurve
else:
data[data_path].append(bl_fcurve)
# same for all animations we export currently
componentType = 'FLOAT'
# create location sampler
if location.count(None) < 3:
sampler_name = prefix + action.name + "_translation"
if getIndex(samplers, sampler_name) == -1:
interpolation = animateGetInterpolation(exportSettings, location)
if interpolation == 'CUBICSPLINE' and node_type == 'JOINT':
interpolation = 'CONVERSION_NEEDED'
translation_data, in_tangent_data, out_tangent_data = animateLocation(
exportSettings, location, interpolation, node_type, bl_obj,
bl_bone)
keys = sorted(translation_data.keys())
values = []
final_keys = []
key_offset = 0.0
if len(keys) > 0 and exportSettings['move_keyframes']:
key_offset = bpy.context.scene.frame_start / bpy.context.scene.render.fps
for key in keys:
if key - key_offset < 0.0:
continue
final_keys.append(key - key_offset)
if interpolation == 'CUBICSPLINE':
for i in range(0, 3):
values.append(in_tangent_data[key][i])
for i in range(0, 3):
values.append(translation_data[key][i])
if interpolation == 'CUBICSPLINE':
for i in range(0, 3):
values.append(out_tangent_data[key][i])
count = len(final_keys)
if count:
sampler = {}
sampler['interpolation'] = interpolation
if interpolation == 'CONVERSION_NEEDED':
sampler['interpolation'] = 'LINEAR'
type = 'SCALAR'
input = gltf.generateAccessor(glTF, exportSettings['binary'],
final_keys, componentType, count, type, '')
sampler['input'] = input
count = len(values) // 3
type = 'VEC3'
output = gltf.generateAccessor(glTF, exportSettings['binary'],
values, componentType, count, type, '')
sampler['output'] = output
sampler['name'] = sampler_name
samplers.append(sampler)
# create rotation sampler
rotation_data = None
rotation_in_tangent_data = [0.0, 0.0, 0.0, 0.0]
rotation_out_tangent_data = [0.0, 0.0, 0.0, 0.0]
interpolation = None
sampler_name = prefix + action.name + "_rotation"
if getIndex(samplers, sampler_name) == -1:
has_axis_angle = rotation_axis_angle.count(None) < 4
has_euler = rotation_euler.count(None) < 3
has_quaternion = rotation_quaternion.count(None) < 4
if has_axis_angle:
interpolation = animateGetInterpolation(exportSettings, rotation_axis_angle)
# conversion required in any case
if interpolation == 'CUBICSPLINE':
interpolation = 'CONVERSION_NEEDED'
rotation_data = rotation_data or {}
rotation_data.update(animateRotationAxisAngle(exportSettings,
rotation_axis_angle, interpolation, node_type, bl_obj,
bl_bone))
if has_euler:
interpolation = animateGetInterpolation(exportSettings, rotation_euler)
# conversion required in any case
# also for linear interpolation to fix issues with e.g 2*PI keyframe differences
if interpolation == 'CUBICSPLINE' or interpolation == 'LINEAR':
interpolation = 'CONVERSION_NEEDED'
rotation_data = rotation_data or {}
rotation_data.update(animateRotationEuler(exportSettings,
rotation_euler, rotation_mode, interpolation, node_type,
bl_obj, bl_bone))
if has_quaternion:
interpolation = animateGetInterpolation(exportSettings, rotation_quaternion)
if interpolation == 'CUBICSPLINE' and node_type == 'JOINT':
interpolation = 'CONVERSION_NEEDED'
rotation_data_quat, rotation_in_tangent_data, rotation_out_tangent_data = animateRotationQuaternion(
exportSettings, rotation_quaternion, interpolation,
node_type, bl_obj, bl_bone)
rotation_data = rotation_data or {}
rotation_data.update(rotation_data_quat)
if has_quaternion and (has_axis_angle or has_euler):
# NOTE: set tangent data to zeros just in case, since it's not clear
# what to do with it when mixing different types of rotation keyframes
rotation_in_tangent_data = [0.0, 0.0, 0.0, 0.0]
rotation_out_tangent_data = [0.0, 0.0, 0.0, 0.0]
if rotation_data is not None:
keys = sorted(rotation_data.keys())
values = []
final_keys = []
key_offset = 0.0
if len(keys) > 0 and exportSettings['move_keyframes']:
key_offset = bpy.context.scene.frame_start / bpy.context.scene.render.fps
for key in keys:
if key - key_offset < 0.0:
continue
final_keys.append(key - key_offset)
if interpolation == 'CUBICSPLINE':
for i in range(0, 4):
values.append(rotation_in_tangent_data[key][i])
for i in range(0, 4):
values.append(rotation_data[key][i])
if interpolation == 'CUBICSPLINE':
for i in range(0, 4):
values.append(rotation_out_tangent_data[key][i])
count = len(final_keys)
if count:
sampler = {}
sampler['interpolation'] = interpolation
if interpolation == 'CONVERSION_NEEDED':
sampler['interpolation'] = 'LINEAR'
type = 'SCALAR'
input = gltf.generateAccessor(glTF, exportSettings['binary'], final_keys, componentType, count, type, '')
sampler['input'] = input
count = len(values) // 4
type = 'VEC4'
output = gltf.generateAccessor(glTF, exportSettings['binary'], values, componentType, count, type, '')
sampler['output'] = output
sampler['name'] = sampler_name
samplers.append(sampler)
# create scale sampler
if scale.count(None) < 3:
sampler_name = prefix + action.name + "_scale"
if getIndex(samplers, sampler_name) == -1:
interpolation = animateGetInterpolation(exportSettings, scale)
if interpolation == 'CUBICSPLINE' and node_type == 'JOINT':
interpolation = 'CONVERSION_NEEDED'
scale_data, in_tangent_data, out_tangent_data = animateScale(
exportSettings, scale, interpolation, node_type, bl_obj,
bl_bone)
keys = sorted(scale_data.keys())
values = []
final_keys = []
key_offset = 0.0
if len(keys) > 0 and exportSettings['move_keyframes']:
key_offset = bpy.context.scene.frame_start / bpy.context.scene.render.fps
for key in keys:
if key - key_offset < 0.0:
continue
final_keys.append(key - key_offset)
if interpolation == 'CUBICSPLINE':
for i in range(0, 3):
values.append(in_tangent_data[key][i])
for i in range(0, 3):
values.append(scale_data[key][i])
if interpolation == 'CUBICSPLINE':
for i in range(0, 3):
values.append(out_tangent_data[key][i])
count = len(final_keys)
if count:
sampler = {}
sampler['interpolation'] = interpolation
if interpolation == 'CONVERSION_NEEDED':
sampler['interpolation'] = 'LINEAR'
type = 'SCALAR'
input = gltf.generateAccessor(glTF, exportSettings['binary'], final_keys, componentType, count, type, '')
sampler['input'] = input
count = len(values) // 3
type = 'VEC3'
output = gltf.generateAccessor(glTF, exportSettings['binary'], values, componentType, count, type, '')
sampler['output'] = output
sampler['name'] = sampler_name
samplers.append(sampler)
# create morph target sampler
if len(value) > 0 and is_morph_data:
sampler_name = prefix + action.name + "_weights"
if getIndex(samplers, sampler_name) == -1:
interpolation = animateGetInterpolation(exportSettings, value)
if interpolation == 'CUBICSPLINE' and node_type == 'JOINT':
interpolation = 'CONVERSION_NEEDED'
value_data, in_tangent_data, out_tangent_data = animateValue(
exportSettings, value, interpolation, node_type)
keys = sorted(value_data.keys())
values = []
final_keys = []
key_offset = 0.0
if len(keys) > 0 and exportSettings['move_keyframes']:
key_offset = bpy.context.scene.frame_start / bpy.context.scene.render.fps
for key in keys:
if key - key_offset < 0.0:
continue
final_keys.append(key - key_offset)
if interpolation == 'CUBICSPLINE':
for i in range(0, len(in_tangent_data[key])):
values.append(in_tangent_data[key][i])
for i in range(0, len(value_data[key])):
values.append(value_data[key][i])
if interpolation == 'CUBICSPLINE':
for i in range(0, len(out_tangent_data[key])):
values.append(out_tangent_data[key][i])
count = len(final_keys)
if count:
sampler = {}
sampler['interpolation'] = interpolation
if interpolation == 'CONVERSION_NEEDED':
sampler['interpolation'] = 'LINEAR'
type = 'SCALAR'
input = gltf.generateAccessor(glTF, exportSettings['binary'], final_keys, componentType, count, type, '')
sampler['input'] = input
count = len(values)
type = 'SCALAR'
output = gltf.generateAccessor(glTF, exportSettings['binary'], values, componentType, count, type, '')
sampler['output'] = output
sampler['name'] = sampler_name
samplers.append(sampler)
# create material node anim sampler
def_val_dim = len(default_value)
# NOTE: only value/colors supported for now
if (def_val_dim == 1 or def_val_dim == 4) and default_value.count(None) < def_val_dim:
sampler_name = prefix + action.name + "_mat_node_anim"
if getIndex(samplers, sampler_name) == -1:
interpolation = animateGetInterpolation(exportSettings, default_value)
def_val_data, in_tangent_data, out_tangent_data = animateDefaultValue(exportSettings,
default_value, interpolation)
keys = sorted(def_val_data.keys())
values = []
final_keys = []
key_offset = 0.0
if len(keys) > 0 and exportSettings['move_keyframes']:
key_offset = bpy.context.scene.frame_start / bpy.context.scene.render.fps
for key in keys:
if key - key_offset < 0.0:
continue
final_keys.append(key - key_offset)
if interpolation == 'CUBICSPLINE':
for i in range(0, def_val_dim):
values.append(in_tangent_data[key][i])
for i in range(0, def_val_dim):
values.append(def_val_data[key][i])
if interpolation == 'CUBICSPLINE':
for i in range(0, def_val_dim):
values.append(out_tangent_data[key][i])
count = len(final_keys)
if count:
sampler = {}
sampler['interpolation'] = interpolation
if interpolation == 'CONVERSION_NEEDED':
sampler['interpolation'] = 'LINEAR'
type = 'SCALAR'
input = gltf.generateAccessor(glTF, exportSettings['binary'],
final_keys, componentType, count, type, "")
sampler['input'] = input
count = len(values) // def_val_dim
if def_val_dim == 1:
type = 'SCALAR'
else:
type = 'VEC4'
output = gltf.generateAccessor(glTF, exportSettings['binary'],
values, componentType, count, type, "")
sampler['output'] = output
sampler['name'] = sampler_name
samplers.append(sampler)
if energy.count(None) < 1:
sampler_name = prefix + action.name + '_energy'
if getIndex(samplers, sampler_name) == -1:
interpolation = animateGetInterpolation(exportSettings, energy)
energy_data, in_tangent_data, out_tangent_data = animateEnergy(exportSettings,
energy, interpolation)
keys = sorted(energy_data.keys())
values = []
final_keys = []
key_offset = 0.0
if len(keys) > 0 and exportSettings['move_keyframes']:
key_offset = bpy.context.scene.frame_start / bpy.context.scene.render.fps
for key in keys:
if key - key_offset < 0.0:
continue
final_keys.append(key - key_offset)
if interpolation == 'CUBICSPLINE':
values.append(in_tangent_data[key][0])
values.append(energy_data[key][0])
if interpolation == 'CUBICSPLINE':
values.append(out_tangent_data[key][0])
count = len(final_keys)
if count:
sampler = {}
sampler['interpolation'] = interpolation
if interpolation == 'CONVERSION_NEEDED':
sampler['interpolation'] = 'LINEAR'
type = 'SCALAR'
input = gltf.generateAccessor(glTF, exportSettings['binary'],
final_keys, componentType, count, type, "")
sampler['input'] = input
count = len(values)
type = 'SCALAR'
output = gltf.generateAccessor(glTF, exportSettings['binary'],
values, componentType, count, type, "")
sampler['output'] = output
sampler['name'] = sampler_name
samplers.append(sampler)
if len(eval_time) > 0:
sampler_name = prefix + action.name + "_eval_time"
if getIndex(samplers, sampler_name) == -1:
interpolation = animateGetInterpolation(exportSettings, eval_time)
value_data, in_tangent_data, out_tangent_data = animateValue(
exportSettings, eval_time, interpolation, node_type)
keys = sorted(value_data.keys())
values = []
final_keys = []
key_offset = 0.0
if len(keys) > 0 and exportSettings['move_keyframes']:
key_offset = bpy.context.scene.frame_start / bpy.context.scene.render.fps
for key in keys:
if key - key_offset < 0.0:
continue
final_keys.append(key - key_offset)
if interpolation == 'CUBICSPLINE':
for i in range(0, len(in_tangent_data[key])):
values.append(in_tangent_data[key][i])
for i in range(0, len(value_data[key])):
values.append(value_data[key][i])
if interpolation == 'CUBICSPLINE':
for i in range(0, len(out_tangent_data[key])):
values.append(out_tangent_data[key][i])
count = len(final_keys)
if count < 1:
log.warning('Follow path constraint supports only keyframe animation, constraint name: ' + constraint_name)
return None
sampler = {}
sampler['interpolation'] = interpolation
if interpolation == 'CONVERSION_NEEDED':
sampler['interpolation'] = 'LINEAR'
bl_follow_path_constraint = None
for bl_cons in bl_obj.constraints:
if bl_cons.is_valid and bl_cons.name == constraint_name:
bl_follow_path_constraint = bl_cons
break
if bl_follow_path_constraint is None:
log.warning('Can not export follow path animation, constraint name: ' + constraint_name)
return None
bl_spline = bl_follow_path_constraint.target.data
ratio = 1.0 / bl_spline.path_duration
for i in range(len(values)):
values[i] *= ratio
type ='SCALAR'
input = gltf.generateAccessor(glTF, exportSettings['binary'], final_keys, componentType, count, type, "")
sampler['input'] = input
count = len(values)
type = 'SCALAR'
output = gltf.generateAccessor(glTF, exportSettings['binary'], values, componentType, count, type, "")
sampler['output'] = output
sampler['name'] = sampler_name
samplers.append(sampler)
processed_paths = []
# gather fcurves in data dict
for bl_fcurve in action.fcurves:
node_name = getNameInBrackets(bl_fcurve.data_path)
if node_name != None and not is_morph_data:
if (node_type == 'JOINT' or node_type == 'MAT_NODE') and used_node_name != node_name:
continue
elif node_type == 'NODE' or node_type in ROTATION_NODE_TYPES:
continue
else:
prefix = node_name + "_"
postfix = "_" + node_name
data_path = getAnimParam(bl_fcurve.data_path)
if data_path == 'location':
path = 'translation'
if path in processed_paths:
continue
processed_paths.append(path)
sampler_name = prefix + action.name + '_' + path
generateAnimChannel(glTF, bl_obj, sampler_name, path, bl_node_name + postfix, samplers, channels)
elif (data_path == 'rotation_axis_angle' or data_path == 'rotation_euler' or
data_path == 'rotation_quaternion'):
path = 'rotation'
if path in processed_paths:
continue
processed_paths.append(path)
sampler_name = prefix + action.name + '_' + path
generateAnimChannel(glTF, bl_obj, sampler_name, path, bl_node_name + postfix, samplers, channels)
elif data_path == 'scale':
path = 'scale'
if path in processed_paths:
continue
processed_paths.append(path)
sampler_name = prefix + action.name + '_' + path
generateAnimChannel(glTF, bl_obj, sampler_name, path, bl_node_name + postfix, samplers, channels)
elif data_path == 'value':
path = 'weights'
if path in processed_paths:
continue
processed_paths.append(path)
sampler_name = prefix + action.name + '_' + path
generateAnimChannel(glTF, bl_obj, sampler_name, path, bl_node_name + postfix, samplers, channels)
elif data_path == 'default_value':
if def_val_dim == 1:
path = 'material.nodeValue["' + used_node_name + '"]'
else:
path = 'material.nodeRGB["' + used_node_name + '"]'
if path in processed_paths:
continue
processed_paths.append(path)
sampler_name = prefix + action.name + '_mat_node_anim'
channel = generateAnimChannel(glTF, bl_obj, sampler_name, path, bl_node_name, samplers, channels)
if channel and bl_mat_name != None:
channel['target']['extras'] = {
'material': gltf.getMaterialIndex(glTF, bl_mat_name)
}
elif data_path == 'energy':
path = 'intensity'
if bl_obj.type == 'LIGHT' and bl_obj.data.type in ['POINT', 'SPOT', 'AREA']:
path = 'power'
if path in processed_paths:
continue
processed_paths.append(path)
sampler_name = prefix + action.name + '_energy'
generateAnimChannel(glTF, bl_obj, sampler_name, path, bl_node_name, samplers, channels)
elif data_path == 'eval_time':
path = 'constraint["' + constraint_name + '"].value'
if path in processed_paths:
continue
processed_paths.append(path)
sampler_name = prefix + action.name + '_eval_time'
generateAnimChannel(glTF, bl_obj, sampler_name, path, bl_node_name, samplers, channels)
#
# Property: animations
#
def generateAnimations(operator, context, exportSettings, glTF):
"""
Generates the top level animations, channels and samplers entry.
"""
animations = []
channels = []
samplers = []
filtered_objects_with_dg = exportSettings['filtered_objects_with_dg']
bl_backup_action = {}
if exportSettings['bake_armature_actions']:
start = None
end = None
for current_bl_action in bpy.data.actions:
# filter out non-object actions
if current_bl_action.id_root != 'OBJECT':
continue
for current_bl_fcurve in current_bl_action.fcurves:
if current_bl_fcurve is None:
continue
if start == None:
start = current_bl_fcurve.range()[0]
else:
start = min(start, current_bl_fcurve.range()[0])
if end == None:
end = current_bl_fcurve.range()[1]
else:
end = max(end, current_bl_fcurve.range()[1])
if start is None or end is None or exportSettings['frame_range']:
start = bpy.context.scene.frame_start
end = bpy.context.scene.frame_end
for bl_obj in filtered_objects_with_dg:
if bl_obj.animation_data is not None:
bl_backup_action[bl_obj.name] = bl_obj.animation_data.action
if bl_obj.pose is None:
continue
obj_scene = getSceneByObject(bl_obj)
if obj_scene is not None:
prev_active_scene = bpy.context.scene
bpy.context.window.scene = obj_scene
setSelectedObject(bl_obj)
# NOTE: int to prevent crashes in Blender 3.1+
bpy.ops.nla.bake(frame_start=int(start), frame_end=int(end),
only_selected=False, visual_keying=True)
restoreSelectedObjects()
bpy.context.window.scene = prev_active_scene
for bl_obj in filtered_objects_with_dg:
if bl_obj.animation_data is None:
continue
bl_action = bl_obj.animation_data.action
if bl_action is None:
continue
generateAnimationsParameter(operator, context, exportSettings, glTF, bl_action,
channels, samplers, bl_obj, None, None, None, bl_obj.rotation_mode,
False)
if exportSettings['skins']:
if bl_obj.type == 'ARMATURE' and len(bl_obj.pose.bones) > 0:
# Precalculate joint animation data.
start = None
end = None
for current_bl_action in bpy.data.actions:
# filter out non-object actions
if current_bl_action.id_root != 'OBJECT':
continue
for current_bl_fcurve in current_bl_action.fcurves:
if current_bl_fcurve is None:
continue
if start == None:
start = current_bl_fcurve.range()[0]
else:
start = min(start, current_bl_fcurve.range()[0])
if end == None:
end = current_bl_fcurve.range()[1]
else:
end = max(end, current_bl_fcurve.range()[1])
if start is None or end is None:
start = bpy.context.scene.frame_start
end = bpy.context.scene.frame_end
for frame in range(int(start), int(end) + 1):
bpy.context.scene.frame_set(frame)
for bl_bone in bl_obj.pose.bones:
jointKey = getPtr(bl_bone)
if not exportSettings['joint_cache'].get(jointKey):
exportSettings['joint_cache'][jointKey] = {}
joint_matrix = getBoneJointMatrix(bl_obj, bl_bone,
exportSettings['bake_armature_actions'])
tmp_location, tmp_rotation, tmp_scale = decomposeTransformSwizzle(joint_matrix)
exportSettings['joint_cache'][jointKey][float(frame)] = [tmp_location, tmp_rotation, tmp_scale]
for bl_bone in bl_obj.pose.bones:
generateAnimationsParameter(operator, context, exportSettings, glTF,
bl_action, channels, samplers, bl_obj, bl_bone,
None, None, bl_bone.rotation_mode, False)
# export morph targets animation data
processed_meshes = []
for bl_obj in filtered_objects_with_dg:
if bl_obj.type != 'MESH' or bl_obj.data is None:
continue
bl_mesh = bl_obj.data
if bl_mesh in processed_meshes:
continue
if bl_mesh.shape_keys is None or bl_mesh.shape_keys.animation_data is None:
continue
bl_action = bl_mesh.shape_keys.animation_data.action
if bl_action is None:
continue
generateAnimationsParameter(operator, context, exportSettings, glTF, bl_action,
channels, samplers, bl_obj, None, None, None, bl_obj.rotation_mode,
True)
processed_meshes.append(bl_mesh)
# export light animation
for bl_obj in filtered_objects_with_dg:
if bl_obj.type != 'LIGHT' or bl_obj.data is None:
continue
bl_light = bl_obj.data
if bl_light.animation_data is None:
continue
bl_action = bl_light.animation_data.action
if bl_action is None:
continue
generateAnimationsParameter(operator, context, exportSettings, glTF, bl_action,
channels, samplers, bl_obj, None, None, None, bl_obj.rotation_mode,
True)
# export material animation
for bl_obj in filtered_objects_with_dg:
# export morph targets animation data.
if bl_obj.type != 'MESH' or bl_obj.data is None:
continue
bl_mesh = bl_obj.data
for bl_mat in bl_mesh.materials:
if bl_mat == None:
continue
if bl_mat.node_tree == None or bl_mat.node_tree.animation_data == None:
continue
bl_action = bl_mat.node_tree.animation_data.action
if bl_action == None:
continue
node_names = [n.name for n in bl_mat.node_tree.nodes]
for name in node_names:
generateAnimationsParameter(operator, context, exportSettings, glTF,
bl_action, channels, samplers, bl_obj, None,
bl_mat.name, name, bl_obj.rotation_mode, False)
# export follow path constraint's animation
for bl_obj in filtered_objects_with_dg:
bl_follow_path_constraint = None
for bl_cons in bl_obj.constraints:
if bl_cons.is_valid and bl_cons.type == 'FOLLOW_PATH':
bl_follow_path_constraint = bl_cons
break
if (bl_obj.data is None or bl_follow_path_constraint is None or
bl_follow_path_constraint.target is None):
continue
bl_folow_path_obj = bl_follow_path_constraint.target
bl_spline = bl_folow_path_obj.data
if bl_spline is None or bl_spline.animation_data is None: