Stretchy IK Script

Hello! This is a script that sets up a 2 joint stretchy IK setup. It pairs with my post from a few days ago and uses the same node setup.

There are a few issues with this script, including my wonky workaround for creating unique names and the fact that if you try to run it twice without moving the first joint away from the origin, it will not create a second joint chain.

However, it works for my purposes, and I’m proud that I was able to get it up and running. The script doesn’t do everything for you. You still need to set up the IK to be a Rotate Plane Solver, add the PoleVector knee locator, and set up an Enum switch to toggle it to FK, but it sure speeds up most of the process!

import maya.cmds as cmds

def setupStretchyLimb():
upLimb = nameUniquely("upLimb")
lowLimb = nameUniquely("lowLimb")
endLimb = nameUniquely("endLimb")
stretchyIK = nameUniquely("stretchyIK")
distShapeStart = nameUniquely("distShape_Start")
distShapeEnd = nameUniquely("distShape_End")
stretchyDist = nameUniquely("stretchyDist")
limbAnim = nameUniquely("limb_ANIM")
jointGrp = nameUniquely("jointGRP")
controlGrp = nameUniquely("controlGroup")
distDimGrp = nameUniquely("distDimGrp")
distCompensate = nameUniquely("distCompensate")
scaleCompensate = nameUniquely("scaleCompensate")
currDist = nameUniquely("currDistance")
getScaleAmt = nameUniquely("getScaleAmt")
scaleCondition = nameUniquely("scaleCondition")
stretchyDistShape = nameUniquely("stretchyDistShape")
scaleJointLen = nameUniquely("scaleJointLen")

cmds.joint(n=upLimb,p=(0,0,0),rad=.3)
cmds.joint(n=lowLimb,p=(1,0,.05),rad=.3)
cmds.joint(n=endLimb,p=(2,0,0),rad=.3)

cmds.select(upLimb)
cmds.OrientJoint()

cmds.select(endLimb,add=1)

cmds.ikHandle()
cmds.rename("ikHandle1",stretchyIK)
cmds.spaceLocator(p=(0,0,0),n=distShapeStart)
cmds.spaceLocator(p=(2,0,0),n=distShapeEnd)
cmds.distanceDimension(sp=(0,0,0),ep=(2,0,0))
cmds.rename("distanceDimension1",stretchyDist)
cmds.select(stretchyDist)
b = cmds.pickWalk(d="down")
stretchyDistShape = b[0]
cmds.spaceLocator(p=(0,0,0),n=limbAnim)
cmds.move(2,0,0,limbAnim,wd=1)
cmds.parentConstraint(limbAnim,distShapeEnd,mo=1)
cmds.pointConstraint(limbAnim,stretchyIK)

cmds.group(upLimb,n=jointGrp)
cmds.group(distShapeStart,distShapeEnd,stretchyDist,limbAnim,stretchyIK,n=controlGrp)
limbGrp = cmds.group(jointGrp,controlGrp)
cmds.rename(limbGrp,nameUniquely("Limb_GRP"))

globalScale = cmds.getAttr(controlGrp + ".sx")

print globalScale

cmds.createNode("multiplyDivide",n=distCompensate)
cmds.createNode("multiplyDivide",n=currDist)
cmds.createNode("multiplyDivide",n=scaleCompensate)
cmds.createNode("condition",n=scaleCondition)

cmds.setAttr(distCompensate + ".i1x",2)
cmds.connectAttr(controlGrp + ".sx",distCompensate + ".i2x")
cmds.connectAttr(distCompensate + ".ox",currDist + ".i2x")
cmds.connectAttr(stretchyDistShape + ".distance", currDist + ".i1x")

cmds.setAttr(currDist + ".op",2)
cmds.connectAttr(currDist + ".ox", scaleCompensate + ".i2x")
cmds.connectAttr(controlGrp + ".sx", scaleCompensate + ".i1x")

cmds.connectAttr(scaleCompensate + ".ox",scaleCondition + ".ctr")
cmds.connectAttr(controlGrp + ".sx",scaleCondition + ".cfr")
cmds.setAttr(scaleCondition + ".op",2)
cmds.connectAttr(currDist + ".ox",scaleCondition + ".ft")
cmds.setAttr(scaleCondition + ".st",1)

cmds.connectAttr(scaleCondition + ".ocr",lowLimb + ".tx")
cmds.connectAttr(scaleCondition + ".ocr",endLimb + ".tx")

cmds.setAttr(distShapeEnd + ".visibility",0)
cmds.setAttr(stretchyIK + ".visibility",0)

def nameUniquely(myName):
newName = myName
i = 1
if(cmds.objExists(myName)):
while (cmds.objExists(newName)):
newName = myName + "_" + str(i)
i += 1
return newName
else:
return myName

setupStretchyLimb()

 

 

Advertisements
Image

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s