7.3. Gravis Scripting

7.3.1. Introduction

You can add extra functionality to Gravis using Python scripts. Scripts can be

  • launched when Gravis starts via a command-line option,

  • added as Gravis menu entries, or

  • launched interactively from the Gravis UI.

Scripts can access the loaded RFG, load files, open new windows, scan and manipulate nodes shown in graph windows, apply layouts, etc. Gravis scripting builds upon RFG scripting (see Section RFG Scripting) and uses some of its Python classes, most notably Graph, View, Node, Edge, NodeSet, and EdgeSet. It also introduces new classes, e. g., Graphical_Node.

You can run a Python script when launching Gravis using the --script<filename> command-line option. The given script is executed immediately after loading any RFG and GXL files mentioned on the command-line. Parameters can be passed to the script via one or more --script-param<parameter> options. If the --quit option is also specified, Gravis executes the script in non-interactive mode and quits upon completion. This allows Gravis to be used in a batch-processing environment, e.g., to generate PDF or PNG diagrams during a build process.

The section titled Adding Scripts to Menus, Shortcuts and Events describes how to add Python scripts as new menu entries to Gravis.

7.3.1.1. Introductory Example: Generating a Report as CSV File

Before detailing the Gravis scripting interface, let us start with a small example illustrating a common use case.

The script shown in Listing Example script implementing a CSV export of the attributes of the currently selected nodes generates a report in CSV format. If started from within Gravis, the currently selected nodes in the clicked view are exported.

Example script implementing a CSV export of the attributes of the currently selected nodes
# Copyright (C) 2025 Axivion GmbH
# Copyright (C) 2025-2026 The Qt Company GmbH, a subsidiary of The Qt Group
# https://www.qt.io
# pyright: standard


"""Exports attribute values of a set of nodes to a CSV file."""
import csv
import os.path
import sys

try:
    from bauhaus import gravis
except ImportError:
    gravis = None


def export_as_csv(filename, attribute_name_list, nodes):
    """Exports the attribute values of attributes in
    attribute_name_list in CSV format for all nodes to filename."""
    csv_writer = csv.writer(
        open(filename, 'w', encoding="utf-8"),
        delimiter=';',  # suitable for German Excel
        quotechar='"',
        quoting=csv.QUOTE_MINIMAL,
    )
    # write header row
    row = []
    for attribute_name in attribute_name_list:
        row.append(attribute_name)
    csv_writer.writerow(row)

    # write body rows
    for current_node in nodes:
        row = []
        for current_attribute_name in attribute_name_list:
            try:
                attr_value = current_node[current_attribute_name]
                row.append(attr_value)
            except KeyError:
                row.append('')
        csv_writer.writerow(row)


if __name__ == '__main__':
    if gravis:
        # if called from within gravis, export Name, File, and Line
        # attributes of the selected nodes in the clicked view
        if not gravis.graph_loaded():
            gravis.error('Need an RFG,\n' 'so please load an RFG file first.')
        elif not gravis.get_clicked_view():
            gravis.error(
                'Need a view,\n'
                'so please run this script from the context menu '
                'of a graph window.'
            )
        else:
            clicked_view = gravis.get_clicked_view()
            assert clicked_view is not None
            nodes = gravis.selected_nodes(clicked_view)
            doit = True
            if not nodes:
                doit = gravis.warning(
                    'There are no nodes selected,\n'
                    'so the resulting file will contain\n'
                    'no attribute data.'
                )
            if doit:
                filename = gravis.get_string(
                    "Query from Gravis Pro",
                    "Full output path",
                    "Please enter a filename to save to",
                )
                if filename is not None and (
                    not os.path.exists(filename)
                    or gravis.confirmation(
                        'File "%s" already exists, overwrite?' % filename
                    )
                ):
                    export_as_csv(
                        filename, ('Source.Name', 'Source.File', 'Source.Line'), nodes
                    )
    else:
        print('this demo is not intended for command line usage')
        sys.exit(1)

We will go through the script line by line:

lines

description

7

A docstring explains the purpose of the script.

8 – 10

Imports the csv, os.path, and sys modules.

12 – 15

Imports gravis if running within the Gravis UI. In batch mode, gravis is None.

18–42

Definition of the export_as_csv function, which performs the export.

18

Parameters (and variables) are not typed.

19–20

A docstring explains the purpose of the function. The docstrings for this module and its functions are displayed in an interactive interpreter session using help(export_as_csv) after the module has been imported.

21–26

