Retrieving list of all materials in a scene

Hi everybody:

In order to change several properties that are not properly exported to GLTF file from the Maya Exporter, I need to access to the list of all materials used in the scene.

How can it be done?

Best regards.

Hey there,

you can access the materials present in the scene with scene.materials. This will return an array which you can later use.

More on this here :
https://doc.babylonjs.com/api/classes/babylon.scene#materials

Best,
Null

1 Like

Hi Null:

Thanks for your time and for the info.

Sadly, I can read about this API that the returned list of materials β€œβ€¦it is not supposed to be modified manually”.

This way I suppose than in order to update the materials I need to clone every one, update the cloned and then swap the former for the new.

Best regards.

Hi,

You are right, however that is more for removing or adding since there are methods available for that. If you want to edit you can also use the method getMaterialByName if you already know the materials name.

I do not think that accessing and then editing a material via scene.materials will break anything as long as you do not modify the array itself aka adding or removing materials without using the appropriate methods. I hope this was helpful.

Best,
Null

1 Like

If you know the material id from gltf you could simply scene.getMaterialById(…) and then modify at will ?

1 Like

Finally it has worked like a charm:

 BABYLON.SceneLoader.Append("./3dmodel/", "Abando.gltf", scene, function (scene) {

...

//modifico material
var material=scene.getMaterialByName("Cabina_ST_material");
material.separateCullingPass=true;

//modifico material
material=scene.getMaterialByName("Torre_ST_material");
material.separateCullingPass=true;
material.needDepthPrepass=true;

...

}

Thanks.

1 Like