-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
advanced_ik.py
2383 lines (1865 loc) · 124 KB
/
advanced_ik.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
from math import radians
import bpy
from . import utils
from .armature_rename import armature_rename, bone_rename
from .utils import bone_convert, generate_armature, generate_shapekey_dict, update
def anim_armature(action):
satproperties = bpy.context.scene.satproperties
satinfo = bpy.context.scene.satinfo
def generate_rigify(action): #Creates Rigify armature and fills in all the Rigify parameters
#Armature creation
generate_armature('anim', action)
unit = satinfo.unit
#Creation
if action == 0:
armature = utils.arm.animation_armature
#Selects animation armature
update(1, armature)
#Hides all but the first layer
for i in [1,2,3,5,4,6,7]:
armature.data.layers[i] = False
#Checks how many spine bones there are for the spines.basic_spine Rigify parameter (At least 3 are required)
spines = 0
for container, bone in utils.arm.central_bones.items():
if container.count('spine'):
if bone:
spines += 1
#Creates 2 pelvis bones for whatever Rigify does with em
rigify_pelvis = ['Pelvis_L', 'Pelvis_R']
if spines > 2:
if utils.arm.central_bones['pelvis']:
prefix, bone = bone_convert(utils.arm.central_bones['pelvis'][0])
ppelvis = armature.pose.bones[prefix + bone]
epelvis = armature.data.edit_bones[prefix + bone]
for index, bone in enumerate(rigify_pelvis):
ebone = armature.data.edit_bones.new(bone)
ebone.head = epelvis.head
ebone.parent = epelvis
ebone.layers[3] = True
ebone.layers[0] = False
ebone.layers[8] = False
ebone.layers[9] = False
#New pelvis bone positioning
if satinfo.sbox:
ebone.tail.yz = ppelvis.head.y-10*unit, ppelvis.head.z+12*unit
else:
ebone.tail.yz = ppelvis.head.y-3*unit, ppelvis.head.z+4*unit
if index == 0:
if satinfo.sbox:
ebone.tail.x = ppelvis.head.x+10*unit
else:
ebone.tail.x = ppelvis.head.x+3.25*unit
elif index == 1:
if satinfo.sbox:
ebone.tail.x = ppelvis.head.x-10*unit
else:
ebone.tail.x = ppelvis.head.x-3.25*unit
rigify_palm = {}
if utils.arm.symmetrical_bones['fingers'].get('indexmeta') or utils.arm.symmetrical_bones['fingers'].get('middlemeta') or utils.arm.symmetrical_bones['fingers'].get('ringmeta'):
for container, bone in utils.arm.symmetrical_bones['fingers'].items():
if container.count('meta'):
for bone in bone:
if bone:
prefix, bone = bone_convert(bone)
pbone = armature.pose.bones[prefix + bone]
if container.count('indexmeta'):
pbone.rigify_type = 'limbs.super_palm'
else:
pbone.rigify_type = ''
elif utils.arm.symmetrical_bones['fingers'].get('fingercarpal'):
for bone in utils.arm.symmetrical_bones['fingers']['fingercarpal']:
if bone:
prefix, bone = bone_convert(bone)
pbone = armature.pose.bones[prefix + bone]
pbone.rigify_type = 'basic.super_copy'
pbone.rigify_parameters.super_copy_widget_type = 'bone'
#Disabled, more trouble than they're worth and rather purposeless anyways, only for S&Box armatures (Since they already have palm bones)
'''else:
#Creates multiple palm bones for fingers
rigify_palm = {'finger1': [], 'finger2': [], 'finger3': [], 'finger4': []}
#How many finger roots there are (There must be at least 2 for palm bones)
fingers = 0
for container, bone in utils.arm.symmetrical_bones['fingers'].items():
if container == 'finger1' or container == 'finger2' or container == 'finger3' or container == 'finger3' or container == 'finger4':
if bone:
fingers += 1
if fingers > 1:
for container, bone in utils.arm.symmetrical_bones['fingers'].items():
if container == 'finger1' or container == 'finger2' or container == 'finger3' or container == 'finger3' or container == 'finger4':
for index, bone in enumerate(bone):
if bone:
prefix, bone = bone_convert(bone)
if satinfo.scheme == 0 and not satinfo.sbox:
bone2 = bone_rename(1, bone, index)
palm = 'Palm_' + bone2
else:
palm = 'Palm_' + bone
efinger = armature.data.edit_bones[prefix + bone]
epalm = armature.data.edit_bones.new(palm)
rigify_palm[container].append(palm)
efinger.layers[5] = True
efinger.layers[0] = False
epalm.layers[5] = True
epalm.layers[0] = False
epalm.layers[8] = False
efinger.parent = epalm
if utils.arm.symmetrical_bones['arms']['hand'] and utils.arm.symmetrical_bones['arms']['hand'][index]:
prefix, bone = bone_convert(utils.arm.symmetrical_bones['arms']['hand'][index])
ehand = armature.data.edit_bones[prefix + bone]
epalm.parent = ehand
epalm.tail = efinger.head
epalm.head.xyz = ehand.head.x, epalm.tail.y, ehand.head.z'''
#Creates heels for easier leg tweaking
rigify_heel = ['Heel_L', 'Heel_R']
#Creates heel bone if none are present
rigify_toe = ['Toe_L', 'Toe_R']
if utils.arm.symmetrical_bones['legs']['foot']:
for index, bone in enumerate(utils.arm.symmetrical_bones['legs']['foot']):
prefix, bone = bone_convert(utils.arm.symmetrical_bones['legs']['foot'][index])
pfoot = armature.pose.bones[prefix + bone]
efoot = armature.data.edit_bones[prefix + bone]
ebone = armature.data.edit_bones.new(rigify_heel[index])
if index == 0:
if satinfo.goldsource:
ebone.head.xyz = efoot.head.x - 2*unit, efoot.head.y + 3*unit, 0
ebone.tail.xyz = efoot.head.x + 2*unit, efoot.head.y + 3*unit, 0
elif satinfo.sbox:
ebone.head.xyz = efoot.head.x - 2*unit, efoot.head.y + 5*unit, 0
ebone.tail.xyz = efoot.head.x + 2*unit, efoot.head.y + 5*unit, 0
else:
ebone.head.xyz = efoot.head.x - 2*unit, efoot.head.y + 2.4*unit, 0
ebone.tail.xyz = efoot.head.x + 2*unit, efoot.head.y + 2.4*unit, 0
elif index == 1:
if satinfo.goldsource:
ebone.head.xyz = efoot.head.x + 2*unit, efoot.head.y + 3*unit, 0
ebone.tail.xyz = efoot.head.x - 2*unit, efoot.head.y + 3*unit, 0
elif satinfo.sbox:
ebone.head.xyz = efoot.head.x + 2*unit, efoot.head.y + 5*unit, 0
ebone.tail.xyz = efoot.head.x - 2*unit, efoot.head.y + 5*unit, 0
else:
ebone.head.xyz = efoot.head.x + 2*unit, efoot.head.y + 2.4*unit, 0
ebone.tail.xyz = efoot.head.x - 2*unit, efoot.head.y + 2.4*unit, 0
ebone.parent = efoot
if index == 0:
ebone.layers[13] = True
elif index == 1:
ebone.layers[16] = True
ebone.layers[0] = False
ebone.layers[8] = False
ebone.layers[9] = False
if not utils.arm.symmetrical_bones['legs']['toe0']:
ebone = armature.data.edit_bones.new(rigify_toe[index])
ebone.head = pfoot.tail
if pfoot.tail.y < 0:
ebone.tail.xyz = pfoot.tail.x, pfoot.tail.y*1.25, pfoot.tail.z
elif pfoot.tail.y > 0:
ebone.tail.xyz = pfoot.tail.x, pfoot.tail.y*-1.25, pfoot.tail.z
ebone.parent = efoot
ebone.use_connect = True
if index == 0:
ebone.layers[13] = True
elif index == 1:
ebone.layers[16] = True
ebone.layers[0] = False
ebone.layers[8] = False
#Creates hand bones
rigify_hands = ['Hand_L', 'Hand_R']
if utils.arm.symmetrical_bones['arms']['forearm']:
for index, bone in enumerate(utils.arm.symmetrical_bones['arms']['forearm']):
prefix, bone = bone_convert(bone)
eforearm = armature.data.edit_bones[prefix + bone]
if not utils.arm.symmetrical_bones['arms']['hand'] or not utils.arm.symmetrical_bones['arms']['hand'][index]:
ebone = armature.data.edit_bones.new(rigify_hands[index])
ebone.head = eforearm.tail
length = eforearm.length
eforearm.length = eforearm.length*1.4
ebone.tail = eforearm.tail
eforearm.length = length
ebone.parent = eforearm
ebone.use_connect = True
#Creates camera target if armature is a viewmodel
if satinfo.viewmodel:
marked = False
if utils.arm.attachment_bones['viewmodel'].get('attach_camera'):
bone = utils.arm.attachment_bones['viewmodel']['attach_camera'][0]
marked = True
elif utils.arm.attachment_bones['viewmodel'].get('camera'):
bone = utils.arm.attachment_bones['viewmodel']['camera'][0]
marked = True
if marked:
prefix, bone = bone_convert(bone)
pcamera = armature.pose.bones[prefix + bone]
etarget = armature.data.edit_bones.new('Camera_Target')
prefix, bone = bone_convert(utils.arm.central_bones['pelvis'][0])
ppelvis = armature.pose.bones[prefix + bone]
etarget.head.xyz = pcamera.head.x, -ppelvis.head.z, pcamera.head.z
etarget.tail.xyz = etarget.head.x, etarget.head.y*0.5*unit, etarget.head.z
etarget.length = 2*unit
update(0)
#Parent and rigify parameters
if spines > 2:
#Rigify pelvis
if utils.arm.central_bones['pelvis']:
for bone in rigify_pelvis:
pbone = armature.pose.bones[bone]
pbone.rigify_type = 'basic.super_copy'
pbone.rigify_parameters.make_control = False
#Rigify palm
if rigify_palm:
for bone in rigify_palm['finger1']:
pbone = armature.pose.bones[bone]
pbone.rigify_type = 'limbs.super_palm'
#For reference:
#Face (Primary) = Layer 0
#Face (Secondary) = Layer 1
#Central bones + Clavicle = Layer 3
#Finger = Layer 5
#Left arm = Layer 7
#Right arm = Layer 10
#Left leg = Layer 13
#Right leg = Layer 16
#Symmetrical
for cat in utils.arm.symmetrical_bones.keys():
if cat == 'fingers':
for container, bone in utils.arm.symmetrical_bones[cat].items():
for bone in bone:
if bone:
prefix, bone = bone_convert(bone)
ebone = armature.data.edit_bones[prefix + bone]
pbone = armature.pose.bones[prefix + bone]
param = pbone.rigify_parameters
if container == 'finger0' or container == 'finger1' or container == 'finger2' or container == 'finger3' or container == 'finger4':
if ebone.children:
pbone.rigify_type = 'limbs.super_finger'
if utils.arm.symmetrical_bones['legs'].get('thighlow'):
if container == 'finger0':
param.primary_rotation_axis = 'Z'
else:
param.primary_rotation_axis = '-X'
elif satinfo.viewmodel and not satinfo.special_viewmodel:
if container == 'finger0':
param.primary_rotation_axis = '-Z'
else:
pbone.rigify_type = 'basic.super_copy'
param.tweak_layers[6] = True
param.tweak_layers[1] = False
ebone.layers[5] = True
ebone.layers[0] = False
ebone.layers[1] = False
ebone.layers[2] = False
else:
ebone.layers[5] = True
ebone.layers[0] = False
ebone.layers[1] = False
ebone.layers[2] = False
elif cat == 'arms':
for container, bone in utils.arm.symmetrical_bones[cat].items():
for index, bone in enumerate(bone):
if bone:
prefix, bone = bone_convert(bone)
ebone = armature.data.edit_bones[prefix + bone]
pbone = armature.pose.bones[prefix + bone]
param = pbone.rigify_parameters
if index == 0:
ebone.layers[7] = True
elif index == 1:
ebone.layers[10] = True
if container == 'clavicle':
pbone.rigify_type = 'basic.super_copy'
param.make_widget = False
if not satinfo.viewmodel:
ebone.layers[3] = True
elif container == 'upperarm':
pbone.rigify_type = 'limbs.super_limb'
param.tweak_layers[1] = False
param.fk_layers[1] = False
if index == 0:
param.fk_layers[8] = True
param.tweak_layers[9] = True
elif index == 1:
param.fk_layers[11] = True
param.tweak_layers[12] = True
param.segments = 1
ebone.layers[0] = False
ebone.layers[1] = False
ebone.layers[2] = False
elif cat == 'legs':
for container, bone in utils.arm.symmetrical_bones[cat].items():
for index, bone in enumerate(bone):
if bone:
prefix, bone = bone_convert(bone)
ebone = armature.data.edit_bones[prefix + bone]
pbone = armature.pose.bones[prefix + bone]
param = pbone.rigify_parameters
if index == 0:
ebone.layers[13] = True
elif index == 1:
ebone.layers[16] = True
if container == 'thigh':
if utils.arm.symmetrical_bones['legs']['calf'] and utils.arm.symmetrical_bones['legs']['foot']:
pbone.rigify_type = 'limbs.super_limb'
if utils.arm.symmetrical_bones['legs'].get('thighlow'):
param.limb_type = 'paw'
else:
param.limb_type = 'leg'
param.tweak_layers[1] = False
param.fk_layers[1] = False
else:
pbone.rigify_type = 'basic.copy_chain'
if index == 0:
param.fk_layers[14] = True
param.tweak_layers[15] = True
elif index == 1:
param.fk_layers[17] = True
param.tweak_layers[18] = True
param.segments = 1
elif container == 'hip':
pbone.rigify_type = 'basic.super_copy'
ebone.layers[0] = False
ebone.layers[3] = False
ebone.layers[4] = False
#Central
for container, bone in utils.arm.central_bones.items():
for bone in bone:
if bone:
prefix, bone = bone_convert(bone)
ebone = armature.data.edit_bones[prefix + bone]
pbone = armature.pose.bones[prefix + bone]
param = pbone.rigify_parameters
ebone.layers[3] = True
if not satinfo.viewmodel:
if container == 'pelvis':
if spines > 2:
pbone.rigify_type = 'spines.basic_spine'
param.pivot_pos = 2
param.tweak_layers[1] = False
param.tweak_layers[4] = True
param.fk_layers[1] = False
param.fk_layers[4] = True
else:
pbone.rigify_type = 'basic.copy_chain'
if container == 'neck':
if utils.arm.central_bones['head']:
pbone.rigify_type = 'spines.super_head'
if utils.arm.central_bones['pelvis']:
param.connect_chain = True
param.tweak_layers[1] = False
param.tweak_layers[4] = True
else:
pbone.rigify_type = 'basic.super_copy'
ebone.layers[0] = False
for cat in utils.arm.helper_bones.keys():
for container, bone in utils.arm.helper_bones[cat].items():
for bone in bone:
if bone:
prefix, bone = bone_convert(bone)
ebone = armature.data.edit_bones[prefix + bone]
pbone = armature.pose.bones[prefix + bone]
param = pbone.rigify_parameters
ebone.layers[28] = True
ebone.layers[0] = False
ebone.layers[5] = False
pbone.rigify_type = 'basic.super_copy'
param.super_copy_widget_type = 'bone'
for cat in utils.arm.attachment_bones.keys():
for container, bone in utils.arm.attachment_bones[cat].items():
for bone in bone:
if bone:
prefix, bone = bone_convert(bone)
ebone = armature.data.edit_bones[prefix + bone]
pbone = armature.pose.bones[prefix + bone]
param = pbone.rigify_parameters
if cat == 'weapon':
ebone.layers[20] = True
ebone.layers[0] = False
ebone.layers[7] = False
pbone.rigify_type = 'basic.super_copy'
param.super_copy_widget_type = 'bone'
elif cat == 'attachment':
ebone.layers[19] = True
ebone.layers[0] = False
ebone.layers[6] = False
ebone.layers[7] = False
pbone.rigify_type = 'basic.super_copy'
param.super_copy_widget_type = 'bone'
marked = False
if satinfo.viewmodel:
if utils.arm.attachment_bones['viewmodel'].get('attach_camera'):
bone = utils.arm.attachment_bones['viewmodel']['attach_camera'][0]
marked = True
elif utils.arm.attachment_bones['viewmodel'].get('camera'):
bone = utils.arm.attachment_bones['viewmodel']['camera'][0]
marked = True
if marked:
prefix, bone = bone_convert(bone)
pbone = armature.pose.bones[prefix + bone]
param = pbone.rigify_parameters
ebone = armature.data.edit_bones[prefix + bone]
ebone.layers[24] = True
ebone.layers[0] = False
ebone.layers[8] = False
pbone.rigify_type = 'basic.super_copy'
param.super_copy_widget_type = 'bone'
etarget = armature.data.edit_bones['Camera_Target']
ptarget = armature.pose.bones['Camera_Target']
param = ptarget.rigify_parameters
ptarget.rigify_type = 'basic.raw_copy'
param.optional_widget_type = 'circle'
ptarget.lock_location[1] = True
ptarget.lock_rotation[0] = True
ptarget.lock_rotation[2] = True
etarget.layers[24] = True
etarget.layers[0] = False
etarget.layers[7] = False
etarget.layers[8] = False
#Custom bones
for container, bone in utils.arm.custom_bones.items():
for bone in bone:
if bone:
if bone.count('eye') or bone.count('lid_upper') or bone.count('lid_lower'):
continue
prefix, bone2 = bone_convert(bone)
ebone = armature.data.edit_bones[prefix + bone2]
pbone = armature.pose.bones[prefix + bone2]
param = pbone.rigify_parameters
ebone.layers[21] = True
ebone.layers[0] = False
ebone.layers[9] = False
if utils.arm.chain_start.count(bone):
pbone.rigify_type = 'basic.copy_chain'
param.super_copy_widget_type = 'bone'
elif utils.arm.chainless_bones.count(bone):
pbone.rigify_type = 'basic.super_copy'
param.super_copy_widget_type = 'bone'
armature = utils.arm.animation_armature_real
#Creates bone groups
for group in ['Root', 'IK', 'Special', 'Tweak', 'FK', 'Extra']:
color = armature.rigify_colors.add()
color.name = group
armature.rigify_colors[group].select = (0.3140000104904175, 0.7839999794960022, 1.0)
armature.rigify_colors[group].active = (0.5490000247955322, 1.0, 1.0)
armature.rigify_colors[group].standard_colors_lock = True
if group == 'Root':
armature.rigify_colors[group].normal = (0.43529415130615234, 0.18431372940540314, 0.41568630933761597)
if group == 'IK':
armature.rigify_colors[group].normal = (0.6039215922355652, 0.0, 0.0)
if group== 'Special':
armature.rigify_colors[group].normal = (0.9568628072738647, 0.7882353663444519, 0.0470588281750679)
if group== 'Tweak':
armature.rigify_colors[group].normal = (0.03921568766236305, 0.21176472306251526, 0.5803921818733215)
if group== 'FK':
armature.rigify_colors[group].normal = (0.11764706671237946, 0.5686274766921997, 0.03529411926865578)
if group== 'Extra':
armature.rigify_colors[group].normal = (0.9686275124549866, 0.250980406999588, 0.0941176563501358)
#Creates layers
for i in range(29):
armature.rigify_layers.add()
#Rigify layers
names = ['Face', 'Face (Primary)','Face (Secondary)','Torso', 'Torso (Tweak)', 'Fingers', 'Fingers (Detail)', 'Arm.L (IK)', 'Arm.L (FK)', 'Arm.L (Tweak)', 'Arm.R (IK)', 'Arm.R (FK)', 'Arm.R (Tweak)', 'Leg.L (IK)', 'Leg.L (FK)', 'Leg.L (Tweak)', 'Leg.R (IK)', 'Leg.R (FK)', 'Leg.R (Tweak)', 'Attachments', 'Weapon', 'Custom (FK)', 'Custom (IK)', 'Custom (Tweak)', 'Others']
row_groups = [1,2,2,3,4,5,6,7,8,9,7,8,9,10,11,12,10,11,12,13,13,14,14,15,15]
layer_groups = [5,2,3,3,4,6,5,2,5,4,2,5,4,2,5,4,2,5,4,6,6,5,2,4,6]
for i, name, row, group in zip(range(25), names, row_groups, layer_groups):
armature.rigify_layers[i].name = name
armature.rigify_layers[i].row = row
armature.rigify_layers[i]['group_prop'] = group
armature.rigify_layers[28].name = 'Root'
armature.rigify_layers[28].row = 14
armature.rigify_layers[28]['group_prop'] = 1
for i in range(0, 32):
armature.layers[i] = False
for i in [1,2,3,5,7,10,13,16,19,20,21,22]:
armature.layers[i] = True
bpy.ops.object.mode_set(mode='OBJECT')
#Renames armature to allow it being compatible with pose symmetry
if satinfo.scheme == 0 and not satinfo.sbox:
armature_rename(1, utils.arm.animation_armature)
print("Animation armature created!")
elif action == 1:
print("Animation armature deleted")
def face_flex_setup(): #Sets up drivers for face flexes that will be controlled by face bones
unit = satinfo.unit
armature = utils.arm.animation_armature
armature['target_object'] = None
armature['material_eyes'] = False
armature['has_shapekeys'] = False
bpy.ops.object.mode_set(mode='EDIT')
#Shapekey drivers
if satproperties.target_object:
armature['target_object'] = satproperties.target_object
armature['has_shapekeys'] = True
satproperties.target_object = None
target_object = armature['target_object']
try:
shapekeys_raw = target_object.data.shape_keys.key_blocks.keys()
except:
shapekeys_raw = None
print("No shape keys detected")
utils.arm.facial_bones = []
utils.arm.unused_shapekeys = ['AU6L+AU6R', 'AU25L+AU25R', 'AU22L+AU22R', 'AU20L+AU20R', 'AU18L+AU18R', 'AU26ZL+AU26ZR', 'AU12AU25L+AU12AU25R', 'upper_right', 'upper_right.001', 'lower_right', 'lower_right.001', 'upper_left', 'upper_left.001', 'lower_left', 'lower_left.001']
utils.arm.shapekeys = {'basis': {'basis': ''}, 'eyebrows': {'inner_eyebrow_raise': '', 'outer_eyebrow_raise': '', 'eyebrow_drop': '', 'eyebrow_raise': '', 'outer_eyebrow_drop': '', 'inner_eyebrow_drop': ''}, 'eyes': {'upper_eyelid_close': '', 'upper_eyelid_raise': '', 'lower_eyelid_drop': '', 'lower_eyelid_raise': '', 'upper_eyelid_drop': ''}, 'cheek': {'squint': '', 'cheek_puff': ''}, 'nose': {'nose_wrinkler': '', 'breath': ''}, 'mouth': {'smile': '', 'frown': '', 'upper_lip_raise': '', 'lower_lip_raise': '', 'lower_lip_drop': '', 'bite': '', 'tightener': '', 'puckerer': '', 'light_puckerer': '', 'mouth_left': '', 'mouth_right': ''}, 'chin': {'chin_clench': '', 'light_chin_drop': '', 'medium_chin_drop': '', 'full_chin_drop': '', 'chin_left': '', 'chin_right': '', 'chin_raise': ''}}
if shapekeys_raw:
object_data = target_object.data.copy()
object_data.name = target_object.data.name + '.anim'
object_data['original_data'] = target_object.data
target_object.data = object_data
utils.arm.shapekeys = generate_shapekey_dict(utils.arm.shapekeys, shapekeys_raw)
#Generates widgets for easier representation of every driver bone
create_widgets()
#Checks to make sure bones aren't repeated
eyebrows = False
eyes = False
cheek = False
nose = False
mouth = False
lower_lip = False
upper_lip = False
middle_lip = False
chin = False
## Bone creation ##
for cat in utils.arm.shapekeys.keys():
for container, shapekey in utils.arm.shapekeys[cat].items():
if cat == 'eyebrows':
if container:
if not eyebrows:
eyebrows = True
#Inner, outer and full eyebrows
for bone in ['Eyebrow_L', 'Eyebrow_R', 'Inner_Eyebrow_L', 'Inner_Eyebrow_R', 'Outer_Eyebrow_L', 'Outer_Eyebrow_R']:
utils.arm.facial_bones.append(bone)
ebone = armature.data.edit_bones.new(bone)
ebone.use_deform = False
if bone == 'Eyebrow_L':
ebone.head.xyz = 1.18783*unit, -4.17032*unit, 68.5886*unit
elif bone == 'Eyebrow_R':
ebone.head.xyz = -1.18783*unit, -4.17032*unit, 68.5886*unit
elif bone == 'Inner_Eyebrow_L':
ebone.head.xyz = 0.574764*unit, -4.17032*unit, 68.3012*unit
elif bone == 'Inner_Eyebrow_R':
ebone.head.xyz = -0.574764*unit, -4.17032*unit, 68.3012*unit
elif bone == 'Outer_Eyebrow_L':
ebone.head.xyz = 1.82008*unit, -4.17032*unit, 68.3012*unit
elif bone == 'Outer_Eyebrow_R':
ebone.head.xyz = -1.82008*unit, -4.17032*unit, 68.3012*unit
ebone.tail.xyz = ebone.head.x, ebone.head.y + 0.5*unit, ebone.head.z
elif cat == 'eyes':
if container:
if not eyes:
eyes = True
#Upper and lower eyelids
for bone in ['UpperEye_L', 'UpperEye_R', 'LowerEye_L', 'LowerEye_R']:
utils.arm.facial_bones.append(bone)
ebone = armature.data.edit_bones.new(bone)
ebone.use_deform = False
if bone == 'UpperEye_L':
ebone.head.xyz = 1.18783*unit, -3.53386*unit, 68.0906*unit
elif bone == 'UpperEye_R':
ebone.head.xyz = -1.18783*unit, -3.53386*unit, 68.0906*unit
elif bone == 'LowerEye_L':
ebone.head.xyz = 1.18783*unit, -3.53386*unit, 67.5157*unit
elif bone == 'LowerEye_R':
ebone.head.xyz = -1.18783*unit, -3.53386*unit, 67.5157*unit
ebone.tail.xyz = ebone.head.x, ebone.head.y + 0.5*unit, ebone.head.z
elif cat == 'cheek':
if container:
if not cheek:
cheek = True
#Cheeks for cheek_puffing and squinting
for bone in ['Cheek_L', 'Cheek_R']:
utils.arm.facial_bones.append(bone)
ebone = armature.data.edit_bones.new(bone)
ebone.use_deform = False
if bone == 'Cheek_L':
ebone.head.xyz = 1.91587*unit, -3.25701*unit, 65.6189*unit
ebone.tail.xyz = 1.76452*unit, -2.78046*unit, ebone.head.z
elif bone == 'Cheek_R':
ebone.head.xyz = -1.91587*unit, -3.25701*unit, 65.6189*unit
ebone.tail.xyz = -1.76452*unit, -2.78046*unit, ebone.head.z
ebone.length = 0.5*unit
elif cat == 'nose':
if container:
if not nose:
nose = True
#Nostrils
for bone in ['Nostril_L', 'Nostril_R']:
utils.arm.facial_bones.append(bone)
ebone = armature.data.edit_bones.new(bone)
ebone.use_deform = False
if bone == 'Nostril_L':
ebone.head.xyz = 0.766339*unit, -3.92756*unit, 65.6*unit
elif bone == 'Nostril_R':
ebone.head.xyz = -0.766339*unit, -3.92756*unit, 65.6*unit
ebone.tail.xyz = ebone.head.x, ebone.head.y + 0.5*unit, ebone.head.z
if cat == 'mouth':
if container:
if not mouth:
#Mouth corners
if container == 'smile' or container == 'frown' or container == 'tightener' or container == 'puckerer' or container == 'light_puckerer':
mouth = True
for bone in ['MouthCorner_L', 'MouthCorner_R']:
utils.arm.facial_bones.append(bone)
ebone = armature.data.edit_bones.new(bone)
ebone.use_deform = False
if bone == 'MouthCorner_L':
ebone.head.xyz = 1.20563*unit, -3.80961*unit, 64.8528*unit
ebone.tail.xyz = 0.976012*unit, -3.36545*unit, ebone.head.z
elif bone == 'MouthCorner_R':
ebone.head.xyz = -1.20563*unit, -3.80961*unit, 64.8528*unit
ebone.tail.xyz = -0.976012*unit, -3.36545*unit, ebone.head.z
ebone.length = 0.5*unit
elif not upper_lip:
#Upper lip
if container == 'upper_lip_raise':
upper_lip = True
for bone in ['UpperLip_L', 'UpperLip_R']:
utils.arm.facial_bones.append(bone)
ebone = armature.data.edit_bones.new(bone)
ebone.use_deform = False
if bone == 'UpperLip_L':
ebone.head.xyz = 0.459803*unit, -4.21496*unit, 65.1402*unit
elif bone == 'UpperLip_R':
ebone.head.xyz = -0.459803*unit, -4.21496*unit, 65.1402*unit
ebone.tail.xyz = ebone.head.x, ebone.head.y + 0.5*unit, ebone.head.z
elif not lower_lip:
#Lower lip
if container == 'lower_lip_raise' or container == 'lower_lip_drop' or container == 'bite':
lower_lip = True
for bone in ['LowerLip_L', 'LowerLip_R']:
utils.arm.facial_bones.append(bone)
ebone = armature.data.edit_bones.new(bone)
ebone.use_deform = False
if bone == 'LowerLip_L':
ebone.head.xyz = 0.459803*unit, -4.13831*unit, 64.5654*unit
elif bone == 'LowerLip_R':
ebone.head.xyz = -0.459803*unit, -4.13831*unit, 64.5654*unit
ebone.tail.xyz = ebone.head.x, ebone.head.y + 0.5*unit, ebone.head.z
elif not middle_lip:
#Middle lip
if container == 'mouth_left' or container == 'mouth_right':
middle_lip = True
for bone in ['MiddleLip']:
utils.arm.facial_bones.append(bone)
ebone = armature.data.edit_bones.new(bone)
ebone.use_deform = False
ebone.head.xyz = 0, -4.40654*unit, 64.8528*unit
ebone.tail.xyz = ebone.head.x, ebone.head.y + 0.5*unit, ebone.head.z
if cat == 'chin':
if container:
if not chin:
chin = True
for bone in ['Chin']:
utils.arm.facial_bones.append(bone)
ebone = armature.data.edit_bones.new(bone)
ebone.use_deform = False
ebone.head.xyz = 0, -4.31075*unit, 62.8409*unit
ebone.tail.xyz = ebone.head.x, -3.83612*unit, 62.9982*unit
ebone.length = 0.5*unit
## Linking ##
keyblocks = target_object.data.shape_keys.key_blocks
#Vertex group creation
left_group = target_object.vertex_groups.new(name='Left')
right_group = target_object.vertex_groups.new(name='Right')
#Left side
for vertex in target_object.data.vertices:
#Left side
if vertex.co[0] > 0.005*unit:
left_group.add([vertex.index], 1, 'REPLACE')
#Right side
elif vertex.co[0] < -0.005*unit:
right_group.add([vertex.index], 1, 'REPLACE')
elif vertex.co[0] < 0.005*unit and vertex.co[0] > -0.005*unit:
left_group.add([vertex.index], 0.75, 'REPLACE')
right_group.add([vertex.index], 0.75, 'REPLACE')
#Divides old shapekeys from generated ones
target_object.shape_key_add(name='----------', from_mix=False)
target_object.show_only_shape_key = False
utils.arm.rigify_shapekeys = {'basis': {'basis': ''}, 'eyebrows': {'inner_eyebrow_raise': [], 'outer_eyebrow_raise': [], 'eyebrow_drop': [], 'eyebrow_raise': [], 'outer_eyebrow_drop': [], 'inner_eyebrow_drop': []}, 'eyes': {'upper_eyelid_close': [], 'upper_eyelid_raise': [], 'lower_eyelid_drop': [], 'lower_eyelid_raise': [], 'upper_eyelid_drop': []}, 'cheek': {'squint': [], 'cheek_puff': []}, 'nose': {'nose_wrinkler': [], 'breath': []}, 'mouth': {'smile': [], 'frown': [], 'upper_lip_raise': [], 'lower_lip_raise': [], 'lower_lip_drop': [], 'bite': [], 'tightener': [], 'puckerer': [], 'light_puckerer': [], 'mouth_left': [], 'mouth_right': []}, 'chin': {'chin_clench': [], 'light_chin_drop': [], 'medium_chin_drop': [], 'full_chin_drop': [], 'chin_left': [], 'chin_right': [], 'chin_raise': []}}
for cat in utils.arm.shapekeys.keys():
for container, shapekey in utils.arm.shapekeys[cat].items():
if shapekey:
#Makes sure no other shapekey is active
if container != 'basis':
keyblocks[shapekey].value = 0
#Appends central shapekeys, since they don't need L/R versions of them
if container == 'chin_raise' or container == 'light_chin_drop' or container == 'medium_chin_drop' or container == 'full_chin_drop' or container == 'chin_left' or container == 'chin_right' or container == 'light_puckerer' or container == 'mouth_left' or container == 'mouth_right':
utils.arm.rigify_shapekeys[cat][container].append(shapekey)
continue
if container != 'basis':
keyblocks[shapekey].value = 1
left_shapekey = target_object.shape_key_add(name=shapekey + '_L', from_mix=True)
right_shapekey = target_object.shape_key_add(name=shapekey + '_R', from_mix=True)
utils.arm.rigify_shapekeys[cat][container].append(left_shapekey.name)
utils.arm.rigify_shapekeys[cat][container].append(right_shapekey.name)
#Assigns shapekeys to group
left_shapekey.vertex_group = left_group.name
right_shapekey.vertex_group = right_group.name
keyblocks[shapekey].value = 0
#Removes single shapekeys as well as unused shapekeys
for container, shapekey in utils.arm.shapekeys[cat].items():
if shapekey:
if container == 'basis' or container == 'chin_raise' or container == 'light_chin_drop' or container == 'medium_chin_drop' or container == 'full_chin_drop' or container == 'chin_left' or container == 'chin_right' or container == 'light_puckerer' or container == 'mouth_left' or container == 'mouth_right':
continue
else:
shapekey = target_object.data.shape_keys.key_blocks[shapekey]
target_object.shape_key_remove(shapekey)
for shapekey in utils.arm.unused_shapekeys:
try:
shapekey = target_object.data.shape_keys.key_blocks[shapekey]
target_object.shape_key_remove(shapekey)
except:
pass
del utils.arm.shapekeys
del utils.arm.unused_shapekeys
utils.arm.eye_left = ''
utils.arm.eye_right = ''
## Material eyes ##
for material in target_object.data.materials:
if material.name.title().count('Eyeball'):
armature['material_eyes'] = True
name = material.name
edriver = armature.data.edit_bones.new('driver_' + name)
edriver.use_deform = False
if name.title().count('L_') or name.title().count('_L'):
edriver.head.xyz = 1.18783*unit, -15*unit, 67.8032*unit
elif name.title().count('R_') or name.title().count('_R'):
edriver.head.xyz = -1.18783*unit, -15*unit, 67.8032*unit
edriver.tail.xyz = edriver.head.x, edriver.head.y+0.5*unit, edriver.head.z
if utils.arm.central_bones['head']:
prefix, bone = bone_convert(utils.arm.central_bones['head'][0])
edriver.parent = armature.data.edit_bones[prefix + bone]
edriver.layers[1] = True
edriver.layers[0] = False
edriver.layers[8] = False
edriver.layers[9] = False
update(0)
pdriver = armature.pose.bones['driver_' + name]
param = pdriver.rigify_parameters
#Locks rotation and scale since they aren't meant to be used
pdriver.lock_location = False, True, False
pdriver.lock_rotation_w = True
pdriver.lock_rotation = True, True, True
pdriver.lock_scale = True, True, True
pdriver.custom_shape_scale = 3
param.optional_widget_type = 'circle'
pdriver.rigify_type = 'basic.raw_copy'
eye_texture = False
if not material.use_nodes:
material.use_nodes = True
link = material.node_tree.links
node = material.node_tree.nodes
try:
imgtexture = node['Image Texture']
output_loc = imgtexture.location
eye_texture = True
except:
try:
output_loc = node['Material Output'].location
except:
output_loc = (0,0)
#Checks if mapping node already exists
try:
mapping = node['SAT Eye Movement']
except:
mapping = node.new('ShaderNodeMapping')
mapping.name = "SAT Eye Movement"
mapping.width = 315 #So all the label is visible
if eye_texture:
mapping.location = output_loc[0] - 400, output_loc[1]
else:
mapping.location = output_loc[0], output_loc[1] + 420
mapping.label = "Connect to iris(+Normal/Specular) texture's vector input"
#Checks if texture coordinates node already exists
try:
texcoord = node['SAT Eye Movement Origin']
except:
texcoord = node.new('ShaderNodeTexCoord')
texcoord.name = "SAT Eye Movement Origin"
texcoord.location = mapping.location[0] - 200, mapping.location[1]
if not texcoord.outputs['UV'].links:
link.new(texcoord.outputs['UV'], mapping.inputs['Vector'])
if eye_texture:
if not mapping.outputs['Vector'].links:
link.new(mapping.outputs['Vector'], imgtexture.inputs['Vector'])
imgtexture.extension = 'EXTEND'
#Driver portion
driver = mapping.inputs['Location'].driver_add('default_value')
if not driver[0].driver.variables:
variable = driver[0].driver.variables.new() #Creates new variable onto the shapekey
else:
variable = driver[0].driver.variables[0]
variable.name = "eye_x"
driver[0].driver.expression = variable.name #Changes expression to created variable's name
variable.type = 'TRANSFORMS' #Changes type of variable to transform
target = variable.targets[0]
target.id = utils.arm.animation_armature
target.transform_space = 'LOCAL_SPACE'
target.transform_type = 'LOC_X'
target.bone_target = 'driver_' + material.name
if material.name.title().count('L_') or material.name.title().count('_L'):
driver[0].modifiers[0].coefficients[1] = -0.25/unit
utils.arm.eye_left = 'driver_' + material.name
elif material.name.title().count('R_') or material.name.title().count('_R'):
driver[0].modifiers[0].coefficients[1] = 0.25/unit