Vedo: About the mouse move event

Created on 30 Nov 2020  路  8Comments  路  Source: marcomusy/vedo

The example provides an example about how to implement user-self mouse event:

from vedo import *


def onLeftClick(mesh):
    printc("Left   button pressed on", [mesh], c="g")


def onMiddleClick(mesh):
    printc("Middle button pressed on", [mesh], c="y")


def onRightClick(mesh):
    printc("Right  button pressed on", [mesh], c="r")


vp = Plotter()

vp.load(datadir+"teapot.vtk").c("gold")

vp.mouseLeftClickFunction   = onLeftClick
vp.mouseMiddleClickFunction = onMiddleClick
vp.mouseRightClickFunction  = onRightClick

printc("Click object to trigger function call", invert=1, box="-")

vp += __doc__
vp.show()

The above code shows the left/middle/right click event. I wonder how to implement the mouse move event?

In addition, the onLeftClick is called by:

        plotter::_mouseleft(self, iren, event):
             ...vedo code
             if self.mouseLeftClickFunction:
                 self.mouseLeftClickFunction(clickedActor)

Actually, the vedo will finish some code, and then call self.mouseLeftClickFunction(clickedActor). If I don't need the ...vedo code, I want vedo can directly call my-self onLeftClick, how can I achieve this purpose? Thank you in advance.

All 8 comments

You can use the following:

"""Mouse click event example
click of the mouse causes a call to a custom function"""
from vedo import printc, Plotter, datadir

printc("Click object to trigger function call", invert=1, box="-")


def onLeftClick(mesh):
    printc("Left button pressed on", [mesh], c="g")

def onEvent(iren, event):
    x, y = iren.GetEventPosition()
    printc(event+' happened', [x,y])


vp = Plotter()

vp.load(datadir+"teapot.vtk").c("gold")

# simplified way to create an observer with ready access to the clicked mesh:
vp.mouseLeftClickFunction = onLeftClick

# a more general way, see:
# https://vtk.org/doc/nightly/html/classvtkCommand.html
# KeyPressEvent, RightButtonPressEvent, MouseMoveEvent, ..etc
vp.addCallback('MouseMoveEvent', onEvent)

vp += __doc__
vp.show()

thx!

Let's say the following code:

from vedo import printc, Plotter, datadir

printc("Click object to trigger function call", invert=1, box="-")


def onLeftClick(mesh):
    printc("Left button pressed on", [mesh], c="g")

def leftButtonPress(iren, event):
    print('leftButtonPress')

def KeyPressEvent(iren, event):
    print('key')

def onEvent(iren, event):
    x, y = iren.GetEventPosition()
    printc(event+' happened', [x,y])


vp = Plotter()

vp.load(datadir+"teapot.vtk").c("gold")

# simplified way to create an observer with ready access to the clicked mesh:
vp.mouseLeftClickFunction = onLeftClick

# a more general way, see:
# https://vtk.org/doc/nightly/html/classvtkCommand.html
# KeyPressEvent, RightButtonPressEvent, MouseMoveEvent, ..etc
vp.addCallback('MouseMoveEvent', onEvent)
vp.addCallback('LeftButtonPressEvent', leftButtonPress)
vp.addCallback('KeyPressEvent', KeyPressEvent)

vp += __doc__
vp.show()

I have three question:

  1. If I click on the object, the program will call both onLeftClick and leftButtonPress. How can I let the program enter one function.
  2. I have add the call back onEvent. Thus, when the mouse is moved, the program will call onEvent. In the onEvent, I do nothing. Thus, I hope the program also do nothing. Actually, even I do nothing on onEvent, the program will rotate the object.
  3. In the KeyPressEvent, how can I know the which keyboard is pressed?
  1. If I click on the object, the program will call both onLeftClick and leftButtonPress. How can I let the program enter one function.

what do you mean ? just remove one of the two..

I have add the call back onEvent. Thus, when the mouse is moved, the program will call onEvent. In the onEvent, I do nothing. Thus, I hope the program also do nothing. Actually, even I do nothing on onEvent, the program will rotate the object.

Yes - because there are other observers active on the default interactor to control the scene.
You might use:
vp.interactor.RemoveAllObservers()
or remove specific observers.

In the KeyPressEvent, how can I know the which keyboard is pressed?

You mean _key_ ? Check out examples/basic/keypress.py

@marcomusy Thank you very much. vp.interactor.RemoveAllObservers() is what I want. I can remove all observers, and add what I want. In this way, I can control all the event call back funtion.

You can subclass your interactor following the examples in
https://vtk.org/Wiki/VTK/Tutorials/InteractorStyleSubclass
https://vtk.org/Wiki/VTK/Examples/Cxx/Interaction/MouseEvents
https://lorensen.github.io/VTKExamples/site/Python/Interaction/MouseEventsObserver/ (python)
Once you have your class ready you can assign it to the current vedo.Plotter instance via:
plt.interactor.SetInteractorStyle(my_interactor_style)

For once it looks like i've been too fast in answering :-)

@marcomusy Really thank you for your very nice reply.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hmralavi picture hmralavi  路  7Comments

Yoorthiziri picture Yoorthiziri  路  5Comments

CMengich picture CMengich  路  5Comments

kemo993 picture kemo993  路  6Comments

mahendra-ramajayam picture mahendra-ramajayam  路  3Comments