4
$\begingroup$

At the moment, i draw ma text in 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 modal operator and update it with:

bpy.app.handlers.scene_update_post

when my mesh is updated please ?

$\endgroup$
7
$\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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.