7.1. Scripting Introduction¶
7.1.1. Prerequisites to Scripting¶
Before starting this guide and using the Python scripting features, ensure you have completed the steps explained in the Installation chapter of the documentation to obtain a working Python installation.
Note
For scripting to work, you need a 64-bit Python 3.15, 3.14, 3.13, or 3.12 installation on your machine.
If Python installation directories are not correctly detected, two environment variables
can be used to modify sys.path.
The PYTHONHOME variable sets an alternative prefix for the Python
installation. See https://docs.python.org/3/using/cmdline.html#envvar-PYTHONHOME for
details.
The PYTHONPATH variable can be set to a list of paths to be added to
the beginning of sys.path. For example, if PYTHONPATH is set to
/www/python:/opt/py, the search path will begin with ['/www/python',
'/opt/py']. (Note that directories must exist to be added to sys.path;
the site module removes non-existent paths.)
Note
Normally, you should not have to modify these environment variables.
7.1.2. Executing Scripts¶
Scripts can be run from within the gravis GUI or in batch processing mode
using rfgscript.
Note
Scripts can be written to run both in the gravis GUI and from
the
console; see Section Executing Scripts from the Command Line and from the GUI.
7.1.2.1. Executing Scripts from the Command Line via rfgscript¶
The Python interpreter is available as the standalone tool rfgscript.
rfgscript allows both the embedding of Python scripts into daily build environments
(in batch processing mode) and interactive work with the Python interpreter.
Using rfgscript is essentially the same as using the Python interpreter,
as rfgscript simply redirects to the Python interpreter.
By default, this interpreter is the one included in the Axivion Suite, but you
can set BAUHAUS_PYTHON to choose a different one.
If no script file is specified on the command line, the Python interpreter starts interactively. To exit the interpreter session, press Ctrl + D on GNU/Linux, or Ctrl + Z followed by Enter on Windows.
The -c <command> option executes only the specified command within the Python
interpreter and then terminates. Any command-line options
following this option are ignored.
Passing the -u option to the rfgscript interpreter (or alternatively
setting the PYTHONUNBUFFERED environment variable to any value) instructs
the interpreter to use unbuffered stdout. This may be necessary in environments where
stdout redirection otherwise fails.
Import statements can be traced by specifying the -v option when
invoking the rfgscript interpreter.
Setting the PYTHONSTARTUP environment variable to a readable file
ensures that the file is automatically read and executed by the rfgscript
interpreter at startup.
If a script filename is passed as the first parameter to rfgscript, the
sys.argv variable is populated as if the Python
interpreter itself
were started.
All other Python interpreter command-line switches are also available, as they
are passed directly from rfgscript to the actual Python
interpreter.
The example rfgscript_params.py in Listing Parameter passing to a script
illustrates how parameters are passed to the
executed script.
#!/usr/bin/env rfgscript
# Copyright (C) 2025 Axivion GmbH
# Copyright (C) 2025 The Qt Company GmbH, a subsidiary of The Qt Group
# https://www.qt.io
import sys
# sys.argv contains the (uninterpreted) command line arguments
# of the script
for count, arg in enumerate(sys.argv):
print("parameter %s: %s" % (count, arg))
Executing it as shown below produces the following output:
C:\> rfgscript rfgscript_params.py 1 "hi world" file name.rfg
parameter 0: rfgscript_params.py
parameter 1: 1
parameter 2: hi world
parameter 3: file
parameter 4: name.rfg
7.1.2.2. Executing Scripts from within gravis¶
Python scripting support in gravis is accessible via a command-line
parameter for batch use and via menus for interactive use.
Calling gravis with the --script <file> command-line switch
executes the Python script found in <file> after gravis
starts. Only one --script switch is permitted. If an input file
argument (i.e., an RFG file) is specified at the end of the command line, that file is loaded
before the script is executed. Additional parameters can be passed to the script using one or
more --script-param <parameter> switches. After the script terminates,
gravis remains in interactive mode. You can force gravis to quit after
executing the
script by also specifying the --quit option.
Unlike rfgscript, gravis does not call the Python executable;
instead, it uses the shared Python interpreter library. This library is part of the
official Python distribution.
To locate the shared Python interpreter library, gravis searches for the
interpreter as rfgscript does (described in Section
Executing Scripts from the Command Line via rfgscript) and uses that base directory to find the
library. gravis looks for the Python library
(python315.dll/python314.dll/python313.dll/python312.dll on Windows and
libpython3.15.so/libpython3.14.so/libpython3.13.so/libpython3.12.so on GNU/Linux)
in the Python installation base directory.
If this approach does not select the correct Python interpreter, you can force
gravis to use a specific one by setting the BAUHAUS_PYTHON_LIB
variable to the full path of the alternative Python
interpreter library. This is the gravis equivalent of the
BAUHAUS_PYTHON variable used by
rfgscript.
7.1.2.3. Executing Scripts from the Command Line and from the GUI¶
The code snippet in Listing Importing module gravis conditionally checks whether the script is running in
the gravis GUI. If not, the module name gravis is set to None.
gravis conditionally¶try:
from bauhaus import gravis
except ImportError:
gravis = None
Code sections that must distinguish between the GUI and the console can be implemented as demonstrated in Listing Using module gravis conditionally.
gravis conditionally¶import sys
from bauhaus import rfg
try:
from bauhaus import gravis
except ImportError:
gravis = None
# variable 'view' is the view the script is to operate on
view = None
graph = None
if gravis:
# example of code using gravis
graph = gravis.get_graph()
if graph:
view = gravis.get_clicked_view()
else:
if len(sys.argv) != 3:
print("Usage information here")
sys.exit(1)
graph = rfg.Graph(sys.argv[1])
if not graph.is_view_name(sys.argv[2]):
print("View %s not found in RFG" % sys.argv[2])
sys.exit(1)
view = graph.view(sys.argv[2])
# view can be used from here on
This snippet uses the currently loaded RFG and the currently selected view when called
from within gravis. In batch mode, when called from rfgscript, it loads the
graph from the file given as the first command-line argument and the view given as the second
command-line argument.
7.1.3. Code Completion and Type Checking in an IDE¶
The Python modules bauhaus and axivion are located in the directory
BAUHAUS/lib/scripts. Their native extension modules come with type stubs
(.pyi files), so an IDE can offer code completion and type checking for them as
well.
To make the modules known to your IDE, select rfgscript as the Python
interpreter of your project, i.e., BAUHAUS/bin/rfgscript on GNU/Linux and
BAUHAUS\bin\rfgscript.exe on Windows. In Visual Studio Code, use the command
Python: Select Interpreter and enter the path. In PyCharm, add a local
interpreter by its path. Scripts started or debugged from within the IDE will then run in the
same environment as on the command line.
Alternatively, add BAUHAUS/lib/scripts to the search path of the IDE’s code
analysis, e.g., python.analysis.extraPaths in Visual Studio Code or the
interpreter paths in PyCharm. This provides code completion and type checking only; the
scripts still have to be executed with rfgscript. Setting PYTHONPATH
globally for this purpose is not recommended, as it affects every Python installation
on your machine.
7.1.4. Learning Python¶
The following chapters assume that you have a basic knowledge of the Python programming language. If you are not yet familiar with the basics of Python, please refer, for example, to https://docs.python.org/3/ (we are not affiliated with Python itself).