Capturing VPython Animations
One of the Frequently Asked Questions on the VPython documentation website is:
"Is there a way to capture VPython graphical output as a movie?"
The answer is "yes," and this web-page is a description of a simple and free method for Linux users that requires no new software beyond that already included in many standard Linux installations.
- Working VPython installation
- ImageMagick suite of graphics tools, in particular the
convert(part of most installations, and available in all major distributions).
- The standard Linux utility
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, ...
Each capture requires a call to the
convert utility is then used to turn this set of
frames into an animated gif that can be viewed on just about
- 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
- 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.popen('import -window 0x3a00003 frames/vp'+str(i).zfill(4)+'.gif')
importutility, once every time through the loop. This captures the VPython window and writes it as a gif in the "frames" directory, with the iterator
ias part of the filename. The result is the ordered set of files vp0001.gif, vp0002.gif, etc.
You can determine the window identifier for the VPython display, in this case, 0x3a00003, by running
xwininfowhile a VPython animation is running. After executing
xwininfo, click on the animation window to see all the information about the window. (There is a suggestion for automating this process here.)
NOTE: You may have to slow down the VPython animation to allow time for each
importcommand 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 using the command line:
convert -delay 50 -loop 0 frames/vp*.gif output.gif
- Movies in other formats can be generated from the captured
Integrating VPython Animation into LaTeX Beamer Presentations
Briefly, save the VPython images in
png format and use
animate package. (The file extension used
in the filenames in the
import command determines the
format.) More details coming soon.
This page maintained by
Marty Ligare, email@example.com