Animating in Panda3D

Packt Publishing

This is the A-Z of Panda3D for developers who have never used the engine before. Step-by-step, it takes you from first principles to ultimately creating a marketable game. You’ll learn through first-hand experience and clear explanations.

Actors and Animations

An Actor is a kind of object in Panda3D that adds more functionality to a static model. Actors can include joints within them. These joints have parts of the model tied to them and are rotated and repositioned by animations to make the model move and change. Actors are stored in .egg and .bam files, just like models.

Animation files include information on the position and rotation of joints at specific frames in the animation. They tell the Actor how to posture itself over the course of the animation. These files are also stored in .egg and .bam files.

Time for action – loading Actors and Animations

Let's load up an Actor with an animation and start it playing to get a feel for how this works:

Open a blank document in NotePad++ and save it as Anim_01.py in the Chapter09 folder.

We need a few imports to start with. Put these lines at the top of the file:

The first line creates an instance of the Actor class. Unlike with models, we don't need to use a method of loader. The Actor class constructor takes two arguments: the first is the filename for the model that will be loaded. This file may or may not contain animations in it. The second argument is for loading additional animations from separate files. It's a dictionary of animation names and the files that they are contained in. The names in the dictionary don't need to correspond to anything; they can be any string.

The names we give animations when the Actor is created are important because we use those names to control the animations. For instance, the last line calls the method loop() with the name of the walking animation as its argument.

If the reference to the Actor is removed, the animations will be lost. Make sure not to remove the reference to the Actor until both the Actor and its animations are no longer needed.

Controlling animations

Since we're talking about the loop() method, let's start discussing some of the different controls for playing and stopping animations. There are four basic methods we can use:

play("AnimName"): This method plays the animation once from beginning to end.

loop("AnimName"): This method is similar to play, but the animation doesn't stop when it's over; it replays again from the beginning.

stop() or stop("AnimName"): This method, if called without an argument, stops all the animations currently playing on the Actor. If called with an argument, it only stops the named animation.

Note that the Actor will remain in whatever posture they are in when this method is called.

pose("AnimName", FrameNumber): Places the Actor in the pose dictated by the supplied frame without playing the animation.

We have some more advanced options as well. Firstly, we can provide option fromFrame and toFrame arguments to play or loop to restrict the animation to specific frames.

myActor.play("AnimName", fromFrame = FromFrame, toFrame = toFrame)

We can provide both the arguments, or just one of them. For the loop() method, there is also the optional argument restart, which can be set to 0 or 1. It defaults to 1, which means to restart the animation from the beginning. If given a 0, it will start looping from the current frame.

We can also use the getNumFrames("AnimName") and getCurrentFrame("AnimName") methods to get more information about a given animation. The getCurrentAnim() method will return a string that tells us which animation is currently playing on the Actor.

The final method we have in our list of basic animation controls sets the speed of the animation.

myActor.setPlayRate(1.5, "AnimName")

The setPlayRate() method takes two arguments. The first is the new play rate, and it should be expressed as a multiplier of the original frame rate. If we feed in .5, the animation will play half as fast. If we feed in 2, the animation will play twice as fast. If we feed in -1, the animation will play at its normal speed, but it will play in reverse.

Have a go hero – basic animation controls

Experiment with the various animation control methods we've discussed to get a feel for how they work. Load the Stand and Thoughtful animations from the animations folder as well, and use player input or delayed tasks to switch between animations and change frame rates. Once we're comfortable with what we've gone over so far, we'll move on.

Animation blending

Actors aren't limited to playing a single animation at a time. Panda3D is advanced enough to offer us a very handy functionality, called blending. To explain blending, it's important to understand that an animation is really a series of offsets to the basic pose of the model. They aren't absolutes; they are changes from the original. With blending turned on, Panda3D can combine these offsets.

Time for action – blending two animations

We'll blend two animations together to see how this works.

Open Anim_01.py in the Chapter09 folder.

