Drawing a background image in my custom node tree

I would like to draw into 2D space in the background of my custom node tree using the GPU shader module. Can I draw an image so the nodes are on top of my image, but my image doesn’t move in 3D space?

I have tried using PRE_VIEW in my draw handler which draws the nodes on top of my image, but the background grid is also drawn on top. This also results in my image being affected by zooming, scrolling etc.

Using POST_PIXEL in the draw handler, ie drawing in 2D space works except the nodes are underneath my image.

Is this possible?

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

IMAGE_NAME = "Untitled"
image = bpy.data.images[IMAGE_NAME]

shader = gpu.shader.from_builtin('2D_IMAGE')
batch = batch_for_shader(
    shader, 'TRI_FAN',
    {
        "pos": ((100, 100), (200, 100), (200, 200), (100, 200)),
        "texCoord": ((0, 0), (1, 0), (1, 1), (0, 1)),
    },
)

if image.gl_load():
    raise Exception()


def draw():
    bgl.glActiveTexture(bgl.GL_TEXTURE0)
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, image.bindcode)

    shader.bind()
    shader.uniform_int("image", 0)
    batch.draw(shader)


bpy.types.SpaceNodeEditor.draw_handler_add(draw, (), 'WINDOW', 'POST_PIXEL')

I ran into the same issue, I added a backdrop callback, patch is up for review here .

Just committed it, should be in tomorrows build.

Well done LazyDodo!

It works perfectly. Many thanks to you and the blender team, what a fantastic community!