Skip to content

Instantly share code, notes, and snippets.

@emleddin
Last active September 14, 2021 01:16
Show Gist options
  • Save emleddin/ef2df30793dc956005bb37376a465967 to your computer and use it in GitHub Desktop.
Save emleddin/ef2df30793dc956005bb37376a465967 to your computer and use it in GitHub Desktop.
A script for making a y-axis rotation GIF in VMD with a transparent background.

Transparent VMD Movies

VMD's Movie Maker is a powerful tool, but modifying a plugin can be a hassle, especially when the program is installed site-wide.

Dependencies

This script relies on installations of

You can check for the latter 2 with:

  • which povray
  • which convert

Depending on the file size of the images, you may need to change the /etc/ImageMagick-6/policy.xml (or whatever version) file Comment out this:

<policy domain="resource" name="disk" value="1GiB"/>

For something like this:

<policy domain="resource" name="disk" value="8GiB"/>

Note: Comments in XML look like:

<!-- THIS IS A COMMENT -->

Usage

  1. Set-up the visualization state you in the video. Some materials may take a really long time or look rough. I suggest doing a test of POV-Ray with the Render command for a static image.

Note: You can access it through File -> Render.

Use this for the Render Command GUI option:

povray +W%w +H%h -I%s -O%s.png -D +X +A +FN +UA
  1. Use Mouse + Center or the C key to pick a central point (maybe a metal???) to rotate about. If you want the central part of a ring, pick the atom above or below the center.

Preparation

Modify the user-level variables for the script.

  • basename: the base for the output file name (ex.: test to later create test.gif
  • imgsavedir: the directory to save the file to.
  • Note: You will need a povray.conf file to specify somewhere other than [pwd] or /tmp/* because of permissions issues!

  • rotateby: integer number of degrees to rotate around the y-axis (e.g., 1)
  • fullrotate: the total integer number of degrees to rotate by (e.g., 360)

Command-Line

Place this script in your working directory and enter source rotate-gif.tcl in the VMD command line.

vmd > source rotate-gif.tcl

Tk Console Extension

You can use the console plugin to read from an existing TCL script.

Extensions -> Tk Console

  • File -> Load File
#!/usr/bin/env tclsh
## Transparent 360 Rotation Gif in VMD
## EML 13 Sept 2021
##
## Modify the `SPECIFY USER VARIABLES` section below!
## Run with `source rotate-gif.tcl` in VMD command line
## Tell the program to expect variables
variable basename
variable basefilename
variable mybasefilename
variable imgsavedir
### !!! SPECIFY USER VARIABLES !!!
## Base for output file name (i.e., use `test` to later create `test.gif`)
set basename "test"
## Save directory -- either use a string or `[pwd]` for current working dir
## Note: if not using the `[pwd]` or `/tmp`, you'll need a `povray.conf` file
#set imgsavedir "/full/path/to/save/dir"
set imgsavedir [pwd]
## Number of degrees to rotate by (integer!)
set rotateby "5"
## Total number of degrees to rotate by (integer!)
set fullrotate "360"
### !!! END USER VARIABLES !!!
#--------------------------------------#
#-- Advanced User Modification Below --#
#--------------------------------------#
## Set the file path using basename and imgsavedir
set mybasefilename [format "%s/$basename" $imgsavedir]
## Use a white background and turn off depthcueing for transparency to render!
color Display Background white
display depthcue off
## Rotation loop: rotate from 0-X degrees by X degree(s) each time
## Note: change both the increment (`incr i 1`) AND rotation `rotate y by 1`
## for different amounts
## Get full rotation distance (counter at 1 so images start naming at 1)
set myfullrotate [expr {$fullrotate + 1}]
for {set i 1} {$i < $myfullrotate} {incr i $rotateby} {
puts {}
puts "Starting to process Frame $i"
rotate y by $rotateby
set basefilename [format "%s/$basename.%04d.pov" $imgsavedir $i ]
## Set the render command
render POV3 $basefilename povray +W%w +H%h -I%s -O%s.png -D +X +A +FN +UA
}
## Other render options
## --------------------
## +W: width `+W%w` uses the current window width
## +H: height. `+H%h` uses the current window height
## +I: the name of the POV file to use. You may need the `.pov`
## extension in $basefilename.
## +O: the name of the final image. If you specify the `.pov` above, it'll
## be something like `.pov.png`.
## -D: switches off the graphics display while being rendered. Using `+D`
## instead means the display will be on!
## +X: make rendering killable
## +FT: write targa file (with `+O%s.tga`)
## +FN: write PNG file (with `+O%s.png`)
## +FJ: write JPG file (with `+O%s.jpg`)
## +FB: write Bitmap file (with `+O%s.bmp`)
## +FP: write PPM format (with `+O%s.ppm`)
## +UA: use background transparency. To use this, save as filetype that
## supports transparency, like PNG.
## The VMD background must be white for this option!!!
## Note: If you need super-high-res images, use `+W%w0 +H%h0` (it WILL be slow
## but very beautiful)
## Note: XXXX is the $basename
##
## TGA -- not transparent
## render POV3 XXXX povray +W%w +H%h -I%s -O%s.tga -D +X +A +FT
##
## JPG -- not transparent
## render POV3 XXXX povray +W%w +H%h -I%s -O%s.jpg -D +X +A +FJ
##
## PNG Format -- yes transparent
## render POV3 XXXX povray +W%w +H%h -I%s -O%s.png -D +X +A +FN +UA
## --------------------
## Print to VMD Terminal
puts {}
puts "Starting GIF conversion with ImageMagick. I'll update you when I'm done."
## Run ImageMagick's convert outside of VMD
## Options:
## -delay: time in 1/100s of a second.
## -layers:
## -dispose: what to do with the previous animation.
## `previous` gets rid of the prior frame!
## Note: This option must be given before the frames are read in.
## -loop: how many times to replay the gif. `0` replays infinitely
::ExecTool::exec convert -delay 4 -layers optimize -dispose previous -loop 0 \
$mybasefilename.*.png $mybasefilename.gif
puts {}
puts "Conversion attempt finished!"
puts "If you ran into issues, try running the command alone in a Terminal."
puts " convert -delay 4 -layers optimize -dispose previous -loop 0 $mybasefilename.*.png $mybasefilename.gif"
puts {}
puts "Note1: I don't delete the intermediate files. I trust you can."
puts {}
puts "Note2: On Linux, it may look like there's 1 frozen frame."
puts "Check this by opening the GIF in an internet browser!"
puts {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment