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

Introduction

These modules are called "basic" because they are general and independant to all kind of data.

Before using a module, remind that you have to import it.

In general, if you want to change the color of an object, you have to type:

module.actor.property.color = fg_color

where fg_color is a "tuple", say (0, 0, 0) for black.

Note: You can set color for each module/filter (when available, of course), as above. But you can also set background/foreground colors for all your MayaVi2 sessions. See Cookbook/MayaVi/Tips. So, your background and foreground colors may (in fact, must) be different from those in the pictures presented here.

Outline module

Nothing special for this very simple module.

Begin to import the Outline module:

from enthought.mayavi.modules.outline import Outline

then

fg_color = (0, 0, 0)    # black foreground color
o = Outline()
script.add_module(o)
o.actor.property.color = fg_color

basic_outline.png

Axes module

For axis, you can set several parameters:

Here's what you can type:

from enthought.mayavi.modules.axes import Axes

then

a = Axes()
script.add_module(a)
a.axes.property.color = fg_color                     # color for axes
a.axes.axis_title_text_property.color = fg_color     # color for axes title
a.axes.x_label = "Lx"                                # label for Ox axis
a.axes.y_label = "Ly"                                # label for Oy axis
a.axes.z_label = "Lz"                                # label for Oz axis
a.axes.label_format = ""                             # no dimensions displayed
a.axes.axis_label_text_property.color = fg_color     # in case we want to display them

Label format is the format for the dimensions along Ox, Oy and Oz axis. By default, it is set to %6.3g.

basic_axes.png

OrientationAxes module

OrientationAxes module will display a little trihedron in the corner of the render window, showing the orientation of the three axes, Ox, Oy, Oz.

Note: VTK > 5.0 must be installed in order to use this module.

Nothing special here, as the Outline module, you can set the labels color:

from enthought.mayavi.modules.orientation_axes import OrientationAxes

and

oa = OrientationAxes()
script.add_module(oa)
oa.text_property.color = fg_color

basic_orientationaxes.png

Text module

You can use this module to display a title for your rendering window.

You need a text (a string), and you have to set up its position in the window (coordinates go from 0 to 1 in x and y) with the x_position and y_position parameters.

Then you can set up the height and the width of your text:

from enthought.mayavi.modules.text import Text

and

# I like my title centered and at top of the window
t = Text()
t.text = "My Title Centered at the Top"
script.add_module(t)
t.actor.scaled_text = False
t.actor.text_property.font_size = 34
t.actor.text_property.color = fg_color
# have to find out text width to center it. Tricky, but works fine. Thanks to Prabhu.
t.width = 1.0*t.actor.mapper.get_width(t.scene.renderer)/t.scene.renderer.size[0]
height = 1.0*t.actor.mapper.get_height(t.scene.renderer)/t.scene.renderer.size[1]
t.x_position = 0.5-t.width/2
t.y_position = 1-height

basic_title.png

Now, we will present how to set up the color bar, called "scalar" or "vector" color bar. It depends of the data you have in your file.

Setting up color bar

Strictly speaking, the color bar is not a module, i.e. you don't need to add it with an add_module() command: you have to associate the "module_manager" object to a module, previously loaded, say the Text module for example.

Then, you can configure the color bar as follows (keywords are self-explanatory):

mmsclut = t.module_manager.scalar_lut_manager
mmsclut.show_scalar_bar = True
mmsclutsc = mmsclut.scalar_bar
mmsclutsc.orientation = "vertical"    # or "horizontal"
mmsclutsc.width = 0.1
mmsclutsc.height = 0.8
mmsclutsc.position = (0.01, 0.15)     # color bar located to the left of the rendering window
mmsclutsc.label_text_property.color = fg_color
mmsclutsc.title_text_property.color = fg_color
mmsclut.number_of_labels = 10
mmsclut.number_of_colors = 64
mmsclut.data_name = "My Label"

basic_colorbar.png

