scripting: support reading and executing scripts

Both Python and ChimeraX command scripts are supported.

Python scripts are executed inside a sandbox module that has the ChimeraX session available in it. For example, to use the timeit module in a Python script:

import timeit
from chimerax.core.comands import sym

m = session.models.list()[0]
t = timeit.timeit(
    "sym.pdb_assemblies(m)",
    "from %s import sym, m" % __name__,
    number=1000
)
print('total time:', t)
open_command_script(session, path, file_name)

Experimental API . Execute utf-8 file as ChimeraX commands.

The current directory is changed to the file directory before the commands are executed and restored to the previous current directory after the commands are executed.

This function is invoked via ChimeraX’s io open_data() API for files whose names end with .cxc.

Parameters:
session : a ChimeraX Session
path : path to file to open
name : how to identify the file
open_python_script(session, stream, file_name, argv=None)

Experimental API . Execute Python script in a ChimeraX context

This function is invoked via ChimeraX’s io open_data() API for files whose names end with .py, .pyc, or .pyo. Each script is opened in an uniquely named importable sandbox (see timeit example above). And the current ChimeraX session is available as a global variable named session.

Parameters:
session : a ChimeraX Session
stream : open data stream
file_name : how to identify the file