When certain events occur, VMD notifies the Tcl interpreter by setting certain Tcl variables to new values. You can use this feature to customize VMD, for instance, by causing new graphics to appear when the user picks an atom, or recalculating secondary structure on the fly.
To make these new feature happen at the right time, you need to write a script that takes a certain set of arguments, and register this script with the variable you are interested. Registering scripts is done with the built-in Tcl command trace; see http://www.tcl.tk/man/tcl8.4/TclCmd/trace.htm for documentation on how to use this command. The idea is that after you register your callback, when VMD changes the value of the variable, your script will immediately be called with the new value of the variable as arguments. Table 9.4 summarizes the callback variables available in VMD.
In the VMD script library at
http://www.ks.uiuc.edu/Research/vmd/script_library/
,
you can find a number of scripts that take advantage of Tcl variable tracing.
Below, we give a simple example. The following procedure takes the picked atom and finds the molecular weight of residue it is on.
proc mol_weight {args} { # use the picked atom's index and molecule id global vmd_pick_atom vmd_pick_mol set sel [atomselect $vmd_pick_mol "same residue as index $vmd_pick_atom"] set mass 0 foreach m [$sel get mass] { set mass [expr $mass + $m] } # get residue name and id set atom [atomselect $vmd_pick_mol "index $vmd_pick_atom"] lassign [$atom get {resname resid}] resname resid # print the result puts "Mass of $resname $resid = $mass" }
Once an atom has been picked, run the command mol_weight to get output like:
Mass of ALA 7 : 67.047
Since VMD sets the vmd_pick_event, it can be traced. The trace function is registered as:
trace add variable ::vmd_pick_event write mol_weight
And now the residue masses will be printed automatically when an atom is picked. Make sure to turn off the trace when you are done with it (e.g. your plugin's window gets closed):
trace remove variable ::vmd_pick_event write mol_weight