# How to write a custom function that uses VTK C++ and then use it in Python?

**URL:** https://discourse.vtk.org/t/how-to-write-a-custom-function-that-uses-vtk-c-and-then-use-it-in-python/9030
**Category:** Support
**Created:** [July 27, 2022, 7:49pm UTC](https://discourse.vtk.org/t/how-to-write-a-custom-function-that-uses-vtk-c-and-then-use-it-in-python/9030 "2022-07-27T19:49:26Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![joaomlap](https://discourse.vtk.org/user_avatar/discourse.vtk.org/joaomlap/32/3201_2.png) [@joaomlap](https://discourse.vtk.org/u/joaomlap)
#### Post date: [July 27, 2022, 7:49pm UTC](https://discourse.vtk.org/t/how-to-write-a-custom-function-that-uses-vtk-c-and-then-use-it-in-python/9030/1 "2022-07-27T19:49:26Z")

</div>

What is the process to write a custom function using VTK in C++ (it’s a time critical function) and then use it’s bindings in Python?  
I think I saw some information about this in the past but can’t seem to find it again.  
Is the best solution to clone VTK, write the function and then compile the entire library or is there a simpler method that allows to write smaller and contained modules?  
Thank you very much in advance.

---

<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: [July 27, 2022, 9:55pm UTC](https://discourse.vtk.org/t/how-to-write-a-custom-function-that-uses-vtk-c-and-then-use-it-in-python/9030/2 "2022-07-27T21:55:27Z")

</div>

VTK has [an example for this](https://gitlab.kitware.com/vtk/vtk/-/tree/master/Examples/Modules/Wrapping). Note that wrapping only works on “VTK Modules” , not arbitrary code, because of the associated data.

---

<div class="post-metadata">

### Author: ![joaomlap](https://discourse.vtk.org/user_avatar/discourse.vtk.org/joaomlap/32/3201_2.png) [@joaomlap](https://discourse.vtk.org/u/joaomlap)
#### Post date: [August 5, 2022, 1:53pm UTC](https://discourse.vtk.org/t/how-to-write-a-custom-function-that-uses-vtk-c-and-then-use-it-in-python/9030/3 "2022-08-05T13:53:28Z")

</div>

Thank you very much Ben, from a Typescript and Python dev, I am not sure how to use CMake and build my module. Is there a tutorial or anything that I can read or follow?

I am running CMake but it is failing on VTK\_DIR:  
VTK\_DIR-NOTFOUND

I am pointing to the VTK repo folder but I am not sure if I need to build VTK first or what are the steps from here to a module that I can use in Python.

I am sorry for the noob questions on this topic but even the slightest help would be very welcomed.

---

<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: [August 5, 2022, 3:23pm UTC](https://discourse.vtk.org/t/how-to-write-a-custom-function-that-uses-vtk-c-and-then-use-it-in-python/9030/4 "2022-08-05T15:23:50Z")

</div>

Yes, you’ll need to build VTK first. You can then point `VTK_DIR` to the build directory. VTK-specific build information is [here](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Documentation/dev/build.md). You’ll want to at least turn on `VTK_WRAP_PYTHON`. It’s not clear what C++ modules you want, but you can turn them on as needed (`VTK_MODULE_ENABLE_*` flags).

---

<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: [August 5, 2022, 3:24pm UTC](https://discourse.vtk.org/t/how-to-write-a-custom-function-that-uses-vtk-c-and-then-use-it-in-python/9030/5 "2022-08-05T15:24:59Z")

</div>

You should also be able to build the “Modules/Wrapping” example if you download a recent vtk-python wheel and its corresponding SDK from the [VTK download site](https://vtk.org/download/).

Steps are as follows. I did this with the macOS SDK, so YMMV if you use a different SDK.

1. Copy “Examples/Module/Wrapping/” from the VTK source tree into your own directory, e.g. call it “WrappingExample”.

2. Make a new, empty directory called “WrappingExampleBuild” which you will use for doing the compilation. (It’s a recommended practice with CMake to keep the source directory and the build directory separate, such that the source directory contains _only_ source).

3. Find the “cmake” directory in the SDK, e.g. in the macOS SDK it is in `vtk-9.2.0.data/headers/cmake`. Set the `VTK_DIR` to this directory when you run cmake.

4. Do a build (e.g. on linux/macOS, run “make” in the build directory).

5. Check to make sure that the python module “vtkWrappable.so/.pyd” was created. For example, in my build it was in `lib/python3.7/site-packages/wrapping` in the build tree.

6. Set your PYTHONPATH so that python can find the module. E.g. I used PYTHONPATH=/path/to/WrappingExampleBuild/lib/python3.7/site-packages.

At this point, it should be possible to run Python and do “import wrapping.vtkWrappable”. But as I said, YMMV. I’m sure my list of steps is incomplete, the SDKs might not be mature yet (they’re quite new), and on different operating systems, different paths might have to be set.

---

<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: [August 8, 2022, 10:04am UTC](https://discourse.vtk.org/t/how-to-write-a-custom-function-that-uses-vtk-c-and-then-use-it-in-python/9030/6 "2022-08-08T10:04:31Z")

</div>

Just to complement on what David said, I did a similar thing on Windows, but installing VTK from conda. It all works pretty much out of the box, except point 6 did not work for me on Windows.

I couldn’t find a way to tell python where to look for the new modules, outside the system paths, and the only way to have python see the built objects was to install into the conda environment directly.

[Here](https://github.com/Kitware/VTK/tree/master/Wrapping/Python#vtkpython) it says one shoud use [`os.add_dll_directory()`](https://docs.python.org/3/library/os.html#os.add_dll_directory) but it didn’t work for me.

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: [August 8, 2022, 1:31pm UTC](https://discourse.vtk.org/t/how-to-write-a-custom-function-that-uses-vtk-c-and-then-use-it-in-python/9030/7 "2022-08-08T13:31:53Z")

</div>

To add some clarification, `os.add_dll_directory()` does not tell Python where to look for modules. Rather, it tells Python where to look for the DLLs that are used by the modules.

Also note that the python devs didn’t make it easy to use: `os.add_dll_directory()` returns a cookie, and that cookie _must not_ destruct before the import is performed.

The Python docs recommend a “with” statement:

```python
with os.add_dll_directory("/path/to/dlls") as cookie:
    import mymodule # this module needs the DLLs

```

One way to think about it is this: as long as `cookie` exists, a portal is open that allows the process to load the DLLs. As soon as `cookie` goes out of scope (or if you explicitly call `cookie.close()`), the portal is closed and the process can no longer load the DLLs.
