Mlab visual example

A very simple example to show how you can use TVTK’s visual module with mlab and create simple animations.

In the example, the grey box bounces back and forth between the two red ones.

The module exposes an API similar to VPython and is useful to create animation based on rigid object movement.

The @animate decorator ( mayavi.mlab.animate() ) is detailed on section Animating a visualization.

If you want to modify the data plotted by the mlab (as in the mlab.test_plot3d() call) to create an animation, please see section Animating the data.


Python source code:

# Author: Prabhu Ramachandran <prabhu [at]>
# Copyright (c) 2009, Enthought, Inc.
# License: BSD Style.

from mayavi import mlab
from import visual
# Create a figure
f = mlab.figure(size=(500,500))
# Tell visual to use this as the viewer.

# A silly visualization.

# Even sillier animation.
b1 =
b2 =,
b3 =,
b1.v = 5.0
def anim():
    """Animate the b1 box."""
    while 1:
        b1.x = b1.x + b1.v*0.1
        if b1.x > 2.5 or b1.x < -2.5:
            b1.v = -b1.v

# Run the animation.