Capturing VPython Animations
One of the Frequently Asked Questions on the VPython documenation website is: "Is there a way to capture VPython graphical output as a movie?" The answer is "yes," and this webpage is a description of a simple and free method for Linux users that requires no new software not already included in many standard Linux distributions.
- Working VPython installation
- ImageMagick suite of graphics tools, in particular the
utilities
import
andconvert
(part of most distributions). - The standard linux utility
xwininfo
.
The basic idea is to capture the contents of the VPython window
once each animation loop, and write a set of graphics files with an
ordered set of names -- something like vp0001.gif, vp0002.gif, ...
This requires system call to the import
command.
This set of frames can then be turned into an animated gif that
can be viewed on just about any browser using convert
.
- For convenience I create a directory under the working directory in which to store the images, e.g, "/frames."
- In the VPython code, import the
os
module. - In the VPython code, create an iterator, say
i
, that is incremented every time through the animation loop. Within the animation loop add a line something like the following:
os.open(import -window 0x3a00003 frames/vp'+str(i).zfill(4)+'.gif)
This is a system call to the linuximport
utility, once every time through the loop, that captures the VPython window and writes it as a gif in the "frames" directory, with the iteratori
as part of the filename, resulting in the ordered files vp0001.gif, vp0002.gif, etc.You can determine the window identifier for the VPython display, in this case, 0x3a00003, by running
xwininfo
while a VPython animation is running. After executingxwininfo
, click on the animation window to see all the information about the window.NOTE: You may have slow down the VPyhthon image to allow time for each
import
command to complete. (The actual speed of the captured animation will depend on the delay chosen in the next step.)- Create an animated gif from the captured frames:
convert -delay 50 -loop 0 frames/vp*.gif output.gif
This can be displayed in just about any browser, as above. - Movies in other formats can be generated from the captured
images using
ffmpeg
.
This page maintained by
Marty Ligare,
mligare@bucknell.edu
