How to find the total number of frames of a skeleton animation?

Simple question, but I couldn’t find an answer anywhere.

ImportMesh returns animationGroups, but apparently not for all animated models. mesh.skeleton.getAnimationRanges() seems to be empty for meshes that have animations. And when it does it doesn’t seem to return the total frames, but the time in .to.

Basic PG with two different models: Babylon.js Playground

1 Like

Hello! It depends on the file format actually.

gltf uses animation groups where .babylon uses direct animations.

So depending on which format you use the info will be in different places (in animationGroup for glTF and directly on the skeleton in .babylon)

3 Likes

In the PG both importResultSpider.animationGroups and importResultSpider.meshes[0].skeleton.getAnimationRanges() return [] for the babylon format.

For GLTF, I get 6.66... for the animationRange.to, which seems to be in seconds. How do I get the actual number of frames, is it always based on 30fps?

Adding @bghgary for the gltf part

Anyone?

Thanks for the reminder @bghgary might have missed the ping :slight_smile:

1 Like

Sorry, definitely missed responding to this. :slight_smile:

It doesn’t look like there is a way to get the number of key frames from an animation (it’s called keys in the code). What are you going to do with these frames? Maybe there is another way to do what you want.

2 Likes

What are you going to do with these frames? Maybe there is another way to do what you want.

This is useful for VATs. It’d be nice to get the number of frames programmatically. Even though the ranges aren’t available in the model since they’re semantic (like 1-30 running, 31 to 45 walking, etc), at least getting the total number of frames would be useful to make the conversion automatic. And many models have just one animation.

1 Like

Ok, I don’t see any reason why we can’t expose the keys. @sebavan/@Deltakosh Any issues with exposing the keys of an animation? It’s already well typed as IAnimationKey.

2 Likes

Not seeing any on my side

1 Like

Hmm, I lied :slight_smile:. We already have a way to get the keys.

https://doc.babylonjs.com/typedoc/classes/babylon.animation#getkeys

1 Like

Okay, so this is the simplest expresion I got for the keys in a GTLF:

importResultAlien.
  animationGroups.
  map(
      (i) => i.animatables.map(
         j => j.getAnimations().map(i => i.animation.getKeys())
      )
   )
);

And for a .babylonjs:

importResultSpider.
    meshes[0].
    skeleton.
    getAnimatables().
    map(i => i.animations).map(k => k.map(i => i.getKeys()))

Both return arrays that can be read to get the total number of keys. Is this really as easy as it gets?

Updated PG: https://playground.babylonjs.com/#5RK780#1

to be fair, this is not something we ever had a need for I guess :slight_smile: I am wondering in which other case you would need all of them like this ?

1 Like

My use case is pretty much “bake a VAT without having to open the model in blender, look at the number of animation frames and manually type it in the code”. It’s a pass the salt problem :slight_smile:

Yup makes total sense so the code above would be fine if you embed it in the VAT code, I am just not sure we need to simplify it and make it a nice public API for now

1 Like

@bghgary sorry, but I’m still struggling with this one. Something is wrong that’s not right. I’m wondering if there’s a bug hidden somewhere. Here’s an updated PG: https://playground.babylonjs.com/#CP2RN9#22

The original blender animation has 40 frames (1 to 40), which was exported to GLTF with a sampling rate of 1. The animationGroups code above returns 60 keys, which I believe should 40 too. But the number that actually works is 91.

I think there might be a bug somewhere (possibly in the baker code? But it’s only beginAnimation on a counter loop) and I’m trying to pinpoint what’s wrong.

@bghgary could you have a look ?

I think the way total frames is computed is wrong ?

you would need to check for each animation how many frames you have a compute an array of distinct frames ?

Currently your length will be the total number of key set in the model:

https://playground.babylonjs.com/#CP2RN9#23

which is 60 here where each of them contain 41 elements

image

That said, as they might not all be aligned you may need to find all the different ones ?

Please disregard if I am fully wrong and give me a A for effort :wink:

I’m not sure I understand this, sorry. 41 makes complete sense, since it is the total number of keys in the original model. I don’t get why I get 60 animations (it’s animations, right?), though. And even less how the total number of frames that have to be baked is 91.

I don’t even know if you are wrong yet, but you seem to know a thing or two about babylon :wink:

My guess is that all the different keys are not at the same exact time so even if 2 different animations have 2 keys each, you might end up with 2 keys if they are all aligned, 3 if only 1 time is aligned or 4 if both couple is out of sync, not sure I am making a lot of sense :slight_smile: ?