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

**URL:** <https://discourse.vtk.org/t/proposal-update-vtkpythoncommand-execute-to-support-custom-event/12866>\
**Category:** Development\
**Created:** [December 7, 2023, 7:24pm UTC](https://discourse.vtk.org/t/proposal-update-vtkpythoncommand-execute-to-support-custom-event/12866 "2023-12-07T19:24:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcfr](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jcfr/32/466_2.png) [@jcfr](https://discourse.vtk.org/u/jcfr)\
**Post date:** [December 7, 2023, 7:24pm UTC](https://discourse.vtk.org/t/proposal-update-vtkpythoncommand-execute-to-support-custom-event/12866/1 "2023-12-07T19:24:45Z")

</div>

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:

```python
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

```pytb
 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](https://github.com/Kitware/VTK/blob/c2788d1b6e016f17c49a52f7f1d637e6bf4cf278/Wrapping/PythonCore/vtkPythonCommand.cxx#L105) 

2. [VTK/Common/Core/vtkCommand.cxx at c2788d1b6e016f17c49a52f7f1d637e6bf4cf278 · Kitware/VTK · GitHub](https://github.com/Kitware/VTK/blob/c2788d1b6e016f17c49a52f7f1d637e6bf4cf278/Common/Core/vtkCommand.cxx#L26-L50)

---

<div class="post-metadata">

**Author:** ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)\
**Post date:** [December 7, 2023, 8:33pm UTC](https://discourse.vtk.org/t/proposal-update-vtkpythoncommand-execute-to-support-custom-event/12866/2 "2023-12-07T20:33:52Z")

</div>

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

```python
@callback_event_type(int)
def callback(caller, event):
    ...

```