We need to load a second animation to be able to blend. Change the line where we create our Actor to look like the following code:

What just happened?

Our Actor is now performing both animations to their full extent at the same time. This is possible because we made the call to the self.kid.enableBlend() method and then set the amount of effect each animation would have on the model with the self.kid.setControlEffect() method. We can turn off blending later on by using the self.kid.disableBlend() method, which will return the Actor to the state where playing or looping a new animation will stop any previous animations. Using the setControlEffect method, we can alter how much each animation controls the model. The numeric argument we pass to setControlEffect() represents a percentage of the animation's offset that will be applied, with 1 being 100%, 0.5 being 50%, and so on. When blending animations together, the look of the final result depends a great deal on the model and animations being used. Much of the work needed to achieve a good result depends on the artist.

Blending works well for transitioning between animations. In this case, it can be handy to use Tasks to dynamically alter the effect animations have on the model over time.

Honestly, though, the result we got with blending is pretty unpleasant. Our model is hardly walking at all, and he looks like he has a nervous twitch or something. This is because both animations are affecting the entire model at full strength, so the Walk and Thoughtful animations are fighting for control over the arms, legs, and everything else, and what we end up with is a combination of both animation's offsets.

Furthermore, it's important to understand that when blending is enabled, every animation with a control effect higher than 0 will always be affecting the model, even if the animation isn't currently playing. The only way to remove an animation's influence is to set the control effect to 0.

This obviously can cause problems when we want to play an animation that moves the character's legs and another animation that moves his arms at the same time, without having them screw with each other. For that, we have to use subparts.

Creating and using Actor subparts

A subpart is a subsection of the joints in an Actor. Think of it like a grouping of some of the joints of the Actor that excludes the rest of the joints. Creating subparts doesn't actually change the Actor at all; it just creates a grouping that we can use when we want an animation to only affect a specific subset of joints. To create a subpart, we just need to call the makeSubpart() method of the Actor:

myActor.makeSubpart("SubpartName", [Included], [Excluded])

We pass three arguments into the makeSubpart() method:

The first is a name for the subpart that we can use to identify it later

The second is a list of joint names for the joints we want to include in the subpart

The third is a list of joint names for the joints we want to exclude from the model

We can find these joint names if we open Kid.egg and scroll down to the bottom. Here's a joint entry for a joint called Chest, for reference:

The makeSubpart() method automatically includes or excludes the children of any joint it is supplied.

When making Actor subparts, exclusion always overrides inclusion.

Let's take a look at some of the joints in our Actor and their hierarchy.

This diagram shows some of our joints, the circles, and their names. It also shows the hierarchy through the connecting arrows. Parents point to their children.

Looking at this, we can tell that the joint named Root is at the top of our joint hierarchy. If we pass Root as an included joint, that will include every joint in the model. If we also pass in Waist as an excluded joint, we'll override part of that inclusion and exclude the Waist joint and its children. Therefore, our subpart will only include Root, R_Hip and its children, and L_Hip and its children, which means only the pelvis and legs.

Time for action – playing animations on subparts

To better understand subparts, we should put them into action.

Open Anim_02.py in the Chapter09 folder.

Remove the code that enables blending, sets control effects, and loops the two animations.

What just happened?

We've got a much nicer combination of the two animations now because on the legs of the Actor we're only playing the Walk animation, and on the torso of the Actor we're only playing the Thoughtful animation.

Note that if we want to play an animation only on a subpart of the Actor, we need to use the partName optional argument for play or loop.

Exposing joints

We can do more with Actors than just play animations on them. We can also attach other NodePaths to specific joints in an Actor so that they will follow that joint wherever it moves. This technique is often used to put objects in a character's hands. For our game, we'll be using it to attach the discs and turret to our cycle.

This technique works well for us because we want to change the rotation of the discs and turret. For that to work correctly, the models need to have the points they will rotate around at (0, 0, 0) in their own coordinate system. That means we can't reposition them in the modeling software, as we did with the static parts of the cycle such as the power plant.

