# Consume a VTK event in Python

**URL:** https://discourse.vtk.org/t/consume-a-vtk-event-in-python/13052
**Category:** Support
**Tags:** python
**Created:** [January 11, 2024, 8:30am UTC](https://discourse.vtk.org/t/consume-a-vtk-event-in-python/13052 "2024-01-11T08:30:02Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Edoardo\_Pasca](https://discourse.vtk.org/user_avatar/discourse.vtk.org/edoardo_pasca/32/742_2.png) [@Edoardo\_Pasca](https://discourse.vtk.org/u/Edoardo_Pasca)
#### Post date: [January 11, 2024, 8:30am UTC](https://discourse.vtk.org/t/consume-a-vtk-event-in-python/13052/1 "2024-01-11T08:30:02Z")

</div>

Hi all,

If I press `s` or `w` in a `vtkRenderWindow` a `KeyPress` event is fired and the representation of the rendered objects changes to surface or wireframe. The event observer, I should think, is handled by the `vtkRenderWindowInteractor`.

I would like to add an observer that would consume the event and stop any further processing of the event in Python.

I googled and found that I should use `SetAbortFlag` but from a couple of (old) links I gather that this is not possible in Python:  
[https://vtk.org/pipermail/vtk-developers/2007-September/020213.html](https://vtk.org/pipermail/vtk-developers/2007-September/020213.html)  
[https://vtk.org/Wiki/VTK/Python\_Wrapper\_Enhancement#Wrap\_vtkCommand\_and\_allow\_it\_to\_be\_subclassed](https://vtk.org/Wiki/VTK/Python_Wrapper_Enhancement#Wrap_vtkCommand_and_allow_it_to_be_subclassed)

Given that the links I could find are old, is this still impossible?  
How could I achieve this otherwise?

Thanks

Edo

PS I have an [example](https://github.com/paskino/CILViewer/blob/event_consume/Wrappers/Python/examples/event_consume.py) where I tried to do some experiments

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [January 11, 2024, 12:50pm UTC](https://discourse.vtk.org/t/consume-a-vtk-event-in-python/13052/2 "2024-01-11T12:50:04Z")

</div>

Cc: @spyridon97 @dgobbi

---

<div class="post-metadata">

### Author: ![Edoardo\_Pasca](https://discourse.vtk.org/user_avatar/discourse.vtk.org/edoardo_pasca/32/742_2.png) [@Edoardo\_Pasca](https://discourse.vtk.org/u/Edoardo_Pasca)
#### Post date: [January 12, 2024, 10:37am UTC](https://discourse.vtk.org/t/consume-a-vtk-event-in-python/13052/3 "2024-01-12T10:37:00Z")

</div>

After further investigation I managed by

1. adding an observer on `CharEvent` with higher priority than the one that is in the interactor. Earlier I used `KeyPressEvent` which was not correct.
2. modifying the key code of the event. I set it to no character to be absolutely sure

```python
def consumeCharEvent(interactor, event):
    if interactor.GetKeyCode() in ['s', 'w']:
        interactor.SetKeyCode("")

iren = vtk.vtkRenderWindowInteractor()
iren.AddObserver("CharEvent", consumeCharEvent, 10)

```

To make it a clean job I would like to be able to get the priority of the `CharEvent` observer I want to override. I do not seem to find a way to get any observers from a `vtkObject`, but printing the object (the interactor) I can find the observer priority and tag.

```auto
vtkObserver (000001F012EB9910)
        Event: 22
        EventName: CharEvent
        Command: 000001F012ECA480
        Priority: 0
        Tag: 81

```

Any help is welcome

Edo

---

<div class="post-metadata">

### Author: ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)
#### Post date: [January 12, 2024, 5:38pm UTC](https://discourse.vtk.org/t/consume-a-vtk-event-in-python/13052/4 "2024-01-12T17:38:41Z")

</div>

The default handling of the event is done in [vtkInteractorStyle](https://gitlab.kitware.com/vtk/vtk/-/blob/07853173896fedbf0fdde072a180846f57eb8bae/Rendering/Core/vtkInteractorStyle.cxx#L1257-1266) and the representation switch is done in the [`OnChar`](https://gitlab.kitware.com/vtk/vtk/-/blob/07853173896fedbf0fdde072a180846f57eb8bae/Rendering/Core/vtkInteractorStyle.cxx#L833-880) method.

---

<div class="post-metadata">

### Author: ![Edoardo\_Pasca](https://discourse.vtk.org/user_avatar/discourse.vtk.org/edoardo_pasca/32/742_2.png) [@Edoardo\_Pasca](https://discourse.vtk.org/u/Edoardo_Pasca)
#### Post date: [January 19, 2024, 5:45pm UTC](https://discourse.vtk.org/t/consume-a-vtk-event-in-python/13052/5 "2024-01-19T17:45:24Z")

</div>

I haven’t found any way to get an observer from an object.  
The following function reads the string representation of the object and returns the priority.

```auto
def GetObserverPriority(vtk_object, event_name: str) -> int:
    '''Given a vtk object and an event name, return the priority of the observer
    
    It is impossible to get an observer from the object programmatically, but
    the string representation of the object contains the observers with priority.

    This code reads the string representation and returns the priority as integer.
    '''
    string = str(vtk_object)

    ss = string.split("vtkObserver")

    eventObservers = []
    for s in ss:
        lines = s.split("\n")
        eo = {}
        for line in lines:
            try:
                k,v = line.split(":")
                k = k.strip()
                v = v.strip()
                if k in ['Event', 'EventName', 'Command', 'Priority', 'Tag']:
                    eo[k] = v
                    eventObservers.append(eo)
            except:
                pass

    for el in eventObservers:
        if el['EventName'] == event_name:
            return int(el['Priority'])

```

Hope this helps

Edo
