-
Notifications
You must be signed in to change notification settings - Fork 0
/
rig_ikfk.py
163 lines (112 loc) · 4.75 KB
/
rig_ikfk.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
import pymel.all as pm
from rig_basic import *
import controls
import utils, settings
class FkRig( BasicRig ) :
PARTNAME = 'fkRig'
# def __init__( self ) :
# super( FkRig, self ).__init__()
def create( self, _jointchain=None ) :
super( FkRig, self ).create( _jointchain )
return True
def addinSquashStretch( self ) :
pass
def addinDistributedTwist( self ) :
pass
class IkRig( BasicRig ) :
PARTNAME = 'ikRig'
def create( self, _jointchain=None ) :
super( IkRig, self ).create( _jointchain )
jointchains = self.tree_children( 'jointchain' )
if( len( jointchains ) != 1 ) :
utils.err( 'IkRig children includes more than ONE jointchain. Not sure which to use. Skipping...' )
return False
jointchain = jointchains[0]
# create a simple joint chain
simplejointchain = jointchain.duplicate_jointchain( self.PARTNAME, 'driver', _simple=True )
self.add_child( simplejointchain )
pm.PyNode( 'leftUpperArm_1_IKJ_DRIVER' ).hide()
pm.PyNode( 'leftUpperArm_1_j' ).hide()
for i in range( len( simplejointchain.rigjoints ) - 1 ) :
# create a curve between each pair of simple rigjoints
rigjoint1 = simplejointchain.rigjoints[i]
rigjoint2 = simplejointchain.rigjoints[i+1]
v1 = pm.datatypes.Vector( rigjoint1.getTranslation( space='world' ) )
v2 = pm.datatypes.Vector( rigjoint2.getTranslation( space='world' ) )
curvelength = float( v1.distanceTo( v2 ) )
dirvector = [ a * b for a, b in zip(
( curvelength, curvelength, curvelength ),
utils.aim_axis_to_vectors( settings.rotationorder )[0]
) ]
curve = pm.curve( degree=1, point=[
( 0, 0, 0 ),
dirvector
# v1, v2
], name=utils.name_from_tags( rigjoint1, 'curve' ) )
# rebuild with numspan 2 and 3 degree
pm.rebuildCurve( curve, degree=3, spans=2 )
# move vtx[1] and vtx[-2] to respective ends of curve
curve.cv[1].setPosition( curve.cv[0].getPosition( space='world' ), space='world' )
curve.cv[-2].setPosition( curve.cv[-1].getPosition( space='world' ), space='world' )
ribbonlength = 0.2
ribbon = pm.extrude(
curve,
polygon=0,
useProfileNormal=1,
extrudeType=0,
length=ribbonlength,
ch=False,
name=utils.name_from_tags( rigjoint1, 'nurbs' )
)[0]
ribbon.setTranslation( ( 0, 0, -(ribbonlength)/2 ) )
ribbon.setPivots( ( 0, 0, 0 ), worldSpace=True )
pm.makeIdentity( ribbon, apply=True )
pm.delete( ribbon, ch=True )
pm.delete( curve )
utils.create_zero_sdk_groups( ribbon, _replacelast=False )
startcluster = pm.cluster( ribbon.cv[0:1][0:1], name=utils.name_from_tags( rigjoint1, 'start', 'cluster') )[1]
midcluster = pm.cluster( ribbon.cv[2][0:1], name=utils.name_from_tags( rigjoint1, 'mid', 'cluster' ) )[1]
endcluster = pm.cluster( ribbon.cv[-2:][0:1], name=utils.name_from_tags( rigjoint1, 'end', 'cluster' ) )[1]
# parent clusters to respective rigjoints
pm.parentConstraint( [ rigjoint1, startcluster ], mo=False )
pm.parentConstraint( [ rigjoint2, endcluster ], mo=False )
# group then point/parent constrain middle cluster to end clusters
sdkgroup, zerogroup = utils.create_zero_sdk_groups( midcluster, _replacelast=False )
zerogroup.setRotation( rigjoint1.getRotation( space='world' ), space='world' )
pm.pointConstraint( [ rigjoint1, rigjoint2, zerogroup ], mo=False )
pm.orientConstraint( [ rigjoint1, zerogroup ], mo=False )
jointsToAttachToCurve = [ jointchain.rigjoints[i] ]
jointsToAttachToCurve += jointchain.minorrigjoints[ jointsToAttachToCurve[0] ]
jointsToAttachToCurve += [ jointchain.rigjoints[i+1] ]
for rigjoint in jointsToAttachToCurve :
p = rigjoint.getTranslation( space='world' )
posi = pm.nodetypes.ClosestPointOnSurface()
ribbon.worldSpace >> posi.inputSurface
posi.inPosition.set( p )
u = min( max( posi.u.get(), 0.001 ), 0.999 )
v = min( max( posi.v.get(), 0.001 ), 0.999 )
pm.delete( posi )
follicleshape = pm.nodetypes.Follicle()
ribbon.local >> follicleshape.inputSurface
ribbon.worldMatrix[0] >> follicleshape.inputWorldMatrix
follicleshape.parameterU.set( u )
follicleshape.parameterV.set( v )
follicle = follicleshape.getParent()
follicle.rename( utils.name_from_tags( rigjoint, 'follicle' ) )
follicleshape.rename( follicle.name() + 'Shape' )
follicleshape.outRotate >> follicle.rotate
follicleshape.outTranslate >> follicle.translate
# remove any constraints already on the joint
pm.delete( rigjoint.getChildren( type='constraint' ) )
pm.parentConstraint( [ follicle, rigjoint ], mo=True )
return True
def addinSquashStretch( self ) :
pass
def addinDistributedTwist( self ) :
pass
class IkFkBlendRig( BlendRig ) :
PARTNAME = 'ikfkBlendRig'
def __init__( self ) :
pass
def create( self, _jointchain=None ) :
pass