Tutorial: Play animations

This tutorial will cover the use of the PlayAnimation component, which unsurprisingly, lets you control the animations of a Spatial through its AnimControl.

It is also a good beginner tutorial since it has few steps and leaves you with something useful in the end.

For starters, we create a simple scene in the SceneComposer. This time, old Jaime is used to illustrate the point. It’s a good model to test things out with as it is rigged and has an AnimControl attached already with a bunch of useful animations.

Next, we create a new Macaq Script by right clicking in the Scripts of the asset folder (or create the folder if it doesn’t exist). Then select ”New.. / Other / Macaq Script”. This should open the editor automatically.

If there isn’t a ”Start” component, we add that first. Its ”onStart” connection is always called when a script is run. Components can be added by right clicking anywhere on the canvas.

The next component we add is a PlayAnimation component. Once added, it can be dragged into a suitable position (preferably to the right of the ”Start” component).
Now, we need to set up few things in the component, both what Spatial it should be looking for, and also some information about the animation to play.

We’ll start with the Spatial to control. If you have the scene open in the SceneComposer, you’ll get the options available in a dropdown menu in spatialName. You can also start typing the name to narrow down the results, if many.

Next is the animation we want to play. At the time of writing this, you need to type this yourself. You can find the options by looking at the AnimControl in the Spatial’s properties in the SceneComposer. Eventually, there will be a dropdown menu to select this.

Channel is usually 0. If the spatial is set up to have different animations for the legs than the arms for example, it can have several channels.

LoopMode is how the animation is played, whether a single cycle, continuous, or play backwards once it reaches the end.

AnimationSpeed should be self-explanatory, and blendTime is how long in seconds it blends from the previous animation (if any).

Once this is done we connect Start’s onStart connection to PlayAnimation’s start connection by clicking on the former and then the latter.

Tip: The Macaq control will listen to AnimationEvents coming from the AnimControl and report back to the component. These will trigger the onAnimationCycleDone and onAnimationChange connections.

As ever, the application code is quite simple. In simpleInitApp we first load the scene and attach it to the rootNode.

Spatial scene = assetManager.loadModel("Scenes/TestAnimation.j3o");
rootNode.attachChild(scene);

Then we load the Macaq Script and attach it to the StateManager.

MacaqScriptAppState scriptAppState = new MacaqScriptAppState("Scripts/TestAnimation.mqs");
stateManager.attach(scriptAppState);

Run it and see the results.