Creates a CSV writer to write to a file opened for writing ('w'). The quoting characteristics of the CSV writer are also configured.

27 – 31

Writes the header row. row is a local variable containing an empty list. The for-loop iterates through the names in the attribute_name_list parameter. append is a list method used to add an element to the list, and writerow exports a list as a line in the CSV file.

33 – 42

Writes the body rows.

34

A for-loop can iterate over any object that implements the iterator interface. Since a set of nodes (a NodeSet) implements this interface, it can be iterated like a standard Python list or set.

38

Node and edge attributes can be queried and set as if the node were a dictionary. This results in a KeyError if no value is set, so the case where an attribute is missing must be handled. In this example, a try-except statement is used, though the condition if current_attribute_name in current_node: could also be used.

45 – 85

This section is executed if the script is started standalone. If the script is imported into another script, the export_as_csv function can be used. If called from within the Gravis UI, the currently selected nodes are exported; if started outside Gravis, the script only prints a message.

This allows interactive usage, test code, and library code to be combined in one file without clutter.

49

Checks if an RFG is loaded: gravis.graph_loaded() returns True if an RFG graph is displayed in Gravis.

51

The script context must provide a view: gravis.get_clicked_view() returns None if the script was not started from the context menu of a graph window or view.

60

Since a graph and a view are available, the script proceeds to get the selected nodes.

61

doit is used to cancel CSV generation if no nodes are selected.

62

If no nodes are selected, the user is prompted for action.

69 – 79

Prompts the user for a filename and asks for permission to overwrite if the file already exists.

80

Executes the export of the attributes Source.Name, Source.File, and Source.Line, using the attribute names as displayed in the Gravis UI.

7.3.1.2. The Gravis Scripting Environment

To access Gravis GUI functionality, import the bauhaus.gravis module (see Listing Import module gravis).

Import module gravis
from bauhaus import gravis

After a script completes, the View Box and all graph windows are updated to reflect any changes. For example, newly created views appear in the View Box, new nodes and edges appear in open graph windows, and graph windows are updated to show changes in selection or marking.

Caution

You can access Gravis-related features from within Gravis, but not from within rfgscript. Attempting to import gravis within a script executed by rfgscript will raise an ImportError, which can be caught.

Scripts can be written to use the Gravis GUI when run within Gravis and the console when run via rfgscript. To do so, use the following code at the start of the file:

try:
    from bauhaus import gravis
except ImportError:
    gravis = None

Later in the script, you can then perform different actions depending on the execution environment. For instance, the script could open a graphical window and apply a layout to it when run inside Gravis, as opposed to displaying text-only output when run via rfgscript.

if gravis:
    # handle the gravis stuff
else:
    # handle the console stuff

7.3.2. Scripting Interface Reference

Version numbers after functions indicate the version in which the function was added. Functions without version numbers were added before Bauhaus 6.5.0.

7.3.2.1. Global Functions

The RFG Displayed in Gravis

get_graph()
get_graph_filename() (7.7.4, 7.6.9)
new_graph() (6.9.10)
graph_loaded()
close_graph()
open_file(name)
open_rfg_buffer(buffer) (7.0.0)

The function get_graph returns the currently loaded RFG graph. It returns None if no RFG is loaded in the Gravis GUI. The convenience function graph_loaded returns a boolean value indicating whether a graph is loaded. get_graph_filename returns the filename of the loaded graph, or an empty string if there is no graph or only a newly created graph that has not yet been saved. close_graph closes and discards the currently loaded graph, if there is one. open_file opens the file name and returns a boolean indicating whether the operation succeeded. open_rfg_buffer opens an RFG file stored in a memory buffer (of type bytes or bytearray) and returns a boolean indicating whether the operation succeeded. new_graph creates a new empty graph with the default schema and returns it as a Graph object.

Caution

If an RFG graph was already loaded when calling open_file, open_rfg_buffer, or new_graph, it is discarded. Any references held by the script to the graph itself, to views, nodes, or edges within that graph, or to graphical nodes in open windows, become invalid. If Gravis is run in non-interactive mode (with --quit), the existing graph is closed without a prompt, even if it was modified. The same caveats apply to calling close_graph.

Configuration Information

get_configured_label_font_family() (7.11.5)

get_configured_label_font_family returns the configured font family to use for node and edge labels in graphical views, as specified in the /Tools/Gravis/graph_view/label_font_family configuration option. This is a comma-separated list of font family names, including the pseudo-fonts serif, sans-serif, monospace, and gui.

Context Information

