I want to use Blender to make a dynamic character. I want to know if it is possible to make a number of keyframes for facial expressions and then have the ability (using python's game engine) to choose which face I want the current face to morph into?

Ok, so I have been trying to master Python scripting with Blender's Game Engine. I would like to make a basic game where you can tell the face, within the Python Console, what expression to do, running code from the Text Editor. Here is some code I wrote in the text editor after making two keyframes 'Basis' and 'Surprise' in Blender Render:

def Face(Expression):
if Expression == 'basis':
    C.object.data.shape_keys.key_blocks['Basis'].value=0.0 #I believe this should make the animation morph to the Basis as defined in my key frame 
elif Expression == 'surprise':
    C.boject.data.shape_keys.key_blocks['Surprise'].value=1.0 #Again this is defined in my key frame
    print('invalide entry')

When I click Run Script it seems to compile with no errors. When I enter the Python Console and type "Face('basis')" I get the following error:

Traceback (most recent call last):
File "<blender_console>", line 1, in <module>
NameError: name 'Face' is not defined  

Firstly is the game engine capable of making a game where you can change character's facial expressions. Would it be better to manipulate bones in the face rather than morphing between key frames? And, how do I call the function I wrote in the text editor in the Python Console? I also want to know if I called the 'object.shape_key' from C properly from the python code? Very detailed Q I know, thanks for the help guys!


There are at least two ways to create and animate facial expressions, not even involving python (although it's possibly to automate this):

  1. Shape Keys - allows you to deform mesh vertices and blend multiple "states" together and also animate
  2. Armature - a skeleton to control the mesh, with strong control over deformation using vertex weights, but also more complicated

I recommend to try out Shape Keys:

  • Start Blender and remove (X) the default cube
  • Add a Monkey (aka Suzanne)
  • Go to Object data tab in Properties Editor
  • Click the plus button + in the Shape Keys panel twice (will create Basis and Key 1)

    Shape Keys Panel with Basis and Key 1 for Monkey mesh
  • Switch Suzanne into Edit Mode (Tab)
  • Deselect all (A) and pick some vertices, e.g. the right eyebrow
  • Enable Proportional Editing if you wish to deform nearby, non-selected vertices as well
  • Move the vertices up a bit (G)
  • Go back to Object Mode (Tab)
  • With Key 1 in the Shape Keys panel selected, move the Value slider to see the transition between the base shape and the keyed shape.
  • Use I over the slider or right-click and pick Insert Keyframe to animate the value and thus the facial expression transition!

    Animated Shape Keys - Suzanne

You can create a Shape Key with Python like:

shapekey = bpy.context.object.shape_key_add(name="Key", from_mix=True)

See the API docs - Object.shape_key_add for details.

The value slider can be animated by inserting a keyframe, here's the most simple way:

# we stored the shape key reference above in the variable "shapekey",
# which should be equal to bpy.context.object.data.shape_keys.key_blocks['Key']
shapekey.value = 0.3 # set value 
shapekey.keyframe_insert(data_path="value", frame=15) # now keyframe 0.3 at frame 15

See the API docs - keyframe_insert()

More on Shape Keys:

If you look into Armature pose animations, make sure to have a look at the Pose Library panel!

It's also worth to have a look at the NLA editor (non-linear animation). It allows you to blend multiple Actions together:

  • $\begingroup$ @CubeBot88 If this is what you want, please consider to mark it as your answer. :) $\endgroup$ – Leon Cheung Jan 30 '14 at 2:04
  • $\begingroup$ This thread pretty much answers my question, thanks anyways guys! blenderartists.org/forum/… $\endgroup$ – CubeBot88 Mar 3 '14 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.