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”.

The overridden context is a dictionary with a set of elements that describe the desired area.

For example, for the 3D viewport area, the overridden context should be compiled as follows:

The main thing is to specify the desired working area – in our case, the 3D viewport “VIEW_3D” area. Of course, at least one such area must be open in Blender windows.

Having received the structure of the overridden context, we can call the operator, passing it as the first parameter:

The full code that will correctly execute from the Blender Text Editor area and opens a tools panel in the 3D Viewport area:

Nikita

Author: Nikita

Blender add-ons developer and articles writer.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comment
Inline Feedbacks
View all comments