This page attempts to provide some advice for writing efficient Tcl visualization and analysis scripts in VMD.
foreach vec [$sel get { x y }] {
# do stuff with $vec
}
it will work no matter how many atoms are in $sel. If you know that you
will only have one item in the list you can also use:
set vec [lindex [$sel get {x y}] 0]
is to use lindex to get the first element of the returned list which
contains the vector you expect.
# make a list of CoM coordinates of a selection over a trajectory
# uses the previous coordinates if the selection is empty...
set num_frames [molinfo $gr_mol get numframes]
set coords {}
if [ catch {
set lastco [measure center $selection weight mass]
} errmsg ] {
set lastco {0.0 0.0 0.0}
}
for {set i 0} {$i < $num_frames} {incr i} {
$selection frame $i
if {$update} { $selection update }
# compute the center of mass ...
if [ catch {
set tmp [measure center $selection weight mass]
} errmsg ] {
set tmp $lastco
}
# ... and append it to the list.
lappend coords $tmp
set lastco $tmp
}