After cloning a mesh, how can animations be played individually for each mesh?

UPDATE: Added simple sandbox version of the problem here → https://playground.babylonjs.com/#JLVTO4#1

Hi, for the last two days I’ve been sort of stuck on this problem and I don’t really know where to look any more to solve this. I’ve tried going through examples, documentation, youtube, etc. all the usual places. I guess I’m missing something? The closest I’ve seen is Babylon.js Playground but that seems to be not quite what I want to achieve.

I load a mesh from a glb file through assetsManager addContainerTask. This works fine. The asset is loaded and the first of three possible animations is rendered. So far so good.

const containerTask = this.assetsManager.addContainerTask(
name,
[“meshName”],
".\folder",
“file”,
);
containerTask.onSuccess = (task) => {
const container = task.loadedContainer;
container.meshes.forEach((loadedMesh) => {
loadedMesh.setEnabled(true);
this.scene.addMesh(loadedMesh);
});
this.meshAnimationGroups.set(“name”, container.animationGroups);
container.animationGroups.forEach((animationGroup) => {
animationGroup.stop();
this.scene.addAnimationGroup(animationGroup);
});
};

Then I clone the mesh and I want to have it play e.g. the second animation, independently of the animation playing on the original mesh.

const mesh = this.scene.getMeshByName(currentMeshName) as Mesh;
const clonedMesh = mesh.clone(clonedMeshName);
clonedMesh.position.set(0,0,0);

This does in fact clone the mesh but it also just duplicates whatever animation is playing on the original mesh (no new AnimationGroups are added).

I want my clones to be able to individually play their animations.
My guess is that the AnimationGroup must also be cloned and then set to the new mesh? But how do I achieve that? And if this is incorrect, what is the proper way of doing having animations play independently on cloned meshes?

Any pointers on this would be much appreciated.

Hello and welcome!

I hope this thread may help - How to clone a .glb model and play seperate animation on each clone?

Thank you for your answer, I’m currently looking at the suggested page but in the mean time I built a simple sandbox version of my problem.

https://playground.babylonjs.com/#JLVTO4#1

Managed to figure it out. My thinking was indeed lacking some crucial insights. Will try to post the solution when I have some time.

1 Like

Here is another thread with different solutions - How the heck do you load in your model with animations?

Manager to find some time to make an example to the solution to the problem. This example loads the asset once and allows the game to add copies of the asset including copies of animations at will.

1 Like