This page is not the main source of documentation. You are invited to refer to the Mayavi2 home page for up-to-date documentation on TVTK. In particular, bear in mind that if you are looking for a high-level Python 3D plotting library, Mayavi also provides the right API, and can be embedded (see the user guide). |
What is tvtk?
tvtk is a traits enabled version of VTK for 3D graphics and visualization.
It provides an exact match to the VTK objects, but with a Pythonic feel, unlike Mayavi which aims to provide new APIs
Most important features are
- All VTK classes are wrapped.
- Support for traits.
- Elementary pickle support.
- Pythonic feel.
- Handles numpy/Numeric/numarray arrays/Python lists transparently.
- Support for a pipeline browser, ivtk
- High-level mlab module.
- Envisage plugins for a tvtk scene and the pipeline browser.
- MayaVi2 is built on top of tvtk
See the enthought TVTK page for more details, in particular the tvtk introduction.
tvtk examples
cone
The following example displays a cone which can be rotated/scaled/... with the mouse.
1 from enthought.tvtk.api import tvtk
2 cs = tvtk.ConeSource(resolution=100)
3 mapper = tvtk.PolyDataMapper(input=cs.output)
4 actor = tvtk.Actor(mapper=mapper)
5
6 # create a renderer:
7 renderer = tvtk.Renderer()
8 # create a render window and hand it the renderer:
9 render_window = tvtk.RenderWindow(size=(400,400))
10 render_window.add_renderer(renderer)
11
12 # create interactor and hand it the render window
13 # This handles mouse interaction with window.
14 interactor = tvtk.RenderWindowInteractor(render_window=render_window)
15 renderer.add_actor(actor)
16 interactor.initialize()
17 interactor.start()
quadrics
A more interesting example is the generation of some contour surfaces of an implicit function.
1 #!/usr/bin/env python
2 """
3 This example demonstrates the use of the contour filter, and the use of
4 the vtkSampleFunction to generate a volume of data samples from an
5 implicit function.
6
7 Conversion of VisQuad.py of the VTK example to tvtk
8 (and some additions).
9 """
10
11 from enthought.tvtk.api import tvtk
12
13 # General form for a quadric to create an elliptical data field.
14 quadric = tvtk.Quadric(coefficients=(.5, 1, .2, 0, .1, 0, 0, .2, 0, 0))
15
16 # Sample implicit function over a specified x-y-z range.
17 # (here it defaults to -1,1 in the x,y,z directions).
18 sample = tvtk.SampleFunction(implicit_function=quadric,
19 sample_dimensions=(30, 30, 30))
20
21 # Create five surfaces F(x,y,z) = constant between range specified:
22 contours = tvtk.ContourFilter(input=sample.output)
23 contours.generate_values(5, 0.0, 1.2)
24
25 contMapper = tvtk.PolyDataMapper(input=contours.output, scalar_range=(0.0, 1.2))
26 contActor = tvtk.Actor(mapper=contMapper)
27
28 # outline around the data.
29 outline = tvtk.OutlineFilter(input=sample.output)
30 outlineMapper = tvtk.PolyDataMapper(input=outline.output)
31 outlineActor = tvtk.Actor(mapper=outlineMapper)
32 outlineActor.property.color=(0, 0, 0)
33
34 # The usual rendering stuff.
35 ren = tvtk.Renderer(background=(0.95, 0.95, 1.0))
36 renWin = tvtk.RenderWindow()
37 renWin.add_renderer(ren)
38 iren = tvtk.RenderWindowInteractor(render_window=renWin)
39 ren.add_actor(contActor)
40 ren.add_actor(outlineActor)
41
42 # some nice view:
43 ren.active_camera.elevation(10)
44
45 iren.initialize()
46 renWin.render()
47
48 # save scene as png:
49 renderLarge = tvtk.RenderLargeImage(input=ren, magnification=1)
50 writer = tvtk.PNGWriter(input=renderLarge.output, file_name="vis_quad.png")
51 writer.write()
52
53 # interactive part starts here:
54 iren.start()
This displays the following scene on screen and also saves it to a file.
ivtk
The module tools.ivtk makes VTK/TVTK easier to use from the Python interpreter. For example, start IPython with:
ipython -wthread
(if you have both wxPython 2.4 and wxPython 2.6 installed you will need a recent IPython and do ipython -wthread -wxversion 2.6).
Then you can paste the following lines:
1 from enthought.tvtk.tools import ivtk
2 from enthought.tvtk.api import tvtk
3 # Create a cone:
4 cs = tvtk.ConeSource(resolution=100)
5 mapper = tvtk.PolyDataMapper(input=cs.output)
6 actor = tvtk.Actor(mapper=mapper)
7
8 # Now create the viewer:
9 v = ivtk.IVTKWithCrustAndBrowser(size=(600,600))
10 v.open()
11 v.scene.add_actors(actor) # or v.scene.add_actor(a)
You can then explore the visualization pipeline and modify any settings.
For creating the viewer there are different options:
- v = ivtk.viewer() - this one does not need the v.open() and the size=(600,600)
- v = ivtk.IVTK()
- v = ivtk.IVTKWithCrust()
- v = ivtk.IVTKWithBrowser()
- v = ivtk.IVTKWithCrustAndBrowser)
For viewers with Crust you can use the python command line window to modify the pipeline.

