Rhino3dm - Threejs geometry import failing

Hello,

I’m trying to export my threejs mesh (imported from obj file) to be used inside rhino. I’m using rhino3dm for that however the file I receive is corrupted. My procedure is:

  1. Import the .OBJ file onto ThreeJS mesh
  2. Join all child mesh geometries onto single one one (this step can be ommited, it works properly)
  3. Export the combined mesh into .obj file (works correctly)
  4. Create new rhino mesh using rhino.Mesh.createFromThreejsJSON({ data: exportGeometry })
  5. Export this to the new 3dm file using doc = rhino.File3dm(); doc.objects().add(mesh, null); fs.writeFileSync('name.3dm', doc.toByteArray());

The result is that rhino cannot open this and says that the file is broken (not an m_archive format). What am I doing wrong?

I’m using rhino3dm@0.13.0 version, nodejs@12 and three.js@r0.125.2export_rhino.txt (908 Bytes)

@fraguada - can you help with this?

I’ll just leave the mesh here as an example.

meshInJoined.obj (8.4 KB)

@gorny.adrian the rhino.Mesh.createFromThreejsJSON() method takes a THREE.BufferGeometry as the input, not the obj format. After you import the obj with threejs, the resulting BufferGeometry from the THREE.Mesh object is what you want.

Check out this sample: src, live

1 Like

Yeah I already know that and in the attached code you can see we’re using mesh.geometry inside. Perhaps our call is wrong? We use rhino.Mesh.createFromThreejsJSON({ data: mesh.geometry }) to simplify it.

This sample in node works for me with a different file: src
Not sure how you referenced three and the obj loader, so I took a crack at it. Maybe there is a better way?

Not sure why your particular file fails.
This seems to be similar to this issue: toByteArray produces invalid 3dm file in some cases · Issue #353 · mcneel/rhino3dm · GitHub

If, for example, I add these two lines before writing the file:

const sphere = new rhino.Sphere([0,0,0], 12)
doc.objects().addSphere(sphere, null)

Then the file opens fine in Rhino:

Well I’ve used ESM imports for that. Perhaps the issue is that I’m not actually rendering the scene anywhere and calculations/updates are not made. I’ll clean-up the script, take another look on that and if it won’t work I’ll create a reproducible case.

Ok so @fraguada I’ve added the sphere code and it magically started working. What’s more I’ve also tried that with adding an arc and surprise - it also starts working. Performed test that uses deleteItem afterward and the error returns. So it seems that the only case it’s working is when you have normal mesh and some kind of nurb-based stuff like arc, circle, sphere or whatever.

After analyzing the code underneath from WASM lib source I’d guess that it has to do something with NURBs actually. Whenever you add an object that bases upon NURBs instead of standard geometry (I hope it makes any sense to you), all other meshes starts working. Not sure if it helps you in any way tho.

Perhaps something is initialized “by the way”?

Yes, but the example I link to has an obj, and converts just fine. Like I mention, we have another case where we’ve seen this behaviour, so there is a bug we need to track down and fix.