get_script_context() (6.5.2)
argv() (6.8.4)

Scripts can determine the context in which they were launched. get_script_context returns one of the following strings:

  • 'Startup': The script was run by launching Gravis with the --script switch.

  • 'Main': The script was run from the Main menu.

  • 'View': The script was run from the context menu over a view in the View Box.

  • 'Window': The script was run from the context menu of a graph window.

  • 'Internal': The script implements internal Gravis functionality and was not launched by the user.

When launching a script from the command line using the --script switch, extra parameters can be passed using any number of --script-param <parameter> switches. The script can obtain the list of parameters by calling argv(). Similar to sys.argv, the first entry of the returned list is the full filename of the script; the remaining entries are the extra parameters specified via --script-param.

get_clicked_view()
get_primary_view()

Many Gravis operations are view-specific, such as selection operations. If a script was launched from the context menu of a graph window, or from the context menu of a view in the View Box, there is a clicked view, which can be obtained by calling get_clicked_view. If the script was run from the main menu, None is returned.

The view shown in the currently active window can be obtained by calling get_primary_view. This works in scripts launched via the main menu or the context menu of a graph window. If the current window has no associated view (e. g., a source code window), it returns None.

Caution

If the active window is a Mapping dialog, which shows two views in a split display, get_primary_view returns the conceptual view edited in the dialog (i.e., the mapping view). If the user launched the script from the context menu over one of the sides of the dialog, get_clicked_view returns the view associated with that part of the dialog.

The returned view is a Python object of class rfg.View.

get_selected_views() (6.5.6)

get_selected_views returns the views selected in the View Box as a list of rfg.View objects. This list can be empty if no views are selected. If the View Box is not the active window, None is returned instead.

This method facilitates implementing scripts that operate on more than one view. Users can conveniently pass views as parameters to the script by selecting them in the View Box before running the script. This is particularly useful for scripts that are added to the View Box context menu.

get_selected_node_type_name() (6.6.3)
get_selected_edge_type_name() (6.6.3)

get_selected_node_type_name returns the name of the selected node type in the Node Types panel. An empty string is returned if the current window is not a diagram window or if no type is selected.

get_selected_edge_type_name returns the name of the selected edge type in the Edge Types panel. An empty string is returned if the current window is not a diagram window or if no type is selected.

get_clicked_node() (6.6.3)
get_clicked_node_symbol() (6.6.3)
get_clicked_edge() (6.6.3)

get_clicked_node returns the node on which the user clicked to launch the script, or None if the script was not launched from the context menu of a node in a diagram window (including its expanded background). The returned node is an instance of class rfg.Node.

get_clicked_node_symbol returns the node symbol on which the user clicked to launch the script, or None if the script was not launched from the context menu of a node symbol in a diagram window (excluding its expanded node background). The returned node is an instance of class rfg.Node.

get_clicked_edge returns the edge on which the user clicked to launch the script, or None if the script was not launched from the context menu of an edge in a diagram window. The returned edge is an instance of class rfg.Edge.

get_current_window_type() (6.9.6)
get_windows() (6.9.6)
activate_window(window_index) (6.9.6)

get_current_window_type returns the type of the currently active Gravis window. Possible return values are: none (if there is no active window), viewbox, diagram.graphical.flat (a flat graph window), diagram.graphical.hierarchy (a hierarchical graph window), diagram.tree, diagram.split (for the mapping dialog), diagram.edge_list.basic (for the list of edges shown when double-clicking on a summary edge), diagram.edge_list.violations (for the violations and/or conformances list of architecture analysis), info.node, info.edge, info.view, info.graph (for node/edge/view/graph info windows), source (for source code windows). The global variable window_types lists these values.

get_windows returns the list of open Gravis windows. Each window is represented by a named tuple of four strings with the fields type (values as for get_current_window_type), title, primary_view and layout. The primary_view field is only applicable to window types starting with diagram. and the layout field is only applicable to window types starting with diagram.graphical. Fields that are not applicable are empty. Graphical windows without a persistent layout have an empty layout field.

activate_window activates the window at the given index in the list returned by get_windows.

Opening Graph Windows

open_view(view_or_name, diagram_type)
open_view(view_or_name, diagram_type, layout = None) (6.9.6)
get_layouts_for_view(view_or_name) (6.9.6)
extract_selection(view, bool strict = True)
extract_marking(view, bool strict = True)

