Skip to content

Add Python implementations of `QVTK` Classes to `vtkmodules`

Adam Hendry requested to merge adam-grant-hendry/vtk:feat/qvtkopengl into master

Using vtkmodules.qt.QVTKRenderWindowInteractor with QVTKRWIBase = 'QOpenGLWidget' gives the error

QPainter::begin: Paint device returned engine == 0, type: #

where # is the corresponding QPaintEngine Type. This occurs because the paintEngine() is overriden to return None and WA_PaintOnScreen is set to support rendering outside Qt's paint system, but WA_PaintOnScreen only is only supported on X11 systems.

Both VTK and Qt (since Qt version 4; see also this) support double-buffering by default. Thus, VTK maintains its own OpenGL context (front and back buffer) as does Qt. WA_PaintOnScreen was set so we may draw straight to Qt's front buffer (disabling Qt's back buffer) and avoid display flickering.

The VTK C++ source contains QVTK classes to integrate with QOpenGLWidget and overcome these issues, but these have not been wrapped in Python.

Due to licensing restrictions and existing differences between the different existing python Qt bindings and versions (Qt objects would have to be wrapped with sip (for PyQtX) or shiboken (for PySideX), which would require creating separate python vtk packages for each Qt python binding), a pure python implementation is preferred.

Developer Notes

  • For ease of review (and to maintain the same interface), the names and casing of variables, functions, and classes has been preserved (even though they are not all pythonic)
  • The Python Wrapper notes and VTK Code Style Guide were followed (to the best of my ability) when translating the C++ source into Python. I attempted to make the translation as close to 1:1 as possible, again for ease of reviewing.

Topics for Discussion

  • Adding QtPy, packaging, and PyOpenGL dependencies?
  • Use of @property decorators for setters/getters, or keep set/get methods for a consistent interface?
  • Use of more pythonic variable naming, or keep same convention for same interface?
  • Modify QVTKRenderWindowInteractor to not use WA_PaintOnScreen so double-buffering is enabled?

Additional Tasks

  • Add unittest tests in the Wrapping/Python/Testing subfolder
  • Raise error if user does not have qtpy or pyopengl installed

Thoughts

  • Emulate QScopedPointers and QScopedValueRollbacks with context managers (i.e. __enter__, __exit__)

Useful Links

Related Issues:

Mayavi:

PyVistaQt:

Note:

The example is run against the latest PyPI version of vtkmodules, which is 9.1.0. If additional changes included in the 9.2.0 release candidates or nightly builds are needed for this to work, please let me know.

I am attempting to get an equivalent version of the QVTKRenderWidgetConeExample to work, but setAlphaBufferSize(8) in defaultFormat causes the entire window to be transparent. Note that this happens whether CoreProfile or CompatibilityProfile is set. This is what happened in ParaView #17159, but method clearAlpha() from MR 2502 does not work in PySide6 since several QOpenGLFunctions are either missing or broken. See:

A workaround will be required, either using PyOpenGL or ctypes to add the missing functions to QOpenGLFunctions. This problem may or may not be due to the listed QOpenGLWidget Limitations. If I set WA_AlwaysStackOnTop however, I get a pure white background. Please provide whatever help you can for this. The cone code example is in QVTKOpenGLNativeWidget.py, so that file can be run directly to run the example.

image

Potentially Related:

Ancillary Issues (Still Open):

Requested Reviewers:

VTK:

Cc: David Gobbi ( @dgobbi ), Mathieu Westphal ( @mwestphal ), Sankhesh Jhaveri ( @sankhesh ), Ken Martin ( @ken-martin ), Will Schroeder ( @will.schroeder ), Lucas Givord ( @lucas.givord )

PyVista:

Cc: Eric Larson ( @larsoner ), Alex Kaszynski ( @akaszynski ), Bane Sullivan ( @banesullivan ), Guillaume Favelier ( @GuillaumeFavelier )

Mayavi:

Cc: Prabhu Ramachandran ( @prabhuramachandran ), Marijn van Vliet ( @wmvanvliet )

Fixes #18606 .

Edited by Adam Hendry

Merge request reports