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

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 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

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

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

Try this:

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 Development.Module and Development.Embed

However, reading CMake docs I read

Changed in version 3.14: Imported Targets are only created when 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

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

Maybe it is due to how VTK modules are found in my example? That is, am I missing the Python component? And what are the available components?

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

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

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

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).