# Can I import PolyData of vtk with python

**URL:** <https://discourse.slicer.org/t/can-i-import-polydata-of-vtk-with-python/4114>\
**Category:** Development\
**Created:** [September 14, 2018, 12:11am UTC](https://discourse.slicer.org/t/can-i-import-polydata-of-vtk-with-python/4114 "2018-09-14T00:11:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![timeanddoctor](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/timeanddoctor/32/5839_2.png) [@timeanddoctor](https://discourse.slicer.org/u/timeanddoctor)\
**Post date:** [September 14, 2018, 12:11am UTC](https://discourse.slicer.org/t/can-i-import-polydata-of-vtk-with-python/4114/1 "2018-09-14T00:11:17Z")

</div>

Can I import this [PolyDataFilter](https://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/BooleanOperationPolyDataFilter) of vtk with python.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [September 14, 2018, 12:24am UTC](https://discourse.slicer.org/t/can-i-import-polydata-of-vtk-with-python/4114/2 "2018-09-14T00:24:16Z")

</div>

Yes, that is available via python:

```auto
>>> vtk.vtkBooleanOperationPolyDataFilter()
(vtkFiltersGeneralPython.vtkBooleanOperationPolyDataFilter)0x7fae97fa98d8

```

---

<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:** [September 14, 2018, 12:52am UTC](https://discourse.slicer.org/t/can-i-import-polydata-of-vtk-with-python/4114/3 "2018-09-14T00:52:12Z")

</div>

Boolean operation on meshes are extremely difficult to implement correctly. Most implementation are flawed, including expensive commercial software and various open-source libraries. Unfortunately, VTK’s `vtkBooleanOperationPolyDataFilter()` is very unreliable, too. It randomly gives incorrect results (typically large artifacts appearing at various places) even for well-behaved, completely valid inputs.

If you need reliable Boolean mesh operations then you can convert meshes to labelmap images, perform Boolean operation in the image domain, then convert the result to mesh. This is of course a somewhat lossy operation, but that’s the only reliable solution that is readily available. You can achieve arbitrarily high fidelity, at the cost of increased memory usage and computation time. This approach is used in Segment Editor / Logical operations effect.

---

<div class="post-metadata">

**Author:** ![timeanddoctor](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/timeanddoctor/32/5839_2.png) [@timeanddoctor](https://discourse.slicer.org/u/timeanddoctor)\
**Post date:** [September 14, 2018, 5:06pm UTC](https://discourse.slicer.org/t/can-i-import-polydata-of-vtk-with-python/4114/4 "2018-09-14T17:06:30Z")

</div>

> [@pieper](#):
>
> vtk.vtkBooleanOperationPolyDataFilter()

Thanks.  
Dear Pieper,  
I donnot know how to set the parameters? The follow code I tried seems wrong… and could you help me correct it, thanks.

```
    input1 = model1.GetOutput()
    sphere1Tri = vtk.vtkTriangleFilter()
    sphere1Tri.SetInputData(input1)

    #model2
    input2 = model2.GetOutput()
    sphere2Tri = vtk.vtkTriangleFilter()
    sphere2Tri.SetInputData(input2)
    #booleanOperation

    booleanOperation = vtk.vtkBooleanOperationPolyDataFilter()
    # booleanOperation.SetOperationToUnion()
    booleanOperation.SetOperationToIntersection()
    # booleanOperation.SetOperationToDifference()

    booleanOperation.SetInputConnection(0, model2)
    booleanOperation.SetInputConnection(1, model1)
    booleanOperation.Update()

```

---

<div class="post-metadata">

**Author:** ![timeanddoctor](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/timeanddoctor/32/5839_2.png) [@timeanddoctor](https://discourse.slicer.org/u/timeanddoctor)\
**Post date:** [September 14, 2018, 5:08pm UTC](https://discourse.slicer.org/t/can-i-import-polydata-of-vtk-with-python/4114/5 "2018-09-14T17:08:01Z")

</div>

Dear Lassoan  
Thank you very much.  
I just try to run it between two cylinder.

---

<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:** [September 14, 2018, 5:30pm UTC](https://discourse.slicer.org/t/can-i-import-polydata-of-vtk-with-python/4114/6 "2018-09-14T17:30:00Z")

</div>

> [@timeanddoctor](#):
>
> I just try to run it between two cylinder.

It can randomly fail, even for two simply cylinders, depending on their size and position.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [September 14, 2018, 6:09pm UTC](https://discourse.slicer.org/t/can-i-import-polydata-of-vtk-with-python/4114/7 "2018-09-14T18:09:39Z")

</div>

I agree with Andras - booleans on polydata are fragile due to numerical rounding issues.

In any case if you want to try it you need something like:

```auto
booleanOperation.SetInputConnection(0, sphere2Tri.GetOutputPort())

```

because it needs the pipeline, not just the data as input.

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [August 15, 2019, 10:08am UTC](https://discourse.slicer.org/t/can-i-import-polydata-of-vtk-with-python/4114/8 "2019-08-15T10:08:41Z")

</div>

Hi Andras,  
Why the vtkpolydata has no faces and no face\_normals.

---

<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:** [August 15, 2019, 3:16pm UTC](https://discourse.slicer.org/t/can-i-import-polydata-of-vtk-with-python/4114/9 "2019-08-15T15:16:56Z")

</div>

I’m not sure what you mean or how it is related to the discussion above. If your question is still relevant then please post it in a new topic, describing in detail what you do, what you expect to happen, and what happens instead.
