Since the Actor class inherits from NodePath, everything that can be done to a NodePath, such as reparentTo() and setPos() , etc., may also be done to an Actor. In addition to the basic NodePath functionality, Actors have several additional methods to control animation. In order for Actors to animate, their pointer (variable) must be retained in memory. The following is only a brief introduction; see the API reference for a complete list.
Basic animation playing
Animations may either be played or looped. When an animation is played, the actor goes through the animation once. When an animation is looped, the animation will play continuously. There is no tweening done between the last and the first frame, so if an animation is going to be looped, it needs to be constructed with that thought in mind. Finally, animations may be stopped at any point. When an animation is stopped, the actor will stay in the position it stopped on.
actor.play('Animation Name')
actor.loop('Animation Name')
actor.stop()
|
You may use the pose() method to tell an actor to hold a particular frame of the animation. Frames are numbered beginning at 0.
actor.pose('Animation Name', FrameNumber)
|
Posing an actor to a frame doesn't automatically specify the start frame of the next starting animation. Instead, if you don't want to start at the first frame, you can specify these using the optional parameters fromFrame and toFrame to the methods play() and loop() :
actor.play('Animation Name', fromFrame = 10)
actor.loop('Animation Name', fromFrame = 24, toFrame = 36)
|
However, the loop method does have another optional parameter called restart , which is 1 by default, meaning the animation will restart from the beginning. If you pass it 0 instead, then the animation will begin looping from the current frame:
actor.pose('Animation Name', 30)
actor.loop('Animation Name', restart = 0, fromFrame = 24, toFrame = 36)
|
Play rate
The animation play rate may be set to any floating point value, which can be used to speed up or slow down the animation. This is a scale factor on the base animation rate; 1.0 means to play the animation at its normal speed, while 2.0 plays it twice as fast, and 0.5 plays it at half speed. It is also possible to play an animation backwards by specifying a negative play rate, for instance -1.0.
actor.setPlayRate(newPlayRate, 'Animation Name')
|
Blending
Multiple different animations for an actor may be played at the same time, and the animations blended together at runtime. The net result is that, each frame, the actor ends up somewhere between the different poses it would be in for each contributing animation, if each animation were playing independently.
Note that in blend mode each contributing animation still affects the actor's entire body. If you want to play one animation on, say, the left arm, while a different animation is playing on the legs, then you need to use half-body animation, which is different from blending.
To use blending, you must first call enableBlend() to activate the blending mode and indicate your intention to play multiple animations at once. While the actor is in blend mode, playing a new animation does not automatically stop the previously playing animation. Also, while in blend mode, you must explicitly specify how much each animation contributes to the overall effect, with the setControlEffect() method (the default for each animation is 0.0, or no contribution). For example:
actor.enableBlend()
actor.setControlEffect('animation1', 0.2)
actor.setControlEffect('animation2', 0.8)
actor.loop('animation1')
actor.loop('animation2')
|
The above specifies that 20% of animation1 and 80% of animation2 will be visible on the character at the same time. Note that you still have to start both animations playing (and they can be playing from different frames or at different play rates). Starting or stopping an animation in blend mode does not change its control effect; you must set an animation's control effect to 0.0 if you don't want it to have any more affect on the actor.
When you call stop() in blend mode, you can stop a particular animation by name, if you want; or you can stop all of the animations by calling stop() with no parameters:
Note that specifying an animation name to stop() is only meaningful when you are in blend mode. When not in blend mode, actor.stop() will always stop whatever animation is currently playing, regardless of the animation name you specify.
When you are done using blending and want to return to the normal mode of only playing one animation at a time, call disableBlend() :
Interpolation
Since Panda 1.3.0 intra-frame interpolation is supported. If you play an animation with only few frames per second you can see your model "jump" from one frame to the next. If you enable interpolation between frames, those "jumps" will be smoothed out. This allows smooth animations with framerates as low as one frame per second or even less.
Intra-frame interpolation is disabled by default.
To enable it just add the following lines to your code
from pandac.PandaModules import loadPrcFileData
loadPrcFileData("", "interpolate-frames 1")
|
Actor Intervals
Another way to play an animation on an actor is to use an ActorInterval, which gives you a lot more frame-by-frame control over the animation, and is particularly useful when building a complex script using Intervals. However, the ActorInterval interface is a little bit slower than the above interfaces at runtime, so you should prefer the more fundamental interfaces unless there is a good reason to use ActorInterval.
The Task manager
On a more complex program, you may find that Animations can not be loaded from any point in your program. In any application there needs to be exactly one call to run(), and it should be the last thing you do after starting up. This starts the task manager. Think of this as the main loop of the application: your startup procedure is to set up your loading screen, start any initial tasks or intervals, hang any initial messenger hooks, and then go get lost in run(). Thereafter everything must run in a task, in an interval, or is a response to a message. This is true for both animations and sound.
|