Exporting a model to STL with markups

I have a model and have a point list of markups on the model, How do i export this model to STL with the control points?

You could use a Python snippet like this

fiducialNode = getNode('F')
import numpy as np
sphereSource = vtk.vtkSphereSource()
sphereSource.SetRadius(1)
for i in range(fiducialNode.GetNumberOfControlPoints()):
  p = np.zeros(3)
  fiducialNode.GetNthControlPointPosition(i, p)
  fiducialModel = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLModelNode', f'Model_{fiducialNode.GetName()}_{i}')
  sphereSource.SetCenter(p)
  polyData = vtk.vtkPolyData()
  sphereSource.Update()
  polyData.DeepCopy(sphereSource.GetOutput())
  fiducialModel.SetAndObservePolyData(polyData)

Please note you’ll need to replace the name of your fiducial node (in my example it is F) and set the sphere radius as desired.

Once you have the model nodes you can append them together in Dynamic Modeler module’s Append feature, then export to STL.

2 Likes

Alternatively, you can use a glyph filter like this:

markup = getNode('F')
glyph = vtk.vtkSphereSource()
glyph.SetRadius(15.0)
glypher = vtk.vtkGlyph3D()
glypher.SetSourceConnection(glyph.GetOutputPort())
glypher.SetInputConnection(markup.GetCurveWorldConnection())
model = slicer.modules.models.logic().AddModel(glypher.GetOutputPort())
slicer.util.saveNode(model, "c:/tmp/something.stl")
2 Likes

Both of the above mentioned solutions work perfectly, i can now export a model that has all the points on it. Thank You.
Would it be possible to have the points and the model to be in different colors? When i use the Append feature in Dynamic Modeler the final Model is of all the same color, i would prefer if the points and my model where of different colours.

Can this be extended to a curve? If my markups are in the form of a curve, can that be exported to an STL?

Yes, it works exactly the same way for curves, just replace the glyph filter by tube filter.

1 Like