Removing drawing with Gpu module


I was reading through the Gpu module page and going through the samples. It looks great but I would also like to know how I can remove or refresh a drawing done on the screen with the Gpu module. All examples just draw on screen but leave it there without any mechanism or explanation to how to remove or erase them from the viewport.


Hi @kurk

When using the GPU module every frame draws the whole screen from scratch. If you want something to not draw anymore you need to remove that from your input buffers. Depending on what you are doing, most of the time only updating the content of the index buffer is enough for this.

Looking at the examples provided you update the indexes and recreate the batch again. This can happen on the outside (via an operator for example) or by moving the index and batch creation to inside the draw method (less optimal).

There is no special API for erasing, as OpenGL and other low-level GPU APIs cannot erase, they can only draw shapes and blend pixels.

1 Like

In case your question is about how to remove the added draw handler, the following is the way:

# Add the handler:
my_draw_handler = bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')

# Remove the handler:
bpy.types.SpaceView3D.draw_handler_remove(my_draw_handler, 'WINDOW')
1 Like

Thanks, that looks much easier than the documentation seemed to imply. Although not sure if removing the draw handler does what I am trying to do with the examples.

From the GPU module page

To keep the examples relatively small, they just register a draw function that can’t easily be removed anymore. Blender has to be restarted in order to delete the draw handlers.