Instead, we'll load an Actor that only contains three joints. The joints are located at the positions where we want to place our discs and turret. If we attach the Actor to the cycle's model, we can use the joints as mounting points for the discs and turret.

Without the exposeJoint() method of Actor, this technique wouldn't be possible. This method creates a NodePath and attaches it to a joint in the Actor, such that wherever that joint moves, or however it rotates, the NodePath will mimic it exactly. The method exposeJoint() takes three arguments:

The first is an optional Node that is usually set to None

The second is the name of the subpart the joint is a part of, or modelRoot if no subparts contain the joint

Once we've used exposeJoint() to create NodePaths, we can use reparentTo to attach things to them, just like any other NodePath.

Time for action – animating our cycles

We're going to add two kinds of animation to our cycle. We'll make the discs at the front and rear of the cycle rotate in accordance with the throttle setting, and we'll make the cycle lean left and right when it turns. Because we don't want the CollisionRays attached to the cycle to lean as well, we'll have to make accommodations for that.

Open CycleClass_00.py in the Chapter09 folder.

Add this line to our import statements to give us access to Actors:

from direct.actor.Actor import Actor

Scroll down to the setupVarsNPs() method and look for the section where we use if statements to position self.root and load the correct model. That section looks like the following code:

Scroll down a little to where we set the cycle's speed, shield, and other variables. Right beneath that there is a line that attaches self.cycle to self.root. Delete that line and put the following two lines in its place:

self.turning = Noneself.lean = 0

Keep moving down until we're in setupCollisions. Look for the following line:

self.shieldCNP = self.cycle.attachNewNode(self.shieldCN)

Change that line from self.cycle to self.model so it looks like this:

self.shieldCNP = self.model.attachNewNode(self.shieldCN)

Head down to the cycleControl() method and find the short block of code that turns the cycle according to user input. It looks like the following code:

Resave the file as CycleClass_01.py and run WorldClass_00.py from the command prompt.

What just happened?

That brings some new life to our cycle! There's one thing we should talk about in this example that we haven't really discussed yet: the reason why we made self.cycle an empty NodePath and changed the model to be self.model. Well, when we lean the cycle during a turn, we don't want those CollisionRays to lean with it, but the CollisionRays still have to turn with the cycle so that the front ray stays in front and the back ray stays at the back. For all that to work out, we need to separate the turning from the leaning, and that's why we are using two NodePaths now. Since we want the CollisionSpheres for the shield to lean with the cycle, we made them children of self.model instead of self.cycle.

Summary

In this article, we talked about loading Actor and animations and how to control an Actor's animations. We also discussed how we can use blending and subparts to further refine our animation control, and we touched on how to include other objects into our Actor's animations by binding them to joints.

Alerts & Offers

Series & Level

We understand your time is important. Uniquely amongst the major publishers, we seek to develop and publish the broadest range of learning and information products on each technology. Every Packt product delivers a specific learning pathway, broadly defined by the Series type. This structured approach enables you to select the pathway which best suits your knowledge level, learning style and task objectives.

Learning

As a new user, these step-by-step tutorial guides will give you all the practical skills necessary to become competent and efficient.

Beginner's Guide

Friendly, informal tutorials that provide a practical introduction using examples, activities, and challenges.

Essentials

Fast paced, concentrated introductions showing the quickest way to put the tool to work in the real world.

Cookbook

A collection of practical self-contained recipes that all users of the technology will find useful for building more powerful and reliable systems.

Blueprints

Guides you through the most common types of project you'll encounter, giving you end-to-end guidance on how to build your specific solution quickly and reliably.

Mastering

Take your skills to the next level with advanced tutorials that will give you confidence to master the tool's most powerful features.

Starting

Accessible to readers adopting the topic, these titles get you into the tool or technology so that you can become an effective user.

Progressing

Building on core skills you already have, these titles share solutions and expertise so you become a highly productive power user.