Python: How to Automate Shape Keys in Blender

Blah Blah Blah

The human mouth is kind of a bastard. Animating it frame-by-frame requires a patience that I just don’t have. Blender has shape keys that can help you mold lips into different configurations for each sound, but you still have to set keyframes to match up with a particular word at each and every frame.

Or you can be lazy. Like me.

When I was back in first grade, my teacher taught syllables by having us hold our chins. Anytime your chin drops, that’s probably a syllable, she said. Vowels make you open your mouth. Consonants not so much.

So if you’ve got a really simple model and you want to make it talk, I figure one of the simplest routes you can take is setting up a single shape key for your mouth—open and closed. Then you can ask Python to look at a sentence and open up for vowels (A, E, I, O U).

The Set-Up

The Face

Meet my model. He’s in a style I’ve termed 2D-3D. It’s a 3D model built up for rendering along 2 set axes. Basically, he has X and Z, but a very limited Y (and only for shading purposes).

His face background, nose, hair, eyes, and lips are all separate objects. The mouth is three meshes, one for each lip and a background mesh of black for when he gets chatty.

I set up a shapekey called “Oh” that looks like this:

Mouth Mesh

For a quick refresher on how to make shape keys, I recommend CGCookie’s quick run-down on YouTube.

The Guts

This is a two part process. First, you use this line to set the value of the shape key.["Key.001"].key_blocks["Oh"].value = .5

Hmmm, you may be asking, how do I know it’s Key.001? Easiest way is to just head over to the Info window and see what value is plugged in when you change the values manually.

Info Window

Then you set the keyframe. I found that it was easier to set my object to a variable and then insert it.

objectVariable =['YourObjectName']["Shape_Key"].keyframe_insert("value",frame=10)

Setting Up the Chat Function

I opted for two parameters in my function. The string to pick vowels from and the increments to set the frames. The higher you set the speed variable, the slower it’ll take your shape key to morph.

It seems counter-intuitive now, but I’m so used to it that way, I can’t reverse it.

Remember, to customize this to your own meshes, you’ll need to change the object names. Mine are named below as mouth and face. And my shape keys are “Oh” and “Chin.” The chin shape key just narrows the face slightly when it opens up.

Basically, the code converts the string into a list that is then looped through. If the character in question is an a, e, i , o, u, or y, it selects the object that has the shape key, changes the shape key value, sets the keyframe, and then unselects the object. Otherwise, it sets the shape key to nothing to make the mouth appear closed.

Chat Function

Trying something new and popping new scripts on Gist. This one is available here.

Happy coding.



Tagged , , , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: