Stretchy IK Leg Re-write

I re-wrote my stretchyIK leg script to work a bit cleaner with object-oriented programming. I eliminated all the global variables, simplified the structure of the stretchy limb nodal setup, and kept it so that I can pause and position my foot joints before going on to create the rest of the setup.

There are still some issues with this though of course. Initially, this was written as several different classes all inheriting from one another. It made sense: the reverse foot inherits attributes from the IK leg, which inherits attributes from the FK leg, which (originally) was going to inherit attributes from this whole locator-based limb positioning setup… I had to scrap that because I couldn’t figure out how to get that “pause” in the creation of my objects to be able to place my joints.

Ideally, I’d be able to customize the position of all the joints before adding on all the bells and whistles so that I could use this setup with a variety of characters and species. But for now, this is what I’ve got.

import maya.cmds as cmds
    
class FkLeg(object):
    def __init__(self):
        self.upLimb= cmds.joint(n="upLimb#",p=(0,0,0),rad=.3)
        self.lowLimb = cmds.joint(n="lowLimb#",p=(0,-1,.001),rad=.3)
        self.endLimb = cmds.joint(n="endLimb#",p=(0,-2,0),rad=.3)

        self.foot = cmds.joint(n="foot#",p=(0,-2,.5),rad=.3)
        self.toe = cmds.joint(n="toe#",p=(0,-2,1),rad=.3)
        self.jointGrp = cmds.group(self.upLimb,n="jointGrp#")
        self.legJntGrp = cmds.group(self.upLimb,n="legJointGrp#")

    # Get location from methods instead of static attributes. 
    def upLimbPos(self):
        return cmds.xform(self.upLimb,q=1,t=1,ws=1)
    def lowLimbPos(self):
        return cmds.xform(self.lowLimb,q=1,t=1,ws=1)
    def endLimbPos(self):
        return cmds.xform(self.endLimb,q=1,t=1,ws=1)
    def footPos(self):
        return cmds.xform(self.foot,q=1,t=1,ws=1)
    def toePos(self):
        return cmds.xform(self.toe,q=1,t=1,ws=1)

class IKleg(FkLeg):
    def __init__(self):
        FkLeg.__init__(self)
        cmds.select(self.upLimb)
        cmds.OrientJoint()
        self.legIK = cmds.ikHandle(sj=self.upLimb,ee=self.endLimb,sol="ikRPsolver")
        self.legIKgrp = cmds.group(self.legIK[0],n="legIK_GRP#")
        self.legIK[0] = cmds.rename(self.legIK[0],"legIK#")
        self.kneePoint = cmds.spaceLocator(n="kneePoint#")
        self.kneePointOrient = cmds.group(self.kneePoint,n="kneePoint_ORIENT")
        cmds.xform(self.kneePointOrient,t=(0,-1,1))
        cmds.poleVectorConstraint(self.kneePoint,self.legIK[0])
        
        self.legGrp = cmds.group(self.jointGrp,self.legIKgrp,self.kneePointOrient,n="ikLeg_GRP#")
        
    def limbScale(self):
        return cmds.getAttr(self.legGrp + ".sx")
    def IKfoot(self):
        self.toeIK = cmds.ikHandle(sj=self.foot,ee=self.toe)
        self.toeIKgrp = cmds.group(self.toeIK[0],n="toeIK_GRP#")
        cmds.rename(self.toeIK[0],"toeIK#")
        self.footIK = cmds.ikHandle(sj=self.endLimb,ee=self.foot)
        self.footIKgrp = cmds.group(self.footIK[0],n="footIK_GRP#")
        cmds.rename(self.footIK[0],"footIK#")
        cmds.parent(self.footIKgrp,self.legGrp)
        cmds.parent(self.toeIKgrp,self.legGrp)
        self.ctrlGrp = cmds.group(self.footIKgrp,self.toeIKgrp,self.legIKgrp,self.kneePointOrient,n="contorlGrp#")
   
    def reverseFoot(self):
        cmds.select(self.legGrp)
        self.reverseHeel = cmds.joint(p=(self.endLimbPos()[0],self.toePos()[1],self.endLimbPos()[2]),n="reverseHeel#",rad=.5)
        self.reverseToe = cmds.joint(p=(self.toePos()[0],self.toePos()[1],self.toePos()[2]),n="reverseToe#",rad=.5)
        self.reverseFoot = cmds.joint(p=(self.footPos()[0],self.footPos()[1],self.footPos()[2]),n="reverseFoot#",rad=.5)
        self.reverseAnkle = cmds.joint(p=(self.endLimbPos()[0],self.endLimbPos()[1],self.endLimbPos()[2]),n="reverseAnkle#",rad=.5)
        
        self.reverseFootGrp = cmds.group(self.reverseHeel,n="reverseFootGrp#")
        
        cmds.select(self.reverseHeel)
        cmds.OrientJoint()

        cmds.parentConstraint(self.reverseToe,self.toeIKgrp,mo=1)
        cmds.parentConstraint(self.reverseToe,self.footIKgrp,mo=1)
        cmds.parentConstraint(self.reverseAnkle,self.legIKgrp,mo=1)
        
        cmds.parent(self.reverseFootGrp,self.jointGrp)
    def stretchyLeg(self):
        
        self.distDimStart = cmds.spaceLocator(n="distDimStart#")
        self.distDimEnd = cmds.spaceLocator(n="distDimEnd#")
        cmds.xform(self.distDimEnd,t=(self.endLimbPos()[0],self.endLimbPos()[1],self.endLimbPos()[2]))
        cmds.select(self.distDimStart,self.distDimEnd,add=1)
        self.distDim = cmds.distanceDimension()
        cmds.select(self.distDim)
        self.distDimShape = cmds.pickWalk(d="down")[0]

        cmds.pointConstraint(self.reverseAnkle,self.distDimEnd,mo=1)
        cmds.pointConstraint(self.upLimb,self.distDimStart,mo=1)
        
        self.distGrp = cmds.group(self.distDimStart,self.distDimEnd,self.distDim,n="distGrp#")
        cmds.parent(self.distGrp, self.legGrp)
        
        self.distScaleComp = cmds.createNode("multiplyDivide",n="distScaleComp#")
        cmds.setAttr(self.distScaleComp + ".i1x",2)
        cmds.connectAttr(self.legGrp + ".sx",self.distScaleComp + ".i2x")
        
        self.currDist = cmds.createNode("multiplyDivide",n="currDist#")
        cmds.setAttr(self.currDist + ".op",2)
        cmds.connectAttr(self.distScaleComp + ".ox",self.currDist + ".i2x")
        cmds.connectAttr(self.distDimShape + ".distance",self.currDist + ".i1x")
        
        self.stretchyCondition = cmds.createNode("condition",n="stretchyCondition#")
        cmds.setAttr(self.stretchyCondition + ".op",2)
        cmds.setAttr(self.stretchyCondition + ".cfr",1)
        cmds.connectAttr(self.currDist + ".ox",self.stretchyCondition + ".ctr")
        cmds.connectAttr(self.currDist + ".ox",self.stretchyCondition + ".ft")
        cmds.setAttr(self.stretchyCondition + ".st",1)
        
        cmds.connectAttr(self.stretchyCondition + ".ocr",self.lowLimb + ".tx")
        cmds.connectAttr(self.stretchyCondition + ".ocr",self.endLimb + ".tx")
        
myLeg = IKleg()
myLeg.IKfoot()
myLeg.reverseFoot()
myLeg.stretchyLeg()


 

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