Jmol is an open-source Java viewer for chemical structures in 3D. It is a useful tool for computational material scientists, chemists, and biologists to visualize and analyze molecular structures. Jmol allows users to script and automate tasks, enabling the creation of molecular animations and movies (videos/gifs). In this blog post, I will show how to use a Jmol script to take snapshots of molecular structures and stitch them into a video using FFmpeg.
Our end goal is to create animations that look something like the following:
Prerequisites
The tutorial assumes that you have Jmol and FFmpeg already installed. If you don’t have them then check out the following resources:
- How to install Jmol on Ubuntu
- Jmol official site
- How to install FFmpeg on Windows, Linux and MacOS
Required Inputs
To create the molecular animation, you will need:
- Molecular geometry files (XYZ, CIF, MOL, CUB or other Jmol supported format) to be used to create the animation
- Jmol script to load and take snapshots of the structures and also create a GIF
- Image files (PNG format) output from the Jmol script to create the video/movie
The geometry files should ideally be stored in a separate directory and also be named in a serial manner, such as geom_1.xyz
, geom_2.xyz
, and so on. The geometry files could be from a geometry optimization calculation or they could be demonstrating a reaction, or even show the electron density dynamics from an RT-TDDFT calculation. The Jmol script will iterate through all these files with the given naming pattern and save the snapshots of the structure as image files img_1.png
, img_2.png
, etc.
As an example, I have provided the geometry files here –> Geometry_Files_for_animation. You can download them and extract them and follow the next steps.
Now that we have the geometry files, we need to open Jmol
and run the Jmol
script given below.
Jmol Script
Launch Jmol.jar
. Click on File>Script Editor
.
Paste the following Jmol
script in the script editor window. You will need to make a few changes to the script. I will explain them below the script.
#no data# n=53 # Set animation FPS for GIF animation fps 2 # Set the path for saving the GIF capture transparent "file:///C:/Users/manas/Downloads/GeomOptXYZs/animation.gif" # Loop over the geometry files for (var i = 1; i <= n; i++) { # Path of geometry files add= "file:///C:/Users/manas/Downloads/GeomOptXYZs/geom_"+i+".xyz" print add load @add moveto 0 1 0 0 -90; #Rotate the molecule 90 degrees about the x-axis instantaneously var PNGaddress="file:///C:/Users/manas/Downloads/GeomOptXYZs/img_"+i+".png" print PNGaddress write @PNGaddress } capture end
Changes required for your usage
The above script will load geom_1.xyz
through geom53.xyz
and save snapshots as img_1.png
through img_53.png
. However, this won’t work on your computer because the paths/addresses of the files would be different.
Therefore, you will have to modify the following line to reflect the path where you would want your GIF to be stored.
capture transparent "file:///C:/Users/manas/Downloads/GeomOptXYZs/animation.gif"
Remember, the prefix file:///
is needed irrespective of the path or the operating system.
You will also need to modify the path to the geometry files. If you’re using the files from this tutorial then they will probably be in the Downloads directory.
add= "file:///C:/Users/manas/Downloads/GeomOptXYZs/geom_"+i+".xyz"
Change the above path based on your own custom usage.
Similarly, also change the path for the images that are to be saved.
var PNGaddress="file:///C:/Users/manas/Downloads/GeomOptXYZs/img_"+i+".png"
Finally, you will also need to change the number of the geometry files, depending on how many files you have for your animation. In our case it was 53.
Change the second line of the script depending on your usage:
#no of geometry files# n=53
I also used a command to rotate the molecule about the x-axis by 90 degrees. This may not be needed for your animations. So you can comment it out or remove it.
Running the script
Once you have made the necessary modification, you can run the script by clicking on the run button in the script editor window.
.
Output
After the script is finished running, you will see the animation.gif
and image files in the desired directory.
The resulting GIF is the same as the Benzene GIF you saw in the beginning of this post.
For your own custom usage, you can modify the above script as per your needs. You can modify the loop bounds and filename formats as needed. More Jmol commands can be added to control the molecular display, add labels, change styles, etc.
To summarize, the above Jmol script will:
- Loop through all geometry files
- Load each geometry file and take a snapshot of the molecular structur
- Save the snapshot as an image with the serial naming convention
- Also create a GIF animation.
Creating the Video with FFmpeg
Jmol generated the GIF file through the above script, but it cannot make a video file in say .MP4 format. For that we will have to use another utility like FFmpeg.
FFmpeg is a command-line utility to convert multimedia files. To create the video from the image files, use the following FFmpeg command:
ffmpeg -f image2 -framerate 8 -i 'img_%d.png' -vf "crop=trunc(iw/2)*2:trunc(ih/2)*2" -c:v libx264 -r 8 -pix_fmt yuv420p molecular_animation.mp4
This will:
- Take the images with
img_
naming pattern as input (-i 'img_%d.png'
) - Use the
libx264
codec and set frame rate to 30 fps (-c:v libx264 -r 30
) - Convert the pixel format to
yuv420p
for compatibility (-pix_fmt yuv420p
) - Save the output as
molecular_animation.mp4
Output
This is the movie that I created
The final video can be optimized and effects can be added using additional FFmpeg parameters.
Conclusion
In summary, you can create molecular animations by:
- Using a Jmol script to load molecular structures and take snapshots
- Saving the snapshots as serially-named image files
- Stitching the images into a video with FFmpeg
This method can be used to make instructional animations to demonstrate structural transformations or other features. The Jmol script and FFmpeg commands can be customized to achieve different effects and styles. Additional resources on Jmol scripting and FFmpeg options can be found online.
References
- Jmol Wiki – Creating Movies
- Relaxation movies with Jmol. | Parkhill Group (nd.edu)
- (1) Animations with Jmol – YouTube
- Jmol script for the computational chemistry movie (github.com)
- Jmol to mpeg tutorial (geocities.ws)
- Molecular Modeling Basics: The Computational Chemistry Movie
I’m a physicist specializing in computational material science with a PhD in Physics from Friedrich-Schiller University Jena, Germany. I write efficient codes for simulating light-matter interactions at atomic scales. I like to develop Physics, DFT, and Machine Learning related apps and software from time to time. Can code in most of the popular languages. I like to share my knowledge in Physics and applications using this Blog and a YouTube channel.