2
$\begingroup$

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 = "object.test_operator"
    bl_label = "test"

    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("color", color)

        vert = ((left, bottom), (left, top), (right, top), (right, bottom))
        
        bgl.glEnable(bgl.GL_BLEND)
        self.batch_panel = batch_for_shader(
            self.shader, 'TRIS', {"pos" : 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 {"ESC"}:
            bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
            return {'CANCELLED'}
        return {'RUNNING_MODAL'}

    def invoke(self, context, event):
        self.text = "Hello World"

        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__ == "__main__":
    register()
$\endgroup$
1
  • $\begingroup$ Is your goal to clip everything outside the precise red box you included in your second screenshot (which would cause the left vertical line of the "H" to disappear, as well as the right half of the "r", and the entire top darker part of the background rectangle)? Or is your red rectangle imprecise and your intention is to actually just clip text so it doesn't draw past the background rectangle? $\endgroup$
    – Keavon
    Jan 7 '21 at 9:06
5
+100
$\begingroup$

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)
$\endgroup$
1
  • $\begingroup$ This work! thank you very much. $\endgroup$
    – X Y
    Jan 7 '21 at 9:33

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.