OpenGL drawing: size in 2.80?


I’m converting my 2.7 scripts into 2.8 and there is something I don’t know how to do with OpenGL drawing: before, I used bgl.glPointSize() and bgl.glLineWidth() to make the display thicker. Is it possible to do that with the gpu high level API? I see ‘pos’ and ‘color’ attributes in content of batch_for_shader() but nothing related to the size.

I’m using:
shader = gpu.shader.from_builtin(‘3D_FLAT_COLOR’)
batch_for_shader(shader, ‘POINTS’, {‘pos’: [(…)], ‘color’: [(…)]})

Should I use low level OpenGL to set the size? if this is the only way, is there some example I could look into to understand it?


Aparently, it does work

import bpy
import gpu
import gpu_extras
import bgl
from random import random

positions = [(random(), random(), random()) for i in range(500)]
colors = [(random(), random(), random(), random()) for i in range(500)]

shader = gpu.shader.from_builtin("3D_FLAT_COLOR")
batch = gpu_extras.batch.batch_for_shader(shader, "POINTS", {"pos": positions, "color": colors})

def draw():

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


1 Like

Oh thank you ! I didn’t know I could just do it that way.

When I try your code snippet I get the error below (Win10 x64). Is that the full snippet?

Traceback (most recent call last):
  File "\Text", line 12, in <module>
AttributeError: module 'gpu_extras' has no attribute 'batch'
Error: Python script fail, look in the console for now...

Only way to make it work seems to be (which is interesting)

from gpu_extras import batch
batch = batch.batch_for_shader(shader, "POINTS", {"pos": positions, "color": colors})

are you sure you are using an up to date blender 2.8 build?
maybe its a weird bug on the build you are using.

Ok I will try it with a recent version.

Ok it works with the most recent version, sorry about the noise.

update on Oct 29, 2019:
today somehow the add-on stopped working on the laptop too. I have to add from gpu_extras import batch. Then I can even use gpu_extras.batch.batch_for_shader directly.

On Oct 26, 2019:

I have also encountered this error on a desktop and surface pro 6 yesterday, but not on my laptop. All of them have blender 2.80 (sub 75) installed. The only relevant difference seems to be the graphics cards. Desktop has GTX 1070, surface pro 6 has Intel UHD Graphics 620, and laptop has RTX 2070. On both surface pro 6 and desktop I can use from gpu_extras import batch to get around this problem, as suggested above.
Well, another difference is that I developed the add-on on my laptop, then installed it on desktop and surface pro, but should not be relevant.