open_view opens the view view_or_name (specified either as a name using a string or as an rfg.View object) as a new window of type diagram_type. It does nothing if there is no graph or if the named view does not exist. The global variable diagram_types lists the valid values for diagram_type:

  • 'flat' opens a graphical window without a node hierarchy.

  • 'hierarchy' opens a graphical window showing a hierarchy of nodes.

  • 'tree' opens a tree display (always hierarchical).

The name of the persistent layout with which the view should be opened can be passed in layout. The special name _default_ opens the view with the default persistent layout. The default value of None opens the view without a persistent layout (as in versions before 6.9.6, which do not support this parameter).

open_view returns True if the window has been opened successfully. In that case, the new window becomes the active window and the view becomes the clicked view.

get_layouts_for_view returns a list of the names of the persistent layouts that exist for the given view, with the default layout first.

extract_selection opens a new window for view (specified as a View object, not a name) containing only the selected nodes and edges. If strict is True (default), only selected entities are included in the new diagram; selected edges whose endpoints are not both selected are omitted. If strict is False, at least all selected entities are included in the new diagram, meaning unselected endpoints of selected edges are also included. Returns True on success; in that case, the new window becomes the active window and view becomes the clicked view.

extract_marking works like extract_selection, except that it extracts the marked nodes and edges in view instead of the selected nodes and edges.

Caution

Even though the marking is global and not associated with a particular view, extract_marking only extracts the marked elements of the specified view, not all marked elements.

See below for operations to handle the selection/marking.

Architecture Analysis

run_architecture_analysis(architecture_view, dependency_view, hierarchy_view, mapping_view, output_view_name, options) (7.10.7, 7.11.2, 7.12.0)
Architecture_Analysis_Options(…) (7.10.7, 7.11.2, 7.12.0)

run_architecture_analysis runs the architecture analysis described in Performing Architecture Analysis and stores the result in a new view named output_view_name. The four views are passed as rfg.View objects, not as names. dependency_view corresponds to the Base view of the Analysis dialog. output_view_name must not be the name of an existing view. The function returns an error message if the analysis cannot be run; otherwise, it returns the empty string.

options is an Architecture_Analysis_Options object. This dataclass has the following fields, which correspond to the parameters of the Analysis dialog:

Field

Default

Dialog parameter

root_edge_type_name

'Source_Dependency'

Type hierarchy

forward_declarations

False

Declaration forwarding

declaration_forwarding_edge_type_name

'Declare'

edge type for Declaration forwarding

allow_dependencies_to_parents

False

Allow dependencies to parents

add_violating

True

Add violating edges

add_conforming

False

Add conforming edges

lift_result_view_causing_edges

True

Lift violating/conforming edges

lift_result_view_reflexion_edges

True

Lift architecture check result edges

add_all_hierarchy_nodes

False

Add all nodes from hierarchy

open_result_view

False

Open graphical result view

open_result_list

True

Open result list

At least one of add_violating and add_conforming must be True.

graph = gravis.get_graph()
code_facts = graph.view('Code Facts')
options = gravis.Architecture_Analysis_Options(add_conforming=True)
error = gravis.run_architecture_analysis(graph.view('Architecture'), code_facts,
                                         code_facts, graph.view('Mapping'),
                                         'Architecture Check', options)
if error:
    print(error)

User Interaction

get_string(title[, label[, explanation]])
get_string(title[, label[, explanation[, echo_mode]]]) (7.0.11, 7.1.9, 7.2.4)
get_choice(title, label, explanation, choices_list) (6.6.1)
get_choice(title, label, explanation, choices_list, *, as_buttons=False, allow_cancel=True) (7.7.8, 7.8.2)
get_string_choice(title, label, explanation, choices_list, *, as_buttons=False, allow_cancel=True) (7.7.8, 7.8.2)
select_file(title=’Select File’, default_dir=’’, filters=’All files (*.*)’, dir_only=False, must_exist=False) (6.7.3)

The get_string function displays a modal dialog asking for user input. The title, label, and explanation strings are shown in the title bar, next to the input field, and underneath the input field, respectively. If the user cancels the input procedure (either by clicking the Cancel button or by closing the dialog), None is returned. The echo_mode parameter controls whether the entered characters are shown normally (echo_mode 'normal', the default), not at all ('no_echo'), or as platform-specific password mask characters ('password').

get_choice prompts the user to select one entry from a list of alternatives. choices_list should be a list of strings to display. Other parameters are the same as those for get_string. The explanation parameter is optional and can be left empty if nothing should be displayed beneath the list. It returns the 0-based index of the chosen alternative, or -1 if the user cancels the dialog. If as_buttons is True, the alternatives are presented as a row of push buttons; this is not suitable for a large number of alternatives. If allow_cancel is False, the dialog has no Cancel button, and the user cannot close it by clicking the window’s close icon.

