dissidia
dissidia

Reputation: 1581

How to set keyframes per every 4 frames

I am not sure if there is a term for this, frame offset maybe? but I am trying to set keyframes on controllers per every 4 frames. Eg. If my time slider is in the range of 1 to 23, and so there will be keyframes on Frame 1,5,9,13,17,21. I need this for animation as I have tons of rigs in hundreds-plus frames and sometimes it is pretty insane counting and making sure that I did not go past the 4 frames

However I have no idea how to script to tell Maya to set per every 4 frames. Can someone help me?

Upvotes: 1

Views: 2810

Answers (2)

theodox
theodox

Reputation: 12218

If there are already keyframes on the range and you want regular inbetweens, you can use bakeResults on the existing curves to get regularly spaced keyframes

cmds.bakeResults('pCube1.tx', sampleBy = 4, preserveOutsideKeys=1, sparseAnimCurveBake = 0, time = (1,100))

Will bake the existing curve on pCube1's translate x adding a key every 4 frames between frame 1 and frame 100 docs here: http://help.autodesk.com/view/MAYAUL/2015/ENU/?url=http://help.autodesk.com/cloudhelp/2015/ENU/Maya-Tech-Docs/CommandsPython/bakeResults.html

Upvotes: 1

DrHaze
DrHaze

Reputation: 1318

Here is how you can achieve it, I tried avoiding using multiple for loops and use flags as much as I could to keep the code concise. Select the nodes on which you want keys to be applied and the execute this code.

SET_KEY_STEP = 4 #This is the step 
NODES_LIST = cmds.ls(sl=True) #Apply keys to the selected nodes
ATTRS_LIST = ("tx", "ty", "tz") #Feel free to complete this attribute list
playbackStartTime  = int(cmds.playbackOptions(query=True, min=True)) #Start frame
playbackEndTime    = int(cmds.playbackOptions(query=True, max=True)) #End frame
TIMES_LIST = [i for i in range(playbackStartTime, playbackEndTime+1, SET_KEY_STEP)] #Creates the list 1,5,9,13,17,21...

result = cmds.setKeyframe( NODES_LIST, attribute=ATTRS_LIST, time=TIMES_LIST) #Set all the keys at the same time
print result, "keys added."

Upvotes: 0

Related Questions