Python wrapping (functions are missing in code generated)

Hi supporters

Just started a larger project based on the VTK pipeline and in relation to this I am creating a number of modules, which could potentially be future remote modules of VTK. I looked into how vtk-dicom was made - but ignored any stuff related to VTK prior to 9.0.

I use CMake and the new vtk_module_build and vtk_module_wrap macros. Under Linux, it works flawlessly and after adding VTK::CommonExecutionModel to the DEPENDS in my module file, the functions GetOutput and GetOutputPort was added to the wrapper of module deriving from vtkPolyDataAlgorithm. For the Windows build, the functions never appear in the Python wrappers.

Module file

NAME
  VTK::PCR
LIBRARY_NAME
  vtkPCR
GROUPS
  StandAlone
DEPENDS
  VTK::CommonCore
  VTK::CommonMath
  VTK::CommonTransforms
  VTK::CommonDataModel
  VTK::CommonExecutionModel
PRIVATE_DEPENDS
  VTK::CommonMisc
  VTK::CommonSystem
  VTK::FiltersCore
  VTK::FiltersSources
  VTK::vtksys
TEST_DEPENDS
  VTK::TestingCore

CMakeLists.txt in top folder (the relevant part)

vtk_module_scan(
  MODULE_FILES        "${CMAKE_CURRENT_SOURCE_DIR}/Source/pcr.module" # What is scanned
  REQUEST_MODULES     "VTK::PCR"                                      # What is searched for
  PROVIDES_MODULES    pcr_modules
  ENABLE_TESTS        "${BUILD_TESTING}")

vtk_module_build(
  MODULES             ${pcr_modules}
  INSTALL_EXPORT      PCR
  ARCHIVE_DESTINATION "lib"
  HEADERS_DESTINATION "include/pcr${PCR_SHORT_VERSION}"
  CMAKE_DESTINATION   "${CMAKE_INSTALL_LIBDIR}/cmake/vtk"
  LICENSE_DESTINATION "${CMAKE_INSTALL_LIBDIR}/vtk"
  HIERARCHY_DESTINATION "${CMAKE_INSTALL_LIBDIR}/vtk/hierarchy/${CMAKE_PROJECT_NAME}"
  LIBRARY_NAME_SUFFIX "${PCR_CUSTOM_LIBRARY_SUFFIX}"
  VERSION             "${PCR_VERSION}"
  SOVERSION           "1")

vtk_module_wrap_python(
  MODULES         ${pcr_modules}
  INSTALL_EXPORT  PCRPython
  PYTHON_PACKAGE  "vtk.modules"
  CMAKE_DESTINATION   "${CMAKE_INSTALL_LIBDIR}/cmake/vtk"
  LIBRARY_DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  BUILD_STATIC    OFF) # Python module is always dynamic built

CMakeLists.txt in the source folder

# When building as a module for VTK 8.90 or later
vtk_module_add_module(VTK::PCR
  CLASSES ${classes})
vtk_module_link(VTK::PCR
  PRIVATE
  VTK::CommonDataModel)

Thanks in advance
Jens Munk

There’s no need to link manually; just make sure it is in PRIVATE_DEPENDS.

Your project is not VTK, so a different directory would be wise here. Maybe vtkpcr, but basically whatever you expect find_package(vtkPCR) or whatever to use.

I’d rather not continue adding remote modules to VTK. Code is either in VTK or is not. Personally, I think just using VTK through find_package(VTK) should be sufficient for many VTK-using libraries.

As for the Python issue, does moving the manual link into the vtk.module help? If not, does the generated Python source code look sensible at least?

The manual link, I could safely remove. Moving the link into the .module file did not help.

I totally agree about remote modules. I have already decided to skip the possibility for using my library as a remote module.

The generated .cxx files for the class inheriting from vtkPolyDataAlgorithm is 33502 bytes (in Linux) where everything fine and 34679 bytes (in Windows), where the functions are missing in the wrapper. I think I will look into the contents of the generated files to figure out what is going on

Thanks for reaching out
Jens

I found a solution. It appeared to be a DLL loading issue. Damn you Python 3.8+ on Windows. From Python 3.8 and onwards, you need to issue an os.add_dll_directory(PATH) in addition to adding the path to the system path, `sys.path.insert’. Also, I must have been loading some VTK python DLLs from what has been installed using pip rather than what I have compiled myself.

The recipe to make things work was the following:

  1. Make sure the right DLLs are found

    In [1]: import sys
    In [2]: sys.path.insert(0, “c:/VTK90/build_Release/bin/Lib/site-packages/”)
    In [3]: sys.path.insert(0, “c:/VTK90/build_Release/bin/Lib/site-packages/vtkmodules/”)

  2. Make sure that the are loaded and no errors occur (some were silent)

    In [4]: import os
    In [5]: os.add_dll_directory(“C:/VTK90/build_Release/bin/Lib/site-packages/vtkmodules/”)
    In [6]: os.add_dll_directory(“C:/VTK90/build_Release/bin/Release”)

  3. Import my library

    In [7]: import vtkPCR as lib

Ah, yes. That…change. See how VTK handles this and the associated CMake code for this stuff. It’s a mess.