python bindings of vtk based tools

In reading through this discussion on Remote modules vs external modules

I want to create python bindings for a c++ project that heavily depends on VTK, and possibly make a paraview plugin with it. What would be the best way to approach this given the current state of development and roadmap.

I think it would be best to make a separate library, i.e.
import vtkdocoolsthings
but It would seem a waste not to be able to use VTK tooling to generate python wrappers?

This is a very important topic, as nowadays Python wrapping is a must (if a library is not Python-wrapped then it essentially does not exist) and all the developer groups working with VTK are facing this issue if they want to upgrade to VTK9.

There is one working example, @dgobbi’s https://github.com/dgobbi/vtk-dicom/ library, so one option is to copy-paste CMake scripts from there.

We are looking into generalizing this mechanism by creating a set of CMake functions that can Python-wrap 3D Slicer and its 150+ extensions with minimal changes compared to VTK-8.2. These macros will be first maintained in a separate repository (maybe later can be maintained as part of VTK) and the plan is to make it usable to wrap any VTK-based projects. @jcfr may be able to give more details on the current status.

There is an example in the repository now: https://gitlab.kitware.com/vtk/vtk/-/tree/master/Examples%2FModules%2FWrapping

We can expand on this more if needed, but it does work (modulo some PATH settings we need on Windows, but that’s a dashboard issue, not a code issue as such).

1 Like

I gotta say that my cmake scripts for vtk-dicom are not a great example for how to do this. They’re terribly convoluted, since they support half a dozen different configurations. For that very same reason, though, they expose things at a very low level.

For the curious, the important bits for externally wrapping vtk-dicom for VTK 9 are here, and the module file itself is here.

The vtk docs describe the cmake interface for python wrapping:
https://vtk.org/doc/nightly/html/group__module-wrapping-python.html

1 Like

Hi @ben.boeckel . I can’t figure out how to build the example from
https://gitlab.kitware.com/vtk/vtk/-/tree/master/Examples%2FModules%2FWrapping
Can you help me?

I tried the following steps:

1 - build vtk

cmake -DBUILD_SHARED_LIBS=ON -DBUILD_TESTING=ON \
  -DCMAKE_BUILD_TYPE=Release -DVTK_WRAP_PYTHON=ON -DVTK_PYTHON_VERSION=3 \
  ../vtk .

2 - Inside Examples/Modules/Wrapping

mkdir build 
cd build
cmake  -DVTK_DIR=../../../../../vtkBuild/ . 
$ cd module
$ make install                                                                                      
[ 33%] Generating the wrap hierarchy for Wrapping::Wrappable
/bin/sh: 1: /home/devmessias/projects/fury/vtkBuild/bin/vtkWrapHierarchy-9.0: not found
make[2]: *** [module/CMakeFiles/vtkWrappable-hierarchy.dir/build.make:66: lib/vtk/hierarchy/Wrapping/vtkWrappable-hierarchy.txt] Error 127
make[1]: *** [CMakeFiles/Makefile2:1027: module/CMakeFiles/vtkWrappable-hierarchy.dir/all] Error 2
make: *** [Makefile:141: all] Error 2

Thanks

The error is that one of the executables that VTK builds for wrapping is missing:

The most likely explanation is that the VTK build did not finish. Are you sure that the build of VTK was successful?

Hi! Yes. Everything worked fine.

Is there an executable called vtkWrapHierarchy-9.0 in vtkBuild/bin?

What happens if you do make WrapHierarchy in your vtkBuild directory?

Is there an executable called vtkWrapHierarchy-9.0 in vtkBuild/bin
no
What happens if you do make WrapHierarchy in your vtkBuild directory?
Ok, this created a new file

$ls bin
bin/vtkWrapHierarchy-9.0*

However, now I got a new error

make[2]: *** No rule to make target '/home/devmessias/projects/fury//vtkBuild/lib/libvtkCommonCore-9.0.so.9.0.1', needed by 'lib/libvtkWrappable.so'.  Stop

It looks like you didn’t build VTK. Just run “make” (or “make -j 4”) in your vtkBuild directory.

Note that we are working on providing examples (and later templates) that shows how to build VTK9-based packages that can be used by C++ applications that embed Python (3D Slicer, Paraview, etc.) and also distributed on PyPI. See more information about this effort here.

1 Like

Ah! OK! Thanks! It looks like it will work now.
I have a doubt. I want to install this wrapper inside an anaconda environment.
However, when I do make install inside Examples/Modules/Wrapping

-- Install configuration: ""
-- Installing: /usr/local/lib/python3.8/site-packages/wrapping/vtkWrappable.so
CMake Error at cmake_install.cmake:47 (file):
  file INSTALL cannot copy file
  "/home/.../Examples/Modules/Wrapping/lib/python3.8/site-packages/wrapping/vtkWrappable.so"
  to "/usr/local/lib/python3.8/site-packages/wrapping/vtkWrappable.so".

However, the python paths are set correctly
image

CMAKE_INSTALL_PREFIX=/home/devmessias/anaconda3 ok, this worked!
However, running the example gives a exception

$ python3 ../module/Testing/import_wrappable.py                                                           (base) 
Traceback (most recent call last):
  File "../module/Testing/import_wrappable.py", line 1, in <module>
    from wrapping import vtkWrappable
ImportError: libvtkWrappingPythonCore-9.0.so.1: cannot open shared object file: No such file or directory

Some general instructions:

  1. Find the directory for libvtkWrappingPythonCore-9.0.so.1.
  2. Find the directory that your vtkWrappable.so is in.
  3. Use a symbolic link to put all libs from (1) into (2):
ln -fs /path1/lib*.so.1 /path2/

If that doesn’t work, you can try adding the directory for (1) to your LD_LIBRARY_PATH.

And there are other solutions, like using chrpath to tell vtkWrappable.so where to look for libraries.

1 Like

Thanks for saving my Saturday @dgobbi. Everything working now. I’m trying to create a custom renderpass in python with thisc++ wrapper