1
$\begingroup$

I noticed that the moment I select or deselect an object my blf text appears in all 3d viewports: is there something wrong with he way I use drawback handler?

Here's a gif with the issue: enter image description here

Here's my simple modal:

import bpy
import blf

def draw_text(text, size, x, y, font_id, dpi, color=(1, 1, 1, 1)):
    blf.size(font_id, size, dpi)
    blf.color(0, *color)
    blf.position(font_id, x, y, 0)
    blf.draw(font_id, text)

def draw_callback_px(self, context):
    blf.enable(0, blf.SHADOW)
    blf.shadow_offset(0, 2, -2)
    blf.shadow(0, 3, 0, 0, 0, 1)

    draw_text(self.status, 30, 50, 115, 0, 72)
    draw_text('My Modal', 30, 50, 150, 0, 72)

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

    def modal(self, context, event):
        context.area.tag_redraw()
        
        if event.type == 'MOUSEMOVE':
            pass

        elif event.type == 'LEFTMOUSE' and event.value == 'RELEASE':
            for ob in bpy.context.view_layer.objects:
                ob.select_set(False if ob.select_get() else True)
            self.status = 'Clicked'
                
            return {'RUNNING_MODAL'}

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

        return {'RUNNING_MODAL'}

    def invoke(self, context, event):
        self.status = 'Not Clicked'
        if context.space_data.type == 'VIEW_3D':
            self._draw_handler = bpy.types.SpaceView3D.draw_handler_add(draw_callback_px, (self, context), 'WINDOW', 'POST_PIXEL')
        
        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}


def register():
    bpy.utils.register_class(ModalOperator)


def unregister():
    bpy.utils.unregister_class(ModalOperator)


if __name__ == "__main__":
    register()
$\endgroup$
2
  • $\begingroup$ This is old (uses pre 2.8 bgl) but relevant blender.stackexchange.com/a/41329/15543 The draw call back is added to all spaces of that type. As seen in question once all viewports are updated. Need someway to distinguish one from another. Example in Link looks at screen and area index. $\endgroup$
    – batFINGER
    Sep 1, 2020 at 9:10
  • $\begingroup$ @batFINGER thank you as always. I'm able to find my 'active' viewport but I don't understand how to draw handler only in it. I tried to limit draw_callback_px() to the index of the area but no luck.. $\endgroup$ Sep 1, 2020 at 11:45

0

You must log in to answer this question.

Browse other questions tagged .