From: Oliver Beckstein (orbeckst_at_jhmi.edu)
Date: Wed Nov 21 2007 - 20:27:41 CST

Hi Jiwu,

I can't directly answer your question but I wrote something similar
years ago (
http://www.ks.uiuc.edu/Research/vmd/mailing_list/vmd-l/1276.html ):
start a server in VMD (based on the collab scripts) and then issue
commands from a python script. (Justin helped modifying the
remote_ctl.tcl script to start a simple server in VMD. )

A recent version is at

http://sbcb.bioch.ox.ac.uk/oliver/download/scriptlets/vmd_remote.tar.bz2

It unpacks into a directory named vmd and contains control.py and
remote_ctl.tcl. Make sure that the parent directory is in your
PYTHONPATH and then import the vmd module:

>>> import vmd.control
>>> vmdserver = vmd.control.server() # starts VMD in text mode and
starts server

After some error messages from asyncore (ignore them) you should see
something like

Info) VMD ...
Info) ....
Starting remote_ctl server in VMD.
Listening on port 5555
vmd > ALIVE (ping from pid 30943)
ALIVE (ping from pid 30943)

Send commands to VMD with the command method:
>>> c = vmdserver.command('cd','set w [atomselect top {water}]', '$w
writepdb water.pdb')
>>> c.results()
>>> vmdserver.stop()

Please see the doc strings for usage (help(vmd.control.server)), but
the above covers pretty much it. If your vmd startup script is not in
your PATH, you can do

>>> vmdserver = vmd.server(vmdbinary='/usr/local/vmd-1.8.6/vmd')

If you get

RuntimeError: Failed to bring up the VMD server.

check that you can do something like

>>> import os
>>> os.system('vmd -dispdev text -e 'puts HELLO; pwd')

If this fails, either check your PATH or explicitly give the path to
the vmd script in vmdbinary.

Perhaps this is useful for you.

Oliver

On 21 Nov, 2007, at 14:26, John Stone wrote:

>
> Hi,
>
> On Tue, Nov 06, 2007 at 09:02:27PM -0500, jiwu liu wrote:
>> Hi John,
>>
>> I have tried what you said -- direct output to /dev/null. But
>> it doesn't
>> work. The same problem persists.
>
> I could try taking a look at this if you send me your script, but I
> can't make any promises about solving it since I don't know what the
> source of the problem is.
>
>> What you pointed me to ( the vmdcollab script ) is an
>> interesting
>> solution. I want to try it out. Could you tell me a little more
>> about how
>> to use it? Basically my questions are:
>>
>> (1) Is it part of standard release of vmd?
>
> No, its a separate script that you have to run in VMD.
>
>> (2) It seems my program should play the role of server. So how
>> should I
>> start the vmd client together with this script? Because I really want
>> to
>> finish starting and initializing vmd client in a single command. Can
>> I do
>> something like:
>> "vmd -startup vmdcollab"
>> ( Here I understand I have to give the host and port but I
>> don't know
>> how. )
>
> Read the text in the script page and the script itself and you should
> be able to figure it out. Try using the script as intended first
> before
> you try modifying it or using it with your code.
>
> Cheers,
> John
>
>
>> On Monday 05 November 2007, John Stone wrote:
>>> Is your code flushing the output or otherwise reading it?
>>> It's possible that the stdout channel is backing up and causing
>>> VMD to go to sleep due to a blocking write call on stdout...
>>> You could either redirect the stdout channel to /dev/null, or
>>> else flush the output constantly.
>>>
>>> You might find it easier to control VMD through a socket interface
>>> where a Tcl script handles the command processing on the VMD side
>>> for you so you don't have to worry about output being flushed etc:
>>>
>>> http://www.ks.uiuc.edu/Research/vmd/script_library/scripts/
>>> vmdcollab/
>>>
>>> Cheers,
>>> John Stone
>>> vmd_at_ks.uiuc.edu
>>>
>>> On Mon, Nov 05, 2007 at 05:55:50PM -0500, jiwu liu wrote:
>>>> Hi,
>>>>
>>>> I wrote a simple interface to control vmd from a pipe. In
>>>> python, it
>>>> is: p = os.popen('/usr/local/lib/vmd/vmd_LINUX', 'w')
>>>>
>>>> and then I can write command string to vmd.
>>>>
>>>> But I found that command can not be sent immediately after
>>>> another
>>>> one, I have to insert a delay
>>>> time.sleep(0.5)
>>>> between two consecutive commands.
>>>>
>>>> For examples, I had coloring selection commands like:
>>>>
>>>> (1) set sel [atomselect 0 "name H"]
>>>> (2) $sel set user 1.0
>>>> (3) mol modcolor 0 0 User
>>>>
>>>> I had to wait for 0.5 secs between commands sent through pipe
>>>> to vmd.
>>>> Otherwise the command would not take any effect until next sending,
>>>> namely, command (2) will be run when I send command (3), so will the
>>>> command 3. Everything will be just postponed one step( And I do have
>>>> "p.flush()" after each command).
>>>>
>>>> In vmd TCL scritpt , I don't see any "sleep" between two
>>>> commands.
>>>> Why does it take a 'wait' when I use pipe to write to vmd program?
>>>> does
>>>> it have to do with how vmd read stdin?
>>>> Thanks for any help.
>>>>
>>>> jiwu
>>
>
> --
> NIH Resource for Macromolecular Modeling and Bioinformatics
> Beckman Institute for Advanced Science and Technology
> University of Illinois, 405 N. Mathews Ave, Urbana, IL 61801
> Email: johns_at_ks.uiuc.edu Phone: 217-244-3349
> WWW: http://www.ks.uiuc.edu/~johns/ Fax: 217-244-6078
>
>

--
Oliver Beckstein * orbeckst_at_jhmi.edu
Johns Hopkins University, School of Medicine
Dept. of Physiology, Biophysics 206
725 N. Wolfe St
Baltimore, MD 21205, USA
Tel.: +1 (410) 614-4435