Swapping meshes in Babylon

Hello.
I’m pretty new to Babylon though I’ve used te basic viewer and i have a reasonable understanding of webgk and gltf structure.

I created a gltf file which has multiple child nodes each of which can references a single mesh from a collection of very similar options. Initially each node references the same mesh but the intention is to interactively edit the model in javascript to change the meshes referenced by the nodes. Looking at the model in javascript after loading my single gltf, it looks like it is optimised so that it only contains the active meshes - ie those referenced in the root.
What is the correct way to achieve my objective? Im guessing i need to use asset containers? Any pointers to the relevant documentation or (even better) any sample code showing how to swap gltf meshes would be greatly appreciated.

Many thanks
Des

Unfortunately AssetContainer will mostly provide a addAll or RemoveAll from scene, So the easiest in your case could be to load all your meshes, disable their root with setEnabled(false) and then simply reparent on demand with .parent = … and setEnabled(true)

Many thanks for the response

If I understand you correctly I would create an extra disabled node and add every mesh option to that node to ensure it becomes part of the model but not a visible part. Then I reparent and re-enable a mesh on demand?

In my case I can have multiple instances of each mesh - in theory every top level node could instance the same mesh with a different transform matrix. Would your solution allow for that - I mean can I reparent a mesh to multiple nodes?

Thanks again

A mesh can only have one parent at a time but it will work if you reparent as needed.

Thanks

Looks like I had some misconceptions about how a gltf model mapped onto a Babylon scene.

I’m thinking the easiest solution for me would be if I could load a local gltf json string directly into Babylon. Do you know if there’s any mechanism to do this - can LoadMesh take a local javascript string as an argument rather than a url for example. Is there a LoadMeshFromString - don’t see one.

Thanks again for your help so far.
Des

This should help Loading Any File Type | Babylon.js Documentation

Cheers, yes that does enable me to load from a string.

Hey @DesG

Do you know Variant concept on gltf files? Maybe could be an option for what you want to do!
Look at this sample

1 Like

Hello @DesG just checking in, was your question answered? :slight_smile:

Hi
Thanks for following up and apologies for not responding earlier.
Yes, my question was answered

Regards
Des

1 Like