Is it possible to load obj files with mtl files (it seems not to work !)?

Hello,

I read in the documentation that loadModel() can load a obj file and use the mtl file and read the textures but it seems not to work at all.

model() draws the meshes but without any textures. I’ve seen a lot of questions about that in the forum but no clear response (I think).

I tried with several obj files…

is there a way to do this ?

:slight_smile:

thanks

(I mean : without applying the texture manually with the texture() function)

Update: I didn’t the question properly, and answered for colours not textures!
Original reply follows for continuity, but should be ignored.


Hi! Yes, possible in p5v2. Here’s a fairly minimal working example:
https://openprocessing.org/sketch/2673859

(I don’t know for sure about p5v1.)

Troubleshooting:

You can check your .obj file in a text editor to see if it mentions the expected .mtl filename.

For example, here are the first lines of the .obj file from the above example:

# Blender 4.3.2
# www.blender.org
mtllib pine_tree.mtl

(The first two lines are just comments.)
On the third line you can see the material file mentioned - so p5 tries to load pine_tree.mtl.

Currently not for textures, no, just for other material properties. There’s an open issue for it Support for `.mtl` Files with Textures · Issue #6924 · processing/p5.js · GitHub and the main technical difficulty that needs to be overcome is the fact that one model in p5 cannot have multiple textures referenced within it, so we need to make an entity to import the 3D file as which can have multiple components with multiple textures and that can still be drawn with the same APIs.

ok, thanks, good to know.

it works when applying the texture manually like this

… if there is only one texture image.

(the main thing is to have a method that works!)

:slight_smile:

Your obj file has no texture I think but colors ?

:slight_smile:

Yep, my mistake - I didn’t read your question properly, and thought you were asking about colours in .mtl!. (This, despite the work “texture” being used 4 times… :see_no_evil_monkey:)

Sorry for the noise.

No, no, I’m interested in knowing if the colors indicated in an mtl file are read correctly with the loadModel() function that’s why I was asking this.

in fact I would like to look your obj and mtl files to see how it is written (in the mtl file).

In my project, I’m beginning to think about the 3D version (that’s why I”m beginning to study this) among other things.

:slight_smile:

hey, wanted to chime in here, this has been a known architectural limitation for a while. it is something i have been really interested in and spending time thinking about, so hopefully if things line up, i might get to work on it in the next few months. fingers crossed things go well and obj + mtl with textures just works out of the box soon!

This would be a huge and really useful improvement (especially in the case where there are several textures and colors instead of textures)!!!
That can’t be easy to do!!!

yes fs, would be very useful !!

Hi! Hopefully you were able to find the .mtl file in the openprocessing project no problem. It is pretty minimal - this is all of it - generated in the pair with the .obj file by blender.

# Blender 4.3.2 MTL File: 'None'
# www.blender.org

newmtl tree_bark_brown
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Kd 0.201011 0.130373 0.101419
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2

newmtl tree_green
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Kd 0.072576 0.297737 0.074483
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2

(I notice the same sketch doesn’t load on the web editor due to asset renaming. I mention it just in case you try there. I’ve never had any problem on openprocessing with this .obj + .mtl setup, though)