How to do Modal Timer with Draw Handler?


import bpy
import bgl
import blf
import time


def draw_callback_px(self, context):
    font_id = 0


    # draw some text
    blf.position(font_id, 15, 30, 0)
    blf.size(font_id, 20, 72)
    blf.draw(font_id, str(time.time()))


    # 50% alpha, 2 pixel width line
    # bgl.glEnable(bgl.GL_BLEND)
    # bgl.glColor4f(0.0, 0.0, 0.0, 0.5)
    # bgl.glLineWidth(2)


    # bgl.glBegin(bgl.GL_LINE_STRIP)
    # for x, y in self.mouse_path:
    #     bgl.glVertex2i(x, y)
    # bgl.glEnd()


    # restore opengl defaults
    # bgl.glLineWidth(1)
    # bgl.glDisable(bgl.GL_BLEND)
    # bgl.glColor4f(0.0, 0.0, 0.0, 1.0)


class ModalOperator(bpy.types.Operator):
    bl_idname = "object.modal_operator"
    bl_label = "Simple Modal Operator"


    timer = None
    draw_handle = None


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


        if event.type in {'RIGHTMOUSE', 'ESC'}:
            print("CANCELLED")
            context.window_manager.event_timer_remove(self.timer)
            bpy.types.SpaceView3D.draw_handler_remove(self.draw_handle, 'WINDOW')
            return {'CANCELLED'}


        if event.type == "TIMER":
            print(time.time())
            
        return {'PASS_THROUGH'}


    def invoke(self, context, event):
        self.timer = context.window_manager.event_timer_add(0.1, context.window)
        self.draw_handle = bpy.types.SpaceView3D.draw_handler_add(draw_callback_px, (self, context), 'WINDOW', 'POST_PIXEL')
        context.window_manager.modal_handler_add(self)
        print("INVOKE")
        return {'RUNNING_MODAL'}


if __name__ == "__main__":
    bpy.utils.register_class(ModalOperator)
    bpy.ops.object.modal_operator('INVOKE_DEFAULT')



As you can see in the code, the console is updated normally as event.type == “TIMER”. But unfortunately the draw handler is updated only when a user input event occurs.

Not even putting the tag_redraw() inside the TIMER condition helps.

Do you know how is it done?

There is not documentation about this.
https://www.blender.org/api/blender_python_api_2_77_0/bpy.types.Space.html?highlight=draw_handler_add#bpy.types.Space.draw_handler_add

const, I’m getting the update ok in view3d area using code above, with or without the tag_redraw. 2.77 linux 64.

Hmm, I tried the script in both 2.76b and 2.77rc and it not works, I am in Windows 7. This will be a good opportunity to file a bug report right? :slight_smile: