This is an archival dump of old wiki content --- see scipy.org for current material.
Please see http://scipy-cookbook.readthedocs.org/

This page presents scripting Mayavi2 using the advanced, object-oriented API. Mayavi2 has recently acquired an easy-to-use, thought maybe not as powerful, scripting module: mlab. You are invited to refer to the section of Mayavi2 user guide. Reading this page will give you a deeper understanding of how Mayavi2 works, and it complements the user guide.

Introduction

To script MayaVi2, you need (at least):

Scripting MayaVi2 is quite simple because MayaVi2 is written in python and based on TVTK, which eases the uses of all VTK objects.

In the following, you'll be learned how to script and use MayaVi2 modules and filters.

Modules can be split in two parts:

Before starting, let's see the "main template" of a MayaVi2 script written in python.

Main template: create your MayaVi2 class

A MayaVi2 script should contain at least the following few lines:

   1 #! /usr/bin/env python
   2 
   3 from enthought.mayavi.app import Mayavi
   4 
   5 class MyClass(Mayavi):
   6     
   7     def run(self):
   8         script = self.script
   9         # `self.script` is the MayaVi Script interface (an instance of
  10         # enthought.mayavi.script.Script) that is created by the
  11         # base `Mayavi` class.  Here we save a local reference for
  12         # convenience.
  13   
  14         ## import any Mayavi modules and filters you want (they must be done here!)
  15         .../...
  16 
  17         script.new_scene()                      # to create the rendering scene
  18 
  19         ## your stuff (modules, filters, etc) here
  20         .../...
  21 
  22 if __name__ == '__main__':
  23     
  24     mc = MyClass()
  25     mc.main()

Adding modules or filters is quite simple: you have to import it, and then add it to your MayaVi2 class.

To add a module, type:

from enthought.mayavi.modules.foo_module import FooModule
.../...
mymodule = FooModule()
script.add_module(mymodule)

To add a filter, type:

from enthought.mayavi.filters.bar_filter import BarFilter
.../...
myfilter = BarFilter()
script.add_filter(myfilter)

Notice the used syntax: for modules for example, foo_module is the foo_module python file (without the extension .py) in the subdirectory module/ of mayavi/ directory (lower case, underscore); this file contains the class FooModule (no underscore, capitalized name).

But first of all, before rendering your scene with the modules and the filters you want to use, you have to load some data, of course.

Loading data

You have the choice between:

Loading data from array using ArraySource method

For example, we will create a 50*50*50 3D (scalar) array of a product of cosinus & sinus functions.

To do this, we need to load the appropriate modules:

   1 import scipy
   2 from scipy import ogrid, sin, cos, sqrt, pi
   3 
   4 from enthought.mayavi.sources.array_source import ArraySource
   5 
   6 Nx = 50
   7 Ny = 50
   8 Nz = 50
   9 
  10 Lx = 1
  11 Ly = 1
  12 Lz = 1
  13 
  14 x, y, z = ogrid[0:Lx:(Nx+1)*1j,0:Ly:(Ny+1)*1j,0:Lz:(Nz+1)*1j]
  15 
  16 # Strictly speaking, H is the magnetic field of the "transverse electric" eigenmode m=n=p=1
  17 # of a rectangular resonator cavity, with dimensions Lx, Ly, Lz
  18 Hx = sin(x*pi/Lx)*cos(y*pi/Ly)*cos(z*pi/Lz)
  19 Hy = cos(x*pi/Lx)*sin(y*pi/Ly)*cos(z*pi/Lz)
  20 Hz = cos(x*pi/Lx)*cos(y*pi/Ly)*sin(z*pi/Lz)
  21 Hv_scal = sqrt(Hx**2 + Hy**2 + Hz**2)
  22 
  23 # We want to load a scalars data (Hv_scal) as magnitude of a given 3D vector (Hv = {Hx, Hy, Hz})
  24 # Hv_scal is a 3D scalars data, Hv is a 4D scalars data
  25 src = ArraySource()
  26 src.scalar_data = Hv_scal # load scalars data
  27 
  28 # To load vectors data
  29 # src.vector_data = Hv

Loading data from file using FileReader methods

To load a VTK data file, say heart.vtk file in mayavi/examples/data/ directory, simply type:

from enthought.mayavi.sources.vtk_file_reader import VTKFileReader

src = VTKFileReader()
src.initialize("heart.vtk")

Note: Files with .vtk extension are called "legacy VTK" files. MayaVi2 can read a lot of other files formats (XML, files from Ensight, Plot3D and so on). For example, you can load an XML file (with extension .vti, .vtp, .vtr, .vts, .vtu, etc) using VTKXML!FileReader method.

Add the source to your MayaVi2 class

Then, once your data are loaded using one of the two methods above, add the source with the add_source() method in the body of the class MyClass (after script.new_scene):

script.add_source(src)

The four basic modules Outline, Axes, OrientationAxes and Text will be presented now.

Basic Modules

See the Basic Modules wiki page.

Main Modules

See the Main Modules wiki page.

Filters

See the Filters wiki page.


CategoryCookbook

SciPy: Cookbook/MayaVi/ScriptingMayavi2 (last edited 2015-10-24 17:48:23 by anonymous)