[vtkusers] vtkRenderer in wxPython

John Hunter jdhunter at ace.bsd.uchicago.edu
Mon Nov 19 10:52:06 EST 2001


I am trying to port the SliceViewer.py demo code that ships with
vtk3.2 in contrib/examplesPython to wxPython, in part because I need
the functionality and in part because I am trying to learn how to
integrate vtk with wxPython.

My current problem is that the vtkRenderer appears to default to an X
(Tk?)  window because my test app is creating two windows: my wxPython
window (titled "John's VTK" and another X window (titled
"SliceViewer").  The SliceViewer window contains the image and all the
functionality that I want in my wxFrame.

I believe that the problem is in the call 

        self._Renderer = vtkRenderer()

which identifies itself as a vtkOpenGLRenderer when I 'print self._Renderer'.

Earlier, I was able to view slices in a wxFrame by doing:

        vtkwin = vtk.wxVTKRenderWindow(self, -1)
        viewer = vtkImageViewer()
        ren = viewer.GetRenderer()
        vtkwin.GetRenderWindow().AddRenderer(ren)
        ren.AddActor(viewer.GetActor2D())

but I want more functionality that the vtkImageViewer offers and so i
am trying to use vtkImageReslice.

Below is the beginning of my code, which wraps SliceViewer into
wxPython.  Any suggestions about how to modify it so the images are
displayed in the wxPython app and not in a separate window?  Is there
any way to use interactors with wxpython?

Thanks for any suggestions,
John Hunter

#! /usr/local/bin/python
from wxPython.wx import *
#from wxPython.lib import vtk
import wx_vtk as vtk

from vtkpython import *
import math, os

VTK_DATA = os.environ['VTK_DATA']

class MyApp(wxApp):
     def OnInit(self):
         frame = MyFrame(None, -1, "John's VTK")
         frame.Show(true)
         self.SetTopWindow(frame)
         return true

class MyFrame(wxFrame):
    def __init__(self, parent, ID, title):
        wxFrame.__init__(self, parent, ID, title,
                         style=wxDEFAULT_FRAME_STYLE|
                         wxNO_FULL_REPAINT_ON_RESIZE)

        self.reader = vtkImageReader()
        self.reader.ReleaseDataFlagOff()
        self.reader.SetDataByteOrderToLittleEndian()
        self.reader.SetDataSpacing(1.0,1.0,2.0)
        self.reader.SetDataExtent(0,255,0,255,1,93)
        self.reader.SetDataOrigin(-127.5,-127.5,-94.0)
        self.reader.SetFilePrefix(os.path.join(VTK_DATA,'fullHead/headsq'))
        self.reader.SetDataMask(0x7fff)
        self.reader.UpdateWholeExtent()
        
        self.viewer = SliceViewer(self)
        self.viewer.SetInput(self.reader.GetOutput())
        self.viewer.SetColorWindow(2000) 
        self.viewer.SetColorLevel(1000) 
   
    def OnInit(self):
        self.viewer.Start()

        

class SliceViewer(vtk.wxVTKRenderWindow):
    def __init__(self, parent):

        vtk.wxVTKRenderWindow.__init__(self,
            parent, -1, size=wxSize(256, 256))

        self._RenderWindow = self.GetRenderWindow()
        self._ImageReslice = vtkImageReslice()
        self._ImageReslice.SetInterpolationModeToCubic()
        
        self._ImageMapper = vtkImageMapper()        
        self._ImageMapper.SetInput(self._ImageReslice.GetOutput())
        self._OriginalColorWindow = self._ImageMapper.GetColorWindow()
        self._OriginalColorLevel = self._ImageMapper.GetColorLevel()

        self._Actor = vtkActor2D()
        self._Actor.SetMapper(self._ImageMapper)


        #the next line appears to be causing my problems
        self._Renderer = vtkRenderer()
        self._Renderer.AddActor2D(self._Actor)

        self._RenderWindow.AddRenderer(self._Renderer)
        self._RenderWindow.SetWindowName("SliceViewer")

        self._InteractorStyle = vtkInteractorStyleUser() 
        
        self._Interactor = vtkRenderWindowInteractor()
        self._Interactor.SetRenderWindow(self._RenderWindow)
        self._Interactor.SetInteractorStyle(self._InteractorStyle)

        self.BindSliceViewer()

#the rest of the SliceViewer class is verbatim from SliceViewer.py

if __name__ == '__main__':
    app = MyApp(0)
    app.MainLoop()



More information about the vtkusers mailing list