Creating Movies

From Jmol

Jump to: navigation, search
Jmol / JSmol Tutorials


This section is about making true movies. A true movie is a series of images played in sequence. Unlike a "live" animation in Jmol, if all you have is the movie, you cannot use the mouse to rotate the molecule to watch the movie from different perspectives, nor can you change the color scheme or rendering of the molecule.

There are several possible advantages to true movies. Jmol may be unable to render large complex animations fast enough to avoid a slow, jerky appearance. Transparency and large spheres, especially at high quality (set antialiasdisplay on) are some of the renderings that are slow. Capturing Jmol's renderings as a movie enables much smoother playing rates, e.g. 24 frames per second. Another advantage is that the movie can be shared as a single file, and kept private, in situations where one does not want the animation on a public website, and when the recipient is not familiar with using the stand-alone Jmol application.

Jmol can create a series of jpg, png, or gif images. There is currently no menu option to do this, but a small bit of scripting can do the trick. It is possible to get the movie output from either one frame at several angles or from a series of frames. The images can then be assembled into a single file that will play as a movie.

Contents

Image/Movie Size

Normally you simply adjust the size of the Jmol application window to the desired size before writing the image files. If you want an exact size, such as 640 x 480 pixels, you can set that easily. Open Jmol's Display menu and select Resize. If you want an image larger than you can achieve by resizing your window using a mouse, it can be set using the Display, Resize method. You may not be able to see the entire Jmol window on your screen if you set a large size, but the images will be saved at the desired size (for example 1024 x 768 pixels).

Images from trajectories

Trajectories read in as multiple files

If the trajectory is a series of files read in as 1.0, 2.0, 3.0, etc., the following will work:

 write frames {*} 800 600 "all.jpg" 

Trajectories from a single file

Often, the trajectory is one big file with consecutive frames. Read it in with e.g.:

 load trajectory "filename.xyz" 

create a script "trajectorymovie.jmol"

 frame 1
 num_frames = getProperty("modelInfo.modelCount")
 for (var i = 1; i <= num_frames; i = i+1)
   var filename = "movie"+("00000"+i)[-4][0]+".jpg"
   write IMAGE 800 600 JPG @filename
   frame next
 end for

then perform

 script "trajectorymovie.jmol"

Images from a script

Example I

Here is a loop that will generate a series of jpg images for a 360 degree rotation in 1 degree increments. The jpg files are named m1.jpg, m2.jpg, ..., m360.jpg. This script fragment does not include the commands needed to color, render and orient the molecule as you wish. See below for more on that. See the section below on Windows Media format for further explanation of the "extra frames".

# goal: a 360 degree spin 

~degstep = 1 # or 2, degrees per step
~degrees = 0 # total degrees rotated

while (~degrees < 360)
{
	# SPIN
	rotate y @~degstep
	~degrees += ~degstep
	
	# DISPLAY/WRITE FRAME
	refresh
	
	~whereami = "degrees = " + ~degrees 
	print ~whereami
	
	~fileprefix = "jpg-frames/"

	# UNCOMMENT TO GENERATE EXTRA FRAMES REQUIRED FOR WMV. COMMENT OUT FOR MP4.
	~degreesplus = 1000 + ~degrees
	~towrite = ~fileprefix + "m" + ~degreesplus + ".jpg"

	# UNCOMMENT TO GENERATE FRAMES FOR MP4 (AND WMV). COMMENT OUT TO GENERATE EXTRA FRAMES FOR WMV.
	#~towrite = ~fileprefix + "m" + ~degrees + ".jpg"
	
        # WRITE THE IMAGE
	write image jpg @~towrite
}

Example II (e.g. in pre-11.7 Jmol)

The main part of the script is given here: based on a mail from Bob Hanson on the mailing list. If you want to rotate a single molecule, you need to uncomment the respective 'rotate' commands. If you want to loop over the timeframes in your coordinate file, you only need to change the 'nFrames' variable to the amount of frames you are currently using (this is given in the title of the main Jmol window). You might also want to change the width and height parameters to create a bigger or smaller animation.

name = "./frame0000.jpg";
nFrames = 125;
nDegrees = 1;
thisFrame = 0;
width = 640;
height = 480;

set zoomLarge false;

message loop;
thisFrame = thisFrame + 1;
fileName = name.replace("0000","" + ("0000" + thisFrame)[-4][0]);
#rotate x @nDegrees;  # use these options if you want to rotate the molecule
#rotate y @nDegrees;
#rotate z @nDegrees;
frame next; # only use this if you have a multiframe file.
refresh;
write image @width @height @fileName;
if (thisFrame < nFrames);goto loop;endif;
background black;

Save this text in a file, for example 'movieloop.jmol'.

Example III

This uses the same principles, slightly different (though equivalent) code and is set for the rotation of the model in a single frame. In addition, it exports to gif format (only available in recent 11.7 versions) for later making an animated gif file outside Jmol:

load whatever_file.mol
color background [xD2DFEF]
/*
  add any other rendering commands
*/
for (var i=0; i<36; i=i+1)
  write image 200 200 @{"movie" + ("0000" + i)[-3][0] + ".gif"}
    /* 200 and 200 are width and height */
  rotate axisangle {1 1 0} 10
    /* axis is defined by X Y Z lengths between braces; this one is at 45 degrees
      and 10 (degrees) is angle of rotation, so the 36-loop gives a full turn
    */
end for

Viewstate

Now you still need to create a viewstate for the initial display. To do this, load the system coordinates you are interested in, rotate and change background colors until the picture looks as you want it. This can be saved into a file with the command:

write state "moviestate.jmol"

A state file looks something like this (only some of the important parts are shown):

...
load "CoCr_125frames.XYZ";  # This part is essential!
...
cameraDepth = 3.0;
center {6.424099 3.137966 4.133584};
moveto /* time, axisAngle */ 0.0 { -704 -672 -228 173.73} /* zoom, translation */  62.1
2 0.0 0.06  /* center, rotationRadius */ {6.424099 3.137966 4.133584} 9.94055;;
slab 100;depth 0;
...


Creating images (linux)

  • Linux console:

To create the animation, make a file with first the viewstate and then the loop, for example by:

cat moviestate.jmol movieloop.jmol > movie.jmol

jmol movie.jmol
  • Jmol console (any OS):

Start Jmol, open the script console, and type:

script moviestate.jmol 
script movieloop.jmol 

Jmol will start, and show you the animation, while it is writing the output files.

Converting images to a playable movie

Apple Mac OS X

Quicktime Pro is an easy and inexpensive program that can assemble a series of jpg files into movies in various formats (mp4, avi, wmv, etc.). The free version of Quicktime does not do this job -- you will need to buy a license for the Pro version.

Even if you are using Lion (which comes with Quicktime 10), you should install Quicktime 7. It is easy to find many complaints about Quicktime 10 online. In contrast, using Quicktime 7 is straightforward and it performs well. If your OS X came with Quicktime 10, you can also install Quicktime 7 and use either one that you choose for a particular use.

Assembling the Movie

Creating a Looping Movie

Quicktime has an option, under its View menu, to Loop a move (play it over and over), or to alternate forwards and backwards playing. However, none of the export formats appear to create a movie file that loops automatically when played back in Quicktime.

Loading the Image Files

You must create, with Jmol, a series of sequentially numbered jpg image files, all in the same directory. For example m1.jpg, m2.jpg, ... m360.jpg for a 360 degree rotation in one-degree steps. In Quicktime, open the File menu, and select Open Image Sequence .... Select only the FIRST jpg file in the series (or actually any file in the series). Quicktime does not allow you to block select the range of files to be included in the movie. Therefore the directory containing the first file must contain exactly the files you wish to include -- you cannot select a subset of those files.

Once the series of image files loads, you will be asked to select a frame rate, for example 10, 15, or 24 frames/second. Then you can play the movie (but it has not yet been saved).

Saving the Movie

Open the File menu and select Export. In the Export: menu, pick an export format such as MPEG-4 (mp4), AVI (avi), or Windows Media (wmv). (See more about each format below.) After you have set the correct Options (see below), click the Save button and your movie file will be created.

Note by User:EricMartz, January, 2012: I concluded that it was necessary to produce each movie in two formats, wmv for Windows, and mp4 for Mac OS X. See more about this below.

AVI

AVI format will run in Windows Media Player, and in Mac OS X, in Quicktime (and in web browsers in the Quicktime plugin).

Note by User:EricMartz January 2012: I was unable to discover any combination of the compression and quality options offered in Quicktime Pro that produced an avi movie with acceptable graphics quality. All the movies had image quality far lower than the original jpg files, with very fuzzy or pixelated images. Furthermore, the movie files were very large, 10-70 megabytes, many fold larger than mp4 or wmv movies produced by Quicktime with the same content.

