Material cannot assign through iteration(for-loop)

Hi guys. I’m trying to make another configurator with Babylonjs scripts and this time I am trying to assign meshes with different materials respectively. I attempt to do it by introducing several for-loop functions. But I got struck when I was trying to assign the material from the created material list back to the mesh from the created mesh list. I do some simple test later by no introducing iteration in import mesh (use meshes[0].material = pbr_material_list[0]) I cannot assign the materials to the meshes still.
Is there anyone can help me to solve this small problem?(I need to make sure I can do it before moving to the next steps) Many thanks for that!
Below is my code

window.addEventListener("DOMContentLoaded", function() {
    var canvas = document.getElementById("canvas");
    var engine = new BABYLON.Engine(canvas, true);

    var createScene = function() {
        var ar = [];
        if (ar.length > 8) {
            var index = ar.indexOf(ar[ar.length]);
            ar.splice(index, 1);
        var pbr_list = [];
        var texture_list = [];
        for (v = 0; v < ar.length; v++) {
            var texture = new BABYLON.Texture("" + ar[v], scene, true, false, 2);
            var pbr_mat = new BABYLON.PBRMaterial("pbr_mat" + v, scene);
            pbr_list[v].albedoTexture = texture_list[v];

        BABYLON.SceneLoader.ImportMesh("", "", "untitled.glb", scene, function(meshes) {
            scene.createDefaultCamera(true, true, true);
            let test = meshes[1];
            let test2 = meshes[2];
            test.material = pbr_list[1];
            test2.material = pbr_list[2];
        }, null, null, ".glb")
          return scene;
    var scene = createScene();
    engine.runRenderLoop(function() {

Can you share a playground? also can you check in the inspector the result of your scene?

getting the meshes by indices might be the issue here also, materials for gltf have some special setup about sideOrientation and such so a playground would help here to understand what is wrong.

Hi @Cedric @sebavan
I tried to put the coding in background. But It seems not showing up correctly as it seems.

I also have put the scripts into the domain so that you guys can take a look at the scene.

Hi guys, I can do it now when I further my next step(I introduce drag and drop functions ). My initial idea is to drag and drop different materials into the meshes.

So no worries.

1 Like