remove plotter's default keyboard events #6654
-
First check
Commit to Help
Sample Code What is the problem, question, or error?Write a short description telling me what you are doing, what you expect to happen, and what is currently happening. import pyvista as pv
import pyvistaqt as pvqt
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('PyVista QT Integration')
self.setGeometry(100, 100, 800, 600)
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout()
central_widget.setLayout(layout)
self.plotter = pvqt.QtInteractor(self)
layout.addWidget(self.plotter.interactor)
self.plotter.clear_events_for_key('w')
self.plotter.clear_events_for_key('W')
self.plotter.add_mesh(pv.Sphere())
# Set up the Qt application
app = QApplication([])
window = MainWindow()
window.show() Descriptionis there a way to disable keyboard events such as the wirefame mode ('w')? clear_events_for_key does not work here. System Information--------------------------------------------------------------------------------
Date: Mon Sep 16 22:11:57 2024 CST
OS : Linux
CPU(s) : 20
Machine : x86_64
Architecture : 64bit
RAM : 31.0 GiB
Environment : Python
File system : ext4
GPU Vendor : NVIDIA Corporation
GPU Renderer : NVIDIA RTX 3500 Ada Generation Laptop GPU/PCIe/SSE2
GPU Version : 4.5.0 NVIDIA 550.90.07
MathText Support : True
Python 3.10.0 | packaged by conda-forge | (default, Nov 20 2021, 02:24:10)
[GCC 9.4.0]
pyvista : 0.43.5
vtk : 9.3.0
numpy : 1.26.4
matplotlib : 3.8.2
scooby : 0.9.2
pooch : 1.8.1
pillow : 10.1.0
imageio : 2.34.0
pyvistaqt : 0.11.1
PyQt5 : 5.15.11
IPython : 8.19.0
ipywidgets : 8.1.1
scipy : 1.11.4
tqdm : 4.66.5
jupyterlab : 4.0.9
nest_asyncio : 1.5.8
-------------------------------------------------------------------------------- ScreenshotsNo response |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 4 replies
-
I think that if the key press event is defined in the vtk interactor, then it cannot be cleared through these functions. In other words, See this example, which is a starting point, without the qt portion. I had to go to the vtk methods to avoid the weirdness of the import pyvista as pv
def blank_w(interactor, event):
if interactor.GetKeyCode() == 'w':
interactor.SetKeyCode("")
interactor.SetKeySym("")
return
pl = pv.Plotter()
pl.iren.clear_key_event_callbacks()
pl.iren.interactor.AddObserver("KeyPressEvent", blank_w, 10)
pl.add_key_event(key="w", callback=lambda test=0: print("w pressed"))
pl.add_mesh(pv.Sphere())
pl.show() |
Beta Was this translation helpful? Give feedback.
-
Hi, maybe something changed in VTK, but when I try with the code listed above I get an error saying that the method called blank_w "takes 2 positional arguments but 3 were given". If I remove the "10" it's the same. If I remove "KeyPressEvent" it says that two positional arguments are needed. It's very confusing. In general I strongly support a PyVista API to override VTK events, observers, etc. In the meanwhile if somebody has a working version, this would be VERY welcome! |
Beta Was this translation helpful? Give feedback.
-
Update: the blank_w method must be local so in larger code it cannot be something like self.blank_w(), but it must appear right above the pl.iren.interactor.AddObserver("KeyPressEvent", blank_w, 10) line, exactly as in the example above.. With this I was able to blank the X, Y Z events, but not the V that brings the camera back in hope position. Any suggestion? |
Beta Was this translation helpful? Give feedback.
-
More thinking... actually using V to bring the view back to home position cold be handy, if I was able to record a custom home position. Suggestions on this? THANKS! |
Beta Was this translation helpful? Give feedback.
-
OK, I found out that "V" is controlled by PyVista and can be blanked with this line:
|
Beta Was this translation helpful? Give feedback.
I think that if the key press event is defined in the vtk interactor, then it cannot be cleared through these functions. In other words,
clear_events_for_key
only clears events that are defined explicitly through PyVista. This isn't explained well in the documentation. Clearing the default vtk interactor events are unfortunately more involved than it should be. A work around is to add your own event observer at a higher priority and clear the key code before the vtk default events fire. I got this idea from https://discourse.vtk.org/t/consume-a-vtk-event-in-python/13052/3See this example, which is a starting point, without the qt portion. I had to go to the vtk methods to avoid the weirdβ¦