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.

VTK Integration Overview

The VTK integration consists of the following parts:

UI Integration

  • vtkRenderWidget → MDL Control for showing a VTK viewer in MDL GUIs
  • vtkView → Macro that allows to render stuff
  • GraphicsView VTK item
  • VTKRemoteRendering → Allows to use VTK in remote rendering / web applications
  • SoVTK → Integration of VTK rendering into Open Inventor scene

Data Conversion

There are various modules to convert between ML and VTK datatypes:

VTK Wrappers

VTK Python package

MeVisLab contains the complete VTK python package, so you can simply import vtk and instantiate any VTK object via the Python package.

VTK Python and ML module bridge

PythonQt (the MeVisLab Python/Qt binding) has built-in support for VTK Python objects, this means that you can:

  • get the Python VTK object for any VTK C++ object that is exposed on a ML Base Field on a MeVisLab object
  • set a Python VTK object on any ML Base Field in MeVisLab which expects a VTK object

All the magic of converting between C++ and Python VTK objects is done behind the scene by PythonQt.

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:
  viewer.setRenderer(renderer)

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

  # Create a mapper and actor
  mapper = vtkPolyDataMapper();
  mapper.SetInputConnection(sphereSource.GetOutputPort());

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

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

  # Create a renderer
  renderer = vtkRenderer();
  renderer.SetActiveCamera(camera);

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

  return renderer

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.

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.

Conversion from former MLVTKModules

If a network containing generated VTK modules from MeVisLab < 4.0 should be converted to VTK Python binding, this could be used as a reference:

Conversion.png

The vtkImageConvolve module is replaced by a RunPythonScript containing:

import vtk

# 1
inImage = ctx.field("MLToVTKImage.outputImage").object()

# 2
convolve = vtk.vtkImageConvolve()
convolve.SetInputData(inImage.GetOutput())
convolve.Update()
outImage = convolve.GetOutput()

# 3
ctx.field("VTKToMLImage.inputvtkImageData").setObject(outImage)

This code does the following:

  • Get the converted VTK image from the MLToVTKImage module
  • Setting up the vtkImageConvolve filter, connecting the input image and getting the output image
  • Setting the output image back on the VTKToMLImage module to convert back to the ML