Adding toolbar button (WorkSpaceTool)

I have created a tool for drawing Bezier curves. To activate the tool, I have added a button on the toolbar by subclassing WorkSpaceTool (following the ‘UI Tool Simple’ template).

I am facinng a few issues with this:

  1. If the type is {"type": 'LEFTMOUSE', "value": 'PRESS'}, the user needs to first click the tool button and click again in the 3d viewport, to activate the tool.
    If the type is {"type": 'MOUSEMOVE', "value": 'ANY'} there are too many instantiations of the add-on class.
    So how to make Blender call execute or invoke of the tool class immediately after click on the tool button?

  2. I haven’t been able to figure out a way to catch the tool button deactivation event. So if user opens a new blend file, the toolbar gets reset and the class seems to get removed. This leaves some invalid references and the button does not work any more after this.

  3. If the user clicks the tool button twice, I would like to ignore the second click. But I am unable to find a place where this piece of code should go.

Would appreciate any help.

I have the same question, i want to start/stop a timer with a toolbar button when it is pressed. But i cant find how to do it.

This is how I worked around the problem:

  1. Maintain a boolean class variable running and set its value to True on the first call to invoke
  2. Return not running from poll method, so invoke will get called only if there is no instance running already
  3. Reset the value of running to False in all cancel event handlers
  4. If you want to do object level checking, you can store the tool’s object reference at the class level to access instance members (this sounds hacky, but has worked so for :slight_smile: ).

The relevant script - blenderbezierutils.py - can be found here: GitHub - Shriinivas/blenderbezierutils: Blender Add-on with Bezier Utility Ops
(Please check the definition of class ModalFlexiEditBezierOp)

2 Likes

@Khemadeva Seriously thanks, your code is saving my life

1 Like

Does anyone know if the issues has actually been resolved? (restarting loses keymap reference for the WorkSpaceTool class)