stretchy ik spline maya python

A quick function for creating a stretchy ik spline.

strCurve = name of the curve, or have a curve selected
iNum = number of joints you would like created
CreateStretchyIk(strCurve, iNum)

def CreateStretchyIk(strCurve=None, iNum):
    strCurve = strCurve
    
    if not strCurve:
        Mc.ls(sl=True)[0]
        
    lStrJnt = []
    
    Mc.select(cl=True)
    
    for i in range(iNum):
        strI = '%s' %i
        strJnt = Mc.joint(n=strCurve+strI)
        lStrJnt.append(strJnt)
        
    strCurveIk = Mc.ikHandle(sj=lStrJnt[0], ee=lStrJnt[-1], c=strCurve, n=strCurve+'_ik', sol='ikSplineSolver', ccv=False, rootOnCurve=True, parentCurve=False)[0]   
    
    gArcLen = Mc.arclen(strCurve)
    
    gAverage = gArcLen/(iNum-1)
    
    strCurveNode = Mc.createNode('curveInfo', n=strCurve+'_curveInfo')
    
    strMultNode = Mc.createNode('multiplyDivide', n=strCurve+'_divide')
    Mc.setAttr(strMultNode+'.operation', 2)
    Mc.setAttr(strMultNode+'.input2X', gArcLen)
    
    Mc.connectAttr(strCurveNode+'.arcLength', strMultNode+'.input1X')
    
    Mc.connectAttr(strCurve+'Shape.worldSpace', strCurveNode+'.inputCurve')
    
    for i in range(iNum):
        if i > 0:
            Mc.setAttr(lStrJnt[i]+'.tx', gAverage)
            
    for jnt in lStrJnt:
        Mc.connectAttr(strMultNode+'.outputX', jnt+'.scaleX')

Leave a Reply

Your email address will not be published. Required fields are marked *