For an addon, I want to draw a rectangle next to the mouse cursor. I tried using the gpu package, but it only works with draw handlers in a specific region or space. Since the shape needs to stay with the cursor, it could be anywhere on the window.

That leads me to bgl, but in 2.8x it looks deprecated - most code samples online to draw shapes with bgl don't work since functions and keywords are no longer included. Is there a way to draw a rectangle with bgl still in 2.8? Or do I need to look somewhere else?


1 Answer 1


BGL still works. The old snippets used fixed function pipeline which is no longer available in Blender since it is running on OpenGL Core profile. Programmable pipeline for OpenGL is actually much easier, more understandable and a more streamlined way to do things. Just use regular modern tutorial for OpenGL to draw a rectangle, make sure that tutorial is using Core profile, and not FFP.

In order to draw outside the viewport, you need to hook your draw handler to multiple Space sub-types, and determine visibility on what you draw based on the cursor position.

You can use GPU module alongside with BGL (technically it is also an OpengGL wrapper. The only difference is that BGL is a direct 1:1 mapping of OpenGL function calls and GPU module provides a set of convenience classes to automate certain common tasks. But they are still using the same single OpenGL draw context.

  • $\begingroup$ Good to know! I would use GPU, however it only seems to work when attached to a space. I could add multiple handlers, but this addon would want to be able to a shape over multiple spaces. I feel like bgl is simpler in just drawing one shape over the whole window instead of adding handlers to all spaces. $\endgroup$ Commented May 10, 2020 at 16:40
  • $\begingroup$ There is no difference if that is GPU or BGL. You can combine the too, as I said. I think it'd be possible to create a glViewport and then just draw over everything using either BGL or GPU. $\endgroup$
    – D. Skarn
    Commented May 10, 2020 at 17:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .