Error with smoothing
oculushut opened this issue · 3 comments
I get an error when I use a model with "s" (smoothing) lines in the OBJ. I can repeat this with this Iron Man model: http://tf3dm.com/3d-model/ironman-rigged-original-model--98611.html
I am doing this call in my code:
parser.parse('IronMan.obj', options, function(err, data)
I get this error:
[MY DIRECTORY]\node_modules\objtojs\index.js:288
obj.smoothing = line[1];
^
TypeError: Cannot set property 'smoothing' of null
at parseText ([MY DIRECTORY]\node_modules\objtojs\index.js:288:21)
at [MY DIRECTORY]\node_modules\objtojs\index.js:44:5
at FSReqWrap.wrapper [as oncomplete] (fs.js:527:17)
Thanks for sharing the code by the way!
Here is some verbose logging. This is printed before the error posted earlier:
Setting options...
Opening File...
Getting file stats...
File Size: 16581665
Reading file...
Parse Started...
Parsing Materal lib file reference (mtlib): ./IronMan.mtl
Parsing Group name (g)
Parsing Geometric vertices (v)...
Parsing Vertex normals (vn)...
Parsing Group name (g)
Parsing Materal name (usemtl): darksilver
Parsing Smoothing Group (s)...
Parsing Faces (f)...
Parsing Group name (g)
Parsing Geometric vertices (v)...
Parsing Vertex normals (vn)...
Parsing Group name (g)
Parsing Materal name (usemtl): darksilver
Parsing Smoothing Group (s)...
Parsing Faces (f)...
Parsing Group name (g)
Parsing Geometric vertices (v)...
Parsing Vertex normals (vn)...
Parsing Group name (g)
Parsing Materal name (usemtl): red
Parsing Smoothing Group (s)...
Parsing Faces (f)...
Parsing Group name (g)
Parsing Geometric vertices (v)...
Parsing Vertex normals (vn)...
Parsing Group name (g)
Parsing Materal name (usemtl): red
Parsing Smoothing Group (s)...
Parsing Faces (f)...
Parsing Smoothing Group (s)...
Looks like this bit assumes that there is always a # line after a "f" (face) line in the file. It nulls out the obj:
if(c_type != p_type && (p_type == 'f' || p_type == 'l' || p_type == 'p' || p_type == 'end') && obj != null){
if(!data.objects){ data.objects = []; }
data.objects.push(obj);
obj = null;
}
However, looks like this file contains multiple smoothing groups so the data can look like this:
s 4
f 206//206 203//203 205//205
s 5
f 203//203 207//207 204//204
s 4
f 207//207 209//209 208//208
s 5
f 209//209 212//212 210//210
s 4
f 211//211 214//214 212//212
s 5
f 213//213 216//216 214//214
I've not really read the OBJ spec so not forking and fixing right now, but a conversation on the OpenGL forums suggests that the number associated with the s shows which faces belong to which smoothing groups... so I guess that it just means that this model has groups of faces which belong to different smoothing groups.
The switch statement fails to assign a value to the "smoothing" property of the nulled out obj object.
I think the code expected the switch statement to go to the default. i.e.
default:
write("Unprocessed Line: (#" + i + ") " + lines[i]);
I am guessing that this means that the order of the f and s values is important (i.e. which s precedes which set of f entries.)
I had to go back in and rework how it was collecting the data. The js object returned should more closely reflect the obj file format. It now preserves the element order as it is parsing the data. My latest changes have been pushed up. The file you provided helped me quite a bit. Thanks.