Cura: Assistance exporting node to STL file

Created on 18 Aug 2020  路  3Comments  路  Source: Ultimaker/Cura

I'm sorry if this has already been answered, but could not find in the existing issues or stack overflow. We are attempting to programmatically export our scene to an STL file, similar to how UM.OutputDeviceManager.requestWriteToDevice is called in the QML. We have gotten a bit lost in the code bases; is there a workflow to call the Uranium STLWriter from Cura? Our desired end state is to programmatically export the nodes in a scene to an STL file using a Python script within a plugin. Any tips would be sincerely appreciated, thank you!

Question

Most helpful comment

Alternatively, if you already have a stream you want to write to, you can use:

application = Application.getInstance()
mesh_writer = application.getMeshFileHandler().getWriter("STLWriter")
mesh_writer.write(stream, nodes)

All 3 comments

Writer-plugins register themselves with the kinds of files they can write.

I think you need to (indirectly) call requestWrite in LocalFileOutputDevice with the right mime-type, via a call to OutputDeviceManager.requestWriteToDevice with the device_id set to "local_file".

Alternatively, if you already have a stream you want to write to, you can use:

application = Application.getInstance()
mesh_writer = application.getMeshFileHandler().getWriter("STLWriter")
mesh_writer.write(stream, nodes)

Thank you both so much or your quick responses, we sincerely appreciate them! For anyone reviewing this in the future, I was able to use the following (messy) code. Thank you again!

from cura.CuraApplication import CuraApplication
from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
application = CuraApplication.getInstance()
hardCodedSTLPath = "testpath.stl"
nodes = []
for node in DepthFirstIterator(application.getController().getScene().getRoot()):
  nodes.append(node)
mesh_writer = application.getMeshFileHandler().getWriter("STLWriter")
with open(hardCodedSTLPath,'w') as stream:
  mesh_writer.write(stream, nodes)
Was this page helpful?
0 / 5 - 0 ratings