VTK Python Binding

VTK Python Binding in MeVisLab

MeVisLab comes with a complete Python binding for the VTK library. This means that you can use any Python/VTK example and run them inside of MeVisLab, with minor adoptions to redirect the rendering to a MeVisLab MDL control.

Getting Started

To get started, just open a script console in MeVisLab and do:

import vtk

Now you can instantiate any of the wrapped VTK objects. You can grab most of the Python/VTK examples that are provided with VTK and run them using execfile(path/to/the/example).

NOTE: The examples do not integrate deeply into MeVisLab, since a vtkRenderWindow will show its own native window and will start its own event loop. For deep integration of VTK visualizations into MeVisLab, read the next chapter.

Integrating VTK Visualizations into the MDL

There are several ways to integrate your VTK visualizations into a MeVisLab GUI. The most common way is to add a vtkRenderWidget (or vtkRenderWidgetGL) to your MDL GUI:

Window {
  Vertical {
    vtkRenderWidget {
      name = view
      initCommand = initViewer

In the Python script of your module you can then attach the vtkRenderer to the vtkRenderWidget:

from vtk import *

def initViewer(viewer):
  # Create the VTK scene and renderer:
  renderer = createVTKRenderer()
  # Attach the renderer to the MDL vtkRenderWidget:

def createVTKRenderer():
  # Create a sphere
  sphereSource = vtkSphereSource();
  sphereSource.SetCenter(0.0, 0.0, 0.0);

  # Create a mapper and actor
  mapper = vtkPolyDataMapper ();

  actor = vtkActor();

  camera = vtkCamera ();
  camera.SetPosition(0, 0,100);
  camera.SetFocalPoint(0, 0, 0);

  # Create a renderer
  renderer = vtkRenderer();

  # Add the actor to the scene
  renderer.SetBackground(1,1,1); # Background color white

  return renderer

The difference between vtkRenderWidget and vtkRenderWidgetGL is that vtkRenderWidgetGL is based on Qt’s OpenGL support (using QVTKWidget2), while vtkRenderWidget uses the VTK GL support (using QVTKWidget).

Remote rendering VTK scenes

You can either use the VTKRemoteRendering module to remote render a VTK scene, or you can use the GraphicsView.

Integrating VTK Visualizations into the GraphicsView

VTK Visualizations can be integrated into the GraphicsView MDL control. This is done using the MLABVTKGraphicsItem item that is provided via the MLABGraphicsScene.addVTKGraphicsItem call. Have a look at the VTKGraphicsViewExample module, which demonstrates how to add a VTK item to the GraphicsView scene.

Advanced Integration using Qt widgets

The QtVTK Python module offers wrappers for the QVTKWidget, QVTKWidget2 and QVTKGraphicsView, so that own Python based viewers can be written using these classes. It is recommended to prefer the vtkRenderWidget MDL controls instead.

Cooperating with the VTK ML modules

For details on the VTK ML modules, have a look at VTK Algorithms in MeVisLab.

The VTK Python wrappers can be used together with the VTK ML modules. You can get access to the VTK Python object of a MLBase field via field.object() and you can set VTK Python objects on MLBase fields via field.setObject().

For example, if you want to get access to the vtkImageImport of a MLToVTKImage module, you can do:

image = ctx.field("MLToVTKImage.outputImage").object()

and then use the resulting vtkImageImport object as source for other VTK algorithms. Vice versa, you can create a VTK pipeline in Python and set the result as the input of a module, using setObject().

Have a look at the VTKRayCastingExample to see how this can be used.