Jump to content

How to import a .obj model ?


ILNILEY
 Share

Recommended Posts

Hey, guys.

I want to use Babylon.js to import a .obj model . But I am not sure whether I am right or not. 

In my index.html , I have imported Babylon.ObjFileLoader.js, and after creating a scene, I coded "BABYLON.SceneLoader.Load("","pinecone.obj",engine,scene);". the model file is in the same project file with my index.html.

I can't import my model successfully , error is "Failed to load resource: net ::ERR_FILE_NOT_FOUND" .

I hope someone could tell me the steps to import a .obj model.

Link to comment
Share on other sites

Hi @ILNILEY, welcome to the forum.  Yeah, a file not found is interesting.  Check your typing carefully.

Can you grab this little zip package?

http://webpages.charter.net/wingthing/webGL/objLoad.zip

It's slightly different code (uses Append instead of Load)... but it worked fine for me, here, locally... loading a little .obj file (included in package).  See if it works at your house, if you wish.

Report back, if you would, thanks.  This is likely a simple problem... so check carefully for those "hidden obvious things".  :)  Again, welcome, we'll talk more, be well.  Hope this helps, somehow.

Link to comment
Share on other sites

3 hours ago, Wingnut said:

Hi @ILNILEY, welcome to the forum.  Yeah, a file not found is interesting.  Check your typing carefully.

Can you grab this little zip package?

http://webpages.charter.net/wingthing/webGL/objLoad.zip

It's slightly different code (uses Append instead of Load)... but it worked fine for me, here, locally... loading a little .obj file (included in package).  See if it works at your house, if you wish.

Report back, if you would, thanks.  This is likely a simple problem... so check carefully for those "hidden obvious things".  :)  Again, welcome, we'll talk more, be well.  Hope this helps, somehow.

Thank you for answer ,but your code doesn't work for me , the error is the same.

Link to comment
Share on other sites

Well darn.  Sorry, Ilniley. 

Are you browsing index.html from a web server / web site? 

Or perhaps double-clicking on index.html  ... within a local hard drive folder?  (better way for testing)

This is puzzling.  I'll keep thinking.

Link to comment
Share on other sites

Hey @ILNILEY,

What a coincidence! I was going to create a new thread exactly for this problem.

Although I haven't fixed it myself, I can see already some obviously problems with your code. Take a look here: https://doc.babylonjs.com/extensions/obj

Basically, you are not giving any path and the `Load(...)` method is for getting scenes, not objects. 

Here is how I am currently doing it and, although I am not getting any error and it gets loaded, I cannot access the `loadedMeshes` array:

        let loader = new BABYLON.AssetsManager(scene);
        loader.useDefaultLoadingScreen = true;  // Set to false to remove the default loading
        let mesh_loaded_task = loader.addMeshTask(<TASK_NAME>, <MESHES_NAMES>, <PATH_TO_OBJ_FOLDER>, <FILENAME_DOT_OBJ>);

        mesh_loaded_task.onSuccess = function (task) {
             task.loadedMeshes.forEach(function(m) {
                console.log("Loaded!");
                m.position = BABYLON.Vector3.Zero();
             });
        };


        loader.onFinish = function() {
            engine.runRenderLoop(function () {
                scene.render();
            });
        };

        loader.load();

Try this and enjoy. If anyone sees a problem with this, let me know :D

 

EDIT: For some reason I was led to believe loadedMeshes was an array. Instead `forEach` needs to be used.

Link to comment
Share on other sites

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...