Using Python in Mari

Mari uses Python 3.10.10 and Unicode Character Set (UCS) 4. Most of Mari’s functions are implemented through the use of manager objects, such as mari.projects, mari.menus, and so on.

Accessing Mari’s Python Documentation

Mari's Python Console Palette

Mari's Python Console palette behaves in a very similar way to a standard Python console. To open the Python Console in Mari you can:

click Python Console in the right-hand palette menu,

navigate to View > Palettes > Python Console,

navigate to Python > Show Console in the menu bar or press Ctrl/Cmd+Alt+P, or

right-click in the toolbar area on top of the Mari workspace and select Python Console.

The Python Console is divided into three parts, the output pane, the toolbar, and the input pane. Use the input pane to enter and evaluate Python statements and the output appears in the output pane.

The toolbar manages loading, evaluation, and housekeeping.

Save As

Click to save the contents of the current script tab to disk. A browser is displayed to locate the required file path.

Rename Tab

Click to rename the current script tab to something more descriptive. The tab name is the default name given to the save file when you click Save As.

Evaluate Input Pane

Click to evaluate the contents of the current script tab. You can also use the Ctrl/Cmd+Enter keyboard shortcut. The result is displayed in the output pane.

Evaluate Selected String

Click to evaluate only the highlighted strings in the current script tab. You can also use the Ctrl/Cmd+Shift+Enter keyboard shortcut. The result is displayed in the output pane.

Clear Output Pane

Click to clear the contents of the output pane at the top of the palette.

Clear Input Pane

Click to clear the contents of the input pane at the bottom of the palette.

Clear All

Click to clear the contents of both the output and input panes at once.

Show Python Help

Click to display a browser containing the Mari Python API Help.

Evaluating Statements

Saving Scripts

To save a Python script to disk:

Loading Scripts

To load a script through the Script Path text field:

Custom scripts load from the import directory prior to the scripts being run from the main folder. Mari uses particular order to load modules to ensure the proper, full functionality of the program when using Python scripts.

If existing start-up scripts do not work correctly due to the load module order, you can revert back to the old behavior by setting the environment variable MARI_OLD_PYTHON_INIT to any non-empty string other than 0.

Note:  Mari looks for metadata that is over 250 MB in size and discards anything over this size. This check is performed on project load, and is intended to strip corrupt and problematic data. This affects metadata added using the Python API.

Running Scripts on Start-up

If you would like your script to be automatically executed at start-up, do the following:

Terminal Mode

Mari provides a terminal mode, which allows users to access features through a Python shell on the command line. This is similar to the Python console palette in the Mari GUI, or to a standard Python shell. Terminal mode can be used to perform administrative actions such as archiving projects, exporting textures, and other tasks that do not require graphical interaction from the user.

Terminal mode is not a true "headless mode" because it still requires graphics functionality to run. Mari still initializes its GUI when starting up terminal mode - it just won’t display it. If you try to run a Mari interactive and terminal session at the same time, an error displays on the command line:

MriMainApp.cpp:798 ] : [ !! ] Mari already running somewhere else

Note:  You can run a Mari interactive session and a terminal/execute session concurrently if you have a mari_i interactive and a mari_r render license, which allows you to paint in one project while exporting from another at the same time. See Using a Render License for more information.

You can supply the file name of a Python script to run on the command line, if desired. If supplied, Mari runs the specified script before providing the Python shell input prompt. It is also possible to start in "execute mode", which is the same as terminal mode but exists after running the provided script.

To start terminal mode from a shell, use the following commands:

On Linux

cd /path/to/mari

# Normal terminal mode

./mari -t

# Terminal mode - run the script, then show a Python input prompt

./mari -t /path/to/script_name.py

# Execute mode - run the script, then exit

./mari -x /path/to/script_name.py

On Windows

cd \path\to\mari

# Normal terminal mode

Mari7.1v2.exe -t

# Terminal mode - run the script, then show a Python input prompt

Mari7.1v2.exe -t \path\to\script_name.py

# Execute mode - run the script, then exit

Mari7.1v2.exe -x \path\to\script_name.py

On Mac

cd /path/to/Mari

# Normal terminal mode

./Mari7.1v2 -t

# Terminal mode - run the script, then show a Python input prompt

./Mari7.1v2 -t /path/to/script_name.py

# Execute mode - run the script, then exit

./Mari7.1v2 -x /path/to/script_name.py

Using a Render License

Running Mari in terminal or execute mode searches for a mari_r render license by default. A render license allows you to use Mari in non-interactive mode without the full UI experience. If a render license is not available then Mari looks for a mari_i interactive license.

To force Mari to use a render license, use either of the following arguments:

-r

--render

You can't run two Mari interactive sessions concurrently because there is a risk of cache corruption when two sessions try to write to the same cache location. However, if you have an interactive license and a render license, you can run a Mari interactive session and a terminal/execute session concurrently, which allows you to paint in one project while exporting from another at the same time.

Warning:  Running multiple terminal or execute sessions along side an interactive session is an experimental feature and has no safety mechanism to prevent two sessions writing to the same project cache. A warning message is displayed on the command line if multiple sessions are detected:
IMPORTANT: Render mode allows multiple instances to share the same project location. This has the risk of data corruption if the same project is opened across instances and modified simultaneously.

To launch Mari in terminal or execute mode using a render license, use the following commands:

Linux

./mari -t -r

./mari -x -r

Windows

Mari7.1v2.exe -t -r

Mari7.1v2.exe -x -r

Mac

./Mari7.1v2 -t -r

./Mari7.1v2 -x -r

Tip:  You can also add the -v argument to set the command line output to verbose mode so you can troubleshoot more easily.

Operational logs for terminal and execute sessions are written to the same location as standard Mari logs, but a unique hash is added to the log file for identification. To print the hash for a terminal or execute session, use the following command:

mari.app.logFileName()

Log files are written to the following directories by operating system:

Linux and Mac: /home/<username>/Mari/Logs

Windows: C:\Users\<username>\Documents\Mari\Logs

An example of a Windows log directory containing log files is shown in the image.

Using PySide2 in Mari

Mari ships PySide2 together with its Python scripting engine. PySide2 is a library of Python bindings for Qt, similar to PyQt. For more information on PySide2, visit http://qt-project.org/wiki/PySide.