bgl module: Interpret a buffer as a bgl.Buffer

Authored by Germano Cavalcante (mano-wii) on Sep 27 2017, 3:41 PM.



Working with OpenGL often needs the use of large buffer (like textures, vbos, etc).

It is often more convenient and efficient to work with such buffers using other modules such as numpy, array, or ctypes.

When starting a bgl.Buffer, reading and copying large buffers, even more as a sequence, is inefficient and may even freeze the execution of a code for an inconvenient time.

This patch allows to access the buffer of an object with buffer interface, directly and without intermediate copying.


>>> import numpy as np
>>> import bgl
>>> a = np.arange(10, dtype = 'i4')
>>> buf = bgl.Buffer(bgl.GL_INT, a.shape, a)
>>> buf
Buffer(GL_INT, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

>>> a[0] = 10
>>> buf
Buffer(GL_INT, [10, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Diff Detail

rB Blender

Generally seems fine.


naming isnt clear, could be gl_buffer_type_from_py_format_char


Naming here is also odd. BGL_MakeBuffer_ext makes this seem like an extended version of BGL_MakeBuffer. Maybe call BGL_MakeBuffer_FromData?

This revision is now accepted and ready to land.Sep 27 2017, 4:01 PM
This revision was automatically updated to reflect the committed changes.
Germano Cavalcante (mano-wii) marked 2 inline comments as done.Sep 27 2017, 4:22 PM