# Travers trough all layers in a glb file and add different materials - not working

**URL:** https://discourse.threejs.org/t/travers-trough-all-layers-in-a-glb-file-and-add-different-materials-not-working/15483
**Category:** Questions
**Tags:** materials
**Created:** [May 23, 2020, 5:50pm UTC](https://discourse.threejs.org/t/travers-trough-all-layers-in-a-glb-file-and-add-different-materials-not-working/15483 "2020-05-23T17:50:01Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Fillock](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@Fillock](https://discourse.threejs.org/u/Fillock)
#### Post date: [May 23, 2020, 5:50pm UTC](https://discourse.threejs.org/t/travers-trough-all-layers-in-a-glb-file-and-add-different-materials-not-working/15483/1 "2020-05-23T17:50:01Z")

</div>

Hi. I try to travers trough all layers in a loaded glb file and give each layer a different material, but it’s not working. Any ideas where I do wrong?

function doorMaterial (setLayer) {

```
const assetPath = '../models/greenhouse/'; // model-folder
const loader = new THREE.GLTFLoader();
loader.setPath(assetPath);
loader.load('g-door.glb', function(object){  

    var i = 0;

    object.scene.traverse(function(child){ // travers trough all the layers in the glb file

        if (i == 0) {
            doorMaterial1 = new THREE.MeshPhongMaterial ({color: 0x3333ff, opacity: 0.5, transparent: true, }); // denne funker
            child.material = doorMaterial1;
        }

        if (i == 1) {
            doorMaterial2 = new THREE.MeshPhongMaterial ({color: 0x339933, opacity: 0.5, transparent: true, });  
            child.material = doorMaterial2;
        }

        if (i == 2) {

            doorMaterial3 = new THREE.MeshPhongMaterial ({color: 0x330033, opacity: 0.5, transparent: true, }); 
            child.material = doorMaterial3; 
        }
        child.layers.set(setLayer);
        }
        i ++;
    });

    scene.add(object.scene.children[1]);
    model = new THREE.Object3D();
    scene.add(model);
    model.add(object.scene.children[0]); // skjønner ikke hvorfor man add'er [0], men scenen [1] ?

});

```

}

---

<div class="post-metadata">

### Author: ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)
#### Post date: [May 24, 2020, 9:15am UTC](https://discourse.threejs.org/t/travers-trough-all-layers-in-a-glb-file-and-add-different-materials-not-working/15483/2 "2020-05-24T09:15:57Z")

</div>

> [@Fillock](#):
>
> but it’s not working

Please be more specific. What exactly does not working? Can you please describe in more detail the expected and actual result? So far it’s not clear to me what you are trying to do with your shared code.

---

<div class="post-metadata">

### Author: ![Fillock](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@Fillock](https://discourse.threejs.org/u/Fillock)
#### Post date: [May 24, 2020, 11:06am UTC](https://discourse.threejs.org/t/travers-trough-all-layers-in-a-glb-file-and-add-different-materials-not-working/15483/3 "2020-05-24T11:06:22Z")

</div>

Shure:

I want to load one glb file, it contains three object; 1 - window-glass, 2 - wood-frame, 3 - doorknob. Then I want to travers through all three loaded objects, sort them out one by one, and give each object a unique material.

But when I run the code the Door do not have this materials added.

---

<div class="post-metadata">

### Author: ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)
#### Post date: [May 24, 2020, 11:21am UTC](https://discourse.threejs.org/t/travers-trough-all-layers-in-a-glb-file-and-add-different-materials-not-working/15483/4 "2020-05-24T11:21:48Z")

</div>

In this case, it’s not necessary to work with layers. In general, you might have more success if you select objects contained in your asset hierarchy via name by using [Object3D.getObjectByName()](https://threejs.org/docs/index.html#api/en/core/Object3D.getObjectByName). That works better since there might be intermediate nodes in the hierarchy that you select accidentally with your current approach.

---

<div class="post-metadata">

### Author: ![Fillock](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@Fillock](https://discourse.threejs.org/u/Fillock)
#### Post date: [May 25, 2020, 4:35pm UTC](https://discourse.threejs.org/t/travers-trough-all-layers-in-a-glb-file-and-add-different-materials-not-working/15483/5 "2020-05-25T16:35:03Z")

</div>

Thanks Mugen87, I will try with Object3D.getObjectByName()
