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

**URL:** <https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884>\
**Category:** Questions\
**Tags:** animation\
**Created:** [October 28, 2021, 9:04pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884 "2021-10-28T21:04:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![brunobg](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brunobg/32/5523_2.png) [@brunobg](https://forum.babylonjs.com/u/brunobg)\
**Post date:** [October 28, 2021, 9:04pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/1 "2021-10-28T21:04:21Z")

</div>

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](https://playground.babylonjs.com/#5RK780)

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [October 28, 2021, 9:27pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/2 "2021-10-28T21:27:32Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![brunobg](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brunobg/32/5523_2.png) [@brunobg](https://forum.babylonjs.com/u/brunobg)\
**Post date:** [October 29, 2021, 8:10pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/3 "2021-10-29T20:10:32Z")

</div>

In the [PG](https://playground.babylonjs.com/#5RK780) 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?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [October 29, 2021, 9:21pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/4 "2021-10-29T21:21:51Z")

</div>

Adding @bghgary for the gltf part

---

<div class="post-metadata">

**Author:** ![brunobg](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brunobg/32/5523_2.png) [@brunobg](https://forum.babylonjs.com/u/brunobg)\
**Post date:** [November 3, 2021, 8:03pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/5 "2021-11-03T20:03:24Z")

</div>

Anyone?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [November 3, 2021, 8:04pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/6 "2021-11-03T20:04:10Z")

</div>

Thanks for the reminder @bghgary might have missed the ping 🙂

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [November 4, 2021, 12:36am UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/7 "2021-11-04T00:36:50Z")

</div>

Sorry, definitely missed responding to this. 🙂

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.

---

<div class="post-metadata">

**Author:** ![brunobg](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brunobg/32/5523_2.png) [@brunobg](https://forum.babylonjs.com/u/brunobg)\
**Post date:** [November 4, 2021, 4:54pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/8 "2021-11-04T16:54:17Z")

</div>

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

This is useful for [VATs](https://github.com/BabylonJS/Babylon.js/pull/11317). 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.

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [November 5, 2021, 7:02pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/9 "2021-11-05T19:02:59Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [November 6, 2021, 2:29pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/10 "2021-11-06T14:29:19Z")

</div>

Not seeing any on my side

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [November 8, 2021, 5:13pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/11 "2021-11-08T17:13:35Z")

</div>

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

[https://doc.babylonjs.com/typedoc/classes/babylon.animation#getkeys](https://doc.babylonjs.com/typedoc/classes/babylon.animation#getkeys)

---

<div class="post-metadata">

**Author:** ![brunobg](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brunobg/32/5523_2.png) [@brunobg](https://forum.babylonjs.com/u/brunobg)\
**Post date:** [November 8, 2021, 6:59pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/12 "2021-11-08T18:59:43Z")

</div>

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

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

```

And for a `.babylonjs`:

```auto
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](https://playground.babylonjs.com/#5RK780#1)

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [November 8, 2021, 7:14pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/13 "2021-11-08T19:14:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![brunobg](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brunobg/32/5523_2.png) [@brunobg](https://forum.babylonjs.com/u/brunobg)\
**Post date:** [November 8, 2021, 7:17pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/14 "2021-11-08T19:17:54Z")

</div>

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](https://xkcd.com/974/) problem 🙂

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [November 8, 2021, 7:28pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/15 "2021-11-08T19:28:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![brunobg](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brunobg/32/5523_2.png) [@brunobg](https://forum.babylonjs.com/u/brunobg)\
**Post date:** [November 19, 2021, 6:50pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/16 "2021-11-19T18:50:28Z")

</div>

@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](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.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [November 19, 2021, 8:44pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/17 "2021-11-19T20:44:12Z")

</div>

@bghgary could you have a look ?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [November 19, 2021, 9:28pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/18 "2021-11-19T21:28:21Z")

</div>

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](https://playground.babylonjs.com/#CP2RN9#23)

which is 60 here where each of them contain 41 elements

![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/1/e/1e8d82f8b9bf9cc0b834d5908d2898eb94b398cf.png)

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 😉

---

<div class="post-metadata">

**Author:** ![brunobg](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brunobg/32/5523_2.png) [@brunobg](https://forum.babylonjs.com/u/brunobg)\
**Post date:** [November 22, 2021, 1:45pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/19 "2021-11-22T13:45:34Z")

</div>

> [@sebavan](#):
>
> That said, as they might not all be aligned you may need to find all the different ones ?

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.

> [@sebavan](#):
>
> Please disregard if I am fully wrong and give me a A for effort

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

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [November 22, 2021, 2:12pm UTC](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884/20 "2021-11-22T14:12:18Z")

</div>

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 🙂 ?

[Next page](https://forum.babylonjs.com/t/how-to-find-the-total-number-of-frames-of-a-skeleton-animation/24884.md?page=2)
