Interact with 3D model

Hi all,
I have an actor/mapper misunderstood with 3d slicer.
I want to interact in python, with my 3d model, but I don’t know how to access to the “GetOutput” of my loading model.

Pragmatically, how to find (in bold) that:

reader = vtk.vtkSTLReader()
reader.SetFileName(“human-head.stl”)
X=vtk.X()
X.SetInputConnection(reader.GetOutputPort())`

with that:

model = slicer.util.loadModel(“/netapp/vol1_homeunix/briend/human-head.stl”, returnNode=True)[1]

Thanks in advance,
Frederic

In general, you display surface meshes using a model node. You can create a model node directly from a file using this utility function:

slicer.util.loadModel('something.stl')

If you already have a vtkPolyData object, then you can create a model using:

slicer.modules.models.logic().AddModel(myPolyData)

Thanks Andrea,
But in fact, I want to connect the output port of one algorithm (my model loaded by ‘slicer.util.loadModel(‘model.stl’)’ to the input port of another algorithm (here called X).

model=slicer.util.loadModel(‘model.stl’)
X=vtk.X()
X.SetInputConnection(model.GetOutputPort())`

How to process to that?

You can get/set polydata connection using GetPolyDataConnection and SetPolyDataConnection. For example:

[success, model]=slicer.util.loadModel("model.stl", returnNode=True)
smoother = vtk.vtkSmoothPolyDataFilter()
smoother.SetNumberOfIterations(300)
smoother.SetInputConnection(model.GetPolyDataConnection())
model.SetPolyDataConnection(smoother.GetOutputPort())
1 Like

Thanks Andrea,

But I always miss the reader.GetOutputPort().
I am sorry but I do not understand how slicer use VTK (always this actor/mapper question), thus in Slicer, I am lost how to access to the:

.GetProperty().SetColor(1,0,0) or SetPointSize(20)

Typically, without actor/mapper, how to do that with 3d slicer:

reader = vtk.vtkSTLReader()
reader.SetFileName(‘model.stl’)

p0=[0]*3
pd = reader.GetOutput()
pd.GetNumberOfPoints()
loc = vtk.vtkPointLocator()
loc.SetDataSet(pd)
loc.BuildLocator()
closestPointId = loc.FindClosestPoint(p0)

Those properties can be set through the display node.

myModelNode.GetDisplayNode().Set...

See http://apidocs.slicer.org/master/classvtkMRMLDisplayNode.html and http://apidocs.slicer.org/master/classvtkMRMLModelDisplayNode.html for details.

1 Like

Perfect,
Thanks one again @lassoan, and sorry for my newby question.
Best.

Ps: I completed my script to calculate distance along the surface in slicer. I will probably value it in implementing my python lines in an extension in a couple of days.

2 Likes