Introduction
The scripting interface of OVITO lets you
automate data visualization and analysis steps,
integrate OVITO’s data I/O, analysis and rendering capabilities into custom workflows or Python programs,
extend OVITO capabilities by developing new modifiers or viewports layers that integrate into the graphical user interface.
The script programming interface described in this manual consists of a collection of Python functions and classes, which expose OVITO’s internal data model, data pipeline framework and rendering capabilities.
Automation scripts
A common use case for OVITO’s programming interface is automating laborious tasks. Python scripts don’t
require any user interaction and can (repeatedly) perform sequences of actions that you would otherwise have to carry out by
hand in the graphical application.
The following example script remove_hydrogens.py loads an atomic structure from a simulation file, selects all hydrogen atoms,
deletes them, and writes the resulting dataset back to an output file:
from ovito.io import import_file, export_file
from ovito.modifiers import SelectTypeModifier, DeleteSelectedModifier
pipeline = import_file('input_file.xyz')
pipeline.modifiers.append(SelectTypeModifier(property='Particle Type', types={'H'}))
pipeline.modifiers.append(DeleteSelectedModifier())
export_file(pipeline, 'output_file.xyz', 'xyz', columns=['Particle Type', 'Position.X', 'Position.Y', 'Position.Z']])
Scripts like this example can be easily created with OVITO Pro’s built-in code generator in the graphical user interface. You can then execute the Python script from the system terminal using ovitos, the preconfigured Python interpreter shipping with the OVITO Pro desktop application:
ovitos remove_hydrogens.py
Alternatively, you can execute the script – like any other Python program – using your Python interpreter of choice:
python remove_hydrogens.py
For this to work, you first need to install the ovito module in your Python interpreter as will be explained later.
Running scripts within the desktop application
A Python script can also be executed right within the context of the running OVITO Pro desktop application. This option can be useful especially during script development, because it allows you to directly observe what your script does and how the final outcome, for example the data pipeline created by the script, looks like.
Use the Run Script File function found in the File menu of OVITO Pro to execute an existing Python script file. Note that, in this case, all script actions will be executed within the context of the current program session – as if you would perform them by hand. Alternatively, you can use the --script command line option when invoking OVITO from the system terminal to execute a given Python script right after application startup.
User-defined modifiers and viewport overlays
OVITO’s scripting interface furthermore enables you to develop new types of modifiers that can manipulate or analyze data in ways not covered by any of the built-in modifiers of the program. So-called Python script modifiers (see User-defined modifiers section for more information) participate in the data pipeline system of OVITO and behave just like the built-in modifiers from a user’s perspective. Such a user-defined modifier essentially is a Python function written by you that gets executed automatically by OVITO whenever the data pipeline is evaluated.
Furthermore, you can write custom viewport layers. A script viewport layer is a user-defined Python function that gets called by OVITO every time a viewport image is being rendered. This allows you to enrich rendered images or movies with custom text and graphics, and include additional information such as data plots that are dynamically generated from the simulation data.
Installing the ovito module in your Python interpreter
To make OVITO’s scripting capabilities available to scripts running in your system’s Python interpreter, you first need to install
the ovito module. It may be downloaded from the PyPI repository and installed using the pip install ovito command.
For further instruction please refer to https://pypi.org/project/ovito/.
If you are using the Anaconda or Miniconda environments, you should install the ovito Python module from our channel as follows:
conda install --strict-channel-priority -c https://conda.ovito.org -c conda-forge ovito=3
The conda-forge channel provides additional dependencies required by OVITO. The ovito conda package includes both the graphical desktop application OVITO Pro and the ovito Python module.
OVITO Pro’s Python interpreter
OVITO Pro includes a preconfigured Python interpreter named ovitos that can execute scripts written in the Python 3.x language.
ovitos already has the ovito Python module and all other dependencies preinstalled, which are required to run scripts within the desktop application and externally.
You can execute a standalone Python script from the terminal of your operating system using the ovitos command:
ovitos [-o file] [-g] [script.py] [args...]
The ovitos executable is located in the bin/ subdirectory of OVITO for Linux, in the
Ovito.app/Contents/MacOS/ directory of OVITO for macOS, and in the main application directory
on Windows systems (look for ovitos.exe). It should not be confused with ovito, which starts the
graphical desktop application OVITO Pro.
Let’s use a text editor to write a simple Python script file named hello.py:
import ovito
print("Hello, this is OVITO %i.%i.%i" % ovito.version)
You can execute the script file from a Linux terminal as follows:
user@linux:~/ovito-pro-3.7.9-x86_64/bin$ ./ovitos hello.py
Hello, this is OVITO 3.7.9
The ovitos script interpreter is a console program without a graphical user interface.
This enables you to run scripts on remote machines or computing clusters that don’t possess a graphical display.
ovitos behaves like a regular Python interpreter. Any command line arguments following the
script filename are passed to the script via the sys.argv variable. Furthermore, it is possible to start
an interactive interpreter session by invoking ovitos without any arguments.
Preloading session state
The -o command line option tells ovitos to load an .ovito session state file before executing the
script. This allows you to preload an existing data pipeline or visualization setup that you have
previously prepared using the graphical version of OVITO. All script actions will subsequently be performed in the context of this preloaded session.
This can save you programming work, because things like modifiers and the camera setup already get loaded from the state file and
you don’t need to set everything up programmatically in the script anymore.
Graphical mode
The -g command line option of ovitos starts a graphical program session and the script
will be run in the context of OVITO’s main window. This allows you to follow your script’s actions as they are being
executed. This is useful for debugging purposes if you want to visually check the outcome of your script’s action during the
development phase. Keep in mind that the viewports will only show pipelines that have been inserted into the current scene.
Thus, it is necessary to explicitly call Pipeline.add_to_scene()
to make your imported data visible in this mode.
Number of CPU cores
OVITO uses all available processor cores by default to perform certain computations. To explicitly restrict the program to a smaller number of parallel threads, use the --nthreads command line parameter, e.g. ovitos --nthreads 1 myscript.py. See Specifying the number of processor cores.
Installing third-party Python modules
OVITO’s embedded interpreter ovitos is a preconfigured CPython interpreter with the
ovito Python package already built in. This makes it possible to run scripts both within the OVITO Pro desktop application as well as through the ovitos
command line tool. However, OVITO Pro’s embedded Python interpreter only ships with very few additional packages: NumPy and matplotlib.
If you want to call third-party Python packages from your scripts, you can install them in the ovitos interpreter using the usual pip install mechanism. Simply run
ovitos -m pip install --user <package_name>
from a system terminal to install any package from the PyPI repository. This makes the package available to scripts running in the OVITO Pro desktop application.
Note
This approach only applies to OVITO Pro installed via installer from our website. If you work with the Anaconda version of OVITO Pro, use the conda install command to add additional packages to the Python interpreter shared by OVITO Pro and your Anaconda environment.
Note
Installing a Python extension via pip that includes native code may fail if it is not compatible with the build-time configuration of the ovitos interpreter. In such a case it is recommended to use the Anaconda version of OVITO Pro, which makes installing third-party packages straightforward.