Can't load .obj file

I’m sure this is an easy fix, but I am getting an error when loading my .obj file:

SyntaxError: Unexpected token m in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.importMesh (babylon.js:16)
    at babylon.js:16
    at f (babylon.js:16)
    at XMLHttpRequest.p (babylon.js:16)

I am using SceneLoader.ImportMeshAsync(). Also, I am calling import ‘babylonjs-loaders’;.

The first few lines of the .obj file are:

mtllib lowpoly_nature.mtl

o Object.1
v -198.714966 -221.212341 0.255110
v -192.917786 -225.607422 0.254375
v -189.864487 -231.214478 0.254341

But I’m not sure what I am doing wrong… any help would be appreciated :slightly_smiling_face:

have you included the importers?

Do you mean babylonjs-loaders? Yes

I removed this line from the .obj file, and it can load now. But I still can’t see my mesh :sweat:

1 Like

If your OBJ file doesn’t include a MTL file, then it generally will not load. There is also a reference to a texture if one was used, but the OBJ file will load without this.

FYI - if you remove the reference to the MTL file, it defaults to a generic material.

Galen

I just decided to convert the .obj into a .babylon file in blender.

On a side note, does anybody know how to clone a mesh? I’m not sure why this isn’t working (I only see one Babylon Logo box still):

https://www.babylonjs-playground.com/#17LHMG#8

SceneLoader.ImportMeshAsync returns a promise - use then

Also because of auto position of camera you will then need to scale logo to see both

https://www.babylonjs-playground.com/#17LHMG#9

1 Like

Oh I see. Actually you can also do it using the await/async pattern, but it seems like the problem I had was I was trying to use the mesh at index 0, when I should have been using the mesh at index 1. It turns out that is the same problem I was having when trying to import the .obj file too, haha. Guess I need to debug a bit better before asking questions… Thanks for the help by the way.

1 Like

I learn something new every day :smiley:

Coming from a .NET background, its what I’m more used to haha. But it seems like a lot of javascript developers are more used to using then()

Ok, now I have a new problem. I can finally load the .obj file correctly, and the material is working too. But there is no ‘shading’ on the object. I tried messing around with the emissive/diffuse colors, but had no effect. I don’t need it to be ‘textured’, just having plain colors is fine (in this case, brown for the trunk and green for the leaves). But I am at least needing some ‘shading’ (sorry if I’m not using the proper terminology here).

palmtree

Could be positioning of lights? You may be better off asking a new question where the title indicates the problem.

Yeah, but I’m afraid I’d end up spamming this forum haha.

Anyways, after some fiddling, I was able to fix it by calling convertToFlatShadedMesh() on the mesh. Seems like everything is finally ok now.

Thanks again for the help.