Molecule Tutorials - Herong's Tutorial Examples - v1.21, by Dr. Herong Yang
PyMol Python API
This section provides a tutorial on how to use PyMol Python API to invoke PyMol commands from Python scripts.
PyMol also provides a Python API that allows you to access all PyMol functionalities from a Python script.
All PyMol commands are wrapped as Python functions in the pymol.com class packaged in the "pymol" module. You call those functions to perform any molicule visualization tasks.
For example, the following Python script will rotate the viewing camera 360 degrees in 1 second in a loop:
# Rotate-Camera.py import pymol import time for i in range(1, 21): print("Moving...") pymol.cmd.turn('y', 18) time.sleep(0.05)
One way to execute this script is to use the "run" command at the PyMOL command prompt:
If you try the above command, you probably expect the molecule structure view will be rotated a full circle in 20 steps in 1 second time period. However, PyMol will actuall freeze the viewer for 1 second and display the structure once at the end.
I think the "run" command is actually executing the entire Python script in the background without updating the structure image in the viewer during the execution.
When you use the "run python-script-file" command at the PyMOL command prompt, you are using the PyMOL interpreter to execute the Python script, which calls back PyMol API functions.
See the next tutorial on how to use Python interpreter to execute Python scripts that calls PyMol API functions.
You can also read PyMol Python API reference on line at pymol.org/dokuwiki/doku.php?id=api.
Table of Contents