Widgets selection priority bug

Hello,

When two (or more) widgets are created and one is visually behind the other, the widget selected is the first widget created.
So if the widget behind is created first, it will be selected even if visually we select/click on the widget visually in front.
I guess it is due to event/observers priorities.

Here is a code to illustrate this behaviour:

# First access the VTK module (and any other needed modules) by importing them.
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera
from vtkmodules.vtkInteractionWidgets import vtkSphereWidget
from vtkmodules.vtkRenderingCore import (
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer
)


def main(argv):
    colors = vtkNamedColors()

    ren1 = vtkRenderer()
    ren1.SetBackground(colors.GetColor3d('MidnightBlue'))

    renWin = vtkRenderWindow()
    renWin.AddRenderer(ren1)
    renWin.SetSize(300, 300)
    renWin.SetWindowName('Widgets bug')

    iren = vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)

    style = vtkInteractorStyleTrackballCamera()
    iren.SetInteractorStyle(style)

    sphereWidgetBehind = vtkSphereWidget()
    sphereWidgetBehind.SetInteractor(iren)
    sphereWidgetBehind.SetCenter(0., 0., -5)
    sphereWidgetBehind.SetRadius(5.)
    sphereWidgetBehind.GetSphereProperty().SetColor(colors.GetColor3d('Green'))
    sphereWidgetBehind.GetSelectedSphereProperty().SetColor(colors.GetColor3d('Green'))
    sphereWidgetBehind.SetRepresentationToSurface()
    sphereWidgetBehind.On()

    sphereWidgetFront = vtkSphereWidget()
    sphereWidgetFront.SetInteractor(iren)
    sphereWidgetFront.SetCenter(0., 0., 0)
    sphereWidgetFront.SetRadius(5.)
    sphereWidgetFront.GetSphereProperty().SetColor(colors.GetColor3d('Red'))
    sphereWidgetFront.GetSelectedSphereProperty().SetColor(colors.GetColor3d('Red'))
    sphereWidgetFront.SetRepresentationToSurface()
    sphereWidgetFront.On()

    iren.Initialize()
    iren.Start()


if __name__ == '__main__':
    import sys

    main(sys.argv)

On the example, if you right click on the red sphere et move the mouse, the green sphere behind is actually selected and translated.

How can I change this behaviour so that the widget selected is always the widget visually in front ?

Config info:

  • vtk 9.2.6
  • python 3.10.9

Thanks