Extend

From PyMOLWiki
Jump to: navigation, search

This page is about an API function. For the selection operator with the same name, see extend (selection operator).

Extend is an API-only function which binds a user defined function as a command into the PyMOL scripting language.

Contents

Details

PyMOL API

pymol.cmd.extend(string name, function function)

Simplified (since PyMOL 1.6), takes the function name as command name and can be used as a function decorator:

pymol.cmd.extend(function function)

Simple Example

Put the following in a Python script (e.g. file.py) and "run" the script (e.g. with "File > Run...").

from pymol import cmd
 
def foo(moo=2):
    print moo
 
cmd.extend('foo', foo)

Or with decorator syntax (since PyMOL 1.6):

from pymol import cmd
 
@cmd.extend
def foo(moo=2):
    print moo

The following would now work within PyMOL's command line:

PyMOL>foo
2
PyMOL>foo 3
3
PyMOL>foo moo=5
5
PyMOL>foo ?
Usage: foo [ moo ]

Advanced Example

This example provides a command help, does proper type conversion of arguments and would work with pymol2.

from pymol import cmd
 
@cmd.extend
def adjust_vdw(selection="all", factor=0.5, quiet=1, _self=cmd):
    """
DESCRIPTION
 
    The "adjust_vdw" command alters all vdw radii in the given selection
    by the given scale factor.
    """
    factor = float(factor)
    quiet = int(quiet)
 
    n = _self.alter(selection, "vdw = vdw * %f" % (factor))
 
    if n > 0:
      _self.rebuild()
 
    if not quiet:
        if n == 0:
            print " No atoms in selection"
        else:
            print " Updated VDW radii of %d atoms by factor %.2f" % (n, factor)

Notes and recommendations

See Also

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox