Exporting rhino3dm.File3dm to STL in python

Hello everyone,

I’m using rhino.compute to calculate a mesh.

How could I convert the 3dm decoded mesh to an STL file?

Currently I can only save it as a 3dm:

import compute_rhino3d.Grasshopper as gh
import rhino3dm

output = gh.EvaluateDefinition(definition_path, trees)
mesh = output['values'][0]['InnerTree']['{0}'][0]["data"]

mesh = rhino3dm.CommonObject.Decode(json.loads(mesh))
doc = rhino3dm.File3dm()
doc.Objects.AddMesh(mesh)
doc.Write("model.3dm", version=1)

Thank you very much!

Hi @Mario_Quintana Do any of the methods mentioned in this thread help you? Python script to export STLs - #2 by Willem

Hello Andy, Thank you for your answer.
I’m running my script in a docker container in a lambda function.
I don’t think I can start the Rhino service in order to send commands to it.
Could I do the same using rhino compute?

If you are already using Rhino.Compute, you could convert your 3dm to stl in your gh definition. You’d probably need to save it to disk, encode it as a base 64 string, and have that be a string output to your gh definition. We don’t have an example that does this, but I can cook something up in the next few days.

Great idea!

I implemented your suggestion using the Pancake export to stl component.
It’s working just fine!

This is the implementation:

Grasshoper definition:
stl_exporter.gh (9.8 KB)

Python Decoding:

import compute_rhino3d.Grasshopper as gh
import rhino3dm

output = gh.EvaluateDefinition(definition_path, trees)
stl_b64_mesh = output['values'][0]['InnerTree']['{0}'][0]["data"]

decoded_bytes = base64.b64decode(stl_b64_mesh)

with open(output_path_stl, "wb") as f:
    f.write(decoded_bytes)

Thank you very much Luis and Andy!

3 Likes