Swap between Cursor and Select box

Hello all,
It is possible to change between 3D_cursor and selection_box using python script? There is no code because I don’t have any idea on how to swap 3d cursor with select_box in python and vice versa. Is there any short method for this?

You can do it like this

import bpy


def change_tool(context):
    which_tool = context.workspace.tools.from_space_view3d_mode(context.mode)
    if which_tool.idname == "builtin.cursor":
        bpy.ops.wm.tool_set_by_id(name="builtin.select_box")
        print("selecting box")
    else:
        bpy.ops.wm.tool_set_by_id(name="builtin.cursor")
        print("selecting cursor")

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"
    def execute(self, context):
        change_tool(context)        
        return {'FINISHED'}

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

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

if __name__ == "__main__":
    try: unregister()
    except: pass
    register()

I only wrap this code in a plain operator so it is meant to be executed only within the context of the 3D viewport. If you try to call wm.tool_set_by_id from command prompt, or text editor it won’t work.

2 Likes

Thank you @const. So I just click run on the script and it will work in 3D view?
Can I make a panel button that will select one of the two?

This script can be registered once by running it in the Text editor. Then the operator activated from the 3D View.

In reality this can be turned into an addon as well just adding a simple bl_info at the start.

See these templates:
Creating a module (only the bl_info): 2.83\scripts\templates_py\addon_add_object.py
Creating a panel (only HelloWorldPanel): 2.83\scripts\templates_py\ui_panel_simple.py

1 Like

Sorry @const but how do I activate the operator. After I hit run nothing happens.

Thank you.

I converted it to an addon for ease of use.
2020-07-03 13_42_12-Blender

switch_select_box_cursor.py (1.6 KB)

1 Like

You are a star @const !

1 Like