1

Is it possible to select and setup a tool, by hotkey, or from python ?

For example :

Keymap.py

("wm.tool_set_by_id",
    {"type": 'BUTTON4MOUSE', "value": 'ANY'},
    {"properties":
        [("name", 'builtin.select_circle<<<< (wait_for_input="False", mode="SUB") >>>>'),
        ],
    },
)

(the example target is a Select Circle -Operator with Subtract -Mode)

irvnriir
  • 135
  • 6
  • @JachymMichal see any Keymap file and https://docs.blender.org/api/current/bpy.ops.wm.html , to understand what the example code shows . – irvnriir Jan 08 '21 at 10:55
  • Oh, I don't speak python, that's why I asked :). If the linked thread doesn't help, please edit your original question so it's more clear. Otherwise you risk it being closed by our moderators. – jachym michal Jan 08 '21 at 11:01
  • 1
    @JachymMichal i fixed the title, it was made incorrect by other user . – irvnriir Jan 08 '21 at 11:14
  • Sorry, it was me. Does the linked answer provide an answer to your question ? Is your "or" inclusive or exclusive ? Because you don't need python to add a tool or any of its mode to a custom shortcut. (right click > Add shortcut on the icon) – Gorgious Jan 08 '21 at 12:41
  • @Gorgious the shortcuts in Toolbar (your "modes") is not a Modes which you can find on the Tool Settings pane or a code, including the provided example . "and" is an inclusive version of "or" X| . – irvnriir Jan 08 '21 at 12:45
  • 1
    So, if I understand right, what you call "Tool" is a blender operator ? The answer is the same, Press F3 for the search operator, type in the name of the operator, right click, assign shortcut – Gorgious Jan 08 '21 at 12:53
  • @Gorgious You are right about the "tool" (world standard) -'s name in Blender . But my answer is the same too X) , the Operators grouped on Toolbar is not an Operator's Modes . – irvnriir Jan 08 '21 at 12:58
  • 1
    https://blender.stackexchange.com/q/196483/86891 ? – Gorgious Jan 08 '21 at 13:00
  • @Gorgious edit: +"as i wrote in the example : builtin.select_circle Operator has SUB (subtract), and p.s. also Extend and Set, Modes ." _ _ If i understand correctly . The answer in the topic describes: how to create a shortcut for an Operator, from python, without setting up Modes . In the example case, we have builtin.select_circle that we would put instead of the object.simple_operator , but it wouldn't make the Subtract selection Mode selected after using the hotkey . – irvnriir Jan 08 '21 at 13:14
  • 1
    Imagine all the time neither of us would have wasted had you included this in your question ! :) Short answer is, you can't since this operator, as you can see in the docs you linked, does not have a "mode" parameter. https://i.stack.imgur.com/IoPgy.png – Gorgious Jan 08 '21 at 13:34
  • @Gorgious this applies only to wm.tool_set_by_id , which is not the target at all, its just a 1 way of selecting an active tool . thanks if you are trying to help, but you don't seem to follow the theme at all . _ _ when you select a tool, it restores the previously set Mode and other settings, instead of using default, so they even more probably can be selected by code . _ _ all the explanations beyond the topic description is excessive . – irvnriir Jan 08 '21 at 13:48

1 Answers1

3

Similar to Enable/disable 3D cursor tool properties from python

You can set the active tool using bpy.ops.wm.tool_set_by_id(name="builtin.select_circle") and use ToolSelectPanelHelper class to get a reference to it. Code based on Operator Simple template:

import bpy
from bl_ui.space_toolsystem_common import ToolSelectPanelHelper

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

def execute(self, context):
    # Set the cursor tool
    bpy.ops.wm.tool_set_by_id(name = &quot;builtin.select_circle&quot;)
    # Get the tool
    tool = ToolSelectPanelHelper.tool_active_from_context(context)
    props = tool.operator_properties('view3d.select_circle')

    # Print all properties
    print (dir(props))
    # [...'mode', 'radius', 'rna_type', 'wait_for_input', 'x', 'y']

    # Set the properties
    props.mode = 'SUB'
    props.radius = 50

    return {'FINISHED'}


addon_keymaps = []

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

# Add a shortcut
wm = bpy.context.window_manager
kc = wm.keyconfigs.addon
if kc:
    km = wm.keyconfigs.addon.keymaps.new(name='3D View', space_type='VIEW_3D')
    kmi = km.keymap_items.new(
        SimpleOperator.bl_idname, type='C', value='PRESS', ctrl=True, shift=True)
    addon_keymaps.append((km, kmi))


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

# Remove the shortcut
for km, kmi in addon_keymaps:
    km.keymap_items.remove(kmi)
addon_keymaps.clear()


if name == "main": register()

Press CtrlShiftC to enable the Cirle Select along with your custom tool settings (mode and radius in this case).

brockmann
  • 12,613
  • 4
  • 50
  • 93
  • Thank you . So it can't be achieved without python ? That feature should be (added) in Keymap . – irvnriir Jan 08 '21 at 14:18
  • 1
    I don't think a feature like this should be added. I have a feeling that the percentage of people who might need a feature like this is astronomically small and there are plenty of features way more important waiting to be added - just my opinion... But that small percentage of people can always ask for help from the community and get exactly what they want like in this instance. This is a very nice script and a very nice answer! – Martynas Žiemys Jan 08 '21 at 15:11