Python Pictures and Movies

From KitwarePublic
Jump to navigationJump to search

Saving an Image

If you would like to save an image of the current view then all you need to do is:


The documentation lists more arguments dealing with choosing a view and setting different writers.

WriteImage(filename, view=None, **params)

   Saves the given view (or the active one if none is given) as an
   image. Optionally, you can specify the writer and the magnification
   using the Writer and Magnification named arguments. For example:
    WriteImage("foo.mypng", aview, Writer=vtkPNGWriter, Magnification=2)
   If no writer is provided, the type is determined from the file extension.
   Currently supported extensions are png, bmp, ppm, tif, tiff, jpg and jpeg.
   The writer is a VTK class that is capable of writing images.
   Magnification is used to determine the size of the written image. The size
   is obtained by multiplying the size of the view with the magnification.
   Rendering may be done using tiling to obtain the correct size without
   resizing the view.

Note that most standard image formats are supported and as long as you specific a valid file extension should work.


Note that this section is derived from Python Scripting so you should check there for the latest updates.

Dealing with Time

If a reader or a filter supports time, it is easy to request a certain time step from Python. All time requests are set on views, which then propagate them to the representations which then propagate them to the visualization pipeline. Here is an example demonstrating how a time request can be made.

>>> Show(ExodusIIReader(FileName=".../can.ex2"))
>>> Render()
# Get a nice view angle
>>> cam = GetActiveCamera()
>>> cam.Elevation(45)
>>> Render()
# Check the current view time
>>> view = GetActiveView()
>>> view.ViewTime
>>> reader = GetActiveSource()
>>> reader.TimestepValues
[0.0, 0.00010007373930420727, 0.00019990510190837085, 
0.00029996439116075635, 0.00040008654468692839, 
>>> tsteps = reader.TimestepValues
# Let’s be fancy and use a time annotation filter. This will show the
# current time value of the reader as text in the corner of the view.
>>> annTime = AnnotateTimeFilter(reader)
# Show the filter
>>> Show(annTime)
# Look at a few time steps. Note that the time value is requested not
# the time step index.
>>> view.ViewTime = tsteps[2]
>>> Render()
>>> view.ViewTime = tsteps[4]
>>> Render()


Server Manager has a complicated animation engine based on keyframes and scenes. This section will introduce a few simple ways of animating your visualization. If you have a time-aware reader, you can animate it with AnimateReader().

>>> reader = ExodusIIReader(.../can.ex2)
>>> Show(reader)
>>> Render()
>>> c = GetActiveCamera()
>>> c.Elevation(95)
# Animate over all time steps. Note that we are not passing the optional
# 3rd argument here. If you pass a filename as the 3rd argument, 
# AnimateReader will create a movie.
>>> AnimateReader(reader)
# Save the animation to an avi file
>>> AnimateReader(reader, filename=".../movie.avi")

Generating an avi may or may not be possible on linux. If you want to generate a series of images you need to specify a name like "movie.png" or "movie.jpg" and a series of numbered images will be generated.

Animating with keyframes

To animate properties other than time, you can use regular keyframes.

>>> Sphere()
>>> Show()
>>> Render()

# Create an animation scene
>>> scene = servermanager.animation.AnimationScene()
# Add one view
>>> scene.ViewModules = [GetActiveView()]

# Create a cue to animate the StartTheta property
>>> cue = servermanager.animation.KeyFrameAnimationCue()
>>> cue.AnimatedProxy = GetActiveSource()
>>> cue.AnimatedPropertyName = "StartTheta"
# Add it to the scene's cues
>>> scene.Cues = [cue]

# Create 2 keyframes for the StartTheta track
>>> keyf0 = servermanager.animation.CompositeKeyFrame()
>>> keyf0.Interpolation = 'Ramp'
# At time = 0, value = 0
>>> keyf0.KeyTime = 0
>>> keyf0.KeyValues= [0]

>>> keyf1 = servermanager.animation.CompositeKeyFrame()
# At time = 1.0, value = 200
>>> keyf1.KeyTime = 1.0
>>> keyf1.KeyValues= [200]

# Add keyframes.
>>> cue.KeyFrames = [keyf0, keyf1]

>>> scene.Play()

# Some properties you can change
# Number of frames used in Sequence mode
# scene.NumberOfFrames = 100
# Or you can use real time mode
# scene.PlayMode = 'Real Time'
# scene.Duration = 20


Sandia is a multiprogram laboratory operated by Sandia Corporation, a Lockheed Martin Company, for the United States Department of Energy’s National Nuclear Security Administration under contract DE-AC04-94AL85000.