3
$\begingroup$

Is it possible to choose active tool in Python?

enter image description here

$\endgroup$

2 Answers 2

5
$\begingroup$

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:
        print(ob)


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

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

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


def register():
    bpy.utils.register_class(SimpleOperator)


def unregister():
    bpy.utils.unregister_class(SimpleOperator)


if __name__ == "__main__":
    register()
$\endgroup$
2
  • $\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
    Commented Mar 31, 2019 at 19:23
  • $\begingroup$ Thank you very much! $\endgroup$
    – Alex
    Commented Mar 31, 2019 at 19:55
3
$\begingroup$

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .