# SetOutput data object for non zero port

**URL:** https://discourse.vtk.org/t/setoutput-data-object-for-non-zero-port/1773
**Category:** Support
**Created:** [September 16, 2019, 9:53pm UTC](https://discourse.vtk.org/t/setoutput-data-object-for-non-zero-port/1773 "2019-09-16T21:53:48Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [September 16, 2019, 9:53pm UTC](https://discourse.vtk.org/t/setoutput-data-object-for-non-zero-port/1773/1 "2019-09-16T21:53:48Z")

</div>

Is there a way to set the output data object for a non zero port? It appears to me that the standard `SetOutput()` method can only set the 0th port on an algorithm.

My use case: the `vtkBoxClipDataSet` filter when `GenerateClippedOutputOn()` and using port 1 to manage the clipped output:

```python
import vtk
mesh = ... # some data set
alg = vtk.vtkBoxClipDataSet()
alg.SetInputDataObject(mesh)
alg.GenerateClippedOutputOn()
box_clipped_mesh = vtk.vtkUnstructuredGrid()
alg.SetOutput(box_clipped_mesh) 
# How do i set port 1 and not port 0???

```

---

<div class="post-metadata">

### Author: ![utkarshayachit](https://discourse.vtk.org/user_avatar/discourse.vtk.org/utkarshayachit/32/379_2.png) [@utkarshayachit](https://discourse.vtk.org/u/utkarshayachit)
#### Post date: [September 16, 2019, 10:04pm UTC](https://discourse.vtk.org/t/setoutput-data-object-for-non-zero-port/1773/2 "2019-09-16T22:04:44Z")

</div>

You’re not supposed to. I suspect `SetOutput` lingers from some backwards compatibility API. `vtkAlgorithm` subclasses will create appropriate output data object as and when needed.

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [September 16, 2019, 10:07pm UTC](https://discourse.vtk.org/t/setoutput-data-object-for-non-zero-port/1773/3 "2019-09-16T22:07:05Z")

</div>

Interesting… so I should avoid using the `SetOutput()` method and always grab the generated output?

---

<div class="post-metadata">

### Author: ![utkarshayachit](https://discourse.vtk.org/user_avatar/discourse.vtk.org/utkarshayachit/32/379_2.png) [@utkarshayachit](https://discourse.vtk.org/u/utkarshayachit)
#### Post date: [September 16, 2019, 10:11pm UTC](https://discourse.vtk.org/t/setoutput-data-object-for-non-zero-port/1773/4 "2019-09-16T22:11:23Z")

</div>

You bet. BTW, remember that repeated execution of the algorithm need not create a new data object instance. It may reuse the one created previously. So if you’re hanging on to the output, you may want to shallow copy it.

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [September 16, 2019, 10:25pm UTC](https://discourse.vtk.org/t/setoutput-data-object-for-non-zero-port/1773/5 "2019-09-16T22:25:55Z")

</div>

> [@utkarshayachit](#):
>
> It may reuse the one created previously. So if you’re hanging on to the output, you may want to shallow copy it.

When you say “may reuse the one created previously”, is there a chance that the algorithm would ever create a new object?

* * *

I’m working on a feature (in PyVista) where I grab the output object of the `vtkBoxClipDataSet` algorithm upfront before it runs so that I can set up an actor/mapper to show the output in a rendering scene and have the output updated on each execution implemented through callbacks with a box widget.

This is a pseudo-pipeline as I have that dataset added to the rendering scene by setting the input data object (instead of connections) for the mapper/actor. Is there any danger to setting InputDataObjects instead of Connections when pipelining like this?

---

<div class="post-metadata">

### Author: ![utkarshayachit](https://discourse.vtk.org/user_avatar/discourse.vtk.org/utkarshayachit/32/379_2.png) [@utkarshayachit](https://discourse.vtk.org/u/utkarshayachit)
#### Post date: [September 16, 2019, 10:48pm UTC](https://discourse.vtk.org/t/setoutput-data-object-for-non-zero-port/1773/6 "2019-09-16T22:48:21Z")

</div>

> [@banesullivan](#):
>
> When you say “may reuse the one created previously”, is there a chance that the algorithm would ever create a new object?

Yes. Certain filters, like **Extract Selection** change their output type based on flags set on the filter as well as the input dataset type. There, if the output type changed due to the user toggling some flag on the filter, for example, it will create a new output data object. Generally algorithms reuse output data object unless the type changed, but this is not a requirement.

> [@banesullivan](#):
>
> Is there any danger to setting InputDataObjects instead of Connections when pipelining like this?

Setting input data object (by calling `vtkAlgorithm::SetInputDataObject` or convenience methods `e.g. `SetInput` provided by subclasses) is totally fine.

> [@banesullivan](#):
>
> I grab the output object of the `vtkBoxClipDataSet` algorithm upfront before it runs so that I can set up an actor/mapper to show the output in a rendering scene and have the output updated on each execution implemented through callbacks with a box widget.

This, however, doesn’t sound kosher to me. Instead, what I’d do is create your intended output object and pass that to the mapper. Now, every time you want to update the output via your callback, execute `vtkBoxClipDataSet` and then shallow copy its output to your intended output object.

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [September 17, 2019, 2:50pm UTC](https://discourse.vtk.org/t/setoutput-data-object-for-non-zero-port/1773/7 "2019-09-17T14:50:59Z")

</div>

Ah, the shallow copy makes sense and something I overlooked. Thanks, @utkarshayachit!
