Random Hacks‎ > ‎

VMD Init Script


There are many ways in which the VMD molecular visualization and analysis software can be customized. Here are a few examples from my personal setup or prepared in collaboration with VMD users, some of which are quite unusual.

Delayed Initialization

Color to element or name or type assignments cannot be changed until the corresponding lists have been initialized. However, the VMD init script (.vmdrc or vmd.rc) is processed fairly early in the startup procedure, so we need have to postpone the changes. The after command in Tcl is the right tool for the job. Here is an example that changes the colors of carbon from its default cyan to a more palatable green.

after idle {
  color Name C green
  color Type C green
  color Name P purple
  color Type P purple

Defining Colors for Any Name

Another side effect of the fact, that color categories are not initialized at startup time, is that one cannot set colors for entries that are not yet known. To work around this problem, we have to "fake" a molecule, which can be easily done with the "mol new atoms " syntax. Here is an example:

after idle {
    # create dummy molecule with one atom
    set mol [mol new atoms 1]
    set sel [atomselect $mol all]
    # add items to color categories
    $sel set name E
    $sel set type E
    $sel set name B
    $sel set type B
    $sel set type F
    $sel set name F
    # now we can define colors
    color Name B pink
    color Name C green
    color Name F lime
    color Type B pink
    color Type C green
    color Type F lime
    # clean up
    $sel delete
    mol delete $mol

Desaturated Colors

VMD by default uses "pure" colors defined in RGB color space, i.e. red is coded as rgb 1.0 0.0 0.0, green as rgb 0.0 1.0 0.0, blue as rgb 0.0 0.0 1.0 and so on). However, this choice can often produce problems when the VMD graphics is converted to a different color space with a different color gamut. For example, reds give trouble with mpeg video compression and greens lose a large amount of contrast. Thus it is much better to use "dirty" colors. The new RGB values below have been empirically found to produce much better images when printed and particularly with ambient occlusion lighting. The image on the right illustrates the change with the original color selection on the left and the new color scheme on the right.

  color change rgb  0 0.1 0.2 0.7 ;# blue
  color change rgb  1 0.7 0.2 0.1 ;# red
  color change rgb  3 0.7 0.4 0.0 ;# orange
  color change rgb  4 0.8 0.7 0.1 ;# yellow
  color change rgb  7 0.1 0.7 0.2 ;# green
  color change rgb 10 0.1 0.7 0.8 ;# cyan
  color change rgb 11 0.6 0.1 0.6 ;# purple

Molecule Name in Window Title

Occasionally people ask, whether it would be possible to have VMD display the name of the active molecule in the title of the OpenGL window. While this would be best done from within VMD, this little hack only uses Tcl script. The downside is that it only works with machines that use the X window system and have the corresponding utilities installed. More precisely, it uses the xprop and xwindinfo commands which are part of the xorg-x11-utils package on Red Hat and Fedora and derived distributions.

# define global variable to store X window id.
set vmd_opengl_wid -1

global vmd_opengl_wid

# callback function to be called when the top molecule changes
proc vmd_change_opengl_name {args} {

    global vmd_opengl_wid
    if {[molinfo num] < 1} return

    if {[llength $args] == 0} {
        set name [join [molinfo top get name]]
    } else {
        set name [lindex $args 0]
        if { [string equal $name vmd_molecule] } {
            set name [join [molinfo top get name]]
    if {$vmd_opengl_wid > 0} {
        catch {exec xprop -id $vmd_opengl_wid \
            -set WM_NAME "VMD [vmdinfo version]: $name"}
        catch {exec xprop -id $vmd_opengl_wid \
            -set WM_ICON_NAME $name}

# activate callback
trace variable vmd_molecule w vmd_change_opengl_name

# record window id for automatic title change

after idle {
    global vmd_opengl_wid
    if {![catch {exec xwininfo -name \
        "VMD [vmdinfo version] OpenGL Display"} val ]} {
        set vmd_opengl_wid [lindex $val 3]

Adding Hotkeys

Many keypresses in VMD can be customized and thus offering to speed up frequently use operations significantly. In the examples below the keys 'o' and 'p' allow switching between orthographic and perspective display mode, respectively, 'd' toggles between GLSL and normal OpenGL rendering, and 'D' en- or disables depth cueing.

user add key o {display projection orthographic}
user add key p {display projection perspective}
user add key d { if {[display get rendermode] != "Normal" } {display rendermode Normal} {display rendermode GLSL}}
user add key D { if {[display get depthcue] != "on" } {display depthcue on} {display depthcue off}}

Custom Actions at Molecule Load

Occasionally people ask about having performing custom actions, like having a different visualization than the default, whenever a molecule is loaded. Now several defaults can be changed, but the following scheme goes far beyond that and allows adding multiple representations and performing other scripted actions. In order for this to work, we have to put a trace on the variable vmd_initialize_structure, but calling actions right away at that point would not work. We'll rather have to just schedule a call to a script whenever the main event handler is idle again. Here the procedure reset_viz does the work, but rather than calling it directly in the variable trace, we use a proxy function reset_viz_proxy that generates an after idle {} request to call reset_viz with the specific molecule id to operate on as argument. The final line makes sure that this is also applied to molecules loaded from the command line, i.e. which are initialized before the .vmdrc file is processed and thus before the variable trace is defined.

proc reset_viz {molid} {
  # operate only on existing molecules
  if {[lsearch [molinfo list] $molid] >= 0} {
    # delete all representations
    set numrep [molinfo $molid get numreps]
    for {set i 0} {$i < $numrep} {incr i} {
      mol delrep $i $molid
    # add new representations
    mol color Name
    mol representation VDW 0.3 15.0
    mol selection all
    mol material Opaque
    mol addrep $molid
    mol representation DynamicBonds 1.6 0.3 6.0
    mol selection {name C}
    mol addrep $molid
    mol representation DynamicBonds 1.2 0.3 6.0
    mol selection {name C H}
    mol addrep $molid

proc reset_viz_proxy {args} {
  foreach {fname molid rw} $args {}
  eval "after idle {reset_viz $molid}"

## hook up the function.
trace variable vmd_initialize_structure w reset_viz_proxy
# take care of molecule loaded at start.
after idle { reset_viz 0 }