Is there an easy way to get the name of the animation track asset associated with an anim state

Hi all,
is there an easy way to get the name of the animation associated with a given state at runtime?

I’m able to retrieve the name of the track associated with it, which in my case is “Take 001” but not the actual name of the asset and I need that instead of the internal track name.

@Elliott

To add a bit more context to this, I’m trying to implement a flipbook animation for some sprites that are part of a character’s body (eyes, mouth). The way we approached this is to create an associated JSON file for every animation the character has (e.g for a “Celebrate.fbx” animation I will have a “Celebrate.json”). This file is automatically exported from Maya along with the character animation.
This additional file contains timeline information about what frames index from the eyes or mouth atlas we should use at a particular time during the animation.

On the PlayCanvas side we’ve implemented a script that is attached to the character and on the update method it determines the actual running animation track, current time of the animation and the duration. But in order to get the animation track or animation asset associated with a given state we had to dig deeply inside the engine and use non-public functions and members.

flipbook animation

It would be nice if the public API would expose methods to retrieve all the layers added to an AnimComponent not just the baseLayer.
Also on the AnimComponentLayer it would be great if we can also get methods to allow retrieval of the associated animation assets and animation tracks for each state without having to rely on internal functionality that might change.
One more thing that I think it might be useful is to have automatic animation events (START, END) for each animation with information about the animation (track name, asset name, etc…whatever might be useful).

Hi @heretique

The anim component doesn’t store the animation assets themselves internally. Instead it loads in the AnimTrack resources from the assets during the initialisation. If you’re creating an anim component programatically, then you should have access to the associated assets in your current script already.

However if you’ve set up your anim component in the editor then you can access the assets you linked to the anim component in the editor via the entity.anim.animationAssets object. You can use something similar to the snippet below to retrieve the asset associated with the active state of a given layer:

const layer = entity.anim.findAnimationLayer('MyLayer');
const activeStateAssetId = entity.anim.animationAssets[`${layer.name}:${layer.activeState}`].asset;
const activeStateAsset = pc.app.assets.get(activeStateAssetId);

It’s also possible to call entity.anim.layers to get a list of all layers in your anim component.

It is currently possible to add events to animation assets in the editor. So you can add an event to each animation when they begin and end, but we should definitely consider adding some automatic events in the future.

3 Likes

Thanks @Elliott , I was using them from Typescript and I didn’t notice in the engine that there is an animationAssets getter and setter, I was using internal _animationAssets, same for entity.anim.layers which don’t appear in the documentation. I had a second look at the PC engine code and indeed I can use those. Still it would be nice if they are documented in the API Reference.

Thanks again!

1 Like

You’re right, the public anim component API is currently limited. I need to do another pass over it’s API and fix these inconsistencies. I’ll create a ticket for this in the engine repo. Thanks for highlighting the issue.

Edit: I’ve just noticed you created an issue already. Double thanks :smile: . I’ll add some extra info there.

1 Like