6
$\begingroup$

At the moment, I'm drawing my text in the viewport with this modal operator:

_handle = None

def modal(self, context, event):
    context.area.tag_redraw()
    if not context.window_manager.show_text.enabled:
        return {'CANCELLED'}
    return {'PASS_THROUGH'}
 
@staticmethod
def handle_add(self, context):
    VIEW3D_OT_ADH_display_text._handle = bpy.types.SpaceView3D.draw_handler_add(
           draw_text_callback, 
           (self, context),
           'WINDOW', 'POST_PIXEL')

@staticmethod
def handle_remove(context):
    _handle = VIEW3D_OT_ADH_display_text._handle
    if _handle != None:
        bpy.types.SpaceView3D.draw_handler_remove(_handle, 'WINDOW')
    VIEW3D_OT_ADH_display_text._handle = None
                    
def invoke(self, context, event):
    if context.window_manager.show_text.enabled == False:
        context.window_manager.show_text.enabled = True
        VIEW3D_OT_ADH_display_text.handle_add(self, context)

        return {'RUNNING_MODAL'}
    else:
        context.window_manager.show_text.enabled = False
        VIEW3D_OT_ADH_display_text.handle_remove(context)

        return {'CANCELLED'}

    return {'CANCELLED'}

Is it possible to draw my text without a modal operator, and update it with the following whenever my mesh is updated?

bpy.app.handlers.scene_update_post

$\endgroup$

1 Answer 1

9
$\begingroup$

You don't need the modal operator to draw things to the viewport. Calling bpy.types.SpaceView3D.draw_handler_add is all you need. Instead of (self, context) you could pass an arbitray object and alter its attributes to modify the drawing. I.e.

class foo:
    def __init__(self, prop):
        self.prop = prop

def draw_text_callback(data):
    #...
    blf.draw(data.prop)

data = foo("Test")

handle = bpy.types.SpaceView3D.draw_handler_add(
           draw_text_callback, 
           (data,),
           'WINDOW', 'POST_PIXEL')
$\endgroup$
0

You must log in to answer this question.

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