Proposal: Update `vtkPythonCommand::Execute` to support custom event

The current implementation vtkPythonCommand::Execute does not support custom event. See Test Case below.

Problem

The issue is that vtkPythonCommand::GetStringFromEventId[1] (used in vtkPythonCommand::Execute[2]) returns the string NoEvent for custom events.

Proposal

Introduce a new decorator allowing to modify the behavior of vtkPythonCommand::Execute so that it always return the event as an integer.

Test case

For example:

vtk_obj = vtk.vtkObject()
customEvent = vtk.vtkCommand.UserEvent + 42

def callback(caller, event):
    print(f" current callback event [{event}]")
    print(f"expected callback event [{customEvent}]")
    assert str(event) == str(customEvent), f"callback event [{event}] is different from [{customEvent}]"

tag = vtk_obj.AddObserver(customEvent, callback)
vtk_obj.InvokeEvent(customEvent)

reports the following

 current callback event [NoEvent]
expected callback event [1042]
Traceback (most recent call last):
  File "<string>", line 10, in callback
AssertionError: callback event [NoEvent] is different from [1042]

cc: @dgobbi @banesullivan


  1. https://github.com/Kitware/VTK/blob/c2788d1b6e016f17c49a52f7f1d637e6bf4cf278/Wrapping/PythonCore/vtkPythonCommand.cxx#L105 ↩︎

  2. https://github.com/Kitware/VTK/blob/c2788d1b6e016f17c49a52f7f1d637e6bf4cf278/Common/Core/vtkCommand.cxx#L26-L50 ↩︎

2 Likes

Sounds reasonable. Something like this, where the allowed types are int and str.

@callback_event_type(int)
def callback(caller, event):
    ...
1 Like