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
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:
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.
bpy.data.shape_keys["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.
Then you set the keyframe. I found that it was easier to set my object to a variable and then insert it.
objectVariable = bpy.data.objects['YourObjectName'] objectVariable.data.shape_keys.key_blocks["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.
Trying something new and popping new scripts on Gist. This one is available here.
Happy coding.