To switch the active tool using the Blender Python API, we must use the wm.tool_set_by_id operator, passing the identifier of the required tool in its parameter. In earlier versions of Blender, to call this operator from an area with a different context, for example, from the Text Edit area, it was necessary to override the context.
In Blender 3.6 this operator can be executed either by temporarily overriding the context with “temp_override”, or by passing the type of the area for which the operator is called in its parameter.
Executing a tool select operator using a temporary context override
As usual, we first get a pointer to the area in which context we need to execute the operator.
Next, using “temp_override”, we execute the operator for the required area:
1 2 3 4 |
area = [area for area in bpy.context.screen.areas if area.type == "VIEW_3D"][0] with bpy.context.temp_override(area=area): bpy.ops.wm.tool_set_by_id(name='builtin.select_circle') |
Executing the tool select operator specifying the area in the operator parameter
It is much easier to call the same operator for the required area if its type is passed in the space_type parameter of the operator itself.
1 2 3 4 |
bpy.ops.wm.tool_set_by_id( name='builtin.select_circle', space_type='VIEW_3D' ) |
The result of using both ways in your code is the same.