Windows Media (wmv)

Windows Media (wmv) files play in Windows Media Player, available in all Windows systems. These wmv files can also play in Quicktime on Mac OS X, but only if the user installs a free utility, Flip4Mac.

Export to wmv in Quicktime Pro also depends on the Flip4Mac utility. The free version of Flip4Mac is a "demo" that will only do half of your movie, or 30 seconds, whichever is shorter.

If a movie of less than 30 seconds will suffice, you can make it with the free version by doubling the number of images. For example, a 360 degree rotation in 1 degree increments requires 360 jpg image files. If you create 720 files (it doesn't matter what is in the last 360 images), and use a frame rate of 15 frames/second, the free version will give you the first 360 frames in 24 seconds. (At 24 fps, the 360 frames will fit into 15 seconds.)

A wmv movie of size 1024 x 768 pixels, with reasonable file size (4 to 5 megabytes for 360 frames), and little loss of image quality (for a Jmol trace 0.4 rendering), was obtained using the default settings.

MPEG-4

MPEG-4 (mp4) format will play in Quicktime (including free Quicktime) without complications on Macs, either as a stand-alone mp4 file (double click it), or embedded in a web page using the Quicktime browser plugin. Windows users will need to install free Quicktime. Even then sometimes Quicktime fails to associate properly with the mp4 filetype.

An mp4 movie of size 1024 x 768 pixels, with a reasonable file size (1.9 megabytes for 360 frames), and little loss of image quality (for a Jmol trace 0.4 rendering), was obtained with the following options (click the Options button, and use the Video tab):

  • Video format: H.264
  • Data rate: 1024 kbits/sec
  • Optimized for: CD/DVD-ROM
  • Image size: Custom, 1024 x 768
  • Preserve aspect ratio (NOT checked)
  • Frame rate: 24
  • Key frame: Automatic
  • Video options:
    • Restrict profiles to: Main
    • Encoding mode: Best quality (multi-pass)

Windows

Both PhotoScape and GNU Image Modification Program (GIMP) are capable of producing animated looped structural images.

In PhotoScape one needs to open the Animated GIF tab, then click Add and select all the images one wishes to animate. Then, using the Change time option animate your image files and when you are complete click "Save" to save your work.

Whereas, in GIMP you need to go to File->Open as Layers (or Ctrl+Alt+O) and then open the files that you wish to animate; then, export as and give the file name a ".gif" tag and then press "Export". After this you will be prompted with the following box, tick all the fields that are ticked in this box (especially the As animation and loop forever fields). Then click Export.

Linux

Encoding to the mpeg format

The most straightforward way to encode to mpeg2 in Linux systems is "convert". This will create huge files and the needed mpeg2encode might not always be installed.

convert *.jpg movie.gif
convert *.jpg movie.mpg

Alternatively, encode to smaller mpeg4 files with mencoder, which comes along with mplayer.

Many options and codecs are available, the default settings often create images that are too grainy due to the high image compression. Some testing by Mario Valle at CSCS gave optimal mencoder options for a movie of atomistic simulations. To have it run on default Windows installations (e.g. for use in Powerpoint), also use the msmpeg4v2 codec with autoaspect. The optimal combination of mencoder options then becomes:

mencoder "mf://*.jpg" -o movie.avi -ovc lavc -lavcopts vcodec=msmpeg4v2:autoaspect:vbitrate=2160000:mbd=2:keyint=132:vqblur=1.0:cmp=2:subcmp=2:dia=2:mv0:last_pred=3 -fps 8

If the movie has just to be played on linux, one can also just type:

mencoder "mf://*.jpg" -o movie.avi -ovc lavc -fps 8

Fine tune the speed/length of the animation by varying the -fps (frames per second) option.

Encoding to animated gif format

Animated gif, also called multi-gif, is an older format that plays in all major web browsers without installing any special plugins. You will need third-party software to assemble gif images into a multi-gif movie file, e.g. gifsicle. Load into it the full set of gif files generated by Jmol (gif format is only available in recent 11.7 versions of Jmol), choose the time step and the loop options and save into animated gif.

Some Jmol renderings on a black background (e.g. trace 0.4) do not capture well in gif format -- the edges have white pixels, probably because of the 256-color limitation of gif.

Personal tools