# wrapping vtk filters outside source

**URL:** https://discourse.vtk.org/t/wrapping-vtk-filters-outside-source/5800
**Category:** Development
**Created:** [May 17, 2021, 4:03pm UTC](https://discourse.vtk.org/t/wrapping-vtk-filters-outside-source/5800 "2021-05-17T16:03:53Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![marcwang](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@marcwang](https://discourse.vtk.org/u/marcwang)
#### Post date: [May 17, 2021, 4:03pm UTC](https://discourse.vtk.org/t/wrapping-vtk-filters-outside-source/5800/1 "2021-05-17T16:03:53Z")

</div>

Hi.  
I implemented my own c++ filters but I would like to wrap them the same way vtk does in python. I saw [this nabble post](http://vtk.1045678.n5.nabble.com/Python-wrapping-outside-VTK-source-tree-almost-there-td5733109.html).

Unfortunately, I think it’s a little out of date. The macro vtk\_wrap\_python3 doesn’t seem to exist anymore. Has it been replaced by something else? Is there an example, I can check out?

---

<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: [May 17, 2021, 6:42pm UTC](https://discourse.vtk.org/t/wrapping-vtk-filters-outside-source/5800/2 "2021-05-17T18:42:37Z")

</div>

Yes, it has changed up quite a bit. The CMake API now requires the rest of the VTK module system to provide the information that the wrappers require. Please see [this file](https://vtk.org/doc/nightly/html/group__module.html) for using the module system. Once you have modules, you can wrap them using [these APIs](https://vtk.org/doc/nightly/html/group__module-wrapping-python.html).

---

<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: [May 17, 2021, 7:38pm UTC](https://discourse.vtk.org/t/wrapping-vtk-filters-outside-source/5800/3 "2021-05-17T19:38:52Z")

</div>

There’s also a [bare-bones example project called “Wrapping”](https://gitlab.kitware.com/vtk/vtk/-/tree/master/Examples/Modules/Wrapping) that comes with the VTK source code. To use this example, copy it somewhere outside of the VTK source tree and then build it with CMake.

---

<div class="post-metadata">

### Author: ![marcwang](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@marcwang](https://discourse.vtk.org/u/marcwang)
#### Post date: [May 17, 2021, 8:43pm UTC](https://discourse.vtk.org/t/wrapping-vtk-filters-outside-source/5800/4 "2021-05-17T20:43:12Z")

</div>

Thank you @dgobbi and @ben.boeckel, I will check those out.

---

<div class="post-metadata">

### Author: ![marcwang](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@marcwang](https://discourse.vtk.org/u/marcwang)
#### Post date: [May 19, 2021, 10:07pm UTC](https://discourse.vtk.org/t/wrapping-vtk-filters-outside-source/5800/5 "2021-05-19T22:07:10Z")

</div>

Hi @ben.boeckel and @dgobbi , I examined the cmake function and managed to compile the example mentioned by David.  
I didn’t find/understand how does vtk wrapping install itself into the python environment or package into a whl file. When I compile the example, in my case it creates a `pyd` file in `{cmake_binary_dir}\site-packages\wrapping`. I notice that path is somewhat hardcoded. Does the cmake function offered in `vtkModuleWrapPython` also take care of the installation? Or do I have to write a `setup.py` as well?

The issue I encountered was that setuptools looks for a pyd in`‘build\lib.{platform}-{python\_version}’. But I couldn’t figure out how to change the path where the wrapping builds to or the directory where setuptools searches for the pyd.

---

<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: [May 19, 2021, 11:50pm UTC](https://discourse.vtk.org/t/wrapping-vtk-filters-outside-source/5800/6 "2021-05-19T23:50:01Z")

</div>

I’ll try to answer your questions as best as I can. The package name for the module can be changed by editing the CMakeLists.txt file here:

```
  PYTHON_PACKAGE "wrapping"

```

During the build the package always goes in `{cmake_binary_dir}/site-packages/`, and the expectation is that the install will copy it to where it belongs. I don’t have answers to your questions about setup.py or whl building, unfortunately.

---

<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: [May 20, 2021, 12:11am UTC](https://discourse.vtk.org/t/wrapping-vtk-filters-outside-source/5800/7 "2021-05-20T00:11:02Z")

</div>

Wait a minute, I wasn’t reading things properly. I would expect that, during the build, the package would go into `{cmake_binary_dir}/lib/python-x.y/site-packages/` but the `lib/python-x.y` part is specific to linux/mac and might be different on Windows.

---

<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: [May 20, 2021, 12:12am UTC](https://discourse.vtk.org/t/wrapping-vtk-filters-outside-source/5800/8 "2021-05-20T00:12:13Z")

</div>

> [@marcwang](#):
>
> Does the cmake function offered in `vtkModuleWrapPython` also take care of the installation? Or do I have to write a `setup.py` as well?

It handles installation, but the “default” is for Unix-style installation layouts. Wheels are not compatible with them, so they need other support. If you want to see what VTK does, you can look at `CMake/vtkWheel*.cmake` files in the VTK source tree. Note that such a build is only really useful for then packaging into a wheel not for use as an SDK or anything like that.

> [@marcwang](#):
>
> I didn’t find/understand how does vtk wrapping install itself into the python environment or package into a whl file.

Packaging up into a wheel is not easy. VTK basically does a completely different build layout to make wheels work (because the state of Python packaging is…not great for projects with any non-Python code involved). None of the wheel auditors or whatever will like it either since you’ll need to ship all of the VTK libraries to make it happy (and `delocate` just doesn’t work with `@loader_path`, so you need to setup rpaths manually). @jcfr has ideas on how VTK-using wheels can be done, but it all sounds very hacky and “if it works, great; if not, go ask for better Python packaging support of non-Python code” to me.

---

<div class="post-metadata">

### Author: ![jcfr](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jcfr/32/466_2.png) [@jcfr](https://discourse.vtk.org/u/jcfr)
#### Post date: [May 20, 2021, 12:47am UTC](https://discourse.vtk.org/t/wrapping-vtk-filters-outside-source/5800/9 "2021-05-20T00:47:17Z")

</div>

> [@ben.boeckel](#):
>
> has ideas on how VTK-using wheels can be done

In nutshell, here is the plan that would enable [https://discourse.vtk.org/t/rfc-toward-supporting-distribution-of-vtk-based-modules-on-pypi/4873:](https://discourse.vtk.org/t/rfc-toward-supporting-distribution-of-vtk-based-modules-on-pypi/4873:)

- Leverage prior work of @dgobbi to support building VTK filters either within VTK or externally (some preliminary work [here](https://github.com/Kitware/LookingGlassVTKModule/pull/9)).
  - At first, we would have a CMake module in a standalone repo that could be integrated in project aiming at being built as VTK remote module or python wheels (the integration would be done like we do for [this module](https://github.com/scikit-build/cmake-FindVcvars))

- Update VTK CI configuration to have VTK SDK published daily (e.g as GitHub release)
  - This would enable to easily setup CI using GitHub action

- Update `LookingGlassVTKModule` project to leverage [scikit-build](https://scikit-build.readthedocs.io)
- Publish a template similar to [ITKPythonPackage](https://github.com/InsightSoftwareConsortium/ITKPythonPackage) but for VTK projects.

@marcwang Let us know if you specific requirements or if you would have resources to support this effort. If needed, my email is `jcfr [at] kitware [dot] com`

cc: @thewtex

---

<div class="post-metadata">

### Author: ![marcwang](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@marcwang](https://discourse.vtk.org/u/marcwang)
#### Post date: [May 20, 2021, 2:41am UTC](https://discourse.vtk.org/t/wrapping-vtk-filters-outside-source/5800/10 "2021-05-20T02:41:25Z")

</div>

Hi @jcfr , thanks for the links. I will check them out and see what I can do on my end. My original goal was to shift some of the vtk code inside a pure python GUI app I wrote to cpp.

I have previously attempted wrapping my own vtk based module using pybind11 without much success ([stackoverflow link](https://stackoverflow.com/questions/54871216/pybind11-return-c-class-with-an-existing-python-binding-to-python)). Although, in that post, another person successfully pulled that off but I wasn’t able to replicate his solution at the time. The solution has been integrated into [SMTK](https://gitlab.kitware.com/cmb/smtk/-/blob/master/smtk/extension/vtk/pybind11/PybindVTKTypeCaster.h). Granted in that case, the goal was casting vtk cpp object to it’s python equivalent. Therefore, all that was needed was a type caster in pybind11.
