I have problems with the GPU Module and rendering custom geometry in Blender. When I run the example code from the Blender docs, it works as expected. Running the first line drawing example produces the image below, drawing a thin red line ten units up in Z:
import bpy
import gpu
from gpu_extras.batch import batch_for_shader
coords = [(0, 0, 0), (0, 0, 10)]
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, 0, 0, 1))
batch.draw(shader)
bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')
But, when I try to encapsulate the code in a simple class, things get weird. The same coordinates in the code produces a line in a weird position, downwards at an angle instead of straight up. And I have to execute the code twice to make it even show up.
import bpy
import gpu
from gpu_extras.batch import batch_for_shader
class drawManager:
coords = []
shader = None
batch = None
handle = None
def __init__(self):
self.coords = [(0, 0, 0), (0, 0, 10)]
self.shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
self.batch = batch_for_shader(self.shader, 'LINES', {"pos": self.coords})
def draw(self):
self.shader.bind()
self.shader.uniform_float("color", (1, 0, 0, 1))
self.batch.draw(self.shader)
def doDraw(self):
handle = bpy.types.SpaceView3D.draw_handler_add(self.draw, (), 'WINDOW', 'POST_VIEW')
def stopDraw(self):
bpy.types.SpaceView3D.draw_handler_remove(self.handle, 'WINDOW')
dm = drawManager()
dm.draw()
dm.doDraw()
Am I missing something essential here or is this a weird bug?