/blender-developer-tools:gpu-drawing-and-overlaysfrom blender-developer-tools
Replaces deprecated bgl with gpu module for viewport overlays, offscreen renders, and GPU drawing in Blender add-ons. Covers draw_handler_add/remove, shaders, batching, state, and headless rendering.
Ships in blender-developer-tools. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Use this skill when the user:
import bgl, bgl.glEnable, bgl.glLineWidth, or gets ModuleNotFoundError: No module named 'bgl' on 5.xSystemError: GPU functions for drawing are not available in background modebgl is gonebgl wrapped raw OpenGL calls, which Blender's Metal and Vulkan backends cannot honour. It is deprecated on 4.5 LTS and removed in 5.x. Measured: importlib.util.find_spec('bgl') is None on 5.0.1, 5.1.2 and 5.2.1, and still present on 4.5.14. Code that imports it does not register on 5.x at all. Everything below uses gpu, gpu_extras and blf, which work on both 4.5 and 5.x, so no version branch is needed for drawing itself. The no-bgl rule flags leftovers.
import bpy
import gpu
from gpu_extras.batch import batch_for_shader
_handle = None
def draw_guides():
obj = bpy.context.active_object
if obj is None:
return
shader = gpu.shader.from_builtin("POLYLINE_UNIFORM_COLOR")
origin = obj.matrix_world.translation
tip = origin + obj.matrix_world.to_3x3().col[2].normalized()
batch = batch_for_shader(shader, "LINES", {"pos": [origin, tip]})
region = bpy.context.region
shader.uniform_float("viewportSize", (region.width, region.height))
shader.uniform_float("lineWidth", 3.0)
shader.uniform_float("color", (1.0, 0.6, 0.1, 0.9))
gpu.state.blend_set("ALPHA")
gpu.state.depth_test_set("LESS_EQUAL")
batch.draw(shader)
gpu.state.depth_test_set("NONE")
gpu.state.blend_set("NONE")
def register():
global _handle
_handle = bpy.types.SpaceView3D.draw_handler_add(draw_guides, (), "WINDOW", "POST_VIEW")
def unregister():
global _handle
if _handle is not None:
bpy.types.SpaceView3D.draw_handler_remove(_handle, "WINDOW")
_handle = None
draw_handler_add returns it; draw_handler_remove(handle, region_type) needs it. A handler added without storing the handle cannot be removed, and it keeps drawing after the add-on is disabled. On re-enable you get two.unregister(), symmetric with register() (see addon-scaffolding).args is a tuple passed to the callback: (self, context) from a modal operator, () for a module-level function.POST_VIEW draws in 3D world space under the view matrices (positions are world coordinates). POST_PIXEL draws in region pixels with an orthographic matrix (positions are (x, y) in pixels, origin bottom-left), which is where text and HUD panels go. PRE_VIEW draws under the scene.bpy.data. Guard context.active_object against None (see type-annotate-props-and-defend-context).area.tag_redraw() for the areas that show it, for example from a timer or a depsgraph handler (see drivers-and-app-handlers, timers-modal-and-threading).snippets/gpu-draw-handler.py is this pattern reduced to one fixed line.gpu.shader.from_builtin(name) takes one of these names on both 4.5 LTS and 5.2:
| Name | Attributes | Uniforms |
|---|---|---|
UNIFORM_COLOR | pos | color |
FLAT_COLOR, SMOOTH_COLOR | pos, color | — |
IMAGE | pos, texCoord | image |
IMAGE_COLOR | pos, texCoord | image, color |
POLYLINE_UNIFORM_COLOR | pos | viewportSize, lineWidth, color |
POLYLINE_FLAT_COLOR, POLYLINE_SMOOTH_COLOR | pos, color | viewportSize, lineWidth |
POINT_UNIFORM_COLOR | pos | color, size |
POINT_FLAT_COLOR | pos, color | size |
5.2 adds IMAGE_SCENE_LINEAR_TO_REC709_SRGB and IMAGE_COLOR_SCENE_LINEAR_TO_REC709_SRGB for scene-linear images. The 2D_ / 3D_ prefixed names from 2.8–3.x (2D_UNIFORM_COLOR, 3D_SMOOTH_COLOR) are rejected on 5.2: ValueError: expected a string in ('FLAT_COLOR', 'IMAGE', ...). One shader handles 2D and 3D: pass 2-component or 3-component pos.
gpu.state replaces the bgl state callsbgl | gpu |
|---|---|
glEnable(GL_BLEND) / glDisable(GL_BLEND) | gpu.state.blend_set('ALPHA') / blend_set('NONE') |
glLineWidth(w) | a POLYLINE_* shader's lineWidth and viewportSize uniforms |
glPointSize(s) | POINT_* shader size uniform, or gpu.state.point_size_set(s) |
glEnable(GL_DEPTH_TEST), glDepthFunc | gpu.state.depth_test_set('LESS_EQUAL'); 'NONE' to disable |
glDepthMask | gpu.state.depth_mask_set(True) |
glReadPixels | gpu.state.active_framebuffer_get().read_color(x, y, w, h, 4, 0, 'FLOAT') |
bgl.Buffer | gpu.types.Buffer |
Two traps:
examples/gpu-offscreen-draw: red at alpha 0.5 over opaque blue gives the mix (0.498, 0, 0.502) with blend_set('ALPHA'), and overwrites the target with (1, 0, 0, 0.498) without it. An overlay that "ignores alpha" is missing this call.POLYLINE_* shader. lineWidth 6 drew a 7 px line (one antialiased edge pixel); lineWidth 1 drew 2 px. gpu.state.line_width_set with a non-polyline shader depends on the backend, because core OpenGL, Metal and Vulkan do not guarantee wide-line rasterization. POLYLINE_* builds the width in geometry, so it looks the same everywhere. It needs viewportSize in pixels: (region.width, region.height) in a draw callback, or the offscreen size.Restore every state you set before returning. Draw callbacks share GPU state with the rest of the editor, so a leftover blend_set('ALPHA') or depth test changes how the next overlay draws.
blfimport blf
def draw_label(): # register with draw type "POST_PIXEL"
font_id = 0 # the default UI font
blf.size(font_id, 16) # 4.0+: (fontid, size); the dpi argument is gone
blf.color(font_id, 1.0, 1.0, 1.0, 1.0)
blf.position(font_id, 20, 30, 0) # region pixels, origin bottom-left
blf.draw(font_id, "Edges: 128")
blf.size(font_id, size, dpi) from 3.x raises a TypeError on 4.0 and later. Measure text with blf.dimensions(font_id, text) before laying it out.
import gpu
from gpu_extras.batch import batch_for_shader
if bpy.app.version >= (5, 2, 0):
gpu.init() # 5.2+: makes drawing available under --background
off = gpu.types.GPUOffScreen(256, 256)
try:
with off.bind():
fb = gpu.state.active_framebuffer_get()
fb.clear(color=(0.0, 0.0, 0.0, 0.0))
gpu.matrix.load_identity() # NDC: -1..1 covers the image
shader = gpu.shader.from_builtin("UNIFORM_COLOR")
batch = batch_for_shader(shader, "TRI_FAN",
{"pos": [(-0.5, -0.5), (0.5, -0.5), (0.5, 0.5), (-0.5, 0.5)]})
shader.uniform_float("color", (1.0, 0.0, 0.0, 1.0))
batch.draw(shader)
buf = fb.read_color(0, 0, 256, 256, 4, 0, "FLOAT")
finally:
off.free()
gpu.init() "initializes the GPU module for background use". On 5.1, 5.0 and 4.5 LTS there is no gpu.init, and any draw under --background raises SystemError: GPU functions for drawing are not available in background mode. On those versions, offscreen drawing works only in a session with a window.sys.exit at module level while its batch, shader and framebuffer were still referenced crashed Blender on shutdown (EXCEPTION_ACCESS_VIOLATION, exit 5). The same drawing done inside a function, so those references were gone on return, exited 0. Free the offscreen in a finally, and keep the GPU objects local.buf into an Image's pixels with foreach_set (see use-foreach-set-for-bulk-data).off.draw_view3d(scene, view_layer, view3d, region, view_matrix, projection_matrix).| Mistake | Consequence | Do instead |
|---|---|---|
import bgl | Import fails on 5.x; the add-on never registers | gpu.state, POLYLINE_* shaders |
from_builtin('3D_UNIFORM_COLOR') | ValueError on 5.2 | UNIFORM_COLOR |
Not storing the draw_handler_add handle | Overlay keeps drawing after disable; doubles on re-enable | Store it, remove it in unregister() |
Expecting alpha without blend_set | Transparent colours draw opaque-over | gpu.state.blend_set('ALPHA'), then 'NONE' |
line_width_set for thick lines | Thin lines on core-profile backends | POLYLINE_* with lineWidth |
blf.size(id, size, 72) | TypeError on 4.0+ | blf.size(id, size) |
Drawing under --background on 4.5 / 5.1 | SystemError | Run with a window, or require 5.2 and call gpu.init() |
Each example runs headless, asserts the contract, and exits non-zero when it breaks. Run one with blender --background --python <script> --; pass a falsifier flag to watch the check fail.
gpu-offscreen-draw: Check-only witness for drawing with the gpu module, the replacement for the bgl module that 5.x removed. Blender 5.2+. Falsify: --skip-blend (exit 5).Short standalone patterns to copy. Each one runs in CI on 5.2 and 4.5 with a check and a falsifier.
gpu: https://docs.blender.org/api/current/gpu.html (4.5 LTS)gpu.shader: https://docs.blender.org/api/current/gpu.shader.html (4.5 LTS)gpu.state: https://docs.blender.org/api/current/gpu.state.htmlgpu_extras.batch: https://docs.blender.org/api/current/gpu_extras.batch.htmlblf: https://docs.blender.org/api/current/blf.htmlSpaceView3D.draw_handler_add: https://docs.blender.org/api/current/bpy.types.Space.html#bpy.types.Space.draw_handler_addWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub tmhsdigital/blender-developer-tools --plugin blender-developer-tools27 skills in this plugin
/addon-scaffoldingScaffold a Blender add-on as an Extensions Platform package: blender_manifest.toml, modular file layout, register_classes_factory, symmetric register/unregister. Use when the user starts a new add-on or extension, writes or fixes blender_manifest.toml, asks which layout or registration pattern to use on Blender 4.5 LTS or 5.x, or has classes that fail to register or unregister cleanly. Targets 5.2 LTS with 4.5 LTS fallback./ai-mesh-cleanupOrdered cleanup for an imported generated or scanned mesh before it goes to an engine: scene units, apply rotation and scale, origin to base, recalculate normals, evaluated triangle count, decimate to a budget, convex collider. Use when the user has a GLB, glTF or FBX that is not engine-ready, mentions unit scale, unapplied transforms, triangle budget, LODs or a collision hull, or is about to run mesh operations on an import without checking its scale. Targets 5.2 LTS with 4.5 LTS fallback./bake-high-to-lowCage-bake high-poly surface detail onto a low-poly target as a tangent-space normal map with bpy.ops.object.bake (Cycles, selected-to-active, active Image Texture node, UV layer, save_render). Use when the user wants a normal map from a dense mesh onto a game-resolution or decimated LOD, mentions cage_extrusion or use_selected_to_active, or gets a bake that returns CANCELLED, comes out black or flat, or fails with 'No active UV layer found'. Targets 5.2 LTS with 4.5 LTS fallback./blender-rulesBlender Python anti-pattern rules (bmesh leaks, bpy.ops in loops, deprecated context-copy overrides, bulk mesh writes, prop annotations and context guards, Extensions manifest, import scale, export modifiers, glTF/FBX axis RNA, the removed bgl module, the normals API removed in 4.1). Use when writing, reviewing or fixing any bpy / Blender add-on or script code./bl-info-migrationMigrate a legacy bl_info add-on to the Extensions Platform in three steps, with a before/after diff and the dual-format pattern. Use when the user has a bl_info dictionary in __init__.py, asks whether bl_info is deprecated or still supported, wants to publish to extensions.blender.org or ship an extension .zip, or sees warnings about legacy add-ons at install time. Targets 5.2 LTS; manifests work from 4.2./compositor-nodesBuild and render through the scene compositor from Python across the 4.5 LTS to 5.x split: scene.use_nodes plus scene.node_tree and a Composite node on 4.5, a CompositorNodeTree node group assigned to scene.compositing_node_group with interface sockets and a Group Output on 5.x, a Render Layers to Glare (Fog Glow) chain, the Glare node's move from enum properties to menu input sockets, and render.use_compositing. Use when the user scripts compositor nodes, adds bloom or glow, hits AttributeError on scene.node_tree in 5.x, sets glare_type or quality and gets AttributeError on 5.x, looks for the removed EEVEE use_bloom toggle, or renders and sees no compositor effect. Targets 5.2 LTS with 4.5 LTS fallback.Other plugins' skills for the same job