I’ll see if I can unpack this:
Likewise I am still not following where you are binding the animation to a specific mesh in the example when loading animations as external gITF.
I have many instanced meshes in an assetManager array. I then clone the meshe and play either that same animation playing on the source mesh, or a different animation saved within the gITF, depending on conditions, at least that is the dream.
The meshes currently load fine into assetArray, and the initial mesh autoplays it’s animations fine. It’s when I clone the mesh, the animations don’t clone too. That seems to be normal behaviour, but I don’t know how to hook it back up so a clone plays the animations and clone.animation = source.animation or clone.animationgroup = source.animationgroup did not seem to work.
Let’s try to forget the concept of “binding an animation to a mesh”, as its a little high level for what’s happening here.
Animations can only be applied to nodes (and morph target weights). When you import your mesh, your rig and is composed of of nodes, which the mesh is skinned and/or parented and that is what these animations target. Looking at the API, we can see that our AnimationGroup is composed of TargetedAnimations, which are composed of an Animation track and a target node this poorly drawn diagram represents the rough concept of what data is associated here in the glTF we import and how we represent this in Babylon:
Each Targeted animation is associated to a node in the scene that it targets. You can only have a single target per TargetedAnimation.
When we load our animation from the glTF into the scene, the resulting AnimationGroup in scene has a similar structure, the only difference now is via ImportAnimations/mergeAnimationsTo, we retarget the Animation tracks to nodes in the scene. The default method we use is:
let _targetConverter = targetConverter ? targetConverter : (target: any) => { return scene.getBoneByName(target.name) || scene.getNodeByName(target.name); };
Which searches the scene for the first node that matches the name of the original target in the glTF, and assigns it as the new target for the animation track. We would then see something like this after using the default retargeting implementation:
All of our animations retarget to the first matching node we find in the scene tree!
This constraint on the TargetedAnimation and AnimationGroup makes it so that a single TargetedAnimation CANNOT be shared between multiple nodes. For your case, you may need to manually duplicate your animation group and TargetedAnimations, and implement your own retargeting method to then target the nodes on your other meshes such that each mesh/rig has its own animation group for a particular action. You can also import the same animation file multiple times, and implement your own retargeting callback for greater control of the retargeting behavior. (parameter targetConverter of SceneLoader.ImportAnimations: https://doc.babylonjs.com/api/classes/babylon.sceneloader#importanimations)