Active Tool - Operator Auto Start

Does anyone know if there is a way to automatically start an operator associated with an Active Tool (Blender 2.8), when the tool button is pressed? It seems that Active Tools require a keymap (normally a left mouse click) to get them started, however, my operator needs to execute/invoke automatically when the tool button is pressed, as it highlights geometry on mouse move. I’d prefer not to have to click first to start the operator, it feels a bit clumsy.

It might be that I now need to use gizmos, to handle the highlighting but it would be handy not to need this.

Yeah, the Tools API kind of sucks right now. There are several things that are missing, and I hope that they don’t leave it aside.
Currently you can use some hackish and a bit ugly method to call an operator, as long as you handle it well.
You can add a keymap in your WorkSpaceTool pointing to an operator that will be called on every move of the mouse, like so:

bl_keymap = (("addon.operator_name", {"type": "MOUSEMOVE", "value": "ANY"}, None),)

If your operator is modal, you should check if some instance is running in its invoke method, and prevent new instances from starting a new modal operator. When you are done doing things in modal you can reset its running state to allow a new instance to start when the user comes back and selects the tool again.

If you need the modal state to be running all the time and turn it down when the user selects another tool, you can check if the bl_idname of your WorkSpaceTool is not in context.workspace.tools, then you can finish the modal operator.

1 Like

Thanks yain, appreciate the answer. It’s annoying, as I thought I’d tried on MOUSEMOVE in the keymap. I just spent a few hours trying to use a custom gizmo but it felt a bit hacky too. I think I’ll use your suggestion for now and hope there’s a cleaner way to do it in a later build.

Good tip on handling the user selecting another tool. I’ll definitely use that.

I’m sure they’ll get around to sorting the API eventually but it seems so many things I’m developing need some workaround to get working.

Anyway, thanks again for responding.

I feel the same too often. Luckily some things got better, like the gpu module and access to depsgraph.

Good point. Those two things are big improvements for add-on developers.