CesiumGS / obj2gltf

Convert OBJ assets to glTF
Apache License 2.0
1.72k stars 308 forks source link

Fix unhandled error when mtl is missing #268

Closed lilleyse closed 3 years ago

lilleyse commented 3 years ago

While testing out Node 16.9.0 for https://github.com/CesiumGS/obj2gltf/issues/266 I noticed that missing mtl files were not being handled gracefully anymore. Both unit tests and command line conversions were failing.

slilley@Lithium:~/Code/obj2gltf$ node bin/obj2gltf.js -i ./specs/data/box-missing-mtllib/box-missing-mtllib.obj -o ~/Desktop/test.gltf
node:events:371
      throw er; // Unhandled 'error' event
      ^

Error: ENOENT: no such file or directory, open '/box.mtl'
Emitted 'error' event on Interface instance at:
    at ReadStream.onerror (node:readline:265:10)
    at ReadStream.emit (node:events:406:35)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/box.mtl'
}

Previously in Node 14.17.1 I would see

slilley@Lithium:~/Code/obj2gltf$ node bin/obj2gltf.js -i ./specs/data/box-missing-mtllib/box-missing-mtllib.obj -o ~/Desktop/test.gltf
ENOENT: no such file or directory, open '/box.mtl'
Could not read material file at /box.mtl. Attempting to read the material file from within the obj directory instead.
ENOENT: no such file or directory, open '/home/slilley/Code/obj2gltf/specs/data/box-missing-mtllib/box.mtl'
Could not read material file at /home/slilley/Code/obj2gltf/specs/data/box-missing-mtllib/box.mtl. Using default material instead.
Total: 12.839ms

Which would produce a glTF file with a default material.

The bug ended up being similar to the one here: https://stackoverflow.com/questions/62885667/why-does-this-readline-async-iterator-not-work-properly/62887022#62887022. The suggested fix was to wait for the stream to open before initializing readline. This seemed to do the trick.