This repository has been archived by the owner on Mar 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
/
gltf2_generate.py
2912 lines (2082 loc) · 92.4 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.
#
# 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.
#
# Imports
#
import base64
import bpy
import copy
from .gltf2_constants import *
from .gltf2_animate import *
from .gltf2_create import *
from .gltf2_debug import *
from .gltf2_extract import *
from .gltf2_filter import *
from .gltf2_get import *
#
# Globals
#
#
# Functions
#
def generate_asset(export_settings,
glTF):
"""
Generates the top level asset entry.
"""
asset = {}
#
#
asset['version'] = '2.0'
#
asset['generator'] = 'Khronos Blender glTF 2.0 exporter'
#
if export_settings['gltf_copyright'] != "":
asset['copyright'] = export_settings['gltf_copyright']
#
#
glTF['asset'] = asset
def generate_animations_parameter(
operator,
context,
export_settings,
glTF,
action,
channels,
samplers,
blender_node_name,
blender_bone_name,
rotation_mode,
matrix_correction,
matrix_basis,
is_morph_data
):
"""
Helper function for storing animation parameters.
"""
name = blender_node_name
prefix = ""
postfix = ""
node_type = 'NODE'
used_node_name = blender_node_name
if blender_bone_name is not None:
node_type = 'JOINT'
used_node_name = blender_bone_name
#
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 = []
data = {
'location': location,
'rotation_axis_angle': rotation_axis_angle,
'rotation_euler': rotation_euler,
'rotation_quaternion': rotation_quaternion,
'scale': scale,
'value': value
}
# Gather fcurves by transform
for blender_fcurve in action.fcurves:
node_name = get_node(blender_fcurve.data_path)
if node_name is not None and not is_morph_data:
if blender_bone_name is None:
continue
elif blender_bone_name != node_name:
continue
else:
prefix = node_name + "_"
postfix = "_" + node_name
data_path = get_data_path(blender_fcurve.data_path)
if data_path == 'value':
data[data_path].append(blender_fcurve)
elif data_path in ['location', 'rotation_axis_angle', 'rotation_euler', 'rotation_quaternion', 'scale']:
data[data_path][blender_fcurve.array_index] = blender_fcurve
#
if location.count(None) < 3:
sampler_name = prefix + action.name + "_translation"
if get_index(samplers, sampler_name) == -1:
# Sampler doesn't exist, lets create it
sampler = {}
#
interpolation = animate_get_interpolation(export_settings, location)
if interpolation == 'CUBICSPLINE' and node_type == 'JOINT':
interpolation = 'CONVERSION_NEEDED'
if interpolation == 'CONVERSION_NEEDED':
sampler['interpolation'] = 'LINEAR'
else:
sampler['interpolation'] = interpolation
translation_data, in_tangent_data, out_tangent_data = animate_location(
export_settings,
location,
interpolation,
node_type,
used_node_name,
action.name,
matrix_correction,
matrix_basis
)
#
keys = sorted(translation_data.keys())
values = []
final_keys = []
key_offset = 0.0
if len(keys) > 0 and export_settings['gltf_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)
sampler['input'] = create_accessor(
operator,
context,
export_settings,
glTF,
final_keys,
GLTF_COMPONENT_TYPE_FLOAT,
count,
GLTF_DATA_TYPE_SCALAR,
""
)
#
count = len(values) // 3
sampler['output'] = create_accessor(
operator,
context,
export_settings,
glTF,
values,
GLTF_COMPONENT_TYPE_FLOAT,
count,
GLTF_DATA_TYPE_VEC3,
""
)
#
sampler['name'] = sampler_name
#
if sampler['output'] != -1 and sampler['input'] != -1:
samplers.append(sampler)
else:
print_console('WARNING', 'Skipping sampler ' + sampler_name + ', found missing or invalid data.')
#
#
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 get_index(samplers, sampler_name) == -1:
if rotation_axis_angle.count(None) < 4:
interpolation = animate_get_interpolation(export_settings, rotation_axis_angle)
# Conversion required in any case.
if interpolation == 'CUBICSPLINE':
interpolation = 'CONVERSION_NEEDED'
rotation_data = animate_rotation_axis_angle(export_settings, rotation_axis_angle, interpolation, node_type,
used_node_name, action.name, matrix_correction, matrix_basis)
if rotation_euler.count(None) < 3:
interpolation = animate_get_interpolation(export_settings, rotation_euler)
# Conversion required in any case.
if interpolation == 'CUBICSPLINE':
interpolation = 'CONVERSION_NEEDED'
rotation_data = animate_rotation_euler(export_settings, rotation_euler, rotation_mode, interpolation,
node_type, used_node_name, action.name, matrix_correction, matrix_basis)
if rotation_quaternion.count(None) < 4:
interpolation = animate_get_interpolation(export_settings, rotation_quaternion)
if interpolation == 'CUBICSPLINE' and node_type == 'JOINT':
interpolation = 'CONVERSION_NEEDED'
rotation_data, rotation_in_tangent_data, rotation_out_tangent_data = animate_rotation_quaternion(
export_settings, rotation_quaternion, interpolation, node_type, used_node_name, action.name,
matrix_correction, matrix_basis)
if rotation_data is not None:
#
keys = sorted(rotation_data.keys())
values = []
final_keys = []
key_offset = 0.0
if len(keys) > 0 and export_settings['gltf_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])
#
sampler = {}
#
count = len(final_keys)
sampler['input'] = create_accessor(
operator,
context,
export_settings,
glTF,
final_keys,
GLTF_COMPONENT_TYPE_FLOAT,
count,
GLTF_DATA_TYPE_SCALAR,
""
)
#
count = len(values) // 4
sampler['output'] = create_accessor(
operator,
context,
export_settings,
glTF,
values,
GLTF_COMPONENT_TYPE_FLOAT,
count,
GLTF_DATA_TYPE_VEC4,
""
)
#
sampler['interpolation'] = interpolation
if interpolation == 'CONVERSION_NEEDED':
sampler['interpolation'] = 'LINEAR'
#
sampler['name'] = sampler_name
#
if sampler['output'] != -1 and sampler['input'] != -1:
samplers.append(sampler)
else:
print_console('WARNING', 'Skipping sampler ' + sampler_name + ', found missing or invalid data.')
#
#
if scale.count(None) < 3:
sampler_name = prefix + action.name + "_scale"
if get_index(samplers, sampler_name) == -1:
sampler = {}
#
interpolation = animate_get_interpolation(export_settings, scale)
if interpolation == 'CUBICSPLINE' and node_type == 'JOINT':
interpolation = 'CONVERSION_NEEDED'
sampler['interpolation'] = interpolation
if interpolation == 'CONVERSION_NEEDED':
sampler['interpolation'] = 'LINEAR'
scale_data, in_tangent_data, out_tangent_data = animate_scale(
export_settings,
scale,
interpolation,
node_type,
used_node_name,
action.name,
matrix_correction,
matrix_basis
)
#
keys = sorted(scale_data.keys())
values = []
final_keys = []
key_offset = 0.0
if len(keys) > 0 and export_settings['gltf_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)
sampler['input'] = create_accessor(
operator,
context,
export_settings,
glTF,
final_keys,
GLTF_COMPONENT_TYPE_FLOAT,
count,
GLTF_DATA_TYPE_SCALAR,
""
)
#
count = len(values) // 3
sampler['output'] = create_accessor(
operator,
context,
export_settings,
glTF,
values,
GLTF_COMPONENT_TYPE_FLOAT,
count,
GLTF_DATA_TYPE_VEC3,
""
)
#
sampler['name'] = sampler_name
#
if sampler['output'] != -1 and sampler['input'] != -1:
samplers.append(sampler)
else:
print_console('WARNING', 'Skipping sampler ' + sampler_name + ', found missing or invalid data.')
#
#
if len(value) > 0 and is_morph_data:
sampler_name = prefix + action.name + "_weights"
if get_index(samplers, sampler_name) == -1:
sampler = {}
#
interpolation = animate_get_interpolation(export_settings, value)
if interpolation == 'CUBICSPLINE' and node_type == 'JOINT':
interpolation = 'CONVERSION_NEEDED'
sampler['interpolation'] = interpolation
if interpolation == 'CONVERSION_NEEDED':
sampler['interpolation'] = 'LINEAR'
value_data, in_tangent_data, out_tangent_data = animate_value(export_settings, value, interpolation,
node_type, used_node_name, matrix_correction,
matrix_basis)
#
keys = sorted(value_data.keys())
values = []
final_keys = []
key_offset = 0.0
if len(keys) > 0 and export_settings['gltf_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)
sampler['input'] = create_accessor(
operator,
context,
export_settings,
glTF,
final_keys,
GLTF_COMPONENT_TYPE_FLOAT,
count,
GLTF_DATA_TYPE_SCALAR,
""
)
#
count = len(values)
sampler['output'] = create_accessor(
operator,
context,
export_settings,
glTF,
values,
GLTF_COMPONENT_TYPE_FLOAT,
count,
GLTF_DATA_TYPE_SCALAR,
""
)
#
sampler['name'] = sampler_name
#
if sampler['output'] != -1 and sampler['input'] != -1:
samplers.append(sampler)
else:
print_console('WARNING', 'Skipping sampler ' + sampler_name + ', found missing or invalid data.')
#
#
#
#
write_transform = [False, False, False, False]
# Gather fcurves by transform
for blender_fcurve in action.fcurves:
node_name = get_node(blender_fcurve.data_path)
if node_name is not None and not is_morph_data:
if blender_bone_name is None:
continue
elif blender_bone_name != node_name:
continue
else:
prefix = node_name + "_"
postfix = "_" + node_name
data_path = get_data_path(blender_fcurve.data_path)
if data_path == 'location':
write_transform[0] = True
if data_path in ['rotation_axis_angle', 'rotation_euler', 'rotation_quaternion']:
write_transform[1] = True
if data_path == 'scale':
write_transform[2] = True
if data_path == 'value':
write_transform[3] = True
#
write_transform_index = 0
for path in ['translation', 'rotation', 'scale', 'weights']:
if write_transform[write_transform_index]:
sampler_name = prefix + action.name + "_" + path
sampler_index = get_index(samplers, sampler_name)
# Skip channels containing skipped samplers.
if sampler_index == -1:
print_console('WARNING', 'Skipped channel in action ' + action.name + ', missing sampler.')
continue
#
channel = {}
channel['sampler'] = sampler_index
#
target_name = name + postfix
channel['target'] = {
'path': path,
'node': get_node_index(glTF, target_name)
}
#
channels.append(channel)
write_transform_index += 1
#
# Property: animations
#
def generate_animations(operator,
context,
export_settings,
glTF):
"""
Generates the top level animations entry.
"""
def process_object_animations(blender_object, blender_action):
correction_matrix_local = blender_object.matrix_parent_inverse
matrix_basis = mathutils.Matrix.Identity(4)
#
if export_settings['gltf_bake_skins']:
blender_action = bake_action(export_settings, blender_object, blender_action)
#
if blender_action.name not in animations:
animations[blender_action.name] = {
'name': blender_action.name,
'channels': [],
'samplers': []
}
channels = animations[blender_action.name]['channels']
samplers = animations[blender_action.name]['samplers']
# Add entry to joint cache. Current action may not need skinnning,
# but there are too many places to check for and add it later.
gltf_joint_cache = export_settings['gltf_joint_cache']
if not gltf_joint_cache.get(blender_action.name):
gltf_joint_cache[blender_action.name] = {}
#
generate_animations_parameter(
operator,
context,
export_settings,
glTF,
blender_action,
channels,
samplers,
blender_object.name,
None,
blender_object.rotation_mode,
correction_matrix_local,
matrix_basis,
False
)
if export_settings['gltf_skins']:
if blender_object.type == 'ARMATURE' and len(blender_object.pose.bones) > 0:
#
if export_settings['gltf_yup']:
axis_basis_change = mathutils.Matrix(
((1.0, 0.0, 0.0, 0.0),
(0.0, 0.0, 1.0, 0.0),
(0.0, -1.0, 0.0, 0.0),
(0.0, 0.0, 0.0, 1.0))
)
else:
axis_basis_change = mathutils.Matrix.Identity(4)
# Precalculate joint animation data.
start, end = compute_action_range(export_settings, [blender_action])
# Iterate over frames in export range
for frame in range(int(start), int(end) + 1):
bpy.context.scene.frame_set(frame)
# Iterate over object's bones
for blender_bone in blender_object.pose.bones:
correction_matrix_local, matrix_basis = compute_bone_matrices(
axis_basis_change,
blender_bone,
blender_object,
export_settings
)
if not gltf_joint_cache[blender_action.name].get(blender_bone.name):
gltf_joint_cache[blender_action.name][blender_bone.name] = {}
matrix = correction_matrix_local * matrix_basis
tmp_location, tmp_rotation, tmp_scale = matrix.decompose()
gltf_joint_cache[blender_action.name][blender_bone.name][float(frame)] = [tmp_location, tmp_rotation, tmp_scale]
#
for blender_bone in blender_object.pose.bones:
correction_matrix_local, matrix_basis = compute_bone_matrices(
axis_basis_change,
blender_bone,
blender_object,
export_settings
)
generate_animations_parameter(
operator,
context,
export_settings,
glTF,
blender_action,
channels,
samplers,
blender_object.name,
blender_bone.name,
blender_bone.rotation_mode,
correction_matrix_local,
matrix_basis,
False
)
animations = {}
#
#
filtered_objects = export_settings['filtered_objects']
#
#
processed_meshes = {}
def process_mesh_object(blender_object, blender_action):
blender_mesh = blender_object.data
if not blender_action.name in processed_meshes:
processed_meshes[blender_action.name] = []
if blender_mesh in processed_meshes[blender_action.name]:
return
#
if blender_action.name not in animations:
animations[blender_action.name] = {
'name': blender_action.name,
'channels': [],
'samplers': []
}
channels = animations[blender_action.name]['channels']
samplers = animations[blender_action.name]['samplers']
#
correction_matrix_local = mathutils.Matrix.Identity(4)
matrix_basis = mathutils.Matrix.Identity(4)
generate_animations_parameter(operator, context, export_settings, glTF, blender_action, channels, samplers,
blender_object.name, None, blender_object.rotation_mode, correction_matrix_local,
matrix_basis, True)
processed_meshes[blender_action.name].append(blender_mesh)
for blender_object in filtered_objects:
animation_data = blender_object.animation_data
if animation_data is not None:
object_actions = []
# Collect active action.
if animation_data.action:
object_actions.append(animation_data.action)
# Collect associated strips from NLA tracks.
for track in animation_data.nla_tracks:
# Multi-strip tracks do not export correctly yet (they need to be baked),
# so skip them for now and only write single-strip tracks.
if track.strips is None or len(track.strips) != 1:
continue
for strip in track.strips:
object_actions.append(strip.action)
# Remove duplicate actions.
object_actions = list(set(object_actions))
# Export all collected actions.
for action in object_actions:
active_action = animation_data.action
animation_data.action = action
process_object_animations(blender_object, action)
animation_data.action = active_action
#
# Export shape keys.
if (blender_object.type != 'MESH'
or blender_object.data is None
or blender_object.data.shape_keys is None
or blender_object.data.shape_keys.animation_data is None):
continue
shape_keys = blender_object.data.shape_keys
shape_key_actions = []
if shape_keys.animation_data.action:
shape_key_actions.append(shape_keys.animation_data.action)
for track in shape_keys.animation_data.nla_tracks:
for strip in track.strips:
shape_key_actions.append(strip.action)
for action in shape_key_actions:
active_action = shape_keys.animation_data.action
shape_keys.animation_data.action = action
process_mesh_object(blender_object, action)
shape_keys.animation_data.action = active_action
#
#
if len(animations) > 0:
glTF['animations'] = []
# Sampler 'name' is used to gather the index. However, 'name' is no property of sampler and has to be removed.
for animation in animations.values():
for sampler in animation['samplers']:
del sampler['name']
if len(animation['channels']) > 0:
glTF['animations'].append(animation)
def compute_bone_matrices(axis_basis_change, blender_bone, blender_object, export_settings):
matrix_basis = blender_bone.matrix_basis
if blender_bone.parent is None:
# Bone has no parent
correction_matrix_local = axis_basis_change * blender_bone.bone.matrix_local
else:
# Transform matrix by parent bone's matrix
correction_matrix_local = blender_bone.parent.bone.matrix_local.inverted() * blender_bone.bone.matrix_local
if export_settings['gltf_bake_skins']:
matrix_basis = blender_object.convert_space(
blender_bone,
blender_bone.matrix,
from_space='POSE',
to_space='LOCAL'
)
return correction_matrix_local, matrix_basis
def bake_action(export_settings, blender_object, blender_action):
start, end = compute_action_range(export_settings, [blender_action])
step = export_settings['gltf_frame_step']
#
bpy.context.scene.objects.active = blender_object
blender_object.animation_data.action = blender_action
#
bpy.ops.nla.bake(frame_start=start, frame_end=end, step=step, only_selected=True, visual_keying=True,
clear_constraints=False, use_current_action=True, bake_types={'POSE'})
#
return blender_object.animation_data.action
def compute_action_range(export_settings, actions):
start = None
end = None
for current_blender_action in actions:
for current_blender_fcurve in current_blender_action.fcurves:
if current_blender_fcurve is None:
continue
if start is None:
start = current_blender_fcurve.range()[0]
else:
start = min(start, current_blender_fcurve.range()[0])
if end is None:
end = current_blender_fcurve.range()[1]
else:
end = max(end, current_blender_fcurve.range()[1])
if start is None or end is None or export_settings['gltf_frame_range']:
start = bpy.context.scene.frame_start
end = bpy.context.scene.frame_end
return start, end
def generate_cameras(export_settings, glTF):
"""
Generates the top level cameras entry.
"""
cameras = []
#
#
filtered_cameras = export_settings['filtered_cameras']
for blender_camera in filtered_cameras:
#
# Property: camera
#
camera = {}
if blender_camera.type == 'PERSP':
camera['type'] = 'perspective'
perspective = {}
#
# None of them can get 0, as Blender checks this.
width = bpy.context.scene.render.pixel_aspect_x * bpy.context.scene.render.resolution_x
height = bpy.context.scene.render.pixel_aspect_y * bpy.context.scene.render.resolution_y
aspectRatio = width / height
perspective['aspectRatio'] = aspectRatio
yfov = None
if width >= height:
if blender_camera.sensor_fit != 'VERTICAL':
yfov = 2.0 * math.atan(math.tan(blender_camera.angle * 0.5) / aspectRatio);
else:
yfov = blender_camera.angle
else:
if blender_camera.sensor_fit != 'HORIZONTAL':
yfov = blender_camera.angle
else:
yfov = 2.0 * math.atan(math.tan(blender_camera.angle * 0.5) / aspectRatio);
perspective['yfov'] = yfov
perspective['znear'] = blender_camera.clip_start
if not export_settings['gltf_camera_infinite']:
perspective['zfar'] = blender_camera.clip_end
#
camera['perspective'] = perspective
elif blender_camera.type == 'ORTHO':
camera['type'] = 'orthographic'
orthographic = {}
#
orthographic['xmag'] = blender_camera.ortho_scale
orthographic['ymag'] = blender_camera.ortho_scale
orthographic['znear'] = blender_camera.clip_start
orthographic['zfar'] = blender_camera.clip_end
#
camera['orthographic'] = orthographic
else:
continue
#
camera['name'] = blender_camera.name
#
#
cameras.append(camera)
#
#
if len(cameras) > 0:
glTF['cameras'] = cameras