Replacing meshes on bones for character customization

Hi, I’m trying to import a mesh (that is actually several meshes for different body parts) with skeleton and animations and then replace its meshes with other meshes for character customisation, eg change the hair or the shoes.

I started with some fbx files that I imported in Blender and exported as a single glb file according to this video linked in the documentation: https://youtu.be/9EWJhqPafpk . I can import the mesh with skeleton and animations in Babylon and there’s no problem so far. Now I want to be able to customise the character by swapping out meshes.

When animating, the character’s meshes don’t deform and only translate and rotate (or maybe only rotate). I’ve read attachToBone can be used for things similar to this so I’ve tried it and it does somewhat place the new part correctly and it does animate, but I also want to dispose of the mesh already tied to the bone. There’s a problem though if I remove all the original meshes, then the animation doesn’t work anymore. Similar to this PG: https://playground.babylonjs.com/#QY1WYT#283 . Using setEnabled(false) or isVisible=false instead of dispose has the same effect.

I have looked at the animations to see if I can retarget them but they appear to not target the meshes or bones and instead target some set of transform nodes that I haven’t found how they connect to the bones or meshes.

As a different attempt, I have tried setting the new mesh’s parent to the mesh to be replaced, that mesh’s parent, the bone and the bone’s transform node but none cause the new mesh to animate with the character. I also set the new mesh’s skeleton to be the imported skeleton.

I also found out about matrices weights and indices and saw that those are simple for the meshes I import with the skeleton; the weights are 1,0,0,0 repeated for each vertex and the indices are N,0,0,0 repeated for each vertex with N seeming to be the bone index minus one (each mesh has one corresponding bone). So I tried setting this data for the replacement mesh too, but it hasn’t helped.

const matrixIndex = newMeshes[i].getVerticesData('matricesIndices')[0];
const vertices = mesh.getVerticesData('position').length / 3;
const matricesIndices = Array(vertices).fill([matrixIndex, 0, 0, 0]).flat();
const matricesWeights = Array(vertices).fill([1, 0, 0, 0]).flat();
mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, matricesIndices, false);
mesh.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, matricesWeights, false);