Understading Python generators in Maya

Python has the ability to create generator functions. See Python docs here:
http://docs.python.org/tutorial/classes.html#generators
http://docs.python.org/reference/expressions.html#generator-expressions
http://docs.python.org/reference/expressions.html#yieldexpr
http://docs.python.org/reference/simple_stmts.html#yield

What are they? From the Python docs (above):

“Generators are a simple and powerful tool for creating iterators. They are written like regular functions but use the yield statement whenever they want to return data. Each time next() is called, the generator resumes where it left-off (it remembers all the data values and which statement was last executed)…”

You can imagine that they are like a normal function looping over a list of items, or doing some work on each of the items in the list. The difference is, rather than looping over the whole list when the function is executed, the function pauses after each loop, waiting for you to tell it to continue.

This allows you the ability to make tools that can execute over a list of items, only executing on the next item when you tell it too.

Example below. In this example, the user writes a generator function that will create a locator placed at each vert passed into the function. But the locators are created one by one, only when we call to the generator function:

# Python code
import maya.cmds as mc

# Define our generator function
def generateLocs(verts):
    for v in verts:
        pos = mc.pointPosition(v, world=True)
        loc = mc.spaceLocator(position=pos)
        # Use 'yield' instead of 'return':
        yield loc[0]

Now put the code to use:

# First, select a polygonal object.  Then convert to verts:
verts = mc.ls(mc.polyListComponentConversion(toVertex=True), flatten=True)

# create our generator object called "loc":
loc = generateLocs(verts)

# each time we call to loc.next(), a new locator is created, base on
#  the next vert in the list:
print loc.next()
# locator1
print loc.next()
# locator2
print loc.next()
# locator3

# etc...

This post is also over on my Mel Wiki

New Processing sketch: mote01

See all it’s goodness over on its page.

Faking depth of field in a 2d scene, using… dust motes.

ASIFA “Careers in animation” presentation

I spent 3.5 hours Saturday morning at San Francisco State University as part of a “panel discussion” communicating with the local chapter of ASIFA.  We met at the Coppola Theater in the Fine Arts Building, and I’m guessing over 150 people showed up.  In addition to myself on the panel (Electronic Arts), there was Carlos Baena & Dawn Haagstad from Pixar, Jim Conrads from PDI/Dreamworks, and Josh Book from Wildbrain. It was hosted by Karl Cohen.

Over the course of the discussions we mainly fielded questions from the crowd as far as “how to find a job in animation”, “how should I put together my resumedemo-tape”, and talked a lot about ourselves 😉 It was quite enjoyable to speak with members after and learn more about them. Always fun to be part of these talks.

Just wish I wasn’t recovering from a cold 😉

SFSU Fine Arts building:

View Larger Map

Walkabout pics