Stopping a dynamically loaded animation group

Hi everyone,
I’m a newcomer to Babylon.js; my team is in the process of migrating to using it for our product and so far I’m liking it a lot!

My question is about the behavior of dynamically loaded animation groups and specifically how to stop these animation groups from playing. I found a useful playground from a different thread that shows how to load two animation groups from glTF files and apply them to 5 instances of a rigged character. That example calls scene.stopAllAnimations() and then plays them with different weights: https://www.babylonjs-playground.com/#UGD0Q0#50

What I find confusing is if you don’t call stopAllAnimations() on the scene, the animation groups for two of the five rigs still play, with weights that are consistent every time but seem arbitrary. Moreover, calling stop() and setWeightForAllAnimatables(0.0) on those animation groups doesn’t cause them to stop playing. Here’s a playground that shows this: https://www.babylonjs-playground.com/#UGD0Q0#56

Shouldn’t calling stop() and setWeightForAllAnimatables(0.0) stop those animation groups from playing? And why is the behavior of applying the animation groups to the 5 rigs inconsistent? Is this expected behavior? Thanks!

Hello and welcome!

Animation groups are literally group of animations :slight_smile:

It means it will run simultaneously multiple animations. Each animation can have a specific target

If you run 2 animation groups simultaneously you face the risk to “dual” animate a target (e.g. you will animate a mesh position twice) and depending on how the animations are setup you could overwrite or merge the animations

As you said the best is either to stop all animations or simply stop scene.stopAnimation on the specific target

Thanks for the quick response! Yes, iterating an animation group’s targeted animations and calling scene.stopAnimation() on them does the trick, something like this:

animationGroup.targetedAnimations.forEach((ta) => {
scene.stopAnimation(ta.target, ta.animation.name);
})

I guess I was expecting

animationGroup.stop()

to do the same thing. But I think what’s happening is when the glTFs with the animations are initially loaded, those animations are automatically getting applied to certain bones in the scene, separately from the animation groups that are subsequently getting created. So stopping all the animation groups doesn’t stop all the animations.

You can do that to prevent the gltf loader to launch animations:

BABYLON.SceneLoader.OnPluginActivatedObservable.add(function (plugin) {
        currentPluginName = plugin.name;

        if (plugin.name === "gltf" && plugin instanceof BABYLON.GLTFFileLoader) {
            plugin.animationStartMode = BABYLON.GLTFLoaderAnimationStartMode.NONE;
        }
    });
2 Likes

Great, this all makes sense now. Thanks!