Set Current active tool in a variable

Hey guys,

I would like to set the current active tool (select, rotate, scale, etc ) in a variable to be able to active if after a modal.

  • Select box (as active tool)
  • Launch modal, change to something else
  • Exit modal and use the select box active tool.

If you have an idea :wink:

thx for your help!

You can store the name of the tool in a window manager or scene property and match the name to the function when done. Since these are global and saved with the scene , it can be handy later as way. I did it this way for a similarish need.

You might need to check the undo history to see what was triggered prior to your tool?

something like this?

bpy.types.WindowManager.active_tool_name = StringProperty(type= ?)

Yeah that is how I did, as long as you know the tool’s name.

What I need it to find the active tool name ^^
I don’t have it.

To change the tool it’s this code

bpy.ops.wm.tool_set_by_id(name="builtin.cursor")

But how to find the name?

1 Like

This used to bring up a popup in 2.79 for last actions list, but it is not assinged in 2.8x however the command in there. I wonder if you could get the list from the popup and revert back Maybe there is a stack for the successful command history. There is undo history but I am not seeing how one can get a name, without undoing.

bpy.ops.screen.repeat_history (F3)

raw

ok, I see, but there should be something better than checking for the history.

For example, if we use this in the console and press ctrl space, we have the name

bpy.context.workspace.tools >>> bpy.context.workspace.tools['builtin.select']

I don’t find how to make a variable for this

current_active_tool = bpy.context.workspace.tools ?

If that works for you then you can pass the command as a text then parse it.

>>> last_command="bpy.context.workspace.tools['builtin.select']".split("'")[1]
>>> last_command
'builtin.select'

>>> type(last_command)
<class 'str'>

The thing is that it will not be the last command ^^

Ok, this code give me the name.

tools = bpy.context.workspace.tools
current_active_tool = tools.from_space_view3d_mode(bpy.context.mode).idname

print(current_active_tool)
3 Likes

Btw I did a search in the source and this is all the builtin tools which should work with the method above

editors/transform/transform_gizmo_3d.c:    if (tref && STREQ(tref->idname, "builtin.move")) {
editors/transform/transform_gizmo_3d.c:    else if (tref && STREQ(tref->idname, "builtin.rotate")) {
editors/transform/transform_gizmo_3d.c:    else if (tref && STREQ(tref->idname, "builtin.scale")) {
editors/transform/transform_gizmo_3d.c:    else if (tref && STREQ(tref->idname, "builtin.transform")) {
editors/transform/transform_gizmo_3d.c:      /* This is also correct logic for 'builtin.transform', no special check needed. */
windowmanager/intern/wm_toolsystem.c:          return "builtin.cursor";
windowmanager/intern/wm_toolsystem.c:      return "builtin.select_box";
windowmanager/intern/wm_toolsystem.c:          return "builtin.select";
windowmanager/intern/wm_toolsystem.c:          return "builtin.annotate";
windowmanager/intern/wm_toolsystem.c:          return "builtin.select";
windowmanager/intern/wm_toolsystem.c:      return "builtin.select_box";
windowmanager/intern/wm_toolsystem.c:  return "builtin.select_box";

1 Like