Python

Setting colors for Blender collections with the Python API

Since Blender 2.9, the ability to assign color tags has been added for collections. Collection with such tag is displaying in a specific color in the outliner.

The collection color tag value is stored in its “color_tag” property. To change the color label of a collection, we need to assign a certain value to this property.

For example, to mark the current active collection in green:

How to find which collection contains the desired object

If the object you need is located in a hidden collection, it can be difficult to find it in large scenes.

To find out in which collection the desired object is located by its type, we need to execute the following. For example for cameras:

This code will return a list of cameras and collections in which they are located.

Context override

Some Blender operators can only be called from the workspace for which they are intended.

If you call from the Python console, for example, an operator intended only for working in the 3D Viewport area, the operator will not be executed:

or it will fail with an error message about the incorrect context:

However, we can still execute operators from a non-native working area. To do this, we can pass the first implicit parameter to any operator – a pointer to the context of the area in which this operator should be executed. This parameter commonly named the “overridden context”.

This method is suitable for Blender version 3.1 and earlier. For Blender 3.2 and later, we must use the temp_override() method.

How to change current tool through the Blender Python API

To switch the active tool from the T-panel in the 3D viewport window, we need to call the appropriate operator and pass the “idname” of the required tool in its “name” parameter.

For example, to enable the “Select Circle” selection toll, we need to call: