Vizard 7 » Tutorials & Examples » Avatars » Animating avatars » Tutorial: Animating avatar interactions
7.7

Tutorial: Animating avatar interactions

Now let's create a new world in which our avatars interact. In this script, we will use both our female and male characters. We will have the female shout to the male and have the male dance in response.

Start the script

If you have trouble getting the code in this tutorial to work, you can find the complete example script avatarInteractions.py in the \tutorials\avatars directory.

 

First, let's create a world and add the avatars:

import viz
import vizact

viz.setMultiSample(4)
viz.fov(60)
viz.go()

#This code adds a ground for them to stand on.
ground = viz.addChild('ground.osgb')

#This code adds the avatars and sets them in a neutral state
male = viz.addAvatar('vcc_male.cfg')
female = viz.addAvatar('vcc_female.cfg')
female.setPosition([0,0,7])
female.setEuler([180,0,0])

female.state(1)
male.state(1)

Next, we set the position and orientation of the viewpoint so we can see both avatars clearly.

#Move the viewpoint to see both avatars clearly
viz.MainView.setPosition([-6,1.8,3.5])
viz.MainView.setEuler([90,0,0])

In order to have the male begin dancing after the female has completed shouting we need to know the duration of the female's animation.  To do that add the following to your script.

#This number equals the number of seconds the shouting animation takes
SHOUT_DURATION = female.getduration(3)

Next, we add two functions and use vizact.onkeydown to listen for a keypress.  When key 1 is pressed the sheShouts() function is called.  This starts the shouting animation and a timer using vizact.ontimer2.  With vizact.ontimer2 a timer will expire after the SHOUT_DURATION time and call the heDances() function.  The second argument there specifies how many times to repeat the timer, in this case we only want the timer to expire once, so that is set to 0.  The dancing animation is then added to the male avatar within heDances().  Run the script and press 1 to see this in action.

def heDances():

    male.addAction( vizact.animation(5) )

def sheShouts():

    female.addAction( vizact.animation(3) )
    vizact.ontimer2(SHOUT_DURATION, 0, heDances)

#This keypress will call the function sheShouts
vizact.onkeydown('1', sheShouts)

 

For additional examples, check out:

avatar

Adding an avatar

Animating an avatar

Using built-in animations

Using multiple animations

Animating avatar interactions

Smooth moves