# How to wrap a custom made C++ VTK filter in Python?

**URL:** https://discourse.vtk.org/t/how-to-wrap-a-custom-made-c-vtk-filter-in-python/10070
**Category:** Support
**Created:** [December 5, 2022, 11:26am UTC](https://discourse.vtk.org/t/how-to-wrap-a-custom-made-c-vtk-filter-in-python/10070 "2022-12-05T11:26:54Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Edoardo\_Pasca](https://discourse.vtk.org/user_avatar/discourse.vtk.org/edoardo_pasca/32/742_2.png) [@Edoardo\_Pasca](https://discourse.vtk.org/u/Edoardo_Pasca)
#### Post date: [December 5, 2022, 11:26am UTC](https://discourse.vtk.org/t/how-to-wrap-a-custom-made-c-vtk-filter-in-python/10070/1 "2022-12-05T11:26:54Z")

</div>

Hi all,

I created a VTK algorithm in Python using VTKPythonAlgorithmBase, which served me well. However, it doesn’t scale as it requires a few python for loops, which are infamously slow!

I want to translate the code to C++, but I’d still like to be able to call the functionality from Python. I had a look through discourse and SO but didn’t get much far.

I took inspiration (copied) from [https://gitlab.kitware.com/vtk/vtk/-/tree/master/Examples/Modules/Wrapping](https://gitlab.kitware.com/vtk/vtk/-/tree/master/Examples/Modules/Wrapping) and created a simple CMake/C++ project to prepare for my development.

I installed VTK, CMake and Python within a conda environment, and this would be my preferred way to build/distribute.

My code can be found at [https://github.com/paskino/vtk-wrap-python/tree/main/src/Wrapping](https://github.com/paskino/vtk-wrap-python/tree/main/src/Wrapping)

I am running

`cmake <src_dir> -DCMAKE_INSTALL_PREFIX=%CONDA_PREFIX%` on windows and  
`cmake <src_dir> -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX` on linux.

CMake fails with this error

```auto
CMake Error at /home/edo/scratch/miniconda3/envs/vtkdev/lib/cmake/vtk-9.2/VTK-targets.cmake:360 (set_target_properties):
  The link interface of target "VTK::Python" contains:

    Python3::Module

  but the target was not found. Possible reasons include:

    * There is a typo in the target name.
    * A find_package call is missing for an IMPORTED target.
    * An ALIAS target is missing.

Call Stack (most recent call first):
  /home/edo/scratch/miniconda3/envs/vtkdev/lib/cmake/vtk-9.2/vtk-config.cmake:138 (include)
  CMakeLists.txt:8 (find_package)

```

I added a `find_package(Python COMPONENTS Interpreter Development)` but it seems irrelevant.

What am I missing?

Thanks for your help.

Edoardo

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [December 5, 2022, 1:58pm UTC](https://discourse.vtk.org/t/how-to-wrap-a-custom-made-c-vtk-filter-in-python/10070/2 "2022-12-05T13:58:23Z")

</div>

Try this:

```nohighlight
find_package(Python3 COMPONENTS Interpreter Development.Module)

```

---

<div class="post-metadata">

### Author: ![Edoardo\_Pasca](https://discourse.vtk.org/user_avatar/discourse.vtk.org/edoardo_pasca/32/742_2.png) [@Edoardo\_Pasca](https://discourse.vtk.org/u/Edoardo_Pasca)
#### Post date: [December 5, 2022, 2:34pm UTC](https://discourse.vtk.org/t/how-to-wrap-a-custom-made-c-vtk-filter-in-python/10070/3 "2022-12-05T14:34:16Z")

</div>

> [@dgobbi](#):
>
> `find_package(Python3 COMPONENTS Interpreter Development.Module)`

Thanks. I tried a few combination of `FindPython` earlier and it didn’t help.

For the record `find_package(Python3 COMPONENTS Interpreter Development)` [implies](https://cmake.org/cmake/help/v3.25/module/FindPython.html) `Development.Module` and `Development.Embed`

However, reading CMake docs I read

> Changed in version 3.14: [Imported Targets](https://cmake.org/cmake/help/v3.25/manual/cmake-buildsystem.7.html#imported-targets) are only created when [`CMAKE_ROLE`](https://cmake.org/cmake/help/v3.25/prop_gbl/CMAKE_ROLE.html#prop_gbl:CMAKE_ROLE) is `PROJECT` .

And if I try to print `CMAKE_ROLE` this does not seem to be set, or it is empty. May this be the problem?

Thanks

Edo

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [December 5, 2022, 3:02pm UTC](https://discourse.vtk.org/t/how-to-wrap-a-custom-made-c-vtk-filter-in-python/10070/4 "2022-12-05T15:02:07Z")

</div>

This is probably more of a question for @ben.boeckel

---

<div class="post-metadata">

### Author: ![Edoardo\_Pasca](https://discourse.vtk.org/user_avatar/discourse.vtk.org/edoardo_pasca/32/742_2.png) [@Edoardo\_Pasca](https://discourse.vtk.org/u/Edoardo_Pasca)
#### Post date: [December 6, 2022, 7:01pm UTC](https://discourse.vtk.org/t/how-to-wrap-a-custom-made-c-vtk-filter-in-python/10070/5 "2022-12-06T19:01:46Z")

</div>

Maybe it is due to how VTK modules are found in my [example](https://github.com/paskino/vtk-wrap-python/blob/d7c3e0f05c1c9859ef49ce885517deb898ae34de/CMakeLists.txt#L8-L18)? That is, am I missing the Python component? And what are the available components?

```auto
find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

```

---

<div class="post-metadata">

### Author: ![Edoardo\_Pasca](https://discourse.vtk.org/user_avatar/discourse.vtk.org/edoardo_pasca/32/742_2.png) [@Edoardo\_Pasca](https://discourse.vtk.org/u/Edoardo_Pasca)
#### Post date: [December 6, 2022, 7:04pm UTC](https://discourse.vtk.org/t/how-to-wrap-a-custom-made-c-vtk-filter-in-python/10070/6 "2022-12-06T19:04:06Z")

</div>

OK, I just found that I needed to add `Python` to the `COMPONENTS`

```auto
find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
  Python
)

```

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [December 7, 2022, 1:54pm UTC](https://discourse.vtk.org/t/how-to-wrap-a-custom-made-c-vtk-filter-in-python/10070/7 "2022-12-07T13:54:54Z")

</div>

Yes, VTK only makes components that are requested “real” in any usable sense. VTK cannot stop the creation of all targets because of the way that CMake exports work (we’d need to toposort the requests, figure out dependencies, and include them in that order).
