How to get the length of an animation

Is there a simple way to get the length of an AnimationGroup? Like how many seconds would elapse from I started playing it to now?

I can’t really see a function that helps me? So… I need to reach in to each animation, get the frame number of the last keyframe (multiply by the framerate), and then get the longest in each group? So something like this?

const longest = animationgroup.targetedAnimations.reduce((old, next) => {
  const nextEnd = next.animation.getKeys().slice(-1)[0];
  const oldEnd = old.animation.getKeys().slice(-1)[0];
  const oldLen = old.animation.framePerSecond * oldEnd.frame;
  const nextLen = next.animation.framePerSecond * nextEnd.frame;
  if (oldLen > nextLen) {
    return old;
  } else {
    return next;
  }
})

These are animations that might come from wherever (like, users will upload GLBs from e.g. Blender to the system we’re building), and I’m not sure what kind of pitfalls there might be.

That sounds about right :smiley: For GLB/GLTF imported meshes, an important thing to consider is that animations are saved in seconds, with their original FPS not saved. So when we import them on Babylon, we assume a rate of 60 FPS, which might not align with the user’s expectations relative to their original DCC tool (Blender uses 24 FPS by default, for example). So I would add some kind of prompt where the user, if desired, could inform the original FPS used for authoring the animation, and retarget them accordingly.

1 Like