-
Notifications
You must be signed in to change notification settings - Fork 2
/
rigify.py
1219 lines (1040 loc) · 37.9 KB
/
rigify.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) 2016-2019, Thomas Larsson
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are those
# of the authors and should not be interpreted as representing official policies,
# either expressed or implied, of the FreeBSD Project.
"""
Abstract
Postprocessing of rigify rig
"""
import bpy
import os
from collections import OrderedDict
from bpy.props import *
from mathutils import Vector
from .error import DazError, handleDazError
from .utils import *
R_FACE = 1
R_DEFORM = 29
R_HELP = 30
def setupTables(meta):
global MetaBones, MetaParents, MetaDisconnect, RigifyParams
global RigifySkeleton, GenesisCarpals, GenesisSpine
global Genesis3Spine, Genesis3Mergers, Genesis3Parents
global Genesis3Toes, Genesis3Renames, Carpals
global DeformBones
if meta.DazPre278:
hips = "hips"
spine = "spine"
spine1 = "spine-1"
chest = "chest"
chest1 = "chest-1"
neck = "neck"
head = "head"
rigtype = "rigify"
MetaBones = {
"spine" : spine,
"spine-1" : spine1,
"chest" : chest,
"chest-1" : chest1,
"chestUpper" : chest1,
"neck" : neck,
"head" : head,
}
RigifyParams = {}
DeformBones = {
"neckLower" : "DEF-neck",
"neckUpper" : "DEF-neck",
"ShldrBend" : "DEF-upper_arm.01.%s",
"ForearmBend" : "DEF-forearm.01.%s",
"ThighBend" : "DEF-thigh.01.%s",
"ShldrTwist" : "DEF-upper_arm.02.%s",
"ForearmTwist" : "DEF-forearm.02.%s",
"ThighTwist" : "DEF-thigh.02.%s",
"Shin" : "DEF-shin.02.%s",
}
else:
hips = "spine"
spine = "spine.001"
spine1 = "spine.002"
chest = "spine.003"
chest1 = "spine.004"
neck = "spine.005"
if meta.DazUseSplitNeck:
neck1= "spine.006"
head = "spine.007"
else:
head = "spine.006"
rigtype = "rigify2"
bpy.ops.object.mode_set(mode='EDIT')
eb = meta.data.edit_bones[head]
deleteChildren(eb, meta)
bpy.ops.object.mode_set(mode='OBJECT')
MetaBones = {
"spine" : hips,
"spine-1" : spine1,
"chest" : chest,
"chest-1" : chest1,
"chestUpper" : chest1,
"neck" : neck,
"head" : head,
}
RigifyParams = {
("spine", "neck_pos", 6),
("spine", "pivot_pos", 1),
}
DeformBones = {
"neckLower" : "DEF-spine.005",
"neckUpper" : "DEF-spine.006",
"ShldrBend" : "DEF-upper_arm.%s",
"ForearmBend" : "DEF-forearm.%s",
"ThighBend" : "DEF-thigh.%s",
"ShldrTwist" : "DEF-upper_arm.%s.001",
"ForearmTwist" : "DEF-forearm.%s.001",
"ThighTwist" : "DEF-thigh.%s.001",
"Shin" : "DEF-shin.%s.001",
}
MetaDisconnect = [hips, neck]
MetaParents = {
"breast.L" : chest,
"breast.R" : chest,
"shoulder.L" : chest1,
"shoulder.R" : chest1,
}
RigifySkeleton = {
hips : ("hip", ["hip", "pelvis"]),
"thigh.L" : "lThigh",
"shin.L" : "lShin",
"foot.L" : "lFoot",
"toe.L" : "lToe",
"thigh.R" : "rThigh",
"shin.R" : "rShin",
"foot.R" : "rFoot",
"toe.R" : "rToe",
"abdomen" : "abdomen",
"chest" : "chest",
"neck" : "neck",
"head" : "head",
"shoulder.L" : "lCollar",
"upper_arm.L" : "lShldr",
"forearm.L" : "lForeArm",
"hand.L" : "lHand",
"shoulder.R" : "rCollar",
"upper_arm.R" : "rShldr",
"forearm.R" : "rForeArm",
"hand.R" : "rHand",
"thumb.01.L" : "lThumb1",
"thumb.02.L" : "lThumb2",
"thumb.03.L" : "lThumb3",
"f_index.01.L" : "lIndex1",
"f_index.02.L" : "lIndex2",
"f_index.03.L" : "lIndex3",
"f_middle.01.L" : "lMid1",
"f_middle.02.L" : "lMid2",
"f_middle.03.L" : "lMid3",
"f_ring.01.L" : "lRing1",
"f_ring.02.L" : "lRing2",
"f_ring.03.L" : "lRing3",
"f_pinky.01.L" : "lPinky1",
"f_pinky.02.L" : "lPinky2",
"f_pinky.03.L" : "lPinky3",
"thumb.01.R" : "rThumb1",
"thumb.02.R" : "rThumb2",
"thumb.03.R" : "rThumb3",
"f_index.01.R" : "rIndex1",
"f_index.02.R" : "rIndex2",
"f_index.03.R" : "rIndex3",
"f_middle.01.R" : "rMid1",
"f_middle.02.R" : "rMid2",
"f_middle.03.R" : "rMid3",
"f_ring.01.R" : "rRing1",
"f_ring.02.R" : "rRing2",
"f_ring.03.R" : "rRing3",
"f_pinky.01.R" : "rPinky1",
"f_pinky.02.R" : "rPinky2",
"f_pinky.03.R" : "rPinky3",
"palm.01.L" : "lCarpal1",
"palm.02.L" : "lCarpal2",
"palm.03.L" : "lCarpal3",
"palm.04.L" : "lCarpal4",
"palm.01.R" : "rCarpal1",
"palm.02.R" : "rCarpal2",
"palm.03.R" : "rCarpal3",
"palm.04.R" : "rCarpal4",
}
BreastBones = {
"breast.L" : "lPectoral",
"breast.R" : "rPectoral",
#"breastDrv.L" : "lPectoralDrv",
#"breastDrv.R" : "rPectoralDrv",
}
if meta.DazUseBreasts:
RigifySkeleton = addDicts([RigifySkeleton, BreastBones])
GenesisCarpals = {
"palm.01.L" : (("lCarpal1", "lIndex1"), ["lCarpal1"]),
"palm.02.L" : (("lCarpal1", "lMid1"), []),
"palm.03.L" : (("lCarpal2", "lRing1"), ["lCarpal2"]),
"palm.04.L" : (("lCarpal2", "lPinky1"), []),
"palm.01.R" : (("rCarpal1", "rIndex1"), ["rCarpal1"]),
"palm.02.R" : (("rCarpal1", "rMid1"), []),
"palm.03.R" : (("rCarpal2", "rRing1"), ["rCarpal2"]),
"palm.04.R" : (("rCarpal2", "rPinky1"), []),
}
GenesisSpine = [
("abdomen", spine, hips),
("abdomen2", spine1, spine),
("chest", chest, spine1),
("neck", neck, chest),
("head", head, neck),
]
Genesis3Spine = [
("abdomen", spine, hips),
("abdomen2", spine1, spine),
("chest", chest, spine1),
("chestUpper", chest1, chest),
("neck", neck, chest1),
]
if meta.DazUseSplitNeck:
Genesis3Spine += [
("neckUpper", neck1, neck),
("head", head, neck1)]
else:
Genesis3Spine.append(("head", head, neck))
Genesis3Mergers = {
"lShldrBend" : ["lShldrTwist"],
"lForearmBend" : ["lForearmTwist"],
"lThighBend" : ["lThighTwist"],
"lFoot" : ["lMetatarsals"],
"rShldrBend" : ["rShldrTwist"],
"rForearmBend" : ["rForearmTwist"],
"rThighBend" : ["rThighTwist"],
"rFoot" : ["rMetatarsals"],
}
if not meta.DazUseSplitNeck:
Genesis3Mergers["neckLower"] = ["neckUpper"]
Genesis3Parents = {
"neckLower" : "chestUpper",
"chestUpper" : "chestLower",
"chestLower" : "abdomenUpper",
"abdomenUpper" : "abdomenLower",
"lForearmBend" : "lShldrBend",
"lHand" : "lForearmBend",
"lShin" : "lThighBend",
"lToe" : "lFoot",
"rForearmBend" : "rShldrBend",
"rHand" : "rForearmBend",
"rShin" : "rThighBend",
"rToe" : "rFoot",
}
if meta.DazUseSplitNeck:
Genesis3Parents["head"] = "neckUpper"
Genesis3Parents["neckUpper"] = "neckLower"
else:
Genesis3Parents["head"] = "neckLower"
Genesis3Toes = {
"lBigToe" : "lToe",
"lSmallToe1" : "lToe",
"lSmallToe2" : "lToe",
"lSmallToe3" : "lToe",
"lSmallToe4" : "lToe",
"rBigToe" : "rToe",
"rSmallToe1" : "rToe",
"rSmallToe2" : "rToe",
"rSmallToe3" : "rToe",
"rSmallToe4" : "rToe",
}
Genesis3Renames = {
"abdomenLower" : "abdomen",
"abdomenUpper" : "abdomen2",
"chestLower" : "chest",
"neckLower" : "neck",
"lShldrBend" : "lShldr",
"lForearmBend" : "lForeArm",
"lThighBend" : "lThigh",
"rShldrBend" : "rShldr",
"rForearmBend" : "rForeArm",
"rThighBend" : "rThigh",
}
return rigtype, hips, head
Carpals = {
"Carpal1" : "Index1",
"Carpal2" : "Mid1",
"Carpal3" : "Ring1",
"Carpal4" : "Pinky1",
}
def deleteChildren(eb, meta):
for child in eb.children:
deleteChildren(child, meta)
meta.data.edit_bones.remove(child)
def renameBones(rig, bones):
bpy.ops.object.mode_set(mode='EDIT')
for dname,rname in bones.items():
eb = rig.data.edit_bones[dname]
eb.name = rname
bpy.ops.object.mode_set(mode='OBJECT')
class DazBone:
def __init__(self, eb):
self.name = eb.name
self.head = eb.head.copy()
self.tail = eb.tail.copy()
self.roll = eb.roll
if eb.parent:
self.parent = eb.parent.name
else:
self.parent = None
self.use_deform = eb.use_deform
self.rotation_mode = None
def getPose(self, pb):
self.rotation_mode = pb.rotation_mode
self.lock_location = pb.lock_location
self.lock_rotation = pb.lock_rotation
def setPose(self, pb):
pb.rotation_mode = self.rotation_mode
pb.lock_location = self.lock_location
pb.lock_rotation = self.lock_rotation
def addDicts(structs):
joined = {}
for struct in structs:
for key,value in struct.items():
joined[key] = value
return joined
def setupDazSkeleton(meta):
rigifySkel = RigifySkeleton
if meta.DazRigifyType in ["genesis1", "genesis2"]:
rigifySkel["chestUpper"] = "chestUpper"
rigifySkel["abdomen2"] = "abdomen2"
spineBones = Genesis3Spine
elif meta.DazRigifyType in ["genesis3", "genesis8"]:
spineBones = Genesis3Spine
dazskel = {}
for rbone, dbone in rigifySkel.items():
if isinstance(dbone, tuple):
dbone = dbone[0]
if isinstance(dbone, str):
dazskel[dbone] = rbone
return rigifySkel, spineBones, dazskel
def reparentBones(rig, parents):
bpy.ops.object.mode_set(mode='EDIT')
for bname,pname in parents.items():
if (pname in rig.data.edit_bones.keys() and
bname in rig.data.edit_bones.keys()):
eb = rig.data.edit_bones[bname]
parb = rig.data.edit_bones[pname]
eb.use_connect = False
eb.parent = parb
bpy.ops.object.mode_set(mode='OBJECT')
def setupExtras(rig, rigifySkel, spineBones):
extras = OrderedDict()
taken = []
for dbone,_rbone,_pbone in spineBones:
taken.append(dbone)
for _rbone, dbone in rigifySkel.items():
if isinstance(dbone, tuple):
dbone = dbone[0]
if isinstance(dbone, tuple):
dbone = dbone[0]
taken.append(dbone)
for ob in rig.children:
for vgrp in ob.vertex_groups:
if (vgrp.name not in taken and
vgrp.name in rig.data.bones.keys()):
extras[vgrp.name] = vgrp.name
for dbone in list(extras.keys()):
bone = rig.data.bones[dbone]
while bone.parent:
pname = bone.parent.name
if pname in extras.keys() or pname in taken:
break
extras[pname] = pname
bone = bone.parent
return extras
def fixCarpals(rig):
if "lCarpal3" in rig.data.bones.keys():
return
bpy.ops.object.mode_set(mode='EDIT')
for prefix in ["l", "r"]:
for bname in ["Carpal1", "Carpal2"]:
if prefix+bname in rig.data.edit_bones.keys():
eb = rig.data.edit_bones[prefix+bname]
rig.data.edit_bones.remove(eb)
hand = rig.data.edit_bones[prefix+"Hand"]
hand.tail = 2*hand.tail - hand.head
for bname,cname in Carpals.items():
if prefix+cname in rig.data.edit_bones.keys():
eb = rig.data.edit_bones.new(prefix+bname)
child = rig.data.edit_bones[prefix+cname]
eb.head = hand.head
eb.tail = child.head
eb.roll = child.roll
eb.parent = hand
child.parent = eb
child.use_connect = True
bpy.ops.object.mode_set(mode='OBJECT')
for ob in rig.children:
if ob.type == 'MESH':
for prefix in ["l", "r"]:
for vgrp in ob.vertex_groups:
if vgrp.name == prefix+"Carpal2":
vgrp.name = prefix+"Carpal4"
def splitBone(rig, bname, upname):
if upname in rig.data.bones.keys():
return
bpy.ops.object.mode_set(mode='EDIT')
eblow = rig.data.edit_bones[bname]
vec = eblow.tail - eblow.head
mid = eblow.head + vec/2
ebup = rig.data.edit_bones.new(upname)
for eb in eblow.children:
eb.parent = ebup
ebup.head = mid
ebup.tail = eblow.tail
ebup.parent = eblow
ebup.roll = eblow.roll
eblow.tail = mid
bpy.ops.object.mode_set(mode='OBJECT')
def splitNeck(meta):
bpy.ops.object.mode_set(mode='EDIT')
spine = meta.data.edit_bones["spine"]
spine3 = meta.data.edit_bones["spine.003"]
bonelist={}
bpy.ops.armature.select_all(action='DESELECT')
spine3.select = True
bpy.ops.armature.subdivide()
spinebones = spine.children_recursive_basename
chainlength = len(spinebones)
for x in range(chainlength):
y = str(x)
spinebones[x].name = "spine" + "." + y
for x in range(chainlength):
y = str(x+1)
spinebones[x].name = "spine" + ".00" + y
bpy.ops.armature.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='OBJECT')
def deleteIfNotExist(bnames, rig, meta, context):
setActiveObject(context, meta)
bpy.ops.object.mode_set(mode='EDIT')
for dname,mname in bnames:
if (dname not in rig.data.bones.keys() and
mname in meta.data.edit_bones.keys()):
eb = meta.data.edit_bones[mname]
meta.data.edit_bones.remove(eb)
bpy.ops.object.mode_set(mode='OBJECT')
setActiveObject(context, rig)
def checkRigifyEnabled(context):
for addon in context.user_preferences.addons:
if addon.module == "rigify":
return True
return False
def getRigifyBone(bname, dazSkel, extras, spineBones):
global DeformBones
if bname in DeformBones:
return DeformBones[bname]
if bname[1:] in DeformBones:
prefix = bname[0]
return (DeformBones[bname[1:]] % prefix.upper())
if bname in dazSkel.keys():
rname = dazSkel[bname]
if rname in MetaBones.keys():
return "DEF-" + MetaBones[rname]
else:
return "DEF-" + rname
elif bname in extras.keys():
return extras[bname]
else:
for dname,rname,pname in spineBones:
if dname == bname:
return "DEF-" + rname
print("MISS", bname)
return None
def getDazBones(rig):
# Setup info about DAZ bones
dazBones = OrderedDict()
bpy.ops.object.mode_set(mode='EDIT')
for eb in rig.data.edit_bones:
dazBones[eb.name] = DazBone(eb)
bpy.ops.object.mode_set(mode='POSE')
for pb in rig.pose.bones:
dazBones[pb.name].getPose(pb)
bpy.ops.object.mode_set(mode='OBJECT')
return dazBones
def createMeta(context):
from collections import OrderedDict
from .mhx import connectToParent, unhideAllObjects
from .figure import getRigType
from .merge import mergeBonesAndVgroups
from .fix import fixPelvis, fixHands
print("Create metarig")
rig = context.object
scale = rig.DazScale
scn = context.scene
if not(rig and rig.type == 'ARMATURE'):
raise DazError("Rigify: %s is neither an armature nor has armature parent" % ob)
unhideAllObjects(context, rig)
# Create metarig
bpy.ops.object.mode_set(mode='OBJECT')
try:
bpy.ops.object.armature_human_metarig_add()
except AttributeError:
raise DazError("The Rigify add-on is not enabled. It is found under rigging.")
bpy.ops.object.location_clear()
bpy.ops.object.rotation_clear()
bpy.ops.object.scale_clear()
bpy.ops.transform.resize(value=(100*scale, 100*scale, 100*scale))
bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
meta = context.object
cns = meta.constraints.new('COPY_SCALE')
cns.name = "Rigify Source"
cns.target = rig
cns.mute = True
meta.DazPre278 = ("hips" in meta.data.bones.keys())
meta.DazRigifyType = getRigType(rig)
meta.DazUseBreasts = (not meta.DazPre278 and rig.data.DazExtraDrivenBones)
meta.DazUseSplitNeck = (not meta.DazPre278 and meta.DazRigifyType in ["genesis3", "genesis8"])
if meta.DazUseSplitNeck:
splitNeck(meta)
meta.DazRigType,hips,head = setupTables(meta)
activateObject(context, rig)
setSelected(rig, True)
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
if meta.DazRigifyType in ["genesis1", "genesis2"]:
fixPelvis(rig)
fixCarpals(rig)
splitBone(rig, "chest", "chestUpper")
splitBone(rig, "abdomen", "abdomen2")
delbones = [
("lPectoral", "breast.L"),
("rPectoral", "breast.R"),
]
deleteIfNotExist(delbones, rig, meta, context)
elif meta.DazRigifyType in ["genesis3", "genesis8"]:
mergeBonesAndVgroups(rig, Genesis3Mergers, Genesis3Parents, context)
reparentBones(rig, Genesis3Toes)
renameBones(rig, Genesis3Renames)
else:
activateObject(context, meta)
deleteObject(context, meta)
raise DazError("Cannot rigify %s %s" % (meta.DazRigifyType, rig.name))
connectToParent(rig)
rigifySkel, spineBones, dazSkel = setupDazSkeleton(meta)
dazBones = getDazBones(rig)
# Fit metarig to default DAZ rig
setActiveObject(context, meta)
setSelected(meta, True)
bpy.ops.object.mode_set(mode='EDIT')
for eb in meta.data.edit_bones:
eb.use_connect = False
for eb in meta.data.edit_bones:
try:
dname = rigifySkel[eb.name]
except KeyError:
dname = None
if isinstance(dname, tuple):
dname,_vgrps = dname
if isinstance(dname, str):
if dname in dazBones.keys():
dbone = dazBones[dname]
eb.head = dbone.head
eb.tail = dbone.tail
eb.roll = dbone.roll
elif isinstance(dname, tuple):
if (dname[0] in dazBones.keys() and
dname[1] in dazBones.keys()):
dbone1 = dazBones[dname[0]]
dbone2 = dazBones[dname[1]]
eb.head = dbone1.head
eb.tail = dbone2.head
hip = meta.data.edit_bones[hips]
dbone = dazBones["hip"]
hip.tail = Vector((1,2,3))
hip.head = dbone.tail
hip.tail = dbone.head
if meta.DazRigifyType in ["genesis3", "genesis8"]:
eb = meta.data.edit_bones[head]
eb.tail = eb.head + 1.0*(eb.tail - eb.head)
fixHands(meta)
for suffix in [".L", ".R"]:
shoulder = meta.data.edit_bones["shoulder"+suffix]
upperarm = meta.data.edit_bones["upper_arm"+suffix]
shin = meta.data.edit_bones["shin"+suffix]
foot = meta.data.edit_bones["foot"+suffix]
toe = meta.data.edit_bones["toe"+suffix]
vec = shoulder.tail - shoulder.head
if (upperarm.head - shoulder.tail).length < 0.02*vec.length:
shoulder.tail -= 0.02*vec
if "pelvis"+suffix in meta.data.edit_bones.keys():
thigh = meta.data.edit_bones["thigh"+suffix]
pelvis = meta.data.edit_bones["pelvis"+suffix]
pelvis.head = hip.head
pelvis.tail = thigh.head
#if "breast"+suffix in meta.data.edit_bones.keys():
# breast = meta.data.edit_bones["breast"+suffix]
# breast.head[0] = breast.tail[0]
# breast.head[2] = breast.tail[2]
foot.head = shin.tail
toe.head = foot.tail
xa,ya,za = foot.head
xb,yb,zb = toe.head
heelhead = foot.head
heeltail = Vector((xa, yb-1.3*(yb-ya), zb))
mid = (toe.head + heeltail)/2
r = Vector((yb-ya,0,0))
if xa > 0:
fac = 0.3
else:
fac = -0.3
heel02head = mid + fac*r
heel02tail = mid - fac*r
if "heel"+suffix in meta.data.edit_bones.keys():
heel = meta.data.edit_bones["heel"+suffix]
heel.head = heelhead
heel.tail = heeltail
if "heel.02"+suffix in meta.data.edit_bones.keys():
heel02 = meta.data.edit_bones["heel.02"+suffix]
heel02.head = heel02head
heel02.tail = heel02tail
for eb in meta.data.edit_bones:
if (eb.parent and
eb.head == eb.parent.tail and
eb.name not in MetaDisconnect):
eb.use_connect = True
# Fix spine
mbones = meta.data.edit_bones
for dname,rname,pname in spineBones:
if dname not in dazBones.keys():
continue
dbone = dazBones[dname]
if rname in mbones.keys():
eb = mbones[rname]
else:
eb = mbones.new(dname)
eb.name = rname
eb.use_connect = False
eb.head = dbone.head
eb.tail = dbone.tail
eb.roll = dbone.roll
eb.parent = mbones[pname]
eb.use_connect = True
eb.layers = list(eb.parent.layers)
reparentBones(meta, MetaParents)
# Add rigify properties to spine bones
bpy.ops.object.mode_set(mode='OBJECT')
for _dname,rname,_pname in spineBones:
pb = meta.pose.bones[rname]
if "rigify_type" in pb.keys():
#print("%s: %s" % (rname, pb["rigify_type"]))
if pb["rigify_type"] == "spines.super_head":
pb["rigify_type"] = ""
else:
pb["rigify_type"] = ""
for rname,prop,value in RigifyParams:
if rname in meta.pose.bones:
pb = meta.pose.bones[rname]
setattr(pb.rigify_parameters, prop, value)
print("Metarig created")
return meta
def rigifyMeta(context):
from .driver import getBoneDrivers, copyDriver, changeBoneTarget, changeDriverTarget
from .node import setParent, clearParent
from .daz import copyPropGroups
from .fix import fixCorrectives, checkCorrectives, fixCustomShape
from .mhx import unhideAllObjects
from .figure import copyBoneInfo
print("Rigify metarig")
meta = context.object
rig = None
for cns in meta.constraints:
if cns.type == 'COPY_SCALE' and cns.name == "Rigify Source":
rig = cns.target
if rig is None:
raise DazError("Original rig not found")
unhideAllObjects(context, rig)
if not inSceneLayer(context, rig):
showSceneLayer(context, rig)
bpy.ops.object.mode_set(mode='POSE')
for pb in meta.pose.bones:
if hasattr(pb, "rigify_parameters"):
if hasattr (pb.rigify_parameters, "roll_alignment"):
pb.rigify_parameters.roll_alignment = "manual"
try:
bpy.ops.pose.rigify_generate()
except:
raise DazError("Cannot rigify %s rig %s " % (meta.DazRigifyType, rig.name))
scn = context.scene
gen = context.object
coll = getCollection(context)
print("Fix generated rig", gen.name)
setActiveObject(context, rig)
rigifySkel, spineBones, dazSkel = setupDazSkeleton(meta)
dazBones = getDazBones(rig)
empty = bpy.data.objects.new("Widgets", None)
coll.objects.link(empty)
empty.parent = gen
for ob in getSceneObjects(context):
if ob.parent is None and ob.name[0:4] == "WGT-":
ob.parent = empty
extras = setupExtras(rig, rigifySkel, spineBones)
if meta.DazUseBreasts:
for prefix in ["l", "r"]:
extras[prefix+"PectoralDrv"] = prefix+"PectoralDrv"
driven = {}
for pb in rig.pose.bones:
fcus = getBoneDrivers(rig, pb)
if fcus:
driven[pb.name] = fcus
# Add extra bones to generated rig
faceLayers = R_FACE*[False] + [True] + (31-R_FACE)*[False]
helpLayers = R_HELP*[False] + [True] + (31-R_HELP)*[False]
setActiveObject(context, gen)
bpy.ops.object.mode_set(mode='EDIT')
for dname,rname in extras.items():
if dname not in dazBones.keys():
continue
dbone = dazBones[dname]
eb = gen.data.edit_bones.new(rname)
eb.head = dbone.head
eb.tail = dbone.tail
eb.roll = dbone.roll
eb.use_deform = dbone.use_deform
if eb.use_deform:
eb.layers = faceLayers
eb.layers[R_DEFORM] = True
else:
eb.layers = helpLayers
if dname in driven.keys():
eb.layers = helpLayers
# Add parents to extra bones
for dname,rname in extras.items():
if dname not in dazBones.keys():
continue
dbone = dazBones[dname]
eb = gen.data.edit_bones[rname]
if dbone.parent:
pname = getRigifyBone(dbone.parent, dazSkel, extras, spineBones)
if (pname in gen.data.edit_bones.keys()):
eb.parent = gen.data.edit_bones[pname]
eb.use_connect = (eb.parent != None and eb.parent.tail == eb.head)
else:
print("No parent", dbone.name, dbone.parent, pname)
bones = list(dazSkel.keys())
bones.sort()
print("Bones:", bones)
msg = ("Bone %s has no parent %s" % (dbone.name, dbone.parent))
raise DazError(msg)
if meta.DazUseBreasts:
for prefix,suffix in [("l", ".L"), ("r", ".R")]:
db = gen.data.edit_bones[prefix + "PectoralDrv"]
eb = gen.data.edit_bones["breast" + suffix]
db.parent = eb.parent
eb.parent = db
bpy.ops.object.mode_set(mode='POSE')
# Lock extras
for dname,rname in extras.items():
if dname not in dazBones.keys():
continue
if rname in gen.pose.bones.keys():
pb = gen.pose.bones[rname]
dazBones[dname].setPose(pb)
# Remove breast custom shapes, because they are placed differently in Daz
for rname in ["breast.L", "breast.R"]:
if rname in gen.pose.bones.keys():
pb = gen.pose.bones[rname]
pb.custom_shape = None
# Rescale custom shapes
if meta.DazRigifyType in ["genesis3", "genesis8"]:
fixCustomShape(gen, ["head", "spine_fk.007"], 4)
if bpy.app.version >= (2,82,0):
fixCustomShape(gen, ["chest"], 1, Vector((0,-100*rig.DazScale,0)))
# Add DAZ properties
for key in rig.keys():
gen[key] = rig[key]
for key in rig.data.keys():
gen.data[key] = rig.data[key]
for bname,dname in rigifySkel.items():
if dname in rig.data.bones.keys():
bone = rig.data.bones[dname]
if bname in gen.data.bones.keys():
rbone = gen.data.bones[bname]
copyBoneInfo(bone, rbone)
else:
words = bname.split(".")
if len(words) == 2:
gname,suffix = words
if gname+"_fk."+suffix in gen.data.bones.keys():
fkbone = gen.data.bones[gname+"_fk."+suffix]
elif gname+".fk."+suffix in gen.data.bones.keys():
fkbone = gen.data.bones[gname+".fk."+suffix]
else:
fkbone = None
if fkbone:
copyBoneInfo(bone, fkbone)
# Handle bone parents
boneParents = []
for ob in rig.children:
if ob.parent_type == 'BONE':
boneParents.append((ob, ob.parent_bone))
clearParent(ob)
for ob,dname in boneParents:
rname = getRigifyBone(dname, dazSkel, extras, spineBones)
if rname and rname in gen.data.bones.keys():
print("Parent %s to bone %s" % (ob.name, rname))
bone = gen.data.bones[rname]
setParent(context, ob, gen, bone.name)
else:
print("Did not find bone parent %s %s" %(dname, rname))
setParent(context, ob, gen, None)
# Copy DAZ morph drivers and change armature modifier
activateObject(context, gen)
for ob in rig.children:
if ob.type == 'MESH':
ob.parent = gen
for dname,rname,_pname in spineBones:
if dname in ob.vertex_groups.keys():
vgrp = ob.vertex_groups[dname]
vgrp.name = "DEF-" + rname
for rname,dname in rigifySkel.items():
if dname[1:] in ["Thigh", "Shin", "Shldr", "ForeArm"]:
rigifySplitGroup(rname, dname, ob, rig, True, meta)
elif (meta.DazPre278 and
dname[1:] in ["Thumb1", "Index1", "Mid1", "Ring1", "Pinky1"]):
rigifySplitGroup(rname, dname, ob, rig, False, meta)
elif isinstance(dname, str):
if dname in ob.vertex_groups.keys():
vgrp = ob.vertex_groups[dname]
vgrp.name = "DEF-" + rname
else:
mergeVertexGroups(rname, dname[1], ob)
for dname,rname in extras.items():
if dname in ob.vertex_groups.keys():
vgrp = ob.vertex_groups[dname]
vgrp.name = rname
if ob.animation_data:
for fcu in ob.animation_data.drivers:
changeDriverTarget(fcu, gen)
if ob.data.animation_data:
for fcu in ob.data.animation_data.drivers:
changeDriverTarget(fcu, gen)
if ob.data.shape_keys and ob.data.shape_keys.animation_data:
for fcu in ob.data.shape_keys.animation_data.drivers:
changeDriverTarget(fcu, gen)
for mod in ob.modifiers:
if mod.type == 'ARMATURE' and mod.object == rig:
mod.object = gen
# Add generated rig to group
group = None
if bpy.app.version <= (2,80,0):
for grp in bpy.data.groups:
if rig.name in grp.objects:
group = grp
break
print("Group: %s" % group)
if group:
group.objects.link(gen)
# Fix drivers
assoc = [(rigi,daz) for (daz,rigi,_) in Genesis3Spine]
assoc += [(rigi,daz) for (rigi,daz) in RigifySkeleton.items()]
for bname, fcus in driven.items():
if bname in gen.pose.bones.keys():
if bname not in gen.pose.bones.keys():
continue
pb = gen.pose.bones[bname]
copyPropGroups(rig, gen, pb)