get_string_choice works similarly to get_choice, but instead of an index, it returns the chosen string directly, or None if the user cancels the dialog.

select_file displays a file chooser with title in the title bar. It shows the contents of the default_dir directory (which may be empty, in which case the most recently used directory is shown), filtered by filters. Several filters can be specified, separated by “ ;;“, e. g., “ PNG files (*.png);;JPEG files (*.jpg)“. If dir_only is True, only directories can be selected. If must_exist is True, only existing files or directories can be selected. If must_exist is False, the location is assumed to be for saving; therefore, the user can enter non-existent file or directory names, and selecting an existing file will trigger an “overwrite” warning. An empty string is returned if the user cancels the dialog.

information(message)
confirmation(message)
warning(message)
error(message)

These four information functions display a modal window with the specified message. information and error only display an OK button and return no status. confirmation and warning also display a Cancel button and return True if the user confirms the message by clicking OK, and False if the user clicks Cancel.

Listing Message display example shows an example.

Message display example
# Copyright (C) 2025 Axivion GmbH
# Copyright (C) 2025 The Qt Company GmbH, a subsidiary of The Qt Group
# https://www.qt.io


from bauhaus import gravis

gravis.information("A neat information about what I do now...")

if gravis.confirmation("Should I do it really that way?"):
    gravis.information("Yes, I will do it this way!")
else:
    gravis.information("No, I will not do it this way!")

if gravis.warning("Should I proceed despite of errors?"):
    gravis.information("Yes, I will keep going!")
else:
    gravis.information("No, I will abort!")

gravis.error("Something serious happened")

Exiting Gravis

exit(rc=0)

Calling exit terminates the current Gravis process. The integer rc is passed to the operating system as the process return code. If rc is omitted or is not an integer, the return code is 0.

Note

A call to sys.exit terminates the Gravis process.

7.3.2.2. The Selection

Querying the Selection

selected_nodes(view)
selected_edges(view)

selected_nodes and selected_edges return the selected nodes or edges in view (specified as an rfg.View object). view must belong to the loaded RFG (as returned by get_graph).

Modifying the Selection

select_all_nodes_in_view(view)
select_all_edges_in_view(view)
select_all_in_view(view)
deselect_all_nodes_in_view(view)
deselect_all_edges_in_view(view)
deselect_all_in_view(view)
select_nodes(view, node_set)
set_nodes_selected(view, node_set, b)
select_edges(view, edge_set)
set_edges_selected(view, edge_set, b)

select_all_nodes_in_view, select_all_edges_in_view, select_all_in_view, deselect_all_nodes_in_view, deselect_all_edges_in_view, and deselect_all_in_view select or deselect all nodes, edges, or elements in view, respectively.

select_nodes selects the nodes in node_set (specified as an rfg.NodeSet object) within view. It clears the current selection first, effectively replacing it with the nodes in node_set. To add to the previous selection, use set_nodes_selected instead.

set_nodes_selected sets the selection status of the nodes in node_set within view to b (True means selected, False means not selected). Unlike select_nodes, this does not replace the existing selection; it can be used to add to an existing selection or remove nodes from it.

select_edges works like select_nodes, but for edges. set_edges_selected works like set_nodes_selected, but for edges. The edge_set parameter is specified as an rfg.EdgeSet object.

The view must belong to the loaded RFG (as returned by get_graph()). The nodes and edges in node_set and edge_set must be visible in the view.

7.3.2.3. The Marking

Querying the Marking

marked_nodes()
marked_edges()

marked_nodes and marked_edges return the marked elements in the graph.

Modifying the Marking

unmark_nodes()
unmark_edges()
unmark_all()
mark_nodes(node_set)
mark_edges(edge_set)

unmark_nodes, unmark_edges, and unmark_all unmark all nodes, edges, or elements in the graph.

mark_nodes marks the nodes in node_set (specified as an rfg.NodeSet object). It clears existing marked nodes first, effectively replacing them with the nodes in node_set.

mark_edges performs the same operation for edges. The edge_set parameter is specified as an rfg.EdgeSet object. The nodes and edges passed to mark_nodes and mark_edges, respectively, must belong to the loaded RFG (as returned by get_graph).

Operations on Marked Elements

delete_marking()

delete_marking permanently deletes the marked elements from the graph.

