Eric Pettersen
UCSF Computer Graphics Lab
pett@cgl.ucsf.edu
http://www.cgl.ucsf.edu
Hi Jim,Importing Chimera from your Python script to use its functionality issomething we have put very little effort into supporting. I have donethis in an nuclear magnetic resonance assignment program called Sparkythat is based on Python to run Chimera as a structure viewer. Below isthe code that starts Chimera from the Sparky Python interpretter.There are many problems.You need to set an environment variable to tell the operating systemwhere the Chimera shared libraries are before you start Python. Seeexample below.You need to use the Python included with Chimera because Chimerauses a modified Python. Ick. The modification allows Chimera objectslike Molecules to be given new attributes. There is also some otherissue on the Mac where a stock Python can't find the init_chimera()method in shared library _chimera.so.If you start the Chimera graphical user interface then you will needto run the Tk event-loop to get Chimera to update the graphics. Butthen Chimera will not return to your script that started it. So myexample below does not start the event-loop.If you start Chimera in nogui mode, it quits as soon as it processesany arguments which would probably prevent your script from calling intoChimera after that as Chimera has cleaned up.When I used the script below and then opened a PDB file on my Mac OS10.4.6 system the screen went dim and a message told me to hold thepower button down to do a hard reboot! Probably a graphics driverissue.As you can see we really haven't worked much on this type of Chimera use.Tom----Example starting Chimera on a Mac from Python:$ export CHIMERA="/Users/goddard/Desktop/Chimera 2199.app/Contents/Resources"$ export DYLD_LIBRARY_PATH=$CHIMERA/lib$ "/Users/goddard/Desktop/Chimera 2199.app/Contents/Resources/bin/python2.4"Python 2.4.2 (#1, Jan 30 2006, 11:15:23)[GCC 4.0.1 (Apple Computer, Inc. build 5247)] on darwinType "help", "copyright", "credits" or "license" for more information.import start_chimerastart_chimera.start_chimera()# Now you could "import chimera" and do some stuff.---The following is the start_sparky.py file used in the above example:# -----------------------------------------------------------------------------#start_count = 0def start_chimera():setup_chimera()import chimeraInittry:chimeraInit.init(argv = [], nogui = False, eventloop = False,exitonquit = False)except SystemExit, value:# Chimera raises SystemExit sometimes when it can't start.# Have to catch it here or Sparky will exit.import tracebacktraceback.print_exc()raise EnvironmentError, 'Chimera failed to start'except:global start_countif start_count == 1:warning = '''Chimera can only be started once per Sparky session.This is a limitation in all versions of Chimera released so far(through March 2004).'''import syssys.stdout.write(warning)raisestart_count += 1# -----------------------------------------------------------------------------# Do initial Chimera setup that only needs to be done once even if Chimera# is started and quit several times.#chimera_setup = Falsedef setup_chimera():global chimera_setupif chimera_setup:returnchimera_setup = Trueimport osif not os.environ.has_key('CHIMERA'):raise EnvironmentError, 'CHIMERA environment variable not set.'chimera_path = os.environ['CHIMERA']import os.pathif not os.path.exists(chimera_path):raise EnvironmentError, 'CHIMERA environment variable gives non-existent directory ' + chimera_pathchimera_python = os.path.join(chimera_path, 'share')chimera_lib = os.path.join(chimera_path, 'lib')import sysif sys.platform == 'win32':chimera_site_packages = os.path.join(chimera_path, 'bin', 'Lib','site-packages')else:chimera_site_packages = os.path.join(chimera_path, 'lib', 'python2.4','site-packages')sys.path.insert(0, chimera_site_packages)sys.path.insert(0, chimera_python)sys.path.insert(0, chimera_lib)_______________________________________________Chimera-users mailing listChimera-users@cgl.ucsf.eduhttp://www.cgl.ucsf.edu/mailman/listinfo/chimera-users