[vtkusers] Problem with vtkRenderWindowInteractor with more than one widget

Kenneth Evans evans at aps.anl.gov
Wed Jun 4 16:50:13 EDT 2008


>> There's really no need to subclass anything.  You'll need to do some
>> more reading, but it's also possible to attach a new observer for
>> keyboard events and listen for the 'i' for example.  In the event
>> handler, use a vtkPropPicker to find out where the user is pointing.
>> Based on this information, select the correct interactor do what you
>> need to do.  You'll obviously need to keep around some extra data
>> structures for this.

Thanks.  The following seems to work pretty well and is reasonably simple:

    def interactorKeyCallback(widget, event_string):
      # GetKeySym returns null here, use GetKeyCode
      key = self.renWin.GetKeyCode()
      print 'interactorKeyCallback', event_string, key
#      print widget
      if(key == 't'):
        position = widget.GetEventPosition()
        picker = vtk.vtkPropPicker()
        picked = picker.PickProp(position[0], position[1], self.ren)
        print 'picked', '=', picked
        if(picked):
          prop = picker.GetViewProp()
          if(prop == self.sphereActor):
            print 'Sphere Actor'
            enabled = self.sphereBoxWidget.GetEnabled()
            self.sphereBoxWidget.SetEnabled(enabled - 1)
          elif(prop == self.coneActor):
            print 'Cone Actor'
            enabled = self.coneBoxWidget.GetEnabled()
            self.coneBoxWidget.SetEnabled(enabled - 1)
          else:
            print prop.GetClassName()
        
    self.renWin.AddObserver("KeyPressEvent", interactorKeyCallback)

There is a sphere and a cone in this example.  Otherwise it is similar to
the cone6 example.  I am using WX so the renWin here is actually the
wxVTKRenderWindowInteractor.  It might also more clearly have been called
self.renWinI, self.iren, or something like that.

I used 't' as the key.  If I use 'i', then _both_ the interactor callback
and my callback get executed, which is not what you want.  (Perhaps there is
a way around this, but I didn't see it.)

The following that I had tried before did not work.  The callback never gets
called.  I did not trace down why.

    def sphereKeyCallback(widget, event_string):
      key = self.renWin.GetKeySym()
      print 'sphereKeyCallback', event_string, key
      if(key == 't'):
        enabled = self.sphereBoxWidget.GetEnabled()
        self.sphereBoxWidget.SetEnabled(enabled - 1)
    
    self.sphereBoxWidget.AddObserver("KeyPressEvent", sphereKeyCallback)

I would appreciate any suggestions for a better way.  Thanks for the help.

	-Ken





More information about the vtkusers mailing list