Is it possible to extract OBJ and MTL file from GET request?

I get a three.js script from my colleague and I have to integrate it into our web app. I have no experience in three.js.
The requirement is that when the page loads, the server sends OBJ and MTL files together with other images and texts to front end via request. The js code extract them from the request and load it into three.js scripte.
The backend is written in Python, Flask framework.
Is it possible to do that in this way or we are doing it a wrong way?

Thank you very much in advance,

Is it possible to do that in this way or we are doing it a wrong way?

The js code extract them from the request and load it into three.js scripte.

Well, you did kinda answer yourself. :thinking: Yep, it is possible, most of three.js apps work this way (ie. keep models separately from the code and fetch them on-demand with requests.)

Also, if you want to minimise the amount of requests and trouble - consider using .glb (binary glTF) with embedded materials and images. GLTFLoader will then apply textures and materials automatically for you.

2 Likes

That’s what THREE.OBJLoader is for, see this example. The loader makes a GET request to the server, which returns the files.

It’s usually best to configure your server to have “static hosting” of a folder containing these files, rather than writing server endpoints for particular files, although either will work.

I’m not sure if you could help me further. I kinda pass the OBJ and MTL file to the front end. I encode with base64 in the server and send it in JSON format to front end. Then I decode it with following JS.
$(document).ready(function() { var b = JSON.parse('{{ allData | tojson | safe}}'); var c = atob(b.mtlFile); var d = atob(b.objFile); });
Using typeof function, I find c and d are string. But I can see the content is exactly the same with the one where I open the MTL file with VSCode.So I’m not sure I can use OBJLoader.parse() function to load it.

I mean, it is a way to do that - but is there any question / problem / error with it? I can’t see a question :speak_no_evil:

I actually did that. Now I can show the object in the canvas. Thank you for your advise.