[SOLVED] I can't find a material in a scene if isn't applied to a mesh

Hi everyone,
I have a .babylon file with some meshes with some materials appllied, in the same file i have others material not applied to any mesh. When i try to find material by name i cannot find the materials not applied to a mesh.

The goal is to replace materials between meshes

Thanks a lot

Hello and welcome!

Is it possible to share your .babylon file to have a look?

Thanks for the welcome!

I couldn’t share it, I’ll send a screenshot. From the sandbox I can change with a material not combined with a mesh but present in the babylon file. Via javascript I can’t find it in the scene I don’t know if I’m doing something wrong! Or is there a specific way. Should I find it if I search for it by id in the scene? I use assetsmanager to upload.

Thank you

Hi, just make sure your object (+ materials) is fully loaded when you search for material

1 Like

ita loaded. I’ll do the change later via the interface. So if the material is in the file but not in the scene I should still find it by doing scene.getmaterialbyid ?correct?

Thanks

Hi and welcome to our Community :smiley:

First things first, as said above, the material needs to be loaded in the scene. To ensure this, you can use the Inspector (i.e. in the PG or in scene) after scene is fully loaded. Like it appears you did from your screenshot. If the material is there once the scene is fully loaded, it likely means that you are trying to access the material BEFORE it’s loaded.

Loading async, through asset manager or a promise of any kind, you need to wait for the material to be loaded before attempting to access it. Else, it will generate an error (open the console to see that). The generated error will also prevent a number of script actions following the error.

After load, there are a number of ways you can access a material in scene. Yes, you can do it by ID but you can also do it by the material name or by calling a mesh and requesting for it’s material, again with the option to do it by name or by id. Going even further, you can look for descendants of nodes and you can also look for just a part/split in either the name or id which you can use as an identifier.
For whatever your strategy is, the most important thing is that, at the time you access it, the material (or mesh in case of mesh - or mesh AND material in case of accessing material through the mesh) NEEDS to be loaded in scene.

I hope this helps and again, welcome to the Community :sunglasses:

1 Like

Hi thanks to all for replies.
I solved it by using SceneLoader.Append instead of assetsManager.addMeshTask that doensn’t loads materials are not bind to a mesh.

thank you all, I hope to soon be able to contribute to this forum by increasing my skills.

2 Likes