# Interact with 3D model

**URL:** <https://discourse.slicer.org/t/interact-with-3d-model/1265>\
**Category:** Support\
**Created:** [October 23, 2017, 3:36pm UTC](https://discourse.slicer.org/t/interact-with-3d-model/1265 "2017-10-23T15:36:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)\
**Post date:** [October 23, 2017, 3:36pm UTC](https://discourse.slicer.org/t/interact-with-3d-model/1265/1 "2017-10-23T15:36:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [October 23, 2017, 5:30pm UTC](https://discourse.slicer.org/t/interact-with-3d-model/1265/2 "2017-10-23T17:30:35Z")

</div>

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)
```

---

<div class="post-metadata">

**Author:** ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)\
**Post date:** [October 24, 2017, 9:59am UTC](https://discourse.slicer.org/t/interact-with-3d-model/1265/3 "2017-10-24T09:59:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [October 24, 2017, 1:00pm UTC](https://discourse.slicer.org/t/interact-with-3d-model/1265/4 "2017-10-24T13:00:10Z")

</div>

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())
```

---

<div class="post-metadata">

**Author:** ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)\
**Post date:** [October 24, 2017, 1:59pm UTC](https://discourse.slicer.org/t/interact-with-3d-model/1265/5 "2017-10-24T13:59:23Z")

</div>

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)

```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [October 24, 2017, 11:25pm UTC](https://discourse.slicer.org/t/interact-with-3d-model/1265/6 "2017-10-24T23:25:01Z")

</div>

Those properties can be set through the display node.

```
myModelNode.GetDisplayNode().Set...

```

See [http://apidocs.slicer.org/master/classvtkMRMLDisplayNode.html](http://apidocs.slicer.org/master/classvtkMRMLDisplayNode.html) and [http://apidocs.slicer.org/master/classvtkMRMLModelDisplayNode.html](http://apidocs.slicer.org/master/classvtkMRMLModelDisplayNode.html) for details.

---

<div class="post-metadata">

**Author:** ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)\
**Post date:** [October 25, 2017, 1:25pm UTC](https://discourse.slicer.org/t/interact-with-3d-model/1265/7 "2017-10-25T13:25:10Z")

</div>

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.
