Jump to content

How to load local OBJ file


duleaux
 Share

Recommended Posts

I need to load an OBJ file from my local machine using ImportMesh or addMeshTask, but nothing I've tried so far works.  I've read probably every posting on this topic on the internet, including those in this forum, over the past 24 hours and can't find a solution.  Here is the basic code that fails and triggers the "loader.onTaskError" error handler, all within my createScene() function:

 

            var loader = new BABYLON.AssetsManager(scene);
            var pos = function (t) {
                t.loadedMeshes.forEach(function (m) {
                    m.position.x += 10;
                    m.position.y -= 5;
                    m.material = material;
                });
            };
            var shelves = loader.addMeshTask("", "", "", "shelves.obj");
            shelves.onSuccess = pos;
            loader.onTaskError = function (task) {
                alert('Error with loading by assetsManager...');
            };
            loader.onFinish = function () {
                engine.runRenderLoop(function () {
                    scene.render();
                });
            };
            BABYLON.OBJFileLoader.OPTIMIZE_WITH_UV = true;
            loader.load();


I know that my "shelves.obj" file is okay because I can load it using this Playground example:  https://www.babylonjs-playground.com/#6N13NV#33.  However, this Playground uses a file chooser and converts the selected file into a File object that is loaded using this command:  loader.addMeshTask("", "", "file:", objectFileName).  My understanding is Javascript File objects can't be directly instantiated, so this does not seem to be an option for what I am trying to accomplish.  I also am not sure if the loader in my code is even able to find the "shelves.obj" file (I've placed it in virtually every location in my computer, but to no avail).  If anyone has any ideas please let me know.

 

Link to comment
Share on other sites

Hiya @duleaux, welcome to the forum.  No success and no replies yet?  Sorry. 

I don't have much experience with this subject, but often, people get a free github account, and publish their test-models... there.  Here's my free account:  https://github.com/Wingnutt

Let's look inside my 'misc' folder.  See the file 'mesh01.obj' ?

Now, off to the playground.  https://www.babylonjs-playground.com/#3FV2X#11

Notice the strange 'rawgit' URL domain?   But, it works.  Object loaded.  And, I didn't need to create/find a web server to store my .obj upon.  Github stored it for free, and allowed me to "drag'n'drop" the file into the misc folder (be sure to hit 'commit changes' button at bottom, after the drag-drop).

But yeah, I THINK scene mesh are primarily loaded from web servers, and I'm not sure how well .addMeshTask works... using local or localHost files. 

Stay tuned - others will surely comment soon (and likely, they will have more experience than I have).  :)  Report thoughts/discoveries, please.  (thx)  I have 'featured' this thread, so it has a green star next to its title.  That will surely get lots of attention, but we might need to wait until Christmas folk finish playing-with their new toys.  :D

Link to comment
Share on other sites

Hi Wingnut - this is great, thank you.  I copied your playground code into my local app and all works well.  Substituting your github-hosted file with my locally-stored file, however, yields the same result as before - i.e., nothing is displayed.  loader.addMeshTask(...) seems unable or unwilling to allow access to local files, perhaps as a security measure.  I will sign up for a github account and load my files at that location - seems to be the best solution.  Thanks again for your help!

Link to comment
Share on other sites

you may need to run a local webserver for local access (ie: http:// instead of file://).  if you open your dev tools and go to network tab then you will likely have a 404 error (not found) and perhaps a CORS preflight error. it may just be a matter of adjusting the file location (correct folder and case).

Link to comment
Share on other sites

  • 1 year later...
  • 3 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...