def bakeRotateDelta( src, ctrl, presetStr ): ''' Bakes a post trace command into the ctrl object such that when ctrl is aligned to src (with post trace cmds enabled) src and tgt are perfectly aligned. This is useful because rig controls are rarely aligned to the actual joints they drive, but it can be useful when you have motion from a tool such as SFM, or generated from motion capture that needs to be applied back to a rig. ''' mat_j = Matrix( getAttr( '%s.worldInverseMatrix' % srcJoint ) ) mat_c = Matrix( getAttr( '%s.worldMatrix' % jointControl ) ) #generate the matrix describing offset between joint and the rig control mat_o = mat_j * mat_c #put into space of the control rel_mat = mat_o * Matrix( getAttr( '%s.parentInverseMatrix' % jointControl ) ) #now figure out the euler rotations for the offset ro = getAttr( '%s.ro' % jointControl ) offset = MATRIX_ROTATION_ORDER_CONVERSIONS_TO[ ro ]( rel_mat, True ) cmd.rotate( asEuler[ 0 ], asEuler[ 1 ], asEuler[ 2 ], jointControl, relative=True, os=True ) mel.zooSetPostTraceCmd( ctrl, presetStr % offset ) mel.zooAlign( "-src %s -tgt %s -postCmds 1" % (src, ctrl) ) return offset
def switchToIK( control, ikHandle=None, poleControl=None, onCmd=None, offCmd=None ): ''' this proc will align the IK controller to its fk chain flags used: -control this is the actual control being used to move the ikHandle - it is assumed to be the same object as the ikHandle, but if its different (ie if the ikHandle is constrained to a controller) use this flag -pole tells the script the name of the pole controller - if there is no pole vector control, leave this flag out -ikHandle this flag specifies the name of the ikHandle to work on -onCmd this flag tells the script what command to run to turn the ik handle on - it is often left blank because its assumed we're already in ik mode -offCmd this flag holds the command to turn the ik handle off, and switch to fk mode NOTE: if the offCmd isn't specified, it defaults to: if( `getAttr -se ^.ikb` ) setAttr ^.ikb 1; symbols to use in cmd strings: ^ refers to the ikHandle # refers to the control object example: zooAlignIK "-control somObj -ikHandle ikHandle1 -offCmd setAttr #.fkMode 0"; ''' if ikHandle is None: ikHandle = control if callable( onCmd ): onCmd( control, ikHandle, poleControl ) joints = cmd.ikHandle( ikHandle, q=True, jl=True ) effector = cmd.ikHandle( ikHandle, q=True, ee=True ) effectorCtrl = listConnections( '%s.tx' % effector, d=False )[ 0 ] mel.zooAlign( "-src %s -tgt %s" % (effectorCtrl, control) ) if poleControl is not None and objExists( poleControl ): pos = mel.zooFindPolePosition( "-start %s -mid %s -end %s" % (joints[ 0 ], joints[ 1 ], effectorCtrl) ) move( pos[0], pos[1], pos[2], poleControl, a=True, ws=True, rpr=True ) if callable( offCmd ): offCmd( control, ikHandle, poleControl )