Maya Python scripting – Iterate through the timeline frames

Software:
Maya 2018

The following Python script iterates though Maya’s timeline frames, and for each frame creates a new cube, and aligns it’s position to the selected animated locator.

* There is probably a nicer way to set one object’s position according to anothers but haven’t found it yet (not finding enough examples of the cmds.xform command…) so sorry for that..

import maya.cmds as cmds

selection = cmds.ls(sl=1,sn=True)
for frame in range(1,80):
    cmds.currentTime(frame)
    newCube = cmds.ls (cmds.polyCube( sx=1, sy=1, sz=1), long=True)
    posX = cmds.getAttr(selection[0]+'.translateX')
    posY = cmds.getAttr(selection[0]+'.translateY')
    posZ = cmds.getAttr(selection[0]+'.translateZ')
    cmds.setAttr(newCube[0]+'.translateX',posX)
    cmds.setAttr(newCube[0]+'.translateY',posY)
    cmds.setAttr(newCube[0]+'.translateZ',posZ)

maya_python_frames

Leave a Reply