3
votes

I am trying to load a .obj into Three.js using there objLoader.

var loader = new THREE.OBJLoader( manager );
                loader.load( 'obj/gate-2.obj', function ( object ) {

                    object.traverse( function ( child ) {

                        if ( child instanceof THREE.Mesh ) {

                            child.material.map = texture;

                        }

                    } );

All works fine using blender models however whenever I try to use my own custom 3Ds Max models, the .obj won't load within the browser and gives out an error. Am I exporting wrong or does Three.js not support 3Ds Max .obj exporter?

Error Message

    Error: Unexpected line: s 2
parseOBJLoader.js:339
(anonymous function)OBJLoader.js:24
(anonymous function)three.min.js:376:387
2
Try once to only export something simple like a cube first. And check if it works then... What version of Three.js are you using. - Wilt
@Wilt I assume the latest version as I only downloaded it the other day, the model works when re-rendered in blender, I'm not to sure the problem however will try the cube now! - Matt Hammond
Well, s 2 would be a smoothing setting. Does the loader support smoothing? I would assume that it would just skip over lines that it doesn't understand. I think it's for smoothing groups. - zero298
OBJLoader does work with .obj files exported from 3ds max with the exception of separate objects as outlined here. Export a very simple object and paste the contents of your .obj file in your question - 2pha
Looking at the code for OBJLoader.js, the regex for smoothing groups can only handle "s 1", but no other settings like "s 2", "s 3" etc.. I don't know what that means though, as I'm not familiar enough with the obj format or 3DSMax. But I just encountered the same problem trying to import an OBJ exported from 3DSMax to threejs. - Phasma

2 Answers

5
votes

I am currently experiencing this exact same error:

Error: Unexpected line: s 2

Using an .obj file exported by 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware.

I've followed @2pha's link related to the missing object names, and while this is true, after inserting the object names manually, I still experienced the 'Unexpected line' error.

My solution, while not a specific fix for the reasons why this error is happening, is to import the .obj file within Blender (https://www.blender.org/), and immediately export an .obj file. It seems the .obj file created by the Blender v2.76 (sub 0) OBJ Exporter can be imported with no addition manual labor using the Three.js THREE.OBJLoader.

I hope one day Three.js or 3ds Max will correct this error (it seems to be in the works for Three.js here), but in the meantime ain't nobody got time for that.

Update: I've submitted a pull request to fix this issue: https://github.com/mrdoob/three.js/pull/8118, in the meantime you can modify your smoothing_pattern to support multiple digits:

Line 220: var smoothing_pattern = /^s\s+(\d+|on|off)/;
0
votes

I'm experiencing the exact same error with 3ds Max Wavefront OBJ Exporter v0.97b and the latest version (74) of threejs.

I have no solution, but I can see that the problem is that when OBJLoader.js parses the obj file, it looks for smoothing groups in the format \^s\s+([01]|on|off)\ which only matches "s 1" or "s 0" and thus will throw an error at "s 2" and above. (You can check the regex with https://regex101.com in case like me you can't read regex ;))

It works when importing and then exporting with Blender, because Blender will export it with only "s 1" and "s off" as parameters.