Lights, Cameras, Action : A Simple Example to Embed VTK with wxPython

Well matplotlib with wxPython is cool, but VTK with wxPython is awesome.

What is VTK?
The Visualization ToolKit (VTK) is an open source, freely available software system for 3D computer graphics, image processing, and visualization used by thousands of researchers and developers around the world. VTK consists of a C++ class library, and several interpreted interface layers including Tcl/Tk, Java, and Python. VTK supports a wide variety of visualization algorithms including scalar, vector, tensor, texture, and volumetric methods; and advanced modeling techniques such as implicit modelling, polygon reduction, mesh smoothing, cutting, contouring, and Delaunay triangulation. In addition, dozens of imaging algorithms have been directly integrated to allow the user to mix 2D imaging / 3D graphics algorithms and data.

So here’s a simple basic code to embed and manipulate VTK with wxPython.

Created on Fri Dec 13 08:45:01 2013

@author: Sukhbinder Singh

VTK has an abstract graphics layer above the local hostís graphic 
software interface (usually OpenGL). This insures cross-platform portability 
and creates a device independent graphics layer. In the graphics model class 
names were adapted from the movie-making industry. Lights, cameras, actors, 
and props are all used to create a scene.


import wx
import vtk
from vtk.wx.wxVTKRenderWindowInteractor import wxVTKRenderWindowInteractor

class p1(wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self, parent)
        #to interact with the scene using the mouse use an instance of vtkRenderWindowInteractor. 
        self.widget = wxVTKRenderWindowInteractor(self, -1)
        self.widget.AddObserver("ExitEvent", lambda o,e,f=self: f.Close())
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.widget, 1, wx.EXPAND)
        self.isploted = False
    def renderthis(self):
            # open a window and create a renderer
            ren = vtk.vtkRenderer()
            # to generate polygonal data for a cone.
            cone = vtk.vtkConeSource()

            # o take the polygonal data from the vtkConeSource and
            # create a rendering for the renderer.
            coneMapper = vtk.vtkPolyDataMapper()

            # create an actor for our scene
            coneActor = vtk.vtkActor()
            # Add actor

            axes = vtk.vtkAxesActor()
            self.marker = vtk.vtkOrientationMarkerWidget()
            self.marker.SetInteractor( self.widget._Iren )
            self.marker.SetOrientationMarker( axes )

            cam = ren.GetActiveCamera()
            self.isploted = True

class TestFrame(wx.Frame):
    def __init__(self,parent,title):
        wx.Frame.__init__(self,parent,title=title,size=(650,600), style=wx.MINIMIZE_BOX|wx.SYSTEM_MENU|
        self.sp = wx.SplitterWindow(self)
        self.p1 = p1(self.sp)
        self.p2 = wx.Panel(self.sp,style=wx.SUNKEN_BORDER)

        self.statusbar = self.CreateStatusBar()
        self.statusbar.SetStatusText("Click on the Plot Button")
        self.plotbut = wx.Button(self.p2,-1,"plot", size=(40,20),pos=(10,10))

    def plot(self,event):
        if not self.p1.isploted:
            self.statusbar.SetStatusText("Use your mouse to interact with the model")

app = wx.App(redirect=False)
frame = TestFrame(None,"Lights, Cameras, Action")


One thought on “Lights, Cameras, Action : A Simple Example to Embed VTK with wxPython

  1. Pingback: SLT Viewer – a VTK and wxpython powered App |

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s