Playing animation of uploaded glb model with its name

I have uploaded a glb model with animation group but unfortunately I have no clue on how to play the specific animation with the animation name. How do I achieve this?

setTimeout(function () {
    BABYLON.SceneLoader.ImportMesh("", "LINK TO MODEL", "scene.glb", scene, function (newMeshes) {
        newMeshes.forEach(mesh => {
            mesh.scaling = new BABYLON.Vector3(5, 5, 5);
            mesh.checkCollisions = true;
        })
        scene.createDefaultEnvironment();
        scene.createDefaultLight();

    });
   
}, 1500)
1 Like

BABYLON.SceneLoader.ImportMesh returns these data from your file:
meshes, particleSystems, skeletons, animationGroups
which you can play like this:

BABYLON.SceneLoader.ImportMesh("", "LINK TO MODEL", "scene.glb", scene, function (newMeshes, particleSystems, skeletons, animationGroups) {
    newMeshes.forEach(mesh => {
        mesh.scaling = new BABYLON.Vector3(5, 5, 5);
        mesh.checkCollisions = true;
    }) 
       // loop through all animation groups and play
        for (i = 0; i < animationGroups.length; i++) {
            animationGroups[i].play(true);
        }
      //OR play individually by using index animationGroups[0].play(true);

    scene.createDefaultEnvironment();
    scene.createDefaultLight();

});
1 Like

Hey thank you for the reply. But I actually wanted to play a specific animation with the animation’s name. For example, animation.Play(“Ball”);

Also can I play the animation outside the ImportMesh function?

You could filter or find in the array like:

 let ball = animationGroups.find(a => a.name === 'Your animation name here');
ball.play(true);

Yes, just use scene animationGroups

  let ball = scene.animationGroups.find(a => a.name === 'Your animation name here');
ball.play(true);
2 Likes

I tried this but I am getting an error “Cannot read property ‘play’ of undefined”. What is the issue here?

if you are using scene.animationGroups and playing animation before your file is loaded it will not work, make sure you are calling animation once your file is loaded, otherwise I suggest to create a playground

Ah yes you are right :slight_smile: I need an OnSuccess() callback to check if the model is completely loaded.

2 Likes