1
$\begingroup$

I've taken the "Operator Modal Draw" template example and edited it. I wanted to see if I could pass the modal "event" through "args" in the draw_handler_add function. It works and the draw_callback_px function updates and draws the event type every time I press a key or do something with the mouse except for when I move the mouse (never prints "MOUSEMOVE" like it would if I were printing "event.type" directly within the modal function).

Anyone know why this is? Would be great If I could pass "event" entirely through to the draw_callback_px (not that I would want to run it every time the mouse moved, I'd control it with a conditional, but would like to have full power of the modal event within my bgl,blf drawing). My knowledge is pretty limited about the draw_handler and callbacks.

import bpy
import bgl
import blf


def draw_callback_px(self, contex, event):

    font_id = 0
    blf.position(font_id, 15, 30, 0)
    blf.size(font_id, 20, 72)
    blf.draw(font_id, event.type)



class ModalDrawOperator(bpy.types.Operator):
    """Draw a line with the mouse"""
    bl_idname = "view3d.modal_operator"
    bl_label = "Simple Modal View3D Operator"

    def modal(self, context, event):
        context.area.tag_redraw()

        if event.type in {'ESC'}:
            bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
            return {'CANCELLED'}

        return {'RUNNING_MODAL'}



    def invoke(self, context, event):

        # the arguments we pass the the callback
        args = (self, context, event)
        # Add the region OpenGL drawing callback
        # draw in view space with 'POST_VIEW' and 'PRE_VIEW'
        self._handle = bpy.types.SpaceView3D.draw_handler_add(
        draw_callback_px, args, 'WINDOW', 'POST_PIXEL')


        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}



def register():
    bpy.utils.register_class(ModalDrawOperator)
def unregister():
    bpy.utils.unregister_class(ModalDrawOperator)

if __name__ == "__main__":
    register()
$\endgroup$

1 Answer 1

1
$\begingroup$

Use the event from the modal method.

Will get back to this when I find the link about full event. The event in invoke doesn't much care about mouse moves, rather a shift click or which button, where as the "full event" is available to the modal. Try script below, a quick hack to use modal event when setting up handle.

import bpy
import bgl
import blf

def draw_callback_px(self, contex, event):
    font_id = 0
    blf.position(font_id, 15, 30, 0)
    blf.size(font_id, 20, 72)
    blf.draw(font_id, "%s %s" % (event.type, event.value))


class ModalDrawOperator(bpy.types.Operator):
    """Draw a line with the mouse"""
    bl_idname = "view3d.modal_operator"
    bl_label = "Simple Modal View3D Operator"
    _handle = None
    def modal(self, context, event):
        context.area.tag_redraw()

        # Add the region OpenGL drawing callback
        # draw in view space with 'POST_VIEW' and 'PRE_VIEW'
        if not self._handle:
            args = (self, context, event)
            self._handle = bpy.types.SpaceView3D.draw_handler_add(
            draw_callback_px, args, 'WINDOW', 'POST_PIXEL')
        if event.type in {'ESC'}:
            bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
            return {'CANCELLED'}

        return {'PASS_THROUGH'}

    def invoke(self, context, event):

        # the arguments we pass the the callback
        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}

def register():
    bpy.utils.register_class(ModalDrawOperator)
def unregister():
    bpy.utils.unregister_class(ModalDrawOperator)

if __name__ == "__main__":
    register()
$\endgroup$
4
  • $\begingroup$ I think I understand the thought behind your code. However when I run it event.type only draws "SELECTMOUSE" or nothing and event.value only draws "NONE" or sometimes "ANY" regardless as to what mouse or keyboard button I press. When you run the code does it work as it's supposed to? Perhaps I've got something else weird going on with my system. $\endgroup$
    – Sicut Unum
    Commented Jun 14, 2018 at 18:28
  • $\begingroup$ Put a print in the draw callback and check the output to system console. On ubuntu I get a list of mostly 'INBETWEEN_MOUSEMOVE' $\endgroup$
    – batFINGER
    Commented Jun 15, 2018 at 10:43
  • $\begingroup$ I did. Same problem. I changed the space type to the text editor as well. Same issue. I'm on Windows though. $\endgroup$
    – Sicut Unum
    Commented Jun 15, 2018 at 11:26
  • $\begingroup$ Dunno, not on windows. Wouldn't do it this way anyhow. You need the operator for the event -- can't see the benefit of passing event to drawcallback which "lives and dies with operator anyhow".. [See this answer])blender.stackexchange.com/a/110689/15543) to set up a drawing class. $\endgroup$
    – batFINGER
    Commented Jun 15, 2018 at 12:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .