Maya Python Animation setKeyframe

In this example we’ll be looking at how to create keyframes with code. The script will animate a lot of small cubes placed on a sphere. We need 3 blinn shaders. First we create a sphere with default parameters.Then we create 250 small cubes. We use variable col1 for changing the cube color. pointPosition helps us getting the coordinates of the sphere vertices. We place the cubes on these positions and lastly we animate the cubes sizes, setting two keyframes.

Run the script and trigger playback.

import maya.cmds as cmds
cmds.polySphere()
for i in range(1,500,2):
    cube1=cmds.polyCube(w=.5,h=.5,d=.5)
    col1=i%3+1
    cmds.hyperShade(assign=’blinn%d’%col1)
    pointpos=cmds.pointPosition(‘pSphere1.vtx[%d]’%i)
    cmds.move(pointpos[0],pointpos[1],pointpos[2])
    cmds.setKeyframe(cube1,attribute=’scale’,t=[’10sec’],v=2)
    cmds.setKeyframe(cube1,attribute=’scale’,t=[‘0sec’],v=0)

Leave a comment

Your email address will not be published. Required fields are marked *