https://pymolwiki.org/api.php?action=feedcontributions&user=Suyang&feedformat=atomPyMOLWiki - User contributions [en]2024-03-28T17:00:47ZUser contributionsMediaWiki 1.35.7https://pymolwiki.org/index.php?title=File:Overlapping_peptides.png&diff=13182File:Overlapping peptides.png2019-08-24T01:18:10Z<p>Suyang: </p>
<hr />
<div></div>Suyanghttps://pymolwiki.org/index.php?title=Launching_From_a_Script&diff=12750Launching From a Script2018-05-08T04:01:26Z<p>Suyang: </p>
<hr />
<div><div style="background-color: #ccc; padding: 20px"><br />
The recommended way to run PyMOL-Python scripts is by using PyMOL as the interpreter. This is supported by all versions of PyMOL, including the pre-compiled bundles provided by Schrödinger.<br />
<br />
Example from a shell:<br />
<br />
shell> pymol -cq script.py<br />
<br />
With arguments (''sys.argv'' becomes ''["script.py", "foo", "bar"]''):<br />
<br />
shell> pymol -cq script.py -- foo bar<br />
<br />
Example from a running PyMOL instance:<br />
<br />
PyMOL> run script.py<br />
<br />
</div><br />
<br />
For advanced users, the following PyMOL versions also allow to run PyMOL from an existing Python process:<br />
<br />
* [https://pymol.org/2/#download PyMOL 2.0] based on Anaconda (using Anaconda's python, which is included in bundles provided by Schrödinger)<br />
* Open-Source PyMOL<br />
* Schrödinger-provided "[http://pymol.org/download Mac alternative X11-only build]" of the 1.8.x series<br />
<br />
After importing the '''pymol''' module, PyMOL's event loop has to be started with a call to '''pymol.finish_launching()'''.<br />
<br />
== Usage ==<br />
With PyMOL 2.1, calling any pymol.cmd function will automatically start a backend process without the GUI in the main thread. "finish_launching" should not be necessary, and will launch PyMOL in a new thread with an event loop, which will cause 100% CPU usage (at least with "-c").<br />
<br />
<source lang="python"><br />
from pymol import cmd<br />
cmd.fragment('ala')<br />
cmd.zoom()<br />
cmd.png('/tmp/test.png', 300, 200)<br />
</source><br />
<br />
Since PyMOL 1.7.4, the following form is sufficient:<br />
<br />
<source lang="python"><br />
import pymol<br />
pymol.finish_launching(['pymol', '-q'])<br />
</source><br />
<br />
Before 1.7.4, either "-K" was needed as an additional argument, or arguments had to be assigned to __main__.pymol_argv or pymol.pymol_argv.<br />
<br />
== Example 1 ==<br />
<br />
Here is an example script that launches PyMol for stereo viewing on a [http://www.visbox.com/boxMain.html VisBox]. It runs PyMol fullscreen stereo, and disables the internal gui.<br />
The environment (PYTHON_PATH and PYMOL_PATH) must already be set up for this example to work (see [[#Example 2|Example 2]] below for how to setup within the script).<br />
<br />
<source lang="python"><br />
#!/usr/bin/env python<br />
<br />
# Tell PyMOL to launch quiet (-q), fullscreen (-e) and without internal GUI (-i)<br />
import __main__<br />
__main__.pymol_argv = [ 'pymol', '-qei' ]<br />
<br />
import pymol<br />
<br />
# Call the function below before using any PyMOL modules.<br />
pymol.finish_launching()<br />
<br />
from pymol import cmd<br />
cmd.stereo('walleye')<br />
cmd.set('stereo_shift', 0.23)<br />
cmd.set('stereo_angle', 1.0)<br />
</source><br />
<br />
== Example 2 ==<br />
<br />
This script launches PyMOL without any GUI for scripting only. It enables tab-completion on the python command line and does the PyMOL environment setup (you need to adjust the '''moddir''' variable!). ''Hint: You may save this as "pymol-cli" executable.''<br />
<br />
<source lang="python"><br />
#!/usr/bin/python2.6 -i<br />
<br />
import sys, os<br />
<br />
# autocompletion<br />
import readline<br />
import rlcompleter<br />
readline.parse_and_bind('tab: complete')<br />
<br />
# pymol environment<br />
moddir='/opt/pymol-svn/modules'<br />
sys.path.insert(0, moddir)<br />
os.environ['PYMOL_PATH'] = os.path.join(moddir, 'pymol/pymol_path')<br />
<br />
# pymol launching: quiet (-q), without GUI (-c) and with arguments from command line<br />
import pymol<br />
pymol.pymol_argv = ['pymol','-qc'] + sys.argv[1:]<br />
pymol.finish_launching()<br />
cmd = pymol.cmd<br />
</source><br />
<br />
== STDOUT ==<br />
<br />
PyMOL captures '''sys.stdout''' and '''sys.stderr''', to control it with it's own [[feedback]] mechanism. To prevent that, save and restore both streams, e.g.:<br />
<br />
<source lang="python"><br />
import sys<br />
stdout = sys.stdout<br />
stderr = sys.stderr<br />
pymol.finish_launching(['pymol', '-xiq'])<br />
sys.stdout = stdout<br />
sys.stderr = stderr<br />
</source><br />
<br />
== Independent PyMOL Instances ==<br />
It's possible to have multiple independent instances. <br />
<br />
<source lang="python"><br />
import pymol2<br />
p1 = pymol2.PyMOL()<br />
p1.start()<br />
<br />
p2 = pymol2.PyMOL()<br />
p2.start()<br />
<br />
p1.cmd.fragment('ala')<br />
p1.cmd.zoom()<br />
p1.cmd.png('/tmp/ala.png', 1000, 800, dpi=150, ray=1)<br />
<br />
p2.cmd.fragment('ser')<br />
p2.cmd.zoom()<br />
p2.cmd.png('/tmp/ser.png', 1000, 800, dpi=150, ray=1)<br />
<br />
p1.stop()<br />
p2.stop()<br />
</source><br />
<br />
== See Also ==<br />
<br />
* [[Command Line Options]]<br />
<br />
[[Category:Launching]]<br />
[[Category:Script_Library]]</div>Suyanghttps://pymolwiki.org/index.php?title=Launching_From_a_Script&diff=12749Launching From a Script2018-05-08T03:51:16Z<p>Suyang: /* Usage */</p>
<hr />
<div><div style="background-color: #ccc; padding: 20px"><br />
The recommended way to run PyMOL-Python scripts is by using PyMOL as the interpreter. This is supported by all versions of PyMOL, including the pre-compiled bundles provided by Schrödinger.<br />
<br />
Example from a shell:<br />
<br />
shell> pymol -cq script.py<br />
<br />
With arguments (''sys.argv'' becomes ''["script.py", "foo", "bar"]''):<br />
<br />
shell> pymol -cq script.py -- foo bar<br />
<br />
Example from a running PyMOL instance:<br />
<br />
PyMOL> run script.py<br />
<br />
</div><br />
<br />
For advanced users, the following PyMOL versions also allow to run PyMOL from an existing Python process:<br />
<br />
* [https://pymol.org/2/#download PyMOL 2.0] based on Anaconda (using Anaconda's python, which is included in bundles provided by Schrödinger)<br />
* Open-Source PyMOL<br />
* Schrödinger-provided "[http://pymol.org/download Mac alternative X11-only build]" of the 1.8.x series<br />
<br />
After importing the '''pymol''' module, PyMOL's event loop has to be started with a call to '''pymol.finish_launching()'''.<br />
<br />
== Usage ==<br />
With PyMOL 2.1, calling any pymol.cmd function will automatically start a backend process without the GUI in the main thread. "finish_launching" should not be necessary, and will launch PyMOL in a new thread with an event loop, which will cause 100% CPU usage (at least with "-c").<br />
<br />
<source lang="python"><br />
from pymol import cmd<br />
cmd.fragment('ala')<br />
cmd.zoom()<br />
cmd.png('/tmp/test.png', 300, 200)<br />
</source><br />
<br />
Since PyMOL 1.7.4, the following form is sufficient:<br />
<br />
<source lang="python"><br />
import pymol<br />
pymol.finish_launching(['pymol', '-q'])<br />
</source><br />
<br />
Before 1.7.4, either "-K" was needed as an additional argument, or arguments had to be assigned to __main__.pymol_argv or pymol.pymol_argv.<br />
<br />
== Example 1 ==<br />
<br />
Here is an example script that launches PyMol for stereo viewing on a [http://www.visbox.com/boxMain.html VisBox]. It runs PyMol fullscreen stereo, and disables the internal gui.<br />
The environment (PYTHON_PATH and PYMOL_PATH) must already be set up for this example to work (see [[#Example 2|Example 2]] below for how to setup within the script).<br />
<br />
<source lang="python"><br />
#!/usr/bin/env python<br />
<br />
# Tell PyMOL to launch quiet (-q), fullscreen (-e) and without internal GUI (-i)<br />
import __main__<br />
__main__.pymol_argv = [ 'pymol', '-qei' ]<br />
<br />
import pymol<br />
<br />
# Call the function below before using any PyMOL modules.<br />
pymol.finish_launching()<br />
<br />
from pymol import cmd<br />
cmd.stereo('walleye')<br />
cmd.set('stereo_shift', 0.23)<br />
cmd.set('stereo_angle', 1.0)<br />
</source><br />
<br />
== Example 2 ==<br />
<br />
This script launches PyMOL without any GUI for scripting only. It enables tab-completion on the python command line and does the PyMOL environment setup (you need to adjust the '''moddir''' variable!). ''Hint: You may save this as "pymol-cli" executable.''<br />
<br />
<source lang="python"><br />
#!/usr/bin/python2.6 -i<br />
<br />
import sys, os<br />
<br />
# autocompletion<br />
import readline<br />
import rlcompleter<br />
readline.parse_and_bind('tab: complete')<br />
<br />
# pymol environment<br />
moddir='/opt/pymol-svn/modules'<br />
sys.path.insert(0, moddir)<br />
os.environ['PYMOL_PATH'] = os.path.join(moddir, 'pymol/pymol_path')<br />
<br />
# pymol launching: quiet (-q), without GUI (-c) and with arguments from command line<br />
import pymol<br />
pymol.pymol_argv = ['pymol','-qc'] + sys.argv[1:]<br />
pymol.finish_launching()<br />
cmd = pymol.cmd<br />
</source><br />
<br />
== STDOUT ==<br />
<br />
PyMOL captures '''sys.stdout''' and '''sys.stderr''', to control it with it's own [[feedback]] mechanism. To prevent that, save and restore both streams, e.g.:<br />
<br />
<source lang="python"><br />
import sys<br />
stdout = sys.stdout<br />
stderr = sys.stderr<br />
pymol.finish_launching(['pymol', '-xiq'])<br />
sys.stdout = stdout<br />
sys.stderr = stderr<br />
</source><br />
<br />
== See Also ==<br />
<br />
* [[Command Line Options]]<br />
<br />
[[Category:Launching]]<br />
[[Category:Script_Library]]</div>Suyang