I am trying to find a way to use QVTKRenderWindowInteractor but with the blocked rotation feature. I tried to subclass it in Python and overwrite the left/right click methods but it does not seem to work in any way.
Is there any way to block the rotation possibility in QVTKRenderWindowInteractor (the idea is to block it temporary for some features of the app) without building some custom VTK? Purely from Python?
The QVTKRenderWindowInteractor passes mouse and keyboard events to the vtkRenderWindowInteractor, so customization is done via vtkRenderWindowInteractor or vtkInteractorStyle.
It’s been a long time since I’ve done this, and I don’t have any examples, but a rough outline is as follows:
def onLeftButtonDown(obj, event): # or onLeftButtonDown(self, obj, event)
print(event)
def onLeftButtonUp(obj, event):
print(event)
interactor.AddObserver("LeftButtonPressEvent", onLeftButtonDown) # or self.onLeftButtonDown
interactor.AddObserver("LeftButtonReleaseEvent", onLeftButtonUp)
Hi! Thanks. Thing is, when I do AddObserver, the related callback is indeed called but the original interaction is still possible. I.e. everything in the onLeftButtonDown callback is executed, but the original rotation is still available Any idea how to actually overwrite default behavior, not just add new observer on top of it?
I think that I found the secret sauce, and there are two ingredients:
a) the observers need to be added to the InteractorStyle
b) the InteractorStyle must not be vtkInteractorStyleSwitch
For example:
# The trackball camera style is the most popular style
style = vtkInteractorStyleTrackballCamera()
interactor.SetInteractorStyle(style)
style.AddObserver("LeftButtonPressEvent", onLeftButtonDown)
style.AddObserver("LeftButtonReleaseEvent", onLeftButtonUp)
The problems with vtkInteractorStyleSwitch are
a) it’s the default style, it’s what you get if you don’t set a different style
b) adding observers to vtkInteractorStyleSwitch doesn’t work
The reason observers don’t work with vtkInteractorStyleSwitch is because it actually contains several internal vtkInteractorStyle objects, and it chooses which one to connect to the interactor. It’s not possible to get access to these internal styles to add observers to them. Since vtkInteractorStyleSwitch itself is just a container, adding observers to it has no effect.