-
Notifications
You must be signed in to change notification settings - Fork 15
/
godot.py
1099 lines (892 loc) · 44.2 KB
/
godot.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
import bpy, sys, time
from bpy.props import *
from mathutils import *
from bpy_extras.io_utils import (ExportHelper,
#orientation_helper_factory,
#path_reference_mode,
axis_conversion,
)
from .common import *
def export_gltf(filepath, scene_props, export_materials='EXPORT'):
bpy.ops.export_scene.gltf(
filepath=filepath,
#check_existing=True,
#export_format='GLTF_EMBEDDED',
export_format=scene_props.gltf_format,
#ui_tab='GENERAL',
#export_copyright='',
export_image_format='AUTO',
export_texture_dir='',
export_keep_originals=False,
export_texcoords=True,
export_normals=True,
#export_draco_mesh_compression_enable=False,
#export_draco_mesh_compression_level=6,
#export_draco_position_quantization=14,
#export_draco_normal_quantization=10,
#export_draco_texcoord_quantization=12,
#export_draco_color_quantization=10,
#export_draco_generic_quantization=12,
export_tangents = scene_props.export_tangent,
export_materials=export_materials,
# export_colors = scene_props.export_vcols,
use_mesh_edges=False,
use_mesh_vertices=False,
export_cameras=False,
#export_selected=True,
use_selection=True,
use_visible=False,
use_renderable=False,
use_active_collection=False,
export_extras=False,
export_yup=True,
export_apply = scene_props.apply_modifiers,
export_animations = scene_props.export_animations,
export_frame_range=True,
export_frame_step=1,
export_force_sampling=True,
export_nla_strips = scene_props.export_animations,
export_def_bones=False,
export_optimize_animation_size=True,
export_anim_single_armature=False,
export_current_frame=False,
export_skins=True,
export_all_influences=False,
export_morph=True,
export_morph_normal=True,
export_morph_tangent=True,
export_lights=False,
#export_displacement=False,
#will_save_settings=False,
#filter_glob='*.glb;*.gltf'
)
def get_deform_bones(obj, bone_name):
bones = [b for b in obj.pose.bones if bone_name in b.name and 'DEF-' in b.name]
if len(bones) == 1:
pass
elif bone_name == 'head':
bone = obj.pose.bones.get('DEF-spine.006')
if bone and bone not in bones: bones.append(bone)
elif bone_name == 'neck':
bone = obj.pose.bones.get('DEF-spine.005')
if bone and bone not in bones: bones.append(bone)
bone = obj.pose.bones.get('DEF-spine.004')
if bone and bone not in bones: bones.append(bone)
elif bone_name == 'chest':
bone = obj.pose.bones.get('DEF-spine.003')
if bone and bone not in bones: bones.append(bone)
bone = obj.pose.bones.get('DEF-spine.002')
if bone and bone not in bones: bones.append(bone)
elif bone_name in {'torso', 'hips'}:
bone = obj.pose.bones.get('DEF-spine.001')
if bone and bone not in bones: bones.append(bone)
bone = obj.pose.bones.get('DEF-spine')
if bone and bone not in bones: bones.append(bone)
bone = obj.pose.bones.get('DEF-pelvis.L')
if bone and bone not in bones: bones.append(bone)
bone = obj.pose.bones.get('DEF-pelvis.R')
if bone and bone not in bones: bones.append(bone)
elif 'tweak_spine' in bone_name:
bname = bone_name.replace('tweak_', '')
bbones = [b for b in obj.pose.bones if bname in b.name and 'DEF-' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'upper_arm' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-upper_arm.L' in b.name]
elif '.R' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-upper_arm.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'forearm' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-forearm.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-forearm.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'hand' in bone_name:
bone = None
if '.L' in bone_name:
bone = obj.pose.bones.get('DEF-hand.L')
if '.R' in bone_name:
bone = obj.pose.bones.get('DEF-hand.L')
if bone and bone not in bones: bones.append(bone)
elif 'palm' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-palm' in b.name and '.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-palm' in b.name and '.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'thumb' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-thumb' in b.name and '.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-thumb' in b.name and '.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'f_index' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-f_index' in b.name and '.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-f_index' in b.name and '.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'f_middle' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-f_middle' in b.name and '.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-f_middle' in b.name and '.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'f_ring' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-f_ring' in b.name and '.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-f_ring' in b.name and '.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'f_pinky' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-f_pinky' in b.name and '.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-f_pinky' in b.name and '.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'thigh' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-thigh.L' in b.name]
elif '.R' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-thigh.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'shin' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-shin.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-shin.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'foot' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-foot.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-foot.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'toe' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-toe.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.pose.bones if 'DEF-toe.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
return bones
def get_deform_edit_bones(obj, bone_name):
bones = [b for b in obj.data.edit_bones if 'DEF-'+bone_name == b.name]
if len(bones) == 1:
pass
elif bone_name == 'head':
bone = obj.data.edit_bones.get('DEF-spine.006')
if bone and bone not in bones: bones.append(bone)
elif bone_name == 'neck':
bone = obj.data.edit_bones.get('DEF-spine.005')
if bone and bone not in bones: bones.append(bone)
bone = obj.data.edit_bones.get('DEF-spine.004')
if bone and bone not in bones: bones.append(bone)
elif bone_name == 'chest':
bone = obj.data.edit_bones.get('DEF-spine.003')
if bone and bone not in bones: bones.append(bone)
bone = obj.data.edit_bones.get('DEF-spine.002')
if bone and bone not in bones: bones.append(bone)
elif bone_name in {'torso', 'hips'}:
bone = obj.data.edit_bones.get('DEF-spine.001')
if bone and bone not in bones: bones.append(bone)
bone = obj.data.edit_bones.get('DEF-spine')
if bone and bone not in bones: bones.append(bone)
bone = obj.data.edit_bones.get('DEF-pelvis.L')
if bone and bone not in bones: bones.append(bone)
bone = obj.data.edit_bones.get('DEF-pelvis.R')
if bone and bone not in bones: bones.append(bone)
elif 'tweak_spine' in bone_name:
bname = bone_name.replace('tweak_', '')
bbones = [b for b in obj.data.edit_bones if bname in b.name and 'DEF-' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'upper_arm' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-upper_arm.L' in b.name]
elif '.R' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-upper_arm.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'forearm' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-forearm.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-forearm.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'hand' in bone_name:
bone = None
if '.L' in bone_name:
bone = obj.data.edit_bones.get('DEF-hand.L')
if '.R' in bone_name:
bone = obj.data.edit_bones.get('DEF-hand.L')
if bone and bone not in bones: bones.append(bone)
elif 'palm' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-palm' in b.name and '.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-palm' in b.name and '.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'thumb' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-thumb' in b.name and '.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-thumb' in b.name and '.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'f_index' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-f_index' in b.name and '.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-f_index' in b.name and '.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'f_middle' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-f_middle' in b.name and '.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-f_middle' in b.name and '.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'f_ring' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-f_ring' in b.name and '.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-f_ring' in b.name and '.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'f_pinky' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-f_pinky' in b.name and '.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-f_pinky' in b.name and '.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'thigh' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-thigh.L' in b.name]
elif '.R' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-thigh.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'shin' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-shin.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-shin.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'foot' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-foot.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-foot.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
elif 'toe' in bone_name:
bbones = []
if '.L' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-toe.L' in b.name]
if '.R' in bone_name:
bbones = [b for b in obj.data.edit_bones if 'DEF-toe.R' in b.name]
for bone in bbones:
if bone and bone not in bones: bones.append(bone)
return bones
def copy_some_bones(rig_object, active_export_rig_ob):
ori_mode = active_export_rig_ob.mode
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.armature.select_all(action='DESELECT')
arm = active_export_rig_ob.data
for pb in rig_object.pose.bones:
if pb.gr_props.add_extra_copy_bone:
# Get and select edit bone
b = arm.edit_bones.get(pb.name)
if not b: continue
b.select=True
arm.edit_bones.active = b
# Duplicate edit bone
copyb = arm.edit_bones.new(pb.name + COPY_SUFFIX)
copyb.head = b.head
copyb.tail = b.tail
copyb.matrix = b.matrix.copy()
copyb.parent = b.parent
copyb.select = False
bpy.ops.object.mode_set(mode=ori_mode)
def keep_parent_update(self, context: bpy.types.Context):
edit_bones = context.selected_bones
if len(edit_bones)>1 :
for bone in edit_bones:
def_bone = get_deform_edit_bones(context.active_object, bone.name)
if len(def_bone)>0:
def_bone[0].gr_props["keep_parent"] = self.keep_parent
class ExportRigifyGLTF(bpy.types.Operator, ExportHelper):
bl_idname = "export_mesh.rigify_gltf"
bl_label = "Export Godot Skeleton"
bl_description = "Export rigify mesh as GLTF file"
bl_options = {'REGISTER', 'UNDO'}
filename_ext = ".glb"
filter_glob : StringProperty(default="*.glb, *.gltf", options={'HIDDEN'})
#custom_directory = StringProperty(default='')
@classmethod
def poll(cls, context):
return get_current_armature_object()
def invoke(self, context, event):
obj = get_current_armature_object()
scene_props = context.scene.gr_props
if scene_props.gltf_format == "GLTF_SEPARATE" :
self.filename_ext = ".gltf"
elif scene_props.gltf_format == "GLB":
self.filename_ext = ".glb"
directory = os.path.dirname(self.filepath)
filename = os.path.splitext(os.path.basename(context.blend_data.filepath))[0]
suffix = ''
if scene_props.export_animations:
if scene_props.export_only_selected_action:
try: action = obj.animation_data.action
except: action = None
if action:
filename = action.name
elif not scene_props.export_meshes:
suffix = '_anim'
if scene_props.remove_whitespaces:
filename = filename.replace(' ', '')
self.filepath = os.path.join(directory, filename + suffix + self.filename_ext)
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
T = time.time()
scene_props = context.scene.gr_props
if not scene_props.export_animations and not scene_props.export_meshes:
self.report({'ERROR'}, 'You should at least choose to export meshes or animations!')
return{'CANCELLED'}
# Create save system to save current selection, mode, and active object
state = SaveState(context)
# Get all possible objects
objects = [o for o in context.selected_objects]
if context.object and context.object not in objects:
objects.append(context.object)
use_export_meshes = scene_props.export_meshes and not (
scene_props.export_animations and scene_props.export_only_selected_action)
# Evaluate selected objects to export
rig_object, mesh_objects, failed_mesh_objects, error_messages = evaluate_and_get_source_data(
context.scene, objects, use_export_meshes)
# If found error
if error_messages != '':
self.report({'ERROR'}, error_messages)
state.load(context)
return{'CANCELLED'}
if scene_props.export_animations and scene_props.export_only_selected_action:
if not rig_object.animation_data or not rig_object.animation_data.action:
self.report({'ERROR'}, 'No active action used on the rig!')
state.load(context)
return{'CANCELLED'}
# Remember rig object original matrices and original action
quaternions = {}
eulers = {}
scales = {}
locations = {}
for pb in rig_object.pose.bones:
quaternions[pb.name] = pb.rotation_quaternion.copy()
eulers[pb.name] = pb.rotation_euler.copy()
scales[pb.name] = pb.scale.copy()
locations[pb.name] = pb.location.copy()
if rig_object.animation_data:
ori_action = rig_object.animation_data.action
else: ori_action = None
# Scale of the objects
scale = 1
# Check if armature using rigify
use_rigify = check_use_rigify(rig_object.data)
# Get export rig
unparent_all = True if scene_props.parental_mode == 'UNPARENT_ALL' else False
export_rig_ob = extract_export_rig(context, rig_object, scale, use_rigify, unparent_all=unparent_all)
# Set to object mode and deselect all
if context.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
# Get export mesh objects
export_mesh_objs = []
if use_export_meshes:
export_mesh_objs = extract_export_meshes(context, mesh_objects, export_rig_ob, scale, scene_props.only_export_baked_vcols)
# Export extra objects first because it doesn't need materials or armature
for ob in export_mesh_objs:
if ob.gr_props.add_clear_location_duplicate:
set_active(ob)
select_set(ob, True)
# Remember things
ori_show_viewports = [m.show_viewport for m in ob.modifiers]
ori_show_renders = [m.show_render for m in ob.modifiers]
ori_location = ob.location.copy()
# Disable armature modifiers
for mod in ob.modifiers:
if mod.type == 'ARMATURE':
mod.show_viewport = False
mod.show_render = False
# Clear location
bpy.ops.object.location_clear(clear_delta=False)
# Get filepath
directory = os.path.dirname(self.filepath)
filename = os.path.splitext(os.path.basename(self.filepath))[0]
if ob.name.startswith(filename):
filename = ''
else: filename += ' '
filename += ob.name
if scene_props.remove_whitespaces:
filename = filename.replace(' ', '')
filepath = os.path.join(directory, filename + self.filename_ext)
# Export gltf
export_gltf(filepath, scene_props, 'PLACEHOLDER')
# Recover modifiers
for i, mod in enumerate(ob.modifiers):
if ori_show_viewports[i]:
mod.show_viewport = True
if ori_show_renders[i]:
mod.show_render = True
# Recover location
ob.location = ori_location
# Deselect object
select_set(ob, False)
# Set ucupaint baked outside
ori_not_use_baked = []
ori_not_use_baked_outside = []
use_yp_baked_outside = scene_props.ucupaint_baked_outside and hasattr(bpy.types.ShaderNodeTree, 'yp')
if use_yp_baked_outside:
for ob in export_mesh_objs:
for i, ms in enumerate(ob.material_slots):
mat = ms.material
if not mat or not mat.node_tree: continue
for n in mat.node_tree.nodes:
if n.type == 'GROUP' and n.node_tree and n.node_tree.yp.is_ypaint_node:
yp = n.node_tree.yp
baked_found = any([c for c in yp.channels if n.node_tree.nodes.get(c.baked)])
if baked_found:
if not yp.use_baked or not yp.enable_baked_outside:
set_active(ob)
select_set(ob, True)
ob.active_material_index = i
mat.node_tree.nodes.active = n
if not yp.use_baked:
ori_not_use_baked.append(n.node_tree)
yp.use_baked = True
if not yp.enable_baked_outside:
ori_not_use_baked_outside.append(n.node_tree)
yp.enable_baked_outside = True
select_set(ob, False)
# Select rig export object
set_active(export_rig_ob)
select_set(export_rig_ob, True)
# To store actions
actions = []
baked_actions = []
original_frame_end = context.scene.frame_end
# Go to pose mode
bpy.ops.object.mode_set(mode='POSE')
#bpy.ops.pose.select_all(action='SELECT')
# Copy some bones if necessary
copy_some_bones(rig_object, export_rig_ob)
# Deals with animations
if scene_props.export_animations:
# Disable armature modifier first to make the bake action faster
for obj in export_mesh_objs:
for mod in obj.modifiers:
if mod.type == 'ARMATURE':
mod.object = None
if scene_props.export_only_selected_action:
actions = [rig_object.animation_data.action]
else: actions = [a for a in bpy.data.actions if a.rigify_export_props.enable_export]
# Remove already available tracks
if export_rig_ob.animation_data and len(export_rig_ob.animation_data.nla_tracks) > 0:
for track in reversed(export_rig_ob.animation_data.nla_tracks):
export_rig_ob.animation_data.nla_tracks.remove(track)
# Bake all valid actions
for action in actions:
action_props = action.rigify_export_props
# Reset all bone transformations first
reset_pose_bones(rig_object)
if context.scene.rigify_export_props.sync_rigify_props:
reset_pose_bone_props(rig_object, context.scene, action)
# Set active action
rig_object.animation_data.action = action
# Remember active action name
action_name = action.name
# Set active action name to use temporary name
action.name += TEMP_SUFFIX
# Make constraint
make_constraint(context, rig_object, export_rig_ob)
# Frame start and end
if action.use_frame_range:
frame_start = int(action.frame_start)
frame_end = int(action.frame_end)
else:
frame_start = int(action.frame_range[0])
frame_end = int(action.frame_range[1])
if action_props.enable_loop and action_props.enable_skip_last_frame and not action.use_frame_range:
frame_end -= 1
if frame_end > context.scene.frame_end:
context.scene.frame_end = frame_end
print("INFO: Baking action '" + action_name + "'...")
# Bake animations
bpy.ops.nla.bake(
frame_start=frame_start,
frame_end=frame_end,
only_selected=True,
visual_keying=True,
clear_constraints=True,
use_current_action=True,
clean_curves = True,
bake_types={'POSE'})
# Rename baked action so it will be exported
baked_action = export_rig_ob.animation_data.action
baked_action.name = action_name
if not baked_action.name.endswith('-loop') and action_props.enable_loop:
baked_action.name += '-loop'
if action_props.enable_remove_untransformed:
remove_non_transformed_keyframes(baked_action, tolerance=action_props.untransformed_tolerance)
# Dealing with separated GLTFs
if scene_props.export_action_as_separated_gltf and not scene_props.export_only_selected_action:
# Add nla track for the baked action
track = export_rig_ob.animation_data.nla_tracks.new()
strip = track.strips.new(baked_action.name, int(baked_action.frame_start), baked_action)
# Set the filepath based on action name
directory = os.path.dirname(self.filepath)
filename = action_name
if scene_props.remove_whitespaces:
filename = filename.replace(' ', '')
filepath = os.path.join(directory, filename + '.gltf')
# Export action as gltf file
bpy.ops.object.mode_set(mode='OBJECT')
export_gltf(filepath, scene_props)
bpy.ops.object.mode_set(mode='POSE')
# Remove nla track
track = export_rig_ob.animation_data.nla_tracks[0]
export_rig_ob.animation_data.nla_tracks.remove(track)
# Remember baked actions so it can be removed later
baked_actions.append(baked_action)
# Set active action back to None
export_rig_ob.animation_data.action = None
# Recover armature modifiers
for obj in export_mesh_objs:
for mod in obj.modifiers:
if mod.type == 'ARMATURE':
mod.object = export_rig_ob
# Only add baked actions to NLA if not using separated gltf
if baked_actions and (scene_props.export_only_selected_action or not scene_props.export_action_as_separated_gltf):
# Add baked action to NLA tracks
for ba in baked_actions:
track = export_rig_ob.animation_data.nla_tracks.new()
strip = track.strips.new(ba.name, int(ba.frame_start), ba)
# Back to object mode
bpy.ops.object.mode_set(mode='OBJECT')
# Select export mesh objects
for obj in export_mesh_objs:
select_set(obj, True)
## EXPORT!
# No need to export if there's no meshes or actions
if any(export_mesh_objs) or (export_rig_ob.animation_data and any(export_rig_ob.animation_data.nla_tracks)):
export_gltf(self.filepath, scene_props)
# Delete exported objects
bpy.ops.object.delete()
# Delete baked actions
for action in baked_actions:
bpy.data.actions.remove(action)
context.scene.frame_end = original_frame_end
# Recover original action names
for action in bpy.data.actions:
if action.name.endswith(TEMP_SUFFIX):
action.name = action.name[:-len(TEMP_SUFFIX)]
# Recover original mesh object names
for obj in bpy.data.objects:
if obj.name.endswith(TEMP_SUFFIX):
obj.name = obj.name[:-len(TEMP_SUFFIX)]
# Descale original rig
rig_object.scale /= scale
# Recover ucupaint use baked
if use_yp_baked_outside and (any(ori_not_use_baked) or any(ori_not_use_baked_outside)):
already_not_use_baked = []
already_not_use_baked_outside = []
for ob in context.view_layer.objects:
for i, ms in enumerate(ob.material_slots):
mat = ms.material
if not mat or not mat.node_tree: continue
node_names = []
for n in mat.node_tree.nodes:
if n.type == 'GROUP' : node_names.append(n.name)
for node_name in node_names:
n = mat.node_tree.nodes.get(node_name)
if n.type == 'GROUP' and n.node_tree:
if ((n.node_tree in ori_not_use_baked and n.node_tree not in already_not_use_baked) or
(n.node_tree in ori_not_use_baked_outside and n.node_tree not in already_not_use_baked_outside)
):
set_active(ob)
select_set(ob, True)
ob.active_material_index = i
mat.node_tree.nodes.active = n
if n.node_tree in ori_not_use_baked and n.node_tree not in already_not_use_baked:
n.node_tree.yp.use_baked = False
already_not_use_baked.append(n.node_tree)
if n.node_tree in ori_not_use_baked_outside and n.node_tree not in already_not_use_baked_outside:
n.node_tree.yp.enable_baked_outside = False
already_not_use_baked_outside.append(n.node_tree)
select_set(ob, False)
# Load original state
state.load(context)
# Recover bone matrices and active action
for pb in rig_object.pose.bones:
pb.rotation_quaternion = quaternions[pb.name]
pb.rotation_euler = eulers[pb.name]
pb.scale = scales[pb.name]
pb.location = locations[pb.name]
if rig_object.animation_data:
rig_object.animation_data.action = ori_action
# Failed export objects
if any(failed_mesh_objects):
obj_names = ''
for i, obj in enumerate(failed_mesh_objects):
obj_names += obj.name
if i != len(failed_mesh_objects) - 1:
obj_names += ', '
self.report({'INFO'}, "INFO: Cannot export object [" + obj_names + "] because of reasons")
print('INFO:', 'Export finished at', '{:0.2f}'.format(time.time() - T), 'seconds!')
return {'FINISHED'}
class ToggleGodotRigifyOptions(bpy.types.Operator):
bl_idname = "scene.toggle_godot_rigify_options"
bl_label = "Toggle Godot Rigify Export Options"
bl_description = "Toggle Godot Rigify Export Options"
#bl_options = {'REGISTER', 'UNDO'}
prop : StringProperty(default='show_rig_export_options')
@classmethod
def poll(cls, context):
return True
def execute(self, context):
props = context.scene.gr_props
if self.prop not in dir(props) or not self.prop.startswith('show_'):
return {'CANCELLED'}
cur_value = getattr(props, self.prop)
setattr(props, self.prop, not cur_value)
return {'FINISHED'}
class GODOTHELPER_PT_RigifySkeletonPanel(bpy.types.Panel):
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
#bl_context = "objectmode"
bl_label = "Godot Skeleton"
bl_category = "Ucup Exporter"
def draw(self, context):
scene_props = context.scene.gr_props
c = self.layout.column(align=True)
r = c.row(align=True)
r.operator('export_mesh.rigify_gltf', text='Export as GLTF', icon='MOD_ARMATURE')
if scene_props.show_rig_export_options: r.alert = True
icon = 'PREFERENCES' # if is_greater_than_280() else 'SCRIPTWIN'
r.operator('scene.toggle_godot_rigify_options', text='', icon=icon).prop = 'show_rig_export_options'
if scene_props.show_rig_export_options:
box = c.box()
col = box.column(align=True)
use_export_meshes_visible = not ( scene_props.export_animations and
scene_props.export_only_selected_action)
#(scene_props.export_only_selected_action or scene_props.export_action_as_separated_gltf))
use_export_meshes = scene_props.export_meshes and use_export_meshes_visible
row = col.row()
row.active = use_export_meshes_visible
row.prop(scene_props, 'export_meshes')
col.prop(scene_props, 'export_animations')
row = col.row()
row.active = scene_props.export_animations
row.prop(scene_props, 'export_only_selected_action')
row = col.row()
row.active = scene_props.export_animations and not scene_props.export_only_selected_action
row.prop(scene_props, 'export_action_as_separated_gltf')
col = box.column(align=True)
col.active = use_export_meshes
#row = col.split(factor=0.4)
#row.label(text='Export Mode:')
#row.prop(scene_props, 'export_mode', text='')
col.prop(scene_props, 'apply_modifiers')
col.prop(scene_props, 'export_tangent')
#col.prop(scene_props, 'copy_images')
col.prop(scene_props, 'export_vcols')
row = col.row()
row.active = scene_props.export_vcols
row.prop(scene_props, 'only_export_baked_vcols')
col.prop(scene_props, 'ucupaint_baked_outside')
col = box.column(align=True)
row = col.split(factor=0.4)
row.label(text='Bone Parents:')
row.prop(scene_props, 'parental_mode', text='')
row = col.split(factor=0.4)
row.label(text='GLTF Format:')
row.prop(scene_props, 'gltf_format', text='')
col.prop(scene_props, 'remove_whitespaces')
# For object settings
subbox = box.box()
subcol = subbox.column(align=False)
obj = context.object
obj_props = obj.gr_props
obj_name = obj.name if obj else '-'
r = subcol.row()
rr = r.row(align=True)
if scene_props.show_object_export_options: rr.alert = True
icon = 'PREFERENCES'
rr.operator('scene.toggle_godot_rigify_options', text='', icon=icon).prop = 'show_object_export_options'
r.label(text='Object Settings (' + obj_name + ')')
if scene_props.show_object_export_options:
ssubbox = subcol.box()
ssubbox.prop(obj_props, 'add_clear_location_duplicate')
if obj and obj.mode == 'POSE':
active_bone = obj.data.bones.active
if active_bone:
pb = obj.pose.bones.get(active_bone.name)
def_bones = get_deform_bones(obj, pb.name)
r = subcol.row()
rr = r.row(align=True)
if scene_props.show_posebone_export_options: rr.alert = True
icon = 'PREFERENCES'
rr.operator('scene.toggle_godot_rigify_options', text='', icon=icon).prop = 'show_posebone_export_options'
r.label(text='Bone Settings (' + pb.name + ')')
if scene_props.show_posebone_export_options:
ssubbox = subcol.box()
ccol = ssubbox.column(align=True)
for db in def_bones:
title = 'Add Extra Copy Bone'
title += ' (' + db.name + ')'
ccol.prop(db.gr_props, 'add_extra_copy_bone', text=title)
if len(def_bones) == 0:
ccol.label(text='No related deform bones found!')
if obj and obj.mode == 'EDIT':
active_bone = obj.data.edit_bones.active
if active_bone:
eb = obj.data.edit_bones.get(active_bone.name)
def_bones = get_deform_edit_bones(obj, eb.name)
r = subcol.row()
rr = r.row(align=True)
if scene_props.show_editbone_export_options: rr.alert = True
icon = 'PREFERENCES'
rr.operator('scene.toggle_godot_rigify_options', text='', icon=icon).prop = 'show_editbone_export_options'
r.label(text='Bone Settings (' + eb.name + ')')
if scene_props.show_editbone_export_options:
ssubbox = subcol.box()
ccol = ssubbox.column(align=True)
for db in def_bones:
title = 'Force Keep Parent'
title += ' (' + db.name + ')'
ccol.prop(db.gr_props, 'keep_parent', text=title)
if len(def_bones) == 0:
ccol.label(text='No related deform bones found!')
#c = self.layout.column(align=True)
#r = c.row(align=True)