iterateOverTime()

This simple Maya script came about after a colleague needed to run a few commands on each frame of an animation as a quick fix.

The original I scripted addressed a specific issue, but I thought it may be useful to have as a more generic function.

I’ve added the doThis() function below to provide a working example. Basically, add what you want to do to the doThis() function or supply your own or other existing function. It can also change increments, so for example; iterateOvetime(doThis,5) will execute doThis() on every 5th frame.


import pymel.core as pm

def iterateOverTime(function, step=1):
    """Executes given function for each frame on timeline"""
    min = pm.playbackOptions(query=True, minTime=True)
    max = pm.playbackOptions(query=True, maxTime=True)
    fail_list = []
    for i in range(min,max+1,step):
        pm.setCurrentTime(i)
        try:
            function()
        except:
            fail_list.append('Failed on frame: %s'%(i))
    for fail in fail_list:
        print fail
            
def doThis():
    print 'currentTime is %s'%(pm.getCurrentTime())
    
iterateOverTime(doThis)

Leave a comment