7.3.2.7. Operations for Graphical Windows

The functions in this section apply only to graphical windows (as opposed to other types of graph windows, e.g., tree windows). Graphical windows contain the graphical representations of RFG nodes and edges. An invisible graphical root node exists whose children are all top-level nodes. The graphical root and graphical nodes share a common interface (e.g., both support the get_children method), allowing the entire graphical hierarchy to be traversed using a single recursive scan starting from the root.

Graphical Nodes

get_graphical_root()
get_clicked_graphical_node()
get_clicked_graphical_node_symbol()
find_graphical_node(dg_node) (6.6.1)

get_graphical_root returns the invisible root node of the current graphical window as an instance of Graphical_Root. Returns None if there is no current graph window or if the current graph window is not a graphical window.

get_clicked_graphical_node returns the graphical node clicked by the user to launch the script, or None if the script was not launched from a graphical window or from the context menu of a node (including its expanded node background). The returned node is an instance of the Graphical_Node class.

get_clicked_graphical_node_symbol returns the graphical node symbol clicked by the user to launch the script, or None if the script was not launched from a graphical window or from the context menu of a node symbol (excluding its expanded node background).

find_graphical_node returns the graphical representation of dg_node in the current window. The returned node is an instance of the Graphical_Node class. Returns None if the current window is not a graphical diagram, dg_node is not visible in the current view, or if the graphical representation of dg_node has not yet been created (e.g., if the node is inside a collapsed parent that has not been expanded).

Node Hiding

hide_selection()
unhide_selection()
unhide_all()

hide_selection hides all selected nodes and edges from the current graph window. unhide_selection makes all selected nodes and edges that were hidden using hide or hide_selection visible again in the current window. unhide_all() makes all nodes and edges hidden using hide or hide_selection visible again in the current window.

Caution

Whether a node is displayed in a graph window depends on more than just its “hidden” state. In a hierarchical graphical window, the children of a node that is not expanded are not displayed, regardless of whether they are hidden or not.

Exporting Window Contents

save_as_png(filename)
save_as_pdf(filename)
save_as_svg(filename) (6.8.3)
save_as_svg(filename, include_markup=False) (7.10.6)
create_svg_data() (7.0.0)
create_svg_data(include_markup=False) (7.10.6)

save_as_png saves the current window as a PNG bitmap file at the current zoom factor to filename. Returns True if this succeeded.

save_as_pdf and save_as_svg save the current window as a PDF/SVG file to filename. The output is vector-based and independent of the current zoom factor. Returns True if this succeeded.

create_svg_data returns the SVG representation as a byte string (empty in case of an error or if the current window is not a graphical window).

If include_markup is True, save_as_svg and create_svg_data wrap each node and each edge into an annotated <g> element. Each node group has a unique id attribute, an rfg:node-type attribute and, if the node has a Source.Name value, an rfg:name attribute. Each edge group has rfg:source-id and rfg:target-id attributes containing the id attributes of its source and target nodes, and an rfg:edge-type attribute, which virtual edges do not have.

Other Operations for Graphical Windows

load_layout(layout_filename) (6.5.1)
save_layout(layout_filename) (7.5.11, 7.6.7, 7.7.2)
update_persistent_layout() (7.12.6)
create_persistent_layout(name) (7.12.6)
revert_persistent_layout() (7.12.6)
switch_persistent_layout(name) (7.12.6)
zoom_to_graph() (6.5.1)
zoom_to_100() (6.5.1)
set_zoom(zoom_factor) (6.9.9)
get_zoom() (6.9.9)
set_center(x, y) (6.9.9)
get_center() (6.9.9)
center_on_node(graphical_node) (6.9.9)
zoom_to_selection() (6.5.1)
zoom_to_marking() (6.5.1)
set_visualization_option(option_name, state) (6.6.1)

load_layout applies the saved layout from layout_filename to the current graphical window. The file must be in GVL format (file extension .gvl). Returns True if loading succeeded.

save_layout saves the layout of the current graphical window to layout_filename in GVL format. Returns True if saving succeeded. It is recommended that layout_filename ends with .gvl.

The following four routines operate on the persistent layout of the current graphical window (see Working with persistent layouts).

update_persistent_layout saves the layout of the window to the persistent layout it is displaying, like Layout storage → Update layout. Returns True if the persistent layout is stored on disk afterwards, False if the window is not displaying a persistent layout or if saving failed.

