Chapter 4. Using MayaVi from Python

Table of Contents
An example
Useful Python Modules

If you have installed MayaVi from the sources and are not using a binary release, then you can use MayaVi as a Python module. This chapter details how you can use MayaVi as a Python module. These features were added very recently and hence some of the code is not very mature. However, it still is pretty usable. Hopefully the next release will make things a little cleaner.

An example

Its very easy using MayaVi as a Python module. Thanks to Tkinter, it is also possible to use MayaVi from the Python interpreter. This means that one can script MayaVi! This is a pretty powerful and useful feature. To illustrate using MayaVi as a module and its scriptability, we will consider a few simple examples where the user generates some data and a VTK file and then uses MayaVi to visualize the data.

Generating some data

>>> # generate the data.
>>> from Numeric import *
>>> import scipy
>>> x = (arange(50.0)-25)/2.0
>>> y = (arange(50.0)-25)/2.0
>>> r = sqrt(x[:,NewAxis]**2+y**2)
>>> z = 5.0*scipy.special.j0(r)  # Bessel function of order 0
>>> # now dump the data to a VTK file.
>>> import pyvtk
>>> point_data = pyvtk.PointData(pyvtk.Scalars(z.flat))
>>> grid = pyvtk.StructuredPoints((50,50, 1), (-12.5, -12.5, 0), (0.5, 0.5, 1))
>>> data = pyvtk.VtkData(grid, point_data)
>>> data.tofile('/tmp/test.vtk')

The above example uses the Numeric , SciPy and pyVtk modules. The next step is to visualize the data.

Visualize the generated data

>>> import mayavi
>>> v = mayavi.mayavi() # create a MayaVi window.
>>> d = v.open_vtk('/tmp/test.vtk', config=0) # open the data file.
>>> # The config option turns on/off showing a GUI control for the data/filter/module.
>>> # load the filters.
>>> f = v.load_filter('WarpScalar', config=0) 
>>> n = v.load_filter('PolyDataNormals', 0)
>>> n.fil.SetFeatureAngle (45) # configure the normals.
>>> # Load the necessary modules.
>>> m = v.load_module('SurfaceMap', 0)
>>> a = v.load_module('Axes', 0)
>>> a.axes.SetCornerOffset(0.0) # configure the axes module.
>>> o = v.load_module('Outline', 0)
>>> v.Render() # Re-render the scene.

MayaVi from the Python interpreter

A MayaVi window launched from the Python interpreter.

The result of this is seen in the above figure. It is important to note that the Python interpreter will continue to remain interactive when MayaVi is running. In fact, it is possible to create an animation from the interpreter as done in the following.

>>> # now do some animation.
>>> import time
>>> for i in range (0, 10):
...  f.fil.SetScaleFactor(i*0.1)
...  v.Render()
...  v.renwin.save_png('/tmp/anim%d.png'%i) # save the image to a PNG file
...  time.sleep(1)
>>> 

The above example saves the screen each iteration to a PNG image. One will need VTK 4.0 for PNG support. These images can be later used by some other utility to create a movie. It is therefore possible to create very useful visualizations from within the Python interpreter.

Using VTK data objects

There are times when the user has created a VTK data object that needs to be visualized. MayaVi has a special data handler for such cases. The following shows how this can be used. The example itself uses a VTK file but the data could have also been generated using other means.

>>> # import VTK
>>> import vtkpython
>>> # create some data.
>>> reader = vtkpython.vtkStructuredPointsReader()
>>> reader.SetFileName('/tmp/test.vtk')
>>> reader.Update()
>>> data = reader.GetOutput()  # this is a vtkStructuredPoints object.
>>> import mayavi
>>> v = mayavi.mayavi() # create a MayaVi window
>>> v.open_vtk_data(data) # load the data from the vtkStructuredPoints object.
>>> f = v.load_filter('WarpScalar', 0)
>>> # Load other filters and modules...

The above example uses a vtkStructuredPoints as the input. Other types can also be used as the input. The other valid types are: vtkRectilinearGrid, vtkStructuredGrid, vtkUnstructuredGrid and vtkPolyData. Any of these objects can be used as an input and then visualized using MayaVi.

Right now the best way to find out what functions are available etc. would be to read the sources or use pydoc to browse through the code. Experimenting with MayaVi from the interpreter is also a good idea and will be highly educative.