Draw custom overlay

Hi,
I would like to experiment with custom overlays and drawing.

I found a code snippet, that draws lines into the 3Dview. neat.

import bpy
import gpu
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.bind()
    shader.uniform_float("color", (1, 1, 0, 1))
    batch.draw(shader)


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

Now i wanted to draw lines into the Timeline/Dopesheet. I just changed the last line. however.
There was no error but also no lines appearing.

import bpy
import gpu
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.bind()
    shader.uniform_float("color", (1, 1, 0, 1))
    batch.draw(shader)


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

It is propably naive to think the same lines as in the 3Dview would also appear in the Timeline.

Does anyone of you has any experience with that kind of magic?

How to draw a simple line into the timeline?

I know, I will propably find anything usefull in here:
https://docs.blender.org/api/current/gpu.shader.html

Or: Where to beginn?

This Looks very helpfull too

https://blender.stackexchange.com/questions/57709/how-to-draw-shapes-in-the-node-editor-with-python-bgl

I did it… I drew a line into the dopehseet timeline :slight_smile:

import bpy
import bgl, blf, gpu
from gpu_extras.batch import batch_for_shader
from mathutils import Vector
import math

def draw():
    shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
    batch = batch_for_shader(shader, 'LINES', {"pos": [(100,100),(268,130)]})
    shader.bind()
    shader.uniform_float("color", (1.0, 1.0, 1.0, 1.0))
    batch.draw(shader)
    print("draw")
    
    
    

bpy.types.SpaceDopeSheetEditor.draw_handler_add(draw, (), 'WINDOW', 'POST_PIXEL')
1 Like

Very cool idea, you can draw all sorts of things that way.