create_persistent_layout creates a new persistent layout called name from the layout of the window and makes the window display it, like Layout storage → Create new layout.... Returns True if the new layout was saved. Returns False if name is empty, starts with an underscore (_) or is already used by a layout of the displayed view, and also if saving the layout failed.

revert_persistent_layout restores the saved state of the persistent layout displayed in the window, discarding any unsaved layout changes, like Layout storage → Revert layout.

switch_persistent_layout makes the window display the existing persistent layout called name, like Layout storage → Switch layout. In contrast to the menu entry, unsaved changes to the layout displayed so far are discarded without asking. Returns True for success. Returns False if there is no saved layout called name for the displayed view, if that layout is already open in another window, or if it does not match the type of the window (flat or hierarchical).

zoom_to_graph zooms the window in such a way that the entire graph is visible.

zoom_to_100 resets the zoom factor to its default (100%).

set_zoom sets the zoom factor of the current graphical window to the specified floating-point value zoom_factor. A zoom factor of 1.0 corresponds to 100%.

get_zoom returns the zoom factor of the current graphical window as a floating-point value. A zoom factor of 1.0 corresponds to 100%.

set_center scrolls the current graphical window to show the given scene position (x, y) in its center, if possible. This may not be possible if the window is zoomed out and/or the position is close to the edge of the diagram. You can obtain a graphical node’s scene position via its get_scene_pos() method.

get_center returns the scene position shown in the center of the window as a floating-point tuple (x, y).

center_on_node scrolls the current graphical window to show the given graphical node graphical_node in its center, if possible. This may not be possible if the window is zoomed out and/or the node is close to the edge of the diagram. Note: This requires a graphical node, not a DG node. You can obtain a graphical node for a DG node via find_graphical_node().

zoom_to_selection/zoom_to_marking zoom the window in such a way that the selection/marking is visible.

set_visualization_option controls the options in the Visualization sub-menu. Possible values for option_name are: 'muted_edges', 'transparent_edges', 'edges_behind_nodes', 'show_lifted_edges' (only has an effect if the displayed view has lifted edges), 'transparent_backgrounds', 'show_node_names', 'show_attributes'. The state can be True or False to switch the given option on or off, respectively. The global variable visualization_options lists these names and the names 'add_edge_id_markup' and 'add_full_markup', which are used internally for SVG export.

Graphical Node Classes

The Gravis scripting interface introduces two new classes: Graphical_Root and Graphical_Node. Both can have child nodes and therefore share a common interface for managing them. This simplifies recursive scanning of the node hierarchy, as top-level and child nodes are handled in the same way. The graphical root and graphical nodes are referred to as graphical areas.

Common interface for Graphical_Root and Graphical_Node

The following graphical area interface is implemented by Graphical_Root and Graphical_Node:

get_parent(self)
is_visible(self)
is_user_hidden(self)
is_expanded(self)
get_child_size(self)
get_children(self, include_invisible = False)
remove_overlaps(self)
layout_children(self, layout_name)

get_parent returns the parent area of the current graphical area. This can be an instance of Graphical_Node (for nested nodes), Graphical_Root (for top-level nodes), or None (for the graphical root).

is_visible returns True if this graphical area is visible. The graphical root is always visible.

is_user_hidden returns True if this graphical area has been hidden. Hidden elements are always invisible; however, elements that are not hidden are not necessarily always visible, as they could still be inside a collapsed parent or subject to dynamic filtering. The graphical root cannot be hidden.

is_expanded returns True if this graphical area is expanded. The graphical root is always expanded.

get_child_size returns the size of an unexpanded child node of this area as a tuple (width, height) (in pixels at 100% scaling). Nested nodes usually have a smaller child size than their parents.

get_children returns a list of all child nodes of this area in an arbitrary order, not necessarily in z-order. These are instances of Graphical_Node. If include_invisible is True, invisible nodes (those hidden by the user or with a dynamically hidden type) are also included.

remove_overlaps removes overlaps between child nodes by repositioning them. Returns True if any of the nodes were moved.

layout_children applies a named layout to all child nodes. Possible values for layout_name are: 'Grid', 'Connected grid', 'Step', 'Sawtooth', 'Lineup X', 'Sawtooth X', 'Lineup Y', 'Circle', 'Circle filled', 'Stretch XY', 'Stretch X', 'Stretch Y', 'Tighten XY', 'Tighten X', 'Tighten Y', 'Flip X', 'Flip Y', 'Tilt left', 'Tilt right', 'Rotate left', 'Rotate right', 'Rotate by', 'Sugiyama', 'Spring-electrical', 'Component grid 4:3', 'Component grid 8:9', and 'Component grid 16:9'. Returns False if layout_name is not among these names.

