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.

VPython output

Requirements:

  • Working VPython installation
  • ImageMagick suite of graphics tools, in particular the utilities import and convert (part of most installations, and available in all major 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, ... Each capture requires a call to the import command. The convert utility is then used to turn this set of frames into an animated gif that can be viewed on just about any browser.

Details

  • 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 that imports the canvas. In older versions of VPython you can simply specify the graphics window with a command something like

    os.popen('import -window 0x3a00003 frames/vp'+str(i).zfill(4)+'.gif')

    This is a system call to the linux import utility, once every time through the loop. This captures the VPython window and writes it as a gif in the "frames" directory, with the iterator i as 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 xwininfo while 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 import command to complete. (The actual speed of the captured animation will depend on the delay chosen in the next step.)

    If you are using VPython 7 from within a Jupyter notebook things are a little trickier, because the window you identify using xwininfo is the entire browswer window, not just the VPython graphics. With a little trial-and-error fiddling, you can identify the region of the broswer window you want to capture, and include this information in the import command, using something like

    os.popen('import -window 0x3a00003 -crop x{+-}{+-} frames/vp'+str(i).zfill(4)+'.gif')

    (See ImageMagick documentation.)

  • Create an animated gif from the captured frames using the command line:

    convert -delay 50 -loop 0 frames/vp*.gif output.gif

    This can be displayed in just about any browser; see above for an example.
  • Movies in other formats can be generated from the captured images using ffmpeg.

Integrating VPython Animation into LaTeX Beamer Presentations

Briefly, save the VPython images in png format and use the Beamer 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, mligare@bucknell.edu

Valid HTML 4.0!