Note: To configure a color bar for vectors instead of scalars, replace "scalar_lut_manager" by "vector_lut_manager" above.

At last, to close the "basic" modules section, let's see how we can setting up the scene.

Setting up the scene

By "setting up the scene", you have to read "how the scene will be seen": for example, setting the color background and the point of view of the scene.

As usual, setting these parameters using python & TVTK is very easy.

If you want to change background color, you may need to also change foreground color for all modules. We recall them here.

   1 #! /usr/bin/env python
   2 
   3 from enthought.mayavi.modules.outline import Outline
   4 from enthought.mayavi.modules.axes import Axes
   5 from enthought.mayavi.modules.orientation_axes import OrientationAxes
   6 from enthought.mayavi.modules.text import Text
   7 
   8 # we want a dark foreground color on a bright background
   9 fg_color = (0.06666, 0.06666, 0.1804)   # dark blue
  10 bg_color = (1, 1, 0.94118)              # ivory
  11 
  12 # setting foreground color for Outline module
  13 o = Outline()
  14 script.add_module(o)
  15 o.actor.property.color = fg_color
  16 
  17 # setting foreground color for Axes module
  18 a = Axes()
  19 script.add_module(a)
  20 a.axes.property.color = fg_color                     # color for axes
  21 a.axes.axis_title_text_property.color = fg_color     # color for axes label
  22 a.axes.x_label = "Lx"                                # label for Ox axis
  23 a.axes.y_label = "Ly"                                # label for Oy axis
  24 a.axes.z_label = "Lz"                                # label for Oz axis
  25 a.axes.label_format = ""                             # no dimensions displayed
  26 
  27 
  28 # setting foreground color for OrientationAxes module
  29 oa = OrientationAxes()
  30 script.add_module(oa)
  31 oa.text_property.color = fg_color
  32 
  33 # setting foreground color for Text module
  34 t = Text()
  35 t.text = "My Title Centered at the Top"
  36 script.add_module(t)
  37 t.actor.scaled_text = False
  38 t.actor.text_property.font_size = 34
  39 t.actor.text_property.color = fg_color
  40 t.width = 1.0*t.actor.mapper.get_width(t.scene.renderer)/t.scene.renderer.size[0]
  41 height = 1.0*t.actor.mapper.get_height(t.scene.renderer)/t.scene.renderer.size[1]
  42 t.x_position = 0.5-t.width/2
  43 t.y_position = 1-height
  44 
  45 # setting foreground color for labels and title color bar.
  46 mmsclut = t.module_manager.scalar_lut_manager
  47 mmsclut.show_scalar_bar = True
  48 mmsclutsc = mmsclut.scalar_bar
  49 mmsclutsc.orientation = "vertical"
  50 mmsclutsc.width = 0.1
  51 mmsclutsc.height = 0.8
  52 mmsclutsc.position = (0.01, 0.15)
  53 mmsclutsc.label_text_property.color = fg_color
  54 mmsclutsc.title_text_property.color = fg_color
  55 mmsclut.number_of_labels = 10
  56 mmsclut.number_of_colors = 64
  57 mmsclut.data_name = "My Label"
  58 
  59 # setting background color for the scene.
  60 t.scene.background = bg_color

Some points of view are also predefined in MayaVi2.

If you want:

You can also:

with:

t.scene.x_plus_view()
t.scene.camera.azimuth(62)
t.scene.camera.elevation(19.5)
t.scene.camera.zoom(1.5)

At last, you can choose if you want a perspective view or a parallel projection for your scene:

t.scene.camera.parallel_projection = True

basic_scene_parall.png

for a parallel projection, or:

t.scene.camera.parallel_projection = False

basic_scene_persp.png

for a perspective view.

Here, "t" stands for the Text module previously loaded.

Note: There are a lot of others parameters you can set up for your scene. See Cookbook/MayaVi/Tips to read how to get more information about setting parameters modules.

Now, it's time to read the most interesting part: configuring and using modules and filters which interact with your data.


CategoryCookbook

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