I'm trying to call current PyMOL session from python script (wxpython GUI), and then load some data from PyMOL and send few commands to PyMOL. At the moment I can open a new PyMOL session in python script:
import sys, os
from wx import *
app = wx.App()
dialog = wx.FileDialog ( None, message = 'Set PyMOL directiry', style = wx.OPEN)
if dialog.ShowModal() == wx.ID_OK:
# Pymol path
moddir = dialog.GetDirectory()
sys.path.insert(0, moddir)
os.environ['PYMOL_PATH'] = moddir
import pymol
pymol.finish_launching()
else:
print 'Nothing was selected.'
from pymol import *
dialog1 = wx.FileDialog ( None, message = 'Set PDB file', style = wx.OPEN)
if dialog1.ShowModal() == wx.ID_OK:
pdbfile = dialog1.GetPath()
cmd.load(pdbfile)
else:
print 'Nothing was selected.'
dialog.Destroy()
app.MainLoop()
BUT actually I'd like to check in my python scrip whether any PyMOL session is already opened. I found discussion corresponding to this topic here: Only call function if PyMOL running Following this discussion I tried to call 'print' function in PyMOL:
try:
from pymol import cmd
print 'Connected'
except:
<open new Pymol sesssion>
but I do not see any text in PyMOL cmd. I tried to determine PyMOL path before this statement and again I failed.
Does anyone know how to call current PyMOL session from python script?
To my knowledge, there is no way to interact with an existing PyMOL session from the Python interpreter. There are a few alternatives:
The existing PyMOL command prompt accepts valid Python and you can run any script directly from the prompt. Perhaps you could execute your script from there instead?
You can start up a new PyMOL session as part of your script. PyMOL's
-i
flag may be especially helpful- it enables you to work in a headless environment:Take a careful look at PyMOL's
__init.py__
. You'll find the intricacies of PyMOL's threading there. Perhaps you can find something useful to manipulate?Side note:
Calling
print
from your script will not make the text appear in the PyMOL session, it will merely write the text to standard output (i.e. it will be printed to your terminal).The following PyMOL wiki pages may be of help to you: