Scripting in Blender

In the following, it is explained how to get started with scripting and using pyddg in the Blender GUI with Blender 3.6.

Note

The Blender docs provide a detailed overview of the Blender GUI under Blenders User Interface. You can find detailed information on Blenders python API in Blenders Python API docs.

Launching Blender

Start Blender according to your OS:

Linux and WSL

export PATH="$HOME/blender/blender-3.6.21:$PATH"
export PYTHONPATH="venv/lib/python3.10/site-packages/"
source venv/bin/activate
blender

MacOS

export PATH="/Applications/Blender-3.6.21-pyddg.app/Contents/MacOS/:$PATH"
export PYTHONPATH="venv/lib/python3.10/site-packages/"
source venv/bin/activate
Blender

Windows

"C:\Program Files\Blender Foundation\Blender 3.6\blender.exe"

or by double-clicking on blender.exe or the Blender icon on your Desktop.

Warning

If you close the terminal from which you started Blender, it will kill Blender instantly without saving!

Note

For MacOS, Linux and WSL you need to cd into the directory where you have created your virtual environment. You may need to change the path to Blender, if you have installed it in a different location or a different version.

Output and Error Messages

The outputs and error messages of scripts executed within Blender are printed in the terminal you started Blender with. Error messages also appear in Blender’s info editor (see below), which is not the case for outputs of print statements.

On Windows you can activate a console using the Menu: Window > Toggle System Console.

Executing Python Scripts via Command Line

Blender is usually (and best) started from a console, see Scripting in Blender. Executing a python script in Blenders GUI over and over again may lead to unwanted leakage, caching and unintentionally influencing Blender’s state. Therefore it is recommended to always execute a python script with a new instance of Blender. This can be done by using the --pyhton flag.

blender --python /path-to-python-file/file.py

For Windows and Mac change the blender call to the corresponding call listed in Scripting in Blender.

Executing Blender in the background

If you don’t need Blender’s GUI, for example when directly rendering from a Python script, you may want to run Blender in the background to save extra memory.

blender --background --python /path-to-python-file/file.py

For Windows and Mac change the blender call to the corresponding call listed in Scripting in Blender.

Reducing the number of Blender objects

In our experience Blender is better at handling fewer objects with dense meshes compared to many objects with coarse meshes. Therefore it might make sense to join multiple objects into one object. Nets can be joined in NetCollection s. Halfedge objects can be joined by using the function union(). You can manually join objects in Blender by selecting them and pressing Ctrl + J. Alternatively you do this via script using the function join() that joins meshes or objects with mesh data.

Preventing Animations to Crash during Rendering

In addition to the tips and tricks above you can find more hints for more stable rendering of animations in the Rendering guide under Animation.

Debugging when Blender crashed

If Blender still crashes, Blender’s command line tool offers a lot of options for debugging and getting more informative debugging messages. Run

blender --help

to get an overview. For Windows and Mac change the blender call to the corresponding call listed in Scripting in Blender. Also see Blender Gotchas.