Difference between revisions of "Example Scripts"

From PyMOLWiki
Jump to navigation Jump to search
 
(One intermediate revision by the same user not shown)
Line 31: Line 31:
  
 
Now, just running "sd" would show all the distance objects.
 
Now, just running "sd" would show all the distance objects.
 +
 +
 +
===Comments in Scripts===
 +
The hash-mark, "#" is the Python comment.  However, when scripting in Python for PyMol note that the hash character (#) works to include comments on separate lines, but not at the end of a line that contains commands. So you can do
 +
<source lang="python">
 +
# Create separate dimer
 +
create dimer,(chain A,B)
 +
</source>
 +
but not
 +
<source lang="python">
 +
create dimer,(chain A,B)  # Create separate dimer
 +
</source>
  
 
====Sources====
 
====Sources====

Revision as of 09:04, 2 August 2005

Multiple Object Manipulation Scripts

Dis/Enable Objects

If someone has many (possibly hundreds) of objects -- say distance objects -- one can turn them all on or off by using Python scripts within PyMol. The following script will extend the commands "sd" and "hd" for "show distance" and "hide distance," respectively. The first script,

from pymol import cmd 
num_dist = 100 
        
def show_dist(): 
    """ show all of my distance objects """ 
    for i in range(num_dist): 
        cmd.enable('_dist%s'%i) 
        
def hide_dist(): 
    """ hide all of my distance objects """ 
    for i in range(num_dist): 
        cmd.disable('_dist%s'%i) 
        
cmd.extend('sd',show_dist) 
cmd.extend('hd',hide_dist)

works on 100 objects. We can extend the idea with more elegant scripting to work w/o forcing us to keep track of the number of objects:

def show_dist(): 
    dists = [name for name in cmd.get_names() if cmd.get_type(name) == 'object:distance'] 
    for name in dists: cmd.enable(name)

Now, just running "sd" would show all the distance objects.


Comments in Scripts

The hash-mark, "#" is the Python comment. However, when scripting in Python for PyMol note that the hash character (#) works to include comments on separate lines, but not at the end of a line that contains commands. So you can do

# Create separate dimer
create dimer,(chain A,B)

but not

create dimer,(chain A,B)  # Create separate dimer

Sources

Taken from the PyMol Users list. Python source by Michael Lerner.