….Off to watch Monstors University tonight…. Here’s nk_makeStretchyIK_RPSC

Happy first Wednesday of the week folks!

Here’s a Python script that creates a stretchy joint chain, I’ll aim to incorporate this into a more user friendly GUI soon…

It works on RotatePlane and Single Chain solvers. Enjoy.


import maya.cmds as mc

def nk_makeStretchyIK_RPSC(ikH = "ikHandle1", prefix="prefix", StretchCondition=True):
    '''
    ABOUT
    --------
    Makes an ik handle stretchy.  works on RP or SC solvers.

    REQUIRES
    --------
    nk_measure2Points

    USAGE
    --------
    * Declare ikHandle, give it a prefix run script

    NOTES
    --------
    * This creates a grp with all the distance locators, parent these to the parent of the first joint in the chain.
    ... So if the first joint is an upper arm, parent the Distance grp to the clav joint(assuming that is the parent)

    VERSIONS
    --------
    v_01:
    *   Stretchy all the time.
    v_02:
    *   Introduced an an option to only stretch when extended beyond the full length of the chain.

    TODO
    --------
    * Add an option for inverse scale

    '''

    ## --- Get the joints ---
    joints = mc.ikHandle(ikH, q=True, jl=True)
    # Since the joingList query does not return the end joint
    # ...We also need to get the end joint and append it to the list joints[]
    # ...We can do this by getting the last joint in the list, and querying its child
    lastJointinList = joints[(len(joints)-1)]
    endJoint = mc.listRelatives(lastJointinList, c=True, type="joint")
    joints.append(endJoint[0])

    ## ---Get the Distance---
    measureGrp =mc.group(em=True, n=prefix+'_distanceLocators_grp')
    distLocators = []
    #Get the position of each joint
    for jnt in joints:
        jointPostion = mc.xform(jnt, q=True, ws=True, t=True)
        #Create distance Locators
        distLocator = mc.spaceLocator(n=(prefix+'_'+jnt+'_'+'distLoc'))
        #Position Locators
        mc.xform(distLocator, t = (jointPostion[0], jointPostion[1], jointPostion[2]))
        #parent to grp
        mc.parent(distLocator,measureGrp)
        #add locators to list
        distLocators.append(distLocator[0])

    #Empty List to store Distance Between Noded
    distanceBetweenNodes = []
    #Measure the distance and the dbNodes to the list
    for i in range(len(distLocators)-1):
        distanceNode = nk_measure2Points(distLocators[i], distLocators[i+1],(distLocators[i]+'_to_'+distLocators[i+1]+'_dbNode'))
        distanceBetweenNodes.append(distanceNode)

    ## --- Setup Scale ---
    ## NewDistance/Original scale = StretchScale
    ## OriginalScale/NewDistance = InvScale

    #Add the distance -- create plus minus average --
    pmaNode = mc.shadingNode("plusMinusAverage", asUtility=True, n=(prefix+"_stretch_pma"))

    for i in range(len(distanceBetweenNodes)):

        print distanceBetweenNodes[i]
        print (pmaNode + '.input1[{i}]'.format(i=i))
        mc.connectAttr(('{db}.distance'.format(db=distanceBetweenNodes[i])), (pmaNode + '.input1D[{i}]'.format(i=i)))

    # -- get the Ik distance --
    ##Create a locator point constrain to ikHandle and add to grp
    ikDistanceLoc = mc.spaceLocator(n=(prefix +'_' + ikH + '_distLoc'))

    mc.parent(ikDistanceLoc[0], measureGrp)
    mc.pointConstraint(ikH, ikDistanceLoc[0])
    ikDistanceNode = nk_measure2Points(distLocators[0],ikDistanceLoc[0], (ikDistanceLoc[0]+'_dbNode'))

    # -- Stretch Condition --
    if StretchCondition == True:
        # If the Ik Handle stretches beyond the length of the total length, then the condtion will output the ikDistance to the mdNode
        # ... The scale factor will == ikDistance/OrginalDistance(orThe full Length of the joint chain)
        # If the ik Distance is less than the OrigDistance, the condition will output the OrginalDistance
        # ... The scale factor will == OrigDistance/OrigDistance. So the value of scale will be 1.

        condtionNode = mc.shadingNode("condition", asUtility=True, n=prefix+"_stretchScale_Cond")
        mc.setAttr (('{condtionNode}.operation'.format(condtionNode=condtionNode)), 3)
        mc.connectAttr (('{ikDistanceNode}.distance'.format(ikDistanceNode=ikDistanceNode)) ,('{condtionNode}.firstTerm'.format(condtionNode=condtionNode)))
        mc.connectAttr (('{ikDistanceNode}.distance'.format(ikDistanceNode=ikDistanceNode)) ,('{condtionNode}.colorIfTrueR'.format(condtionNode=condtionNode)))

        mc.connectAttr (('{pmaNode}.output1D'.format(pmaNode=pmaNode)),('{condtionNode}.secondTerm'.format(condtionNode=condtionNode)))
        mc.connectAttr (('{pmaNode}.output1D'.format(pmaNode=pmaNode)), ('{condtionNode}.colorIfFalseR'.format(condtionNode=condtionNode)))

        #create multiply divide node
        stretch_mdNode = mc.shadingNode("multiplyDivide", asUtility=True, n=prefix+"_stretchScale_md")
        mc.setAttr((stretch_mdNode+'.operation'),2)
        # Divide ik distance by OrigDistance
        #mc.connectAttr ikDistanceNode > to mdi1x
        mc.connectAttr ('{condtionNode}.outColorR'.format(condtionNode=condtionNode), ('{stretch_mdNode}.input1X').format(stretch_mdNode=stretch_mdNode))
        mc.connectAttr (('{pmaNode}.output1D'.format(pmaNode=pmaNode)), ('{stretch_mdNode}.input2X'.format(stretch_mdNode=stretch_mdNode)))

    if StretchCondition == False:
        # If the user does not require a condtion, the ikHandle will stretch when extended and when shortened

        #create multiply divide node
        stretch_mdNode = mc.shadingNode("multiplyDivide", asUtility=True, n=prefix+"_stretchScale_md")
        mc.setAttr((stretch_mdNode+'.operation'),2)
        # Divide ik distance by OrigDistance
        #mc.connectAttr ikDistanceNode > to mdi1x
        mc.connectAttr ('{ikDistanceNode}.distance'.format(ikDistanceNode=ikDistanceNode), ('{stretch_mdNode}.input1X').format(stretch_mdNode=stretch_mdNode))
        mc.connectAttr (('{pmaNode}.output1D'.format(pmaNode=pmaNode)), ('{stretch_mdNode}.input2X'.format(stretch_mdNode=stretch_mdNode)))

    # we dont wat to attach to the last joint, so loop through the joints and connect except the last one
    jntCnt = len(joints)-1
    for i in range(jntCnt):

        mc.connectAttr((stretch_mdNode + '.outputX'), (joints[i]+ '.scaleX'), force = True )

    ##For some reason the ikHandle seems to not hold in Place if it is in worldSpace
    #...or not parented to a transform
    #...As such, we just need to pop it back to it's oirginal position
    #...here it just snaps back to the last locator
    mc.delete(mc.pointConstraint(distLocators[0-1],ikH))

#nk_makeStretchyIK_RPSC(ikH = "ikHandle2", prefix = "prefix")

##+++++++++++++++++++++++++++++++++##
##++++++++nk_measure2Points++++++++##
##+++++++++++++++++++++++++++++++++##

def nk_measure2Points(itemA='itemA', itemB='itemA', distanceBetweenNodeName='dbNode'):

    dbNode = mc.createNode ('distanceBetween', name=distanceBetweenNodeName)

    mc.connectAttr('{itemA}.worldMatrix'.format(itemA=itemA),'{dbNode}.inMatrix1'.format(dbNode=dbNode))
    mc.connectAttr('{itemB}.worldMatrix'.format(itemB=itemB),'{dbNode}.inMatrix2'.format(dbNode=dbNode))

    mc.connectAttr('{itemA}.rotatePivotTranslate'.format(itemA=itemA),'{dbNode}.point1'.format(dbNode=dbNode))
    mc.connectAttr('{itemB}.rotatePivotTranslate'.format(itemB=itemB),'{dbNode}.point2'.format(dbNode=dbNode))

    return dbNode


Leave a Reply