Help for walk cycle python script


#1

I’m having a really hard time with python. This is what I have of the script so far. Can anyone help me add a rotation for walk cycle? Nothing fancy is necessary. I just can’t figure out where to place a function to allow for a slight rotation when it hits the top of the sin wave and then the bottom. Anything I’ve tried breaks code and either it starts rotating but not moving forward in x , or stays fixed in rotation angle. Would really appreciate it! Thanks.

import math as m
class walkcycle(object):
    def __init__(self):
        print ('initial all our variable')
        self.leftCurve = ""
        self.rightCurve = ""
        
        self.amp = 1
        self.freq = 1
        self.shift_y = 0
        
        self.shift_xl = 0 #offset of left foot
        self.shift_xr = 0 #offset of right foot
        
        self.footPosR = 0 #position of the right foot
        self.footPosL = 1 #position of the left foot
        
        self.Rcounter = 0 #defines how right foot moves in X
        self.Lcounter = 0 #definte how left foot moves in X
        
        self.RFP = [] #right foot points RFP
        self.LFP = [] #left foot points RFP
        
        self.rightFoot = ""
        self.leftFoot = ""
        
        self.rightScale = 1
        self.leftScale = 1
        
        self.makeFeet() #make the feet
        
        #scale the feet
                
# make the feet        
    def makeFeet(self):
        self.rightFoot = cmds.polyCube()[0]
        self.leftFoot = cmds.polyCube()[0]
        
    def scaleFeet(self,size):
        cmds.scale(size,1,1,self.rightFoot)
        cmds.scale(size,1,1,self.leftFoot)
        self.rightScale = size
        self.leftScale = size
        
# develop algorithm
    def walk_alg(self): 
        
        if cmds.objExists(self.leftCurve): #does the curve exist
            cmds.delete(self.rightCurve) #delete the curve
            cmds.delete(self.leftCurve) #delete the curve
        else:
            print "nothing done"
        
        self.RFP = [] #right foot points RFP
        self.LFP = [] #left foot points RFP
        self.Rcounter = 0 #reset counter
        self.Lcounter = 0 #reset counter
        
        #counter is the number that allows you to move in the x axis
        #we can't use the loop because the loop is contious and walking has pauses
        #because we need to balance on 1 foot while the other foot is in the air
       
        
        #create the dataset/curve for both curves
        for x in range(100):
            
            rightY = self.amp * m.cos(x*self.freq + self.shift_xr) + self.shift_y
            if (rightY < 0):
                rightY = 0
            else:
                self.Rcounter = self.Rcounter - 1
            
            leftY = self.amp * m.cos(x*self.freq + self.shift_xl) + self.shift_y
            if (leftY < 0):
                leftY = 0
            else:
                self.Lcounter = self.Lcounter - 1
            
            self.RFP.append((self.Rcounter,rightY,0))
            self.LFP.append((self.Lcounter,leftY,0))    
        self.animateFeetKeyframe()
        #self.animateFeetCurve()
    
    #animate
    '''def animateFeetCurve(self):
        self.rightCurve = cmds.curve(p=self.RFP) #make right curve
        self.leftCurve =cmds.curve(p=self.LFP) #make left curve
        cmds.move(0,0,self.footPosR,self.rightCurve) #move the curve
        cmds.move(0,0,self.footPosL,self.leftCurve)
        cmds.pathAnimation( self.rightFoot, stu=0, etu=30, fa='z', ua='y', c=self.rightCurve )
        cmds.pathAnimation( self.leftFoot, stu=0, etu=30, fa='z', ua='y', c=self.leftCurve )'''
        
    def animateFeetKeyframe(self):
        cmds.setKeyframe(self.rightFoot+".tz",time=0,value=self.footPosR)
        cmds.setKeyframe(self.leftFoot+".tz",time=0,value=self.footPosL)
        for x in range(len(self.RFP)):
            getPointR = self.RFP[x] #get all the points in the dataset 
            getPointL = self.LFP[x]
            cmds.setKeyframe(self.rightFoot+".tx",time=x,value=getPointR[0])
            cmds.setKeyframe(self.rightFoot+".ty",time=x,value=getPointR[1])
            cmds.setKeyframe(self.leftFoot+".tx",time=x,value=getPointL[0])
            cmds.setKeyframe(self.leftFoot+".ty",time=x,value=getPointL[1])
         
myWalk = walkcycle() #instance classa
myWalk.scaleFeet(2)
myWalk.amp = 3
myWalk.shift_y = 0
myWalk.shift_xl = 4
myWalk.footPosL = 3
myWalk.walk_alg()

#2

Welcome to cgtalk.

When you post code you should select all the code and click the # button in the menu above the compose window. This will enclose you code in tags that will preserve indentation within the code. If you want people to be able to decipher your first post, edit it and use the code tags.

cheers
David


#3

My Bad! I will keep this in mind for any possible future post.


#4

Ok. Interesting approach to animation. Here’s my go at adding the rotation you asked about. I’ve added 6 lines to the end of your animateFeetKeyframe() method.

    def animateFeetKeyframe(self):
        cmds.setKeyframe(self.rightFoot+".tz",time=0,value=self.footPosR)
        cmds.setKeyframe(self.leftFoot+".tz",time=0,value=self.footPosL)
        for x in range(len(self.RFP)):
            getPointR = self.RFP[x] #get all the points in the dataset 
            getPointL = self.LFP[x]
            cmds.setKeyframe(self.rightFoot+".tx",time=x,value=getPointR[0])
            cmds.setKeyframe(self.rightFoot+".ty",time=x,value=getPointR[1])
            cmds.setKeyframe(self.leftFoot+".tx",time=x,value=getPointL[0])
            cmds.setKeyframe(self.leftFoot+".ty",time=x,value=getPointL[1])

            if getPointL[1] < 0.01*self.amp or getPointL[1] > 0.8*self.amp:
                ryVal = 5 if getPointL[1] > 0 else 30
                cmds.setKeyframe(self.leftFoot+".ry",time=x,value=ryVal)
            if getPointR[1] < 0.01*self.amp or getPointR[1] > 0.8*self.amp:
                ryVal = -5 if getPointR[1] > 0 else -30
                cmds.setKeyframe(self.rightFoot+".ry",time=x,value=ryVal)

The idea is to add a key for rotation when the foot ty is either below or above a certain height. These heights cant just be 0 and amp because of the way you are sampling the sin curve. Anyway, hopefully this gives you some ideas. Let us know how it develops.

David


#5

Thanks David really appreciate it! I will play around with this!!