These methods implement the layouts of the Layout operations context menu in Gravis.

Caution

'Rotate by' opens a dialog box asking the user for the angle of rotation; therefore, this layout is likely of limited use in scripting.

Note

You can implement custom layout algorithms by setting node positions directly using the graphical node interface described below.

The Graphical_Root Class

There is only one instance of Graphical_Root per graphical window. It can be retrieved by calling get_graphical_root (see Section Graphical Nodes). Graphical_Root objects implement the common graphical area interface (see Section Common interface for Graphical_Root and Graphical_Node) and offer no additional methods.

The Graphical_Node Class

The following methods manage the graph structure:

get_parent_node(self)
get_dg_node(self)
get_successors(self, include_invisible = False)
get_predecessors(self, include_invisible = False)
get_typed_successors(self, include_invisible = False)
get_typed_predecessors(self, include_invisible = False)

get_parent_node returns the parent node of this node, or None if this node is at the top level. This should not be confused with get_parent in the common graphical area interface (see Section Common interface for Graphical_Root and Graphical_Node).

get_dg_node returns the RFG node with which this graphical node is associated. This is an instance of class rfg.Node.

get_successors returns a list of all successor nodes of this graphical node, in an arbitrary order. These are also instances of Graphical_Node. It only returns nodes that are currently visible (not individually hidden by the user, not of a type that is currently hidden, and not inside a collapsed parent/ancestor) and that are reachable via a visible edge, unless include_invisible is True, in which case node and edge visibility are ignored.

get_predecessors returns a list of all predecessor nodes of this graphical node, in an arbitrary order. These are also instances of Graphical_Node. See get_successors for the meaning of the include_invisible flag.

get_typed_successors returns the typed successors of the node, i.e., successors annotated with the types of the edges leading to them. For each outgoing edge, the returned list contains a pair (t, s), where t is the type of the edge and s is the target node of the edge. The list is returned in an arbitrary order. If there are multiple edges from this node to another node, then each of the edges appears as a separate list entry. See get_successors for the meaning of the include_invisible flag.

get_typed_predecessors returns the typed predecessors of the node, similar to get_typed_successors.

The following methods manage graphical properties:

is_selected(self)
is_marked(self)
get_pos(self)
set_pos(self, x, y)
get_scene_pos(self)
get_symbol_size(self)
get_size(self)
get_bbox(self) (7.6.10, 7.7.4)
expand(self)
collapse(self)
hide(self)
unhide(self)
bring_to_front(self)
put_to_back(self)
shrink_to_fit(self)
set_expanded_size(self, width, height)

Note

The base unit for all position and size values is a screen pixel at 100% scaling; all values are scaling-independent.

is_selected returns True if this graphical node is selected.

is_marked returns True if this graphical node is marked.

get_pos returns the position of this graphical node within its parent as a floating- point tuple (x, y).

set_pos sets the position of this node within its parent node. If the parent node is a graphical node, then x and y should not be negative. If the parent node is the graphical root, then negative coordinates are allowed.

get_scene_pos returns the absolute position of this graphical node in the complete scene as a floating-point tuple (x, y). Scene coordinates are suitable for passing to set_center().

get_symbol_size returns the size of the node symbol (independent of the node’s expansion state) as a floating-point tuple (width, height).

get_size returns the current size of the node based on its current expansion state as a floating-point tuple (width, height).

get_bbox returns the graphical bounding box of the node as a floating-point tuple (x, y, width, height). This may be larger than the value returned by get_size if there are long labels. x and y are in the node’s own coordinate space and can be negative (e.g., x is negative if the node is collapsed and has a centered label, and y is negative if there is a label at the top).

expand expands the node if it is not already expanded.

collapse collapses the node if it is not already collapsed.

hide hides the node. It will no longer be shown in the diagram until it is restored using unhide. This has no effect if the node is already hidden.

unhide unhides a previously hidden node so that it is shown in the diagram again (other circumstances allowing). This has no effect if the node was not hidden.

bring_to_front moves the node to the top of the stacking order relative to its siblings. This is only effective if the nodes overlap.

put_to_back moves the node to the bottom of the stacking order relative to its siblings. This is only effective if the nodes overlap.

shrink_to_fit shrinks the node’s expanded size to fit its children. This should only be called for nodes that are currently visible and expanded.

set_expanded_size sets the width and height of the node’s expanded area. Note: The area may be enlarged to encompass all child nodes.