Archive for the ‘ CG ’ Category

Python: How can I replace text in a string when the case of the text differs?

I process a lot of paths. Some of these paths are entered by hand, by a human. Other paths are machine generated by some tool. Sometimes the tool will respect the case entered originally, other times it makes everything lowercase. Based on these combinations, you could be dealing with two paths that are the exact same (location on disk), but some have upper-case characters defining part of their name, and others, lower-case text. If you need to process these paths, and change them from one relative path to another, these case inconsistencies can become a real pain. Below is one solution around the issue. I’d be interested to see others :)

Using re.findall, we can search in a string. But what really helps is re.IGNORECASE. This gives us a matching string based on the case of the source string, which we can then use to replace with later using the .replace() string method:

import re

srcPath ="c:/my/path/wIth/mIxeD/case"
match = "/with/mixed/"
replace = "/normal/"
resultPath = ""

try:
    sourceCaseMatch = re.findall(match, srcPath, re.IGNORECASE)[0]
    resultPath = srcPath.replace(sourceCaseMatch, replace)
except:
    pass

print "Result: '" + resultPath + "'"
# Result: 'c:/my/path/normal/case'

This is also posted on my Python Wiki

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

New Processing Sketch: tree06

See the goodness on it’s page here.