Change MTL of OBJ while OBJ is already lodaded

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


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

how does it mean?

Something like that:

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

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

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

OK I see :slight_smile: 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.

maybe it is what i looking for.

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

ah now i have understood it.

I have analized the sourcecode on Github xD

  		url : "./files/test.mtl",
  		dataType: "text",
  		success : function (data) {
  			var test = new BABYLON.MTLFileLoader();
  			test.parseMTL(scene, data, "./files/");

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


I tweaked my playground with your trick: Babylon.js Playground

(also, the rabbit mesh now have uv :wink: )

1 Like