6

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

Joachim
  • 549
  • 5
  • 20
pistiwique
  • 1,106
  • 9
  • 22

1 Answers1

9

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')
pink vertex
  • 9,896
  • 1
  • 25
  • 44