Is it possible to choose active tool in Python?

enter image description here


You can use the wm.tool_set_by_id() operator.

enter image description here

Example for a button:

layout.operator("wm.tool_set_by_id").name = "builtin.select_box"

Example operator:

import bpy

def main(context):
    bpy.ops.wm.tool_set_by_id(name = "builtin.move")
    for ob in context.scene.objects:

class SimpleOperator(bpy.types.Operator):
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        return {'FINISHED'}

def register():

def unregister():

if __name__ == "__main__":
  • $\begingroup$ you can call the operator inside another operator or as in my first example you can add button to a menu/panel. $\endgroup$ – Syler Mar 31 '19 at 19:23
  • $\begingroup$ Thank you very much! $\endgroup$ – Alex Mar 31 '19 at 19:55

You can set it active tool via bpy.ops.wm.tool_set_by_id() operator. However it is important to execute the operator in the right context. So running this operator from the wrong context won't work. Here is a simple method that sets the tool with the right context override:

def set_active_tool(tool_name):
    for area in bpy.context.screen.areas:
        if area.type == "VIEW_3D":
            override = bpy.context.copy()
            override["space_data"] = area.spaces[0]
            override["area"] = area
            bpy.ops.wm.tool_set_by_id(override, name=tool_name)

If you want to know how the active tool_name is called, just go into the console and type in bpy.context.workspace.tools and then press Ctrl+Space. This will show you the current and last active tool id name which you can use as parameter for your set_active_tool() method. Just select your desired tool and see how it is called.

  • $\begingroup$ But, would it be possible to have no active tool (like set_active_tool to 'None')? $\endgroup$ – Luis May 24 '20 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.