Python Pictures and Movies

From KitwarePublic
Revision as of 20:05, 2 September 2009 by Tmjung (talk | contribs) (New page: =Saving an Image= If you would like to save an image of the current view then all you need to do is: <source lang="python"> WriteImage("/path/to/image.png") </source> The documentation li...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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: <source lang="python"> WriteImage("/path/to/image.png") </source>

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.

Animation

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.

<source lang="python"> >>> Show(ExodusIIReader(FileName=".../can.ex2")) >>> Render()

  1. Get a nice view angle

>>> cam = GetActiveCamera() >>> cam.Elevation(45) >>> Render()

  1. Check the current view time

>>> view = GetActiveView() >>> view.ViewTime 0.0 >>> reader = GetActiveSource() >>> reader.TimestepValues [0.0, 0.00010007373930420727, 0.00019990510190837085, 0.00029996439116075635, 0.00040008654468692839, ...] >>> tsteps = reader.TimestepValues

  1. Let’s be fancy and use a time annotation filter. This will show the
  2. current time value of the reader as text in the corner of the view.

>>> annTime = AnnotateTimeFilter(reader)

  1. Show the filter

>>> Show(annTime)

  1. Look at a few time steps. Note that the time value is requested not
  2. the time step index.

>>> view.ViewTime = tsteps[2] >>> Render() >>> view.ViewTime = tsteps[4] >>> Render() </source>

Animating

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().

<source lang="python"> >>> reader = ExodusIIReader(“.../can.ex2”) >>> Show(reader) >>> Render() >>> c = GetActiveCamera() >>> c.Elevation(95)

  1. Animate over all time steps. Note that we are not passing the optional
  2. 3rd argument here. If you pass a filename as the 3rd argument,
  3. AnimateReader will create a movie.

>>> AnimateReader(reader)

  1. Save the animation to an avi file

>>> AnimateReader(reader, filename=".../movie.avi") </source>

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.

<source lang="python"> >>> Sphere() >>> Show() >>> Render()

  1. Create an animation scene

>>> scene = servermanager.animation.AnimationScene()

  1. Add one view

>>> scene.ViewModules = [GetActiveView()]

  1. Create a cue to animate the StartTheta property

>>> cue = servermanager.animation.KeyFrameAnimationCue() >>> cue.AnimatedProxy = GetActiveSource() >>> cue.AnimatedPropertyName = "StartTheta"

  1. Add it to the scene's cues

>>> scene.Cues = [cue]

  1. Create 2 keyframes for the StartTheta track

>>> keyf0 = servermanager.animation.CompositeKeyFrame() >>> keyf0.Interpolation = 'Ramp'

  1. At time = 0, value = 0

>>> keyf0.KeyTime = 0 >>> keyf0.KeyValues= [0]

>>> keyf1 = servermanager.animation.CompositeKeyFrame()

  1. At time = 1.0, value = 200

>>> keyf1.KeyTime = 1.0 >>> keyf1.KeyValues= [200]

  1. Add keyframes.

>>> cue.KeyFrames = [keyf0, keyf1]

>>> scene.Play()

  1. Some properties you can change
  2. Number of frames used in Sequence mode
  3. scene.NumberOfFrames = 100
  4. Or you can use real time mode
  5. scene.PlayMode = 'Real Time'
  6. scene.Duration = 20

</source>

Acknowledgements

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.