Cant start modal operator from Tools panel in Blender 2.8

Hello everyone.
Here I add operator to Tools panel in editmode.
So I want my operator working like a knife tool, but I can’t start operator as modal operator.
Can anyone help me to solve the problem?

import bpy
from bpy.types import Operator

from bpy.types import WorkSpaceTool

class MyModalOperator(Operator):
    bl_idname = "view3d.some_selection"
    bl_label = "Some tool"
    
    bl_options = {'REGISTER', 'UNDO', 'BLOCKING'}
    
    def invoke(self, context, event):
        print('I started')
        return {'RUNNING_MODAL'}
    
    def modal(self, context, event):
        print('Now I running modal')
        if event.type == 'ESC':
            print('Canceled')
            return {'CANCELLED'}
        return {'RUNNING_MODAL'} 
    
    def execute(self, context):
        print('I finished')
        return {'FINISHED'}

class SomeWorkSpaceTool(WorkSpaceTool):
    bl_space_type='VIEW_3D'
    bl_context_mode='EDIT_MESH'

    bl_idname = "myops.some_selection"
    bl_label = "Some selection"
    bl_description = ('Description')
    
    
    bl_keymap = (
        ("view3d.some_selection", {"type": 'LEFTMOUSE', "value": 'PRESS'},
    {"properties": []}),
        )

    def draw_settings(context, layout, tool):
        pass

def register():
    bpy.utils.register_class(MyModalOperator)
    bpy.utils.register_tool(SomeWorkSpaceTool, after={"builtin.select_lasso"}, separator=True, group=False)

def unregister():
    bpy.utils.unregister_tool(SomeWorkSpaceTool)
    bpy.utils.unregister_class(MyModalOperator)

if __name__ == "__main__":
    register()

Add a modal handler to the invoke method.

    def invoke(self, context, event):
        wm = context.window_manager
        wm.modal_handler_add(self)

        return {'RUNNING_MODAL'}
1 Like

Yes, I really forgot about that. Maybe you can tell me how to show current modal operator hotkeys in statusbar?

In the modal method

context.workspace.status_text_set("My keymaps: 12345")

When the modal ends

context.workspace.status_text_set(None)

Afaik icons are generated from keymaps and automatic, but you can add text to the status bar as above. Or you can use context.area.header_text_set() if you want the text in the header like the old modal operators.

1 Like