Taking as an example this simple script to display an object's global bounding box:

object with global bounding box drawn over top of it

Is there any way to make lines display in such a way that they are overlapped by the appropriate geometry? I am currently using the batch_for_shader() method, and don't know if I should be using something else instead.

The only solutions I have been able to find involve generating additional objects within the scene, which I would prefer to avoid if possible.


1 Answer 1


Add a depth test bgl.glEnable(bgl.GL_DEPTH_TEST), disabled by default: https://www.khronos.org/opengl/wiki/Depth_Test

enter image description here

Code based on: https://docs.blender.org/api/current/gpu.html#d-lines-with-single-color

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

coords = [(1, 1, 1), (-2, 0, 0), (-2, -1, 3), (0, 1, 1)]
shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
batch = batch_for_shader(shader, 'LINES', {"pos": coords})

def draw():
    shader.uniform_float("color", (1, 1, 0, 1))

bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')

If you want to disable it use bgl.glDisable(bgl.GL_DEPTH_TEST).

  • $\begingroup$ Thank you so much! I guess I didn't realize that I needed to go to the OpenGL documentation rather than the Blender documentation for this. But that does make sense! $\endgroup$
    – Malkalypse
    Commented Jan 25, 2022 at 17:36

You must log in to answer this question.

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