-
Notifications
You must be signed in to change notification settings - Fork 10
/
baseRigPrimitive.py
1198 lines (927 loc) · 39.9 KB
/
baseRigPrimitive.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 filesystem
import typeFactories
from maya.cmds import *
from maya import cmds as cmd
from rigUtils import *
from control import *
from names import Parity, Name, camelCaseToNice, stripParity
from skeletonBuilder import *
from vectors import Vector, Matrix
from mayaDecorators import d_unifyUndo
from common import printInfoStr, printWarningStr, printErrorStr
import apiExtensions
import skeletonBuilder
import spaceSwitching
import triggered
import poseSym
import vectors
import control
import api
__author__ = '[email protected]'
Trigger = triggered.Trigger
AXES = Axis.BASE_AXES
Vector = vectors.Vector
AIM_AXIS = AX_X
ROT_AXIS = AX_Y
#make sure all setDrivenKeys have linear tangents
setDrivenKeyframe = lambda *a, **kw: cmd.setDrivenKeyframe( inTangentType='linear', outTangentType='linear', *a, **kw )
def connectAttrReverse( srcAttr, destAttr, **kw ):
'''
puts a reverse node in between the two given attributes
'''
revNode = shadingNode( 'reverse', asUtility=True )
connectAttr( srcAttr, '%s.inputX' % revNode, **kw )
connectAttr( '%s.outputX' % revNode, destAttr, **kw )
return revNode
class RigPartError(Exception): pass
def isRigPartContainer( node ):
if objectType( node, isType='objectSet' ):
return sets( node, q=True, text=True ) == 'rigPrimitive'
return False
def getRigPartContainers( compatabilityMode=False ):
existingContainers = [ node for node in ls( type='objectSet', r=True ) or [] if sets( node, q=True, text=True ) == 'rigPrimitive' ]
if compatabilityMode:
existingContainers += [ node.split( '.' )[0] for node in ls( '*._rigPrimitive', r=True ) ]
return existingContainers
def getNodesCreatedBy( function, *args, **kwargs ):
'''
returns a 2-tuple containing all the nodes created by the passed function, and
the return value of said function
NOTE: if any container nodes were created, their contents are omitted from the
resulting node list - the container itself encapsulates them
'''
newNodes, ret = apiExtensions.getNodesCreatedBy( function, *args, **kwargs )
#now remove nodes from all containers from the newNodes list
newContainers = apiExtensions.filterByType( newNodes, apiExtensions.MFn.kSet )
#NOTE: nodes are MObject instances at this point
newNodes = set( [ node for node in newNodes if node is not None ] )
for c in newContainers:
for n in sets( c, q=True ) or []:
if n in newNodes:
newNodes.remove( n )
#containers contained by other containers don't need to be returned (as they're already contained by a parent)
newTopLevelContainers = []
for c in newContainers:
parentContainer = sets( c, q=True, parentContainer=True )
if parentContainer:
continue
newTopLevelContainers.append( c )
newNodes.add( c )
return newNodes, ret
def buildContainer( typeClass, kwDict, nodes, controls, namedNodes=() ):
'''
builds a container for the given nodes, and tags it with various attributes to record
interesting information such as rig primitive version, and the args used to instantiate
the rig. it also registers control objects with attributes, so the control nodes can
queried at a later date by their name
'''
#if typeClass is an instance, then set its container attribute, otherwise instantiate an instance and return it
if isinstance( typeClass, RigPart ):
theInstance = typeClass
typeClass = type( typeClass )
elif issubclass( typeClass, RigPart ):
theInstance = typeClass( None )
#build the container, and add the special attribute to it to
theContainer = sets( em=True, n='%s_%s' % (typeClass.__name__, kwDict.get( 'idx', 'NOIDX' )), text='rigPrimitive' )
theInstance.setContainer( theContainer )
addAttr( theContainer, ln='_rigPrimitive', attributeType='compound', numberOfChildren=7 )
addAttr( theContainer, ln='typeName', dt='string', parent='_rigPrimitive' )
addAttr( theContainer, ln='script', dt='string', parent='_rigPrimitive' )
addAttr( theContainer, ln='version', at='long', parent='_rigPrimitive' )
addAttr( theContainer, ln='skeletonPart', at='message', parent='_rigPrimitive' )
addAttr( theContainer, ln='buildKwargs', dt='string', parent='_rigPrimitive' )
addAttr( theContainer, ln='controls',
multi=True,
indexMatters=True,
attributeType='message',
parent='_rigPrimitive' )
addAttr( theContainer, ln='namedNodes',
multi=True,
indexMatters=True,
attributeType='message',
parent='_rigPrimitive' )
#now set the attribute values...
setAttr( '%s._rigPrimitive.typeName' % theContainer, typeClass.__name__, type='string' )
setAttr( '%s._rigPrimitive.script' % theContainer, inspect.getfile( typeClass ), type='string' )
setAttr( '%s._rigPrimitive.version' % theContainer, typeClass.__version__ )
setAttr( '%s._rigPrimitive.buildKwargs' % theContainer, str( kwDict ), type='string' )
#now add all the nodes
nodes = [ str( node ) if node is not None else node for node in nodes ]
controls = [ str( node ) if node is not None else node for node in controls ]
for node in set( nodes ) | set( controls ):
if node is None:
continue
if objectType( node, isAType='dagNode' ):
sets( node, e=True, add=theContainer )
#if the node is a rig part container add it to this container otherwise skip it
elif objectType( node, isAType='objectSet' ):
if isRigPartContainer( node ):
sets( node, e=True, add=theContainer )
#and now hook up all the controls
controlNames = typeClass.CONTROL_NAMES or [] #CONTROL_NAMES can validly be None, so in this case just call it an empty list
for idx, control in enumerate( controls ):
if control is None:
continue
connectAttr( '%s.message' % control, '%s._rigPrimitive.controls[%d]' % (theContainer, idx), f=True )
#set the kill state on the control if its a transform node
if objectType( control, isAType='transform' ):
triggered.setKillState( control, True )
#hook up all the named nodes
for idx, node in enumerate( namedNodes ):
if node is None:
continue
connectAttr( '%s.message' % node, '%s._rigPrimitive.namedNodes[%d]' % (theContainer, idx), f=True )
return theInstance
class RigPart(typeFactories.trackableClassFactory()):
'''
base rig part class. deals with rig part creation.
rig parts are instantiated by passing the class a rig part container node
to create a new rig part, simply call the RigPartClass.Create( skeletonPart, *args )
where the skeletonPart is the SkeletonPart instance created via the skeleton builder
'''
__version__ = 0
PRIORITY = 0
CONTROL_NAMES = None
NAMED_NODE_NAMES = None
AVAILABLE_IN_UI = False #determines whether this part should appear in the UI or not...
ADD_CONTROLS_TO_QSS = True
AUTO_PICKER = True
#if you want to customize the name as it appears in the UI, set this to the desired string
DISPLAY_NAME = None
def __new__( cls, partContainer, skeletonPart=None ):
if cls is RigPart:
clsName = getAttr( '%s._rigPrimitive.typeName' % partContainer )
cls = cls.GetNamedSubclass( clsName )
if cls is None:
raise TypeError( "Cannot determine the part class for the given part container!" )
return object.__new__( cls, partContainer, skeletonPart )
def __init__( self, partContainer, skeletonPart=None ):
if partContainer is not None:
assert isRigPartContainer( partContainer ), "Must pass a valid rig part container! (received %s - a %s)" % (partContainer, nodeType( partContainer ))
self._container = partContainer
self._skeletonPart = skeletonPart
self._worldPart = None
self._worldControl = None
self._partsNode = None
self._qss = None
self._idx = None
if partContainer:
if skeletonPart is None:
try:
self.getSkeletonPart()
#this isn't fatal, although its not good
except RigPartError, x:
printWarningStr( str( x ) )
def __unicode__( self ):
return u"%s_%d( %r )" % (self.__class__.__name__, self.getIdx(), self._container)
__str__ = __unicode__
def __repr__( self ):
return repr( unicode( self ) )
def __hash__( self ):
'''
the hash for the container mobject uniquely identifies this rig control
'''
return hash( apiExtensions.asMObject( self._container ) )
def __eq__( self, other ):
return self._container == other.getContainer()
def __neq__( self, other ):
return not self == other
def __getitem__( self, idx ):
'''
returns the control at <idx>
'''
connected = listConnections( '%s._rigPrimitive.controls[%d]' % (self._container, idx), d=False )
if connected:
assert len( connected ) == 1, "More than one control was found!!!"
return connected[ 0 ]
return None
def __len__( self ):
'''
returns the number of controls registered on the rig
'''
return getAttr( '%s._rigPrimitive.controls' % self._container, size=True )
def __iter__( self ):
'''
iterates over all controls in the rig
'''
for n in range( len( self ) ):
yield self[ n ]
def getContainer( self ):
return self._container
def setContainer( self, container ):
self._container = container
def getNodes( self ):
'''
returns ALL the nodes that make up this rig part
'''
return sets( self._container, q=True )
nodes = getNodes
def isReferenced( self ):
return referenceQuery( self._container, inr=True )
@classmethod
def GetPartName( cls ):
'''
can be used to get a "nice" name for the part class
'''
if cls.DISPLAY_NAME is None:
return camelCaseToNice( cls.__name__ )
return cls.DISPLAY_NAME
@classmethod
def InitFromItem( cls, item ):
'''
inits the rigPart from a member item - the RigPart instance returned is
cast to teh most appropriate type
'''
if isRigPartContainer( item ):
typeClassStr = getAttr( '%s._rigPrimitive.typeName' % partContainer )
typeClass = RigPart.GetNamedSubclass( typeClassStr )
return typeClass( item )
cons = listConnections( item, s=False, type='objectSet' )
if not cons:
raise RigPartError( "Cannot find a rig container for %s" % item )
for con in cons:
if isRigPartContainer( con ):
typeClassStr = getAttr( '%s._rigPrimitive.typeName' % con )
typeClass = RigPart.GetNamedSubclass( typeClassStr )
return typeClass( con )
raise RigPartError( "Cannot find a rig container for %s" % item )
@classmethod
def IterAllParts( cls, skipSubParts=True ):
'''
iterates over all RigParts in the current scene
NOTE: if skipSubParts is True will skip over parts that inherit from RigSubPart - these are assumed to be contained by another part
'''
for c in getRigPartContainers():
if objExists( '%s._rigPrimitive' % c ):
thisClsName = getAttr( '%s._rigPrimitive.typeName' % c )
thisCls = RigPart.GetNamedSubclass( thisClsName )
if thisCls is None:
raise SkeletonError( "No RigPart called %s" % thisClsName )
if skipSubParts and issubclass( thisCls, RigSubPart ):
continue
if issubclass( thisCls, cls ):
yield cls( c )
@classmethod
def IterAllPartsInOrder( cls, skipSubParts=False ):
for skeletonPart in SkeletonPart.IterAllPartsInOrder():
rigPart = skeletonPart.getRigPart()
if rigPart is None:
continue
if skipSubParts and isinstance( rigPart, RigSubPart ):
continue
yield rigPart
@classmethod
def GetUniqueIdx( cls ):
'''
returns a unique index (unique against the universe of existing indices
in the scene) for the current part class
'''
existingIdxs = []
for part in cls.IterAllParts():
idx = part.getBuildKwargs()[ 'idx' ]
existingIdxs.append( idx )
existingIdxs.sort()
assert len( existingIdxs ) == len( set( existingIdxs ) ), "There is a duplicate ID! %s, %s" % (cls, existingIdxs)
#return the first, lowest, available index
for orderedIdx, existingIdx in enumerate( existingIdxs ):
if existingIdx != orderedIdx:
return orderedIdx
if existingIdxs:
return existingIdxs[ -1 ] + 1
return 0
def createSharedShape( self, name ):
return asMObject( createNode( 'nurbsCurve', n=name +'#', p=self.sharedShapeParent ) )
@classmethod
def Create( cls, skeletonPart, *a, **kw ):
'''
you can pass in the following kwargs to control the build process
addControlsToQss defaults to cls.ADD_CONTROLS_TO_QSS
'''
#check to see if the given skeleton part can actually be rigged by this method
if not cls.CanRigThisPart( skeletonPart ):
return
addControlsToQss = kw.get( 'addControlsToQss', cls.ADD_CONTROLS_TO_QSS )
buildFunc = getattr( cls, '_build', None )
if buildFunc is None:
raise RigPartError( "The rigPart %s has no _build method!" % cls.__name__ )
assert isinstance( skeletonPart, SkeletonPart ), "Need a SkeletonPart instance, got a %s instead" % skeletonPart.__class__
if not skeletonPart.compareAgainstHash():
raise NotFinalizedError( "ERROR :: %s hasn't been finalized!" % skeletonPart )
#now turn the args passed in are a single kwargs dict
argNames, vArgs, vKwargs, defaults = inspect.getargspec( buildFunc )
if defaults is None:
defaults = []
argNames = argNames[ 2: ] #strip the first two args - which should be the instance arg (usually self) and the skeletonPart
if vArgs is not None:
raise RigPartError( 'cannot have *a in rig build functions' )
for argName, value in zip( argNames, a ):
kw[ argName ] = value
#now explicitly add the defaults
for argName, default in zip( argNames, defaults ):
kw.setdefault( argName, default )
#generate an index for the rig part - each part must have a unique index
idx = cls.GetUniqueIdx()
kw[ 'idx' ] = idx
#construct an empty instance - empty RigPart instances are only valid inside this method...
self = cls( None )
self._skeletonPart = skeletonPart
self._idx = idx
#generate a default scale for the rig part
kw.setdefault( 'scale', getScaleFromSkeleton() / 10.0 )
self.scale = kw[ 'scale' ]
#make sure the world part is created first - if its created by the part, then its nodes will be included in its container...
self.getWorldPart()
#create the shared shape transform - this is the transform under which all shared shapes are temporarily parented to, and all
#shapes under this transform are automatically added to all controls returned after the build function returns
self.sharedShapeParent = asMObject( createNode( 'transform', n='_tmp_sharedShape' ) )
defaultSharedShape = self.createSharedShape( '%s_sharedAttrs' % cls.GetPartName() )
kw[ 'sharedShape' ] = defaultSharedShape
#run the build function
newNodes, (controls, namedNodes) = getNodesCreatedBy( self._build, skeletonPart, **kw )
realControls = [ c for c in controls if c is not None ] #its possible for a build function to return None in the control list because it wants to preserve the length of the control list returned - so construct a list of controls that actually exist
realNamedNodes = [ c for c in namedNodes if c is not None ]
if addControlsToQss:
for c in realControls:
sets( c, add=self._qss )
#check to see if there is a layer for the rig controls and add controls to it
if controls:
if objExists( 'rig_controls' ) and nodeType( 'rig_controls' ) == 'displayLayer':
rigLayer = 'rig_controls'
else:
rigLayer = createDisplayLayer( name='rig_controls', empty=True )
editDisplayLayerMembers( rigLayer, controls, noRecurse=True )
#make sure there are no intermediate shapes
for c in realControls:
for shape in listRelatives( c, s=True, pa=True ) or []:
if getAttr( '%s.intermediateObject' % shape ):
delete( shape )
#build the container and initialize the rigPrimtive
buildContainer( self, kw, newNodes, controls, namedNodes )
#add shared shapes to all controls, and remove shared shapes that are empty
sharedShapeParent = self.sharedShapeParent
sharedShapes = listRelatives( sharedShapeParent, pa=True, s=True ) or []
for c in realControls:
if objectType( c, isAType='transform' ):
for shape in sharedShapes:
parent( shape, c, add=True, s=True )
for shape in sharedShapes:
if not listAttr( shape, ud=True ):
delete( shape )
delete( sharedShapeParent )
del( self.sharedShapeParent )
#stuff the part container into the world container - we want a clean top level in the outliner
theContainer = self._container
sets( theContainer, e=True, add=self._worldPart.getContainer() )
#make sure the container "knows" the skeleton part - its not always obvious trawling through
#the nodes in teh container which items are the skeleton part
connectAttr( '%s.message' % skeletonPart.getContainer(), '%s._rigPrimitive.skeletonPart' % theContainer )
return self
@classmethod
def GetControlName( cls, control ):
'''
returns the name of the control as defined in the CONTROL_NAMES attribute
for the part class
'''
cons = listConnections( control.message, s=False, p=True, type='objectSet' )
for c in cons:
typeClassStr = getAttr( '%s._rigPrimitive.typeName' % c.node() )
typeClass = RigPart.GetNamedSubclass( typeClassStr )
if typeClass.CONTROL_NAMES is None:
return str( control )
idx = c[ c.rfind( '[' )+1:-1 ]
try: name = typeClass.CONTROL_NAMES[ idx ]
except ValueError:
printErrorStr( 'type: %s control: %s' % (typeClass, control) )
raise RigPartError( "Doesn't have a name!" )
return name
raise RigPartError( "The control isn't associated with a rig primitive" )
@classmethod
def CanRigThisPart( cls, skeletonPart ):
return True
@classmethod
def GetDefaultBuildKwargList( cls ):
'''
returns a list of 2 tuples: argName, defaultValue
'''
buildFunc = getattr( cls, '_build', None )
spec = inspect.getargspec( buildFunc )
argNames = spec[ 0 ][ 2: ] #strip the first two items because the _build method is a bound method - so the first item is always the class arg (usually called cls), and the second arg is always the skeletonPart
defaults = spec[ 3 ]
if defaults is None:
defaults = []
assert len( argNames ) == len( defaults ), "%s has no default value set for one of its args - this is not allowed" % cls
kwargList = []
for argName, default in zip( argNames, defaults ):
kwargList.append( (argName, default) )
return kwargList
def isPartContained( self ):
'''
returns whether this rig part is "contained" by another rig part. Ie if a rig part was build from within another
rig part, then it is contained. Examples of this are things like the arm rig which builds upon the ikfk sub
primitive rig - the sub-primitive is contained within the arm rig
'''
cons = listConnections( '%s.message' % self._container, s=False, type='objectSet' )
if cons:
for con in cons:
if isRigPartContainer( con ):
rigPart = RigPart( con )
if isinstance( rigPart, WorldPart ):
continue
return True
return False
def getBuildKwargs( self ):
theDict = eval( getAttr( '%s._rigPrimitive.buildKwargs' % self._container ) )
return theDict
def getIdx( self ):
'''
returns the index of the part - all parts have a unique index associated
with them
'''
if self._idx is None:
if self._container is None:
raise RigPartError( 'No index has been defined yet!' )
else:
buildKwargs = self.getBuildKwargs()
self._idx = buildKwargs[ 'idx' ]
return self._idx
def getParity( self ):
return self.getSkeletonPart().getParity()
def getSuffix( self ):
return self.getParity().asName()
def getParityColour( self ):
return ColourDesc( 'green 0.7' ) if self.getParity() == Parity.LEFT else ColourDesc( 'red 0.7' )
def getBuildScale( self ):
return self.getBuildKwargs().get( 'scale', self.PART_SCALE )
def getWorldPart( self ):
if self._worldPart is None:
self._worldPart = worldPart = WorldPart.Create()
self._worldControl = worldPart.getControl( 'control' )
self._partsNode = worldPart.getNamedNode( 'parts' )
self._qss = worldPart.getNamedNode( 'qss' )
return self._worldPart
def getWorldControl( self ):
if self._worldControl is None:
self.getWorldPart()
return self._worldControl
def getPartsNode( self ):
if self._partsNode is None:
self.getWorldPart()
return self._partsNode
def getQssSet( self ):
if self._qss is None:
self.getWorldPart()
return self._qss
def getSkeletonPart( self ):
'''
returns the skeleton part this rig part is driving
'''
#have we cached the skeleton part already? if so, early out!
if self._skeletonPart:
return self._skeletonPart
if self._container is None:
return None
connected = listConnections( '%s.skeletonPart' % self._container )
if connected is None:
raise RigPartError( "There is no skeleton part associated with this rig part! This can happen for a variety of reasons such as name changes on the skeleton in the model file (if you're using referencing), or a incomplete conversion from the old rig format..." )
if nodeType( connected[0] ) == 'reference':
raise RigPartError( "A reference node is connected to the skeletonPart attribute - this could mean the model reference isn't loaded, or a node name from the referenced file has changed - either way I can't determine the skeleton part used by this rig!" )
#cache the value so we can quickly return it on consequent calls
self._skeletonPart = skeletonPart = SkeletonPart.InitFromItem( connected[0] )
return skeletonPart
def getSkeletonPartParity( self ):
return self.getSkeletonPart().getParity()
def getControl( self, attrName ):
'''
returns the control named <attrName>. control "names" are defined by the CONTROL_NAMES class
variable. This list is asked for the index of <attrName> and the control at that index is returned
'''
if self.CONTROL_NAMES is None:
raise AttributeError( "The %s rig primitive has no named controls" % self.__class__.__name__ )
idx = list( self.CONTROL_NAMES ).index( attrName )
if idx < 0:
raise AttributeError( "No control with the name %s" % attrName )
connected = listConnections( '%s._rigPrimitive.controls[%d]' % (self._container, idx), d=False )
if connected:
assert len( connected ) == 1, "More than one control was found!!!"
return connected[ 0 ]
return None
def getControlIdx( self, control ):
'''
returns the index of the given control - each control is plugged into a given "slot"
'''
cons = cmd.listConnections( '%s.message' % control, s=False, p=True ) or []
for c in cons:
node = c.split( '.' )[0]
if not isRigPartContainer( node ):
continue
if objExists( node ):
if node != self._container:
continue
idx = int( c[ c.rfind( '[' )+1:-1 ] )
return idx
raise RigPartError( "The control %s isn't associated with this rig primitive %s" % (control, self) )
def getControlName( self, control ):
'''
returns the name of the control as defined in the CONTROL_NAMES attribute
for the part class
'''
if self.CONTROL_NAMES is None:
return str( control )
controlIdx = self.getControlIdx( control )
try:
return self.CONTROL_NAMES[ controlIdx ]
except IndexError:
return None
raise RigPartError( "The control %s isn't associated with this rig primitive %s" % (control, self) )
def getNamedNode( self, nodeName ):
'''
returns the "named node" called <nodeName>. Node "names" are defined by the NAMED_NODE_NAMES class
variable. This list is asked for the index of <nodeName> and the node at that index is returned
'''
if self.NAMED_NODE_NAMES is None:
raise AttributeError( "The %s rig primitive has no named nodes" % self.__class__.__name__ )
idx = list( self.NAMED_NODE_NAMES ).index( nodeName )
if idx < 0:
raise AttributeError( "No node with the name %s" % nodeName )
connected = listConnections( '%s._rigPrimitive.namedNodes[%d]' % (self._container, idx), d=False )
if connected:
assert len( connected ) == 1, "More than one node was found!!!"
return connected[ 0 ]
return None
def delete( self ):
nodes = sets( self._container, q=True )
for node in nodes:
cleanDelete( node )
if objExists( self._container ):
delete( self._container )
#if the skeleton part is referenced, clean all reference edits off skeleton part joints
skeletonPart = self.getSkeletonPart()
if skeletonPart.isReferenced():
skeletonPartJoints = skeletonPart.items
#now unload the reference
partReferenceFile = Path( referenceQuery( skeletonPart.getContainer(), filename=True ) )
file( partReferenceFile, unloadReference=True )
#remove edits from each joint in the skeleton part
for j in skeletonPartJoints:
referenceEdit( j, removeEdits=True, successfulEdits=True, failedEdits=True )
#reload the referenced file
file( partReferenceFile, loadReference=True )
### POSE MIRRORING/SWAPPING ###
def getOppositePart( self ):
'''
Finds the skeleton part opposite to the one this rig part controls, and returns its rig part.
If no rig part can be found, or if no
'''
thisSkeletonPart = self.getSkeletonPart()
oppositeSkeletonPart = thisSkeletonPart.getOppositePart()
if oppositeSkeletonPart is None:
return None
return oppositeSkeletonPart.getRigPart()
def getOppositeControl( self, control ):
'''
Finds the control that is most likely to be opposite the one given. It first gets the name of
the given control. It then finds the opposite rig part, and then queries it for the control
with the determined name
'''
controlIdx = self.getControlIdx( control )
oppositePart = self.getOppositePart()
if oppositePart:
return oppositePart[ controlIdx ]
return None
def setupMirroring( self ):
for control in self:
if control is None:
continue
oppositeControl = self.getOppositeControl( control )
pair = poseSym.ControlPair.Create( control, oppositeControl )
printInfoStr( 'setting up mirroring on %s %s' % (control, oppositeControl) )
def getFilePartDict():
'''
returns a dictionary keyed by scene name containing a list of the parts contained in that scene
'''
scenePartDict = {}
#special case! we want to skip parts that are of this exact type - in older rigs this class was a RigSubPart, not a super class for the biped limb classes
IkFkBaseCls = RigPart.GetNamedSubclass( 'IkFkBase' )
for rigPart in RigPart.IterAllParts():
if IkFkBaseCls:
if type( rigPart ) is IkFkBaseCls:
continue
isReferenced = rigPart.isReferenced()
if isReferenced:
rigScene = Path( referenceQuery( rigPart.getContainer(), filename=True ) )
else:
rigScene = Path( file( q=True, sn=True ) )
scenePartDict.setdefault( rigScene, [] )
partList = scenePartDict[ rigScene ]
partList.append( rigPart )
return scenePartDict
def generateNiceControlName( control ):
niceName = getNiceName( control )
if niceName is not None:
return niceName
try:
rigPart = RigPart.InitFromItem( control )
if rigPart is None: raise RigPartError( "null" )
controlName = rigPart.getControlName( control )
except RigPartError:
controlName = str( control )
controlName = Name( controlName )
parity = controlName.get_parity()
if parity == Parity.LEFT:
controlName = 'Left '+ str( stripParity( controlName ) )
if parity == Parity.RIGHT:
controlName = 'Right '+ str( stripParity( controlName ) )
else:
controlName = str( controlName )
return camelCaseToNice( controlName )
def getSpaceSwitchControls( theJoint ):
'''
walks up the joint chain and returns a list of controls that drive parent joints
'''
parentControls = []
for p in api.iterParents( theJoint ):
theControl = getItemRigControl( p )
if theControl is not None:
parentControls.append( theControl )
return parentControls
def buildDefaultSpaceSwitching( theJoint, control=None, additionalParents=(), additionalParentNames=(), reverseHierarchy=False, **buildKwargs ):
if control is None:
control = getItemRigControl( theJoint )
theWorld = WorldPart.Create()
spaces = getSpaceSwitchControls( theJoint )
spaces.append( theWorld.getControl( 'control' ) )
#determine default names for the given controls
names = []
for s in spaces:
names.append( generateNiceControlName( s ) )
additionalParents = list( additionalParents )
additionalParentNames = list( additionalParentNames )
for n in range( len( additionalParentNames ), len( additionalParents ) ):
additionalParentNames.append( generateNiceControlName( additionalParents[ n ] ) )
spaces += additionalParents
names += additionalParentNames
#we don't care about space switching if there aren't any non world spaces...
if not spaces:
return
if reverseHierarchy:
spaces.reverse()
names.reverse()
return spaceSwitching.build( control, spaces, names, **buildKwargs )
def getParentAndRootControl( theJoint ):
'''
returns a 2 tuple containing the nearest control up the hierarchy, and the
most likely control to use as the "root" control for the rig. either of these
may be the world control, but both values are guaranteed to be an existing
control object
'''
parentControl, rootControl = None, None
for p in api.iterParents( theJoint ):
theControl = getItemRigControl( p )
if theControl is None:
continue
if parentControl is None:
parentControl = theControl
skelPart = SkeletonPart.InitFromItem( p )
if isinstance( skelPart, skeletonBuilder.Root ):
rootControl = theControl
if parentControl is None or rootControl is None:
world = WorldPart.Create()
if parentControl is None:
parentControl = world.getControl( 'control' )
if rootControl is None:
rootControl = world.getControl( 'control' )
return parentControl, rootControl
def createLineOfActionMenu( controls, joints ):
'''
deals with adding a "draw line of action" menu to each control in the controls
list. the line is drawn through the list of joints passed
'''
if not joints: return
if not isinstance( controls, (list, tuple) ):
controls = [ controls ]
joints = list( joints )
jParent = getNodeParent( joints[ 0 ] )
if jParent:
joints.insert( 0, jParent )
for c in controls:
cTrigger = Trigger( c )
spineConnects = [ cTrigger.connect( j ) for j in joints ]
Trigger.CreateMenu( c,
"draw line of action",
"zooLineOfAction;\nzooLineOfAction_multi { %s } \"\";" % ', '.join( '"%%%d"'%idx for idx in spineConnects ) )
class RigSubPart(RigPart):
'''
'''
#this attribute describes what skeleton parts the rig primitive is associated with. If the attribute's value is None, then the rig primitive
#is considered a "hidden" primitive that has
SKELETON_PRIM_ASSOC = None
class PrimaryRigPart(RigPart):
'''
all subclasses of this class are exposed as available rigging methods to the user
'''
AVAILABLE_IN_UI = True
class WorldPart(RigPart):
'''
the world part can only be created once per scene. if an existing world part instance is found
when calling WorldPart.Create() it will be returned instead of creating a new instance
'''
__version__ = 0
CONTROL_NAMES = 'control', 'exportRelative'
NAMED_NODE_NAMES = 'parts', 'masterQss', 'qss'
WORLD_OBJ_MENUS = [ ('toggle rig vis', """{\nstring $childs[] = `listRelatives -pa -type transform #`;\nint $vis = !`getAttr ( $childs[0]+\".v\" )`;\nfor($a in $childs) if( `objExists ( $a+\".v\" )`) if( `getAttr -se ( $a+\".v\" )`) setAttr ( $a+\".v\" ) $vis;\n}"""),
('draw all lines of action', """string $menuObjs[] = `zooGetObjsWithMenus`;\nfor( $m in $menuObjs ) {\n\tint $cmds[] = `zooObjMenuListCmds $m`;\n\tfor( $c in $cmds ) {\n\t\tstring $name = `zooGetObjMenuCmdName $m $c`;\n\t\tif( `match \"draw line of action\" $name` != \"\" ) eval(`zooPopulateCmdStr $m (zooGetObjMenuCmdStr($m,$c)) {}`);\n\t\t}\n\t}"""),
('show "export relative" node', """""") ]
@classmethod
@d_unifyUndo
def Create( cls, **kw ):
for existingWorld in cls.IterAllParts():
return existingWorld
#try to determine scale - walk through all existing skeleton parts in the scene
for skeletonPart in SkeletonPart.IterAllPartsInOrder():
kw.setdefault( 'scale', skeletonPart.getBuildScale() )
break
worldNodes, (controls, namedNodes) = getNodesCreatedBy( cls._build, **kw )
worldPart = buildContainer( WorldPart, { 'idx': 0 }, worldNodes, controls, namedNodes )
#check to see if there is a layer for the rig controls and add controls to it
if objExists( 'rig_controls' ) and nodeType( 'rig_controls' ) == 'displayLayer':
rigLayer = 'rig_controls'
else:
rigLayer = createDisplayLayer( name='rig_controls', empty=True )
editDisplayLayerMembers( rigLayer, controls, noRecurse=True )
return worldPart
@classmethod
def _build( cls, **kw ):
scale = kw.get( 'scale', skeletonBuilder.TYPICAL_HEIGHT )
scale /= 1.5
world = buildControl( 'main', shapeDesc=ShapeDesc( None, 'hex', AX_Y ), oriented=False, scale=scale, niceName='The World' )
parts = group( empty=True, name='parts_grp' )
qss = sets( empty=True, text="gCharacterSet", n="body_ctrls" )
masterQss = sets( empty=True, text="gCharacterSet", n="all_ctrls" )
exportRelative = buildControl( 'exportRelative', shapeDesc=ShapeDesc( None, 'cube', AX_Y_NEG ), pivotModeDesc=PivotModeDesc.BASE, oriented=False, size=(1, 0.5, 1), scale=scale )
parentConstraint( world, exportRelative )
attrState( exportRelative, ('t', 'r', 's'), *LOCK_HIDE )
attrState( exportRelative, 'v', *HIDE )
setAttr( '%s.v' % exportRelative, False )
#turn scale segment compensation off for all joints in the scene
for j in ls( type='joint' ):
setAttr( '%s.ssc' % j, False )
sets( qss, add=masterQss )
attrState( world, 's', *NORMAL )
connectAttr( '%s.scale' % world, '%s.scale' % parts )
connectAttr( '%s.scaleX' % world, '%s.scaleY' % world )
connectAttr( '%s.scaleX' % world, '%s.scaleZ' % world )
#add right click items to the world controller menu
worldTrigger = Trigger( str( world ) )
qssIdx = worldTrigger.connect( str( masterQss ) )
#add world control to master qss