Difference between revisions of "VTK/Examples/Python/Interaction/MouseEventsObserver"

From KitwarePublic
< VTK‎ | Examples‎ | Python
Jump to navigationJump to search
m (Minor code cleanup)
 
Line 5: Line 5:
 
<source lang="python">
 
<source lang="python">
 
#!/usr/bin/env python
 
#!/usr/bin/env python
 +
from __future__ import print_function
  
 
import vtk
 
import vtk
+
 
 
source = vtk.vtkSphereSource()
 
source = vtk.vtkSphereSource()
 
source.SetCenter(0, 0, 0)
 
source.SetCenter(0, 0, 0)
 
source.SetRadius(1)
 
source.SetRadius(1)
 
source.Update()
 
source.Update()
+
 
 
mapper = vtk.vtkPolyDataMapper()
 
mapper = vtk.vtkPolyDataMapper()
 
mapper.SetInputConnection(source.GetOutputPort())
 
mapper.SetInputConnection(source.GetOutputPort())
+
 
 
actor = vtk.vtkActor()
 
actor = vtk.vtkActor()
 
actor.SetMapper(mapper)
 
actor.SetMapper(mapper)
+
 
 
renderer = vtk.vtkRenderer()
 
renderer = vtk.vtkRenderer()
 
renderer.SetBackground(1, 1, 1)
 
renderer.SetBackground(1, 1, 1)
 
renderer.AddActor(actor)
 
renderer.AddActor(actor)
+
 
 
renwin = vtk.vtkRenderWindow()
 
renwin = vtk.vtkRenderWindow()
 
renwin.AddRenderer(renderer)
 
renwin.AddRenderer(renderer)
+
 
 
interactor = vtk.vtkRenderWindowInteractor()
 
interactor = vtk.vtkRenderWindowInteractor()
 
interactor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
 
interactor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
 
interactor.SetRenderWindow(renwin)
 
interactor.SetRenderWindow(renwin)
+
 
 
def DummyFunc1(obj, ev):
 
def DummyFunc1(obj, ev):
     print "Before Event"
+
     print("Before Event")
   
+
 
 
def DummyFunc2(obj, ev):
 
def DummyFunc2(obj, ev):
     print "After Event"
+
     print("After Event")
+
 
# Print interator gives you a list of registered observers of the current  
+
# Print interator gives you a list of registered observers of the current
 
# interactor style
 
# interactor style
#print interactor
+
#print(interactor)
+
 
+
 
## adding priorities allow to control the order of observer execution  
+
## adding priorities allow to control the order of observer execution
 
## (highest value first! if equal the first added observer is called first)
 
## (highest value first! if equal the first added observer is called first)
 
interactor.RemoveObservers('LeftButtonPressEvent')
 
interactor.RemoveObservers('LeftButtonPressEvent')
Line 48: Line 49:
 
interactor.Initialize()
 
interactor.Initialize()
 
interactor.Start()
 
interactor.Start()
 
 
</source>
 
</source>

Latest revision as of 17:02, 23 August 2015

VTK Examples Baseline Interaction TestMouseEvents.png

This example directly changes the observers of the vtkInteractor, which is an easy way to disable events or to add some simple callback functions. For a more general framework using vtkInteractorStyle see VTK/Examples/Python/Interaction/MouseEvents. This specific example just disables the left mouse button of the vtkInteractorStyleTrackballCamera and prints a simple message instead.

MouseEventsObserver.py

#!/usr/bin/env python
from __future__ import print_function

import vtk

source = vtk.vtkSphereSource()
source.SetCenter(0, 0, 0)
source.SetRadius(1)
source.Update()

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(source.GetOutputPort())

actor = vtk.vtkActor()
actor.SetMapper(mapper)

renderer = vtk.vtkRenderer()
renderer.SetBackground(1, 1, 1)
renderer.AddActor(actor)

renwin = vtk.vtkRenderWindow()
renwin.AddRenderer(renderer)

interactor = vtk.vtkRenderWindowInteractor()
interactor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
interactor.SetRenderWindow(renwin)

def DummyFunc1(obj, ev):
    print("Before Event")

def DummyFunc2(obj, ev):
    print("After Event")

# Print interator gives you a list of registered observers of the current
# interactor style
#print(interactor)


## adding priorities allow to control the order of observer execution
## (highest value first! if equal the first added observer is called first)
interactor.RemoveObservers('LeftButtonPressEvent')
interactor.AddObserver('LeftButtonPressEvent', DummyFunc1, 1.0)
interactor.AddObserver('LeftButtonPressEvent', DummyFunc2, -1.0)
interactor.Initialize()
interactor.Start()