Import GLB containing primitives and rename meshes

I have a couple of models of houses in Blender which I export to GLB e.g. house_2.glb and house_3.glb. Both are made up of multiple meshes like “door”, “window” etc.
When I import them I want to be able to then clone specific parts of each house to construct new houses. To do this I need to be able to specifically reference each component. Previously I was using scene.getNodeByName however with multiple “door”, “window”, etc. meshes now being imported this does not work anymore.

I am using react-babylonjs and the useAssetManager hook. In the onSuccess callback I rename the meshes to prepend the file name e.g. the result.loadedMeshes.forEach(m => m.name = ("house_3_" + m.name)) below:

tasks = [
    {
        taskType: TaskType.Mesh,
        name: "load house_3",
        rootUrl: "/public/models/house/",
        sceneFilename: "house_3.glb",
        onSuccess: result =>
        {
            const root = result.loadedMeshes.find(m => m.name === "__root__")
            root?.setEnabled(false) // disable the root which will hide all meshes of the imported model
            if (root)
            {
                result.loadedMeshes.forEach(m => m.name = ("house_3_" + m.name))
                success()
            }
            else reject("Can not find house_3.glb")
        },
        onError: reject
    },
]

useAssetManager(tasks)

This seems to work well for meshes where only one material is used however when I have used multiple materials, e.g. for colouring the inside of a chimney black with soot and the outside grey stone, or the tread of some stairs being a different colour to the stair material, then when these meshes are imported they become: chimney_primitive0 and chimney_primitive1 etc.
I can rename these primitives to house_3_chimney_primitive0 but then the scene.getNodeByName stops working as in their “node form” they are still refered to as chimney, stairs, etc. instead of house_3_chimney

I would like to avoid having to append the model name to each of the meshes in Blender before I export them as these seems clunky and error prone. Is there a solution or a better approach to this?

Maybe use result.loadedTransformNodes as well ?

2 Likes

Perfect. Thanks. Yes I just had to upgrade my dependencies (now at version 5.31.0) and I can now see the loadedTransformNodes array. I now run:

result.loadedMeshes.forEach(m => m.name = ("house_3_" + m.name))
result.loadedTransformNodes.forEach(m => m.name = ("house_3_" + m.name))
success()
2 Likes