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, log=True, for_each_file=None)

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.

Parameters:
  • session (a ChimeraX Session)

  • path (path to file to open)

  • file_name (how to identify the file)

  • log (whether to log each script command)

  • for_each_file (data file paths, iterate opening each file followed by the script which has $file replaced by filename with suffix stripped.)

open_compiled_python_script(session, stream, file_name, argv=None)

Experimental API . Execute compiled Python script in a ChimeraX context

Each script is opened in a 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)

open_python_script(session, stream, file_name, argv=None)

Experimental API . Execute Python script in a ChimeraX context

Each script is opened in a 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)