draw_callback_px in specified area Using the Python code below which draws the text and shape shown in these screenshots, I want to clip the graphics outside the red box shown in my second image so it is not drawn.


Goal:

import bpy, blf, bgl, gpu
from gpu_extras.batch import batch_for_shader

class Test_operator(bpy.types.Operator):
    bl_idname = &quot;object.test_operator&quot;
    bl_label = &quot;test&quot;

    def draw_callback_px(self, op, context):
        self.draw_rec(500, 800, 200, 400, (0.2,0.2,0.2,1.0)) # Window A
        self.draw_rec(500, 800, 400, 428, (0.1,0.1,0.1,1.0)) # Title bar B
        font_id = 1
        blf.size(font_id, 53, 72)
        blf.position(font_id, 500, 300, 0)
        blf.draw(font_id, self.text)
    
    def draw_rec(self, left, right, bottom, top, color):
        self.shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
        self.shader.bind()
        self.shader.uniform_float(&quot;color&quot;, color)

        vert = ((left, bottom), (left, top), (right, top), (right, bottom))
        
        bgl.glEnable(bgl.GL_BLEND)
        self.batch_panel = batch_for_shader(
            self.shader, 'TRIS', {&quot;pos&quot; : vert},
            indices=((0, 1, 2), (0, 2, 3)))
        self.batch_panel.draw(self.shader)
        bgl.glDisable(bgl.GL_BLEND)

    def modal(self, context, event):
        context.area.tag_redraw()
        if event.type in {&quot;ESC&quot;}:
            bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
            return {'CANCELLED'}
        return {'RUNNING_MODAL'}

    def invoke(self, context, event):
        self.text = &quot;Hello World&quot;

        args = (self, context)
        self._handle = bpy.types.SpaceView3D.draw_handler_add(
            self.draw_callback_px, args, 'WINDOW', 'POST_PIXEL'
        )
        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}


def register():
    bpy.utils.register_class(Test_operator)
def unregister():
    bpy.utils.unregister_class(Test_operator)
if __name__ == &quot;__main__&quot;:
    register()
You can use the scissor test.
        bgl.glEnable(bgl.GL_SCISSOR_TEST)
        bgl.glScissor(500, 200, 300, 200)  # adjust to match your red box
        blf.draw(font_id, self.text)
        bgl.glDisable(bgl.GL_SCISSOR_TEST)