# Change MTL of OBJ while OBJ is already lodaded

**URL:** <https://forum.babylonjs.com/t/change-mtl-of-obj-while-obj-is-already-lodaded/3754>\
**Category:** Questions\
**Created:** [June 7, 2019, 12:58pm UTC](https://forum.babylonjs.com/t/change-mtl-of-obj-while-obj-is-already-lodaded/3754 "2019-06-07T12:58:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rotzend](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@Rotzend](https://forum.babylonjs.com/u/Rotzend)\
**Post date:** [June 7, 2019, 12:58pm UTC](https://forum.babylonjs.com/t/change-mtl-of-obj-while-obj-is-already-lodaded/3754/1 "2019-06-07T12:58:30Z")

</div>

Hey guys,

we work on an CAD Viewer and have now following Problem / Question.

We have OBJ Files which have more mtl files. (~3-4). We want to load the OBJ with the main MTL and if the User hit a button, we give the current OBJ a new MTL (Cause there are a view new Textures).

Is there a way to change the whole MTL?

The alternative is to analyse the mtl’s and send texture changes… But the first idea is much better xD

Thanks,  
Daniel

---

<div class="post-metadata">

**Author:** ![Vinc3r](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vinc3r/32/25340_2.png) [@Vinc3r](https://forum.babylonjs.com/u/Vinc3r)\
**Post date:** [June 7, 2019, 1:18pm UTC](https://forum.babylonjs.com/t/change-mtl-of-obj-while-obj-is-already-lodaded/3754/2 "2019-06-07T13:18:46Z")

</div>

Is it not simpler to generate the new material directly into your app rather than generate & loading another .mtl?

---

<div class="post-metadata">

**Author:** ![Rotzend](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@Rotzend](https://forum.babylonjs.com/u/Rotzend)\
**Post date:** [June 7, 2019, 1:23pm UTC](https://forum.babylonjs.com/t/change-mtl-of-obj-while-obj-is-already-lodaded/3754/3 "2019-06-07T13:23:55Z")

</div>

how does it mean?

---

<div class="post-metadata">

**Author:** ![Vinc3r](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vinc3r/32/25340_2.png) [@Vinc3r](https://forum.babylonjs.com/u/Vinc3r)\
**Post date:** [June 7, 2019, 1:54pm UTC](https://forum.babylonjs.com/t/change-mtl-of-obj-while-obj-is-already-lodaded/3754/4 "2019-06-07T13:54:52Z")

</div>

Something like that: [https://www.babylonjs-playground.com/index.html#95MJI8#37](https://www.babylonjs-playground.com/index.html#95MJI8#37)

(it’s weird, the .obj looks like broken here, but the logic is OK - [edit] yep, this .obj doesn’t have UV)

---

<div class="post-metadata">

**Author:** ![Rotzend](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@Rotzend](https://forum.babylonjs.com/u/Rotzend)\
**Post date:** [June 7, 2019, 1:57pm UTC](https://forum.babylonjs.com/t/change-mtl-of-obj-while-obj-is-already-lodaded/3754/5 "2019-06-07T13:57:36Z")

</div>

ah ok. Yeaj this is plan B.

Cause from the Customer we will get only OBJ / MTL files and before we split them, maybe there is a function that do this already. Something like this is also fine (but i can’t load a mtl? xD) [MTLFileLoader - Babylon.js Documentation](https://doc.babylonjs.com/api/classes/babylon.mtlfileloader)

But if there something like  
OBJFile.changeMTL(“path to new mtl”) it will be a lot better, cause it’s smaller.

---

<div class="post-metadata">

**Author:** ![Vinc3r](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vinc3r/32/25340_2.png) [@Vinc3r](https://forum.babylonjs.com/u/Vinc3r)\
**Post date:** [June 7, 2019, 2:02pm UTC](https://forum.babylonjs.com/t/change-mtl-of-obj-while-obj-is-already-lodaded/3754/6 "2019-06-07T14:02:26Z")

</div>

OK I see 🙂 About the MTLFileLoader you’re linking I can’t help, but it looks like you just have to use the parseMTL function using your new .mtl url isn’t it? Then just apply your new materials on your already loaded meshes.

---

<div class="post-metadata">

**Author:** ![Rotzend](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@Rotzend](https://forum.babylonjs.com/u/Rotzend)\
**Post date:** [June 7, 2019, 2:08pm UTC](https://forum.babylonjs.com/t/change-mtl-of-obj-while-obj-is-already-lodaded/3754/7 "2019-06-07T14:08:25Z")

</div>

maybe it is what i looking for.

But how does this work?  
I don’t get it Run 😕

---

<div class="post-metadata">

**Author:** ![Rotzend](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@Rotzend](https://forum.babylonjs.com/u/Rotzend)\
**Post date:** [June 7, 2019, 2:16pm UTC](https://forum.babylonjs.com/t/change-mtl-of-obj-while-obj-is-already-lodaded/3754/8 "2019-06-07T14:16:57Z")

</div>

ah now i have understood it.

I have analized the sourcecode on Github xD

> ```
> $.ajax({
> url : "./files/test.mtl",
> dataType: "text",
> success : function (data) {
> var test = new BABYLON.MTLFileLoader();
>   			
> test.parseMTL(scene, data, "./files/");
> console.log(test.materials);
> }
> });
> 
> ```

First you need the MTL as TEXT (data). You will need your scene and you need the “dir” of the file (for Textures etc.)

---

<div class="post-metadata">

**Author:** ![Vinc3r](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vinc3r/32/25340_2.png) [@Vinc3r](https://forum.babylonjs.com/u/Vinc3r)\
**Post date:** [June 7, 2019, 3:36pm UTC](https://forum.babylonjs.com/t/change-mtl-of-obj-while-obj-is-already-lodaded/3754/9 "2019-06-07T15:36:02Z")

</div>

Nice!

I tweaked my playground with your trick: [Babylon.js Playground](https://www.babylonjs-playground.com/index.html#0SHBCK)

(also, the rabbit mesh now have uv 😉 )
