Working on updating Slicer to build against the latest VTK (see [1]),
I noticed that VTK object returned by Qt slots only include vtkObject
methods.
Question: While I am investigating further, does anyone have ideas on what may be wrong ?
To help with this, you will find below some more context in the form of a FAQ.
[1] https://github.com/Slicer/Slicer/pull/5141
FAQ
What is the problem ?
With Slicer built against VTK 9.0.20200825
, custom VTK C++ class instances wrapped using vtkPythonUtil::GetObjectFromPointer
do not include custom methods:
>>> slicer.app.mrmlScene
<qt slot mrmlScene of qSlicerApplication instance at 0x7fc242b98e88>
>>> slicer.app.mrmlScene()
(vtkmodules.vtkCommonCore.vtkObject)0x7fc22be10f48
>>> 'GetNodes' in dir(slicer.app.mrmlScene())
False
For comparison, here is what we have with Slicer built against VTK 8.2:
>>> slicer.app.mrmlScene
<qt slot mrmlScene of qSlicerApplication instance at 0x7f9d952fbf48>
>>> slicer.app.mrmlScene()
(MRMLCorePython.vtkMRMLScene)0x7f9d8213f768
>>> 'GetNodes' in dir(slicer.app.mrmlScene())
True
What make you think the custom classes are properly wrapped ?
Importing the object from python interpreter allow to confirm the object specific methods are available.
-
from python interpreter:
$ ../python-install/bin/PythonSlicer >>> import mrml >>> 'GetNodes' in dir(mrml.vtkMRMLScene) True
-
from embedded python interpreter
-
from application using
executeString
>>> slicer.app.pythonManager().executeString("import mrml; print('GetNodes' in dir(mrml.vtkMRMLScene))") True
Context: Slicer built against VTK 9.0.20200825
Any idea on where the problem could be ?
Since the custom class is properly wrapped when imported from a python interpreter, I suspect the expected base class is not found in ClassMap
Context: Slicer built against VTK 9.0.20200825
How are the custom VTK classes wrapped ?
We wrap our custom VTK classes using vtkMacroKitPythonWrap.cmake as well as modified version of vtkWrapPython.cmake and vtkWrapHierarchy.cmake adapted from prior version of VTK.
Context: Slicer built against VTK 9.0.20200825
How are PythonQt and VTK integrated ?
Few years ago, we worked with the maintainer of PythonQt to add the concept of PythonQtForeignWrapperFactory. To ensure VTK object associated with Qt signals, slots and properties are properly wrapped/unwrapped, we then implemented [ctkVTKPythonQtWrapperFactory](https://github.com/commontk/CTK/blob/support-build-with-vtk89/Libs/Visualization/VTK/Core/ctkVTKPythonQtWrapperFactory.cpp) distributed in
CTK`.
Context: Slicer built against VTK 9.0.20200825