Page MenuHome

bgl.Buffer using Python Buffer Protocol
Needs RevisionPublic

Authored by Oscar Aceña (OscarAcena) on May 2 2014, 1:36 PM.

Details

Reviewers
None
Summary

This patch adds support on bgl.Buffer with the minimal requiered implementation of Python Buffer Protocol (https://docs.python.org/3/c-api/buffer.html). This is good when anyone needs to work in python with a big bgl.Buffer, because it avoids the copy of the data.

I've used it to take a ImageRender and save it to disk,

Diff Detail

Event Timeline

Patch is simple, but seems like support could be a bit more comprehensive, eg, support for shape, for multi-dimensional buffers. See: https://docs.python.org/3/c-api/buffer.html#c.Py_buffer.shape

source/blender/python/generic/bgl.h
67

Not sure this is a very good method of managing reference counts,

Wouldn't it be better to store a PyObject * to the source?

Thats how mathutils works, see - BASE_MATH_MEMBERS, cb_user.

Yeah, adding support for multi-dimensional buffers could be done. In this case, I've tried to implement the same funcionality as the method Buffer_to_list, using the helper function PyBuffer_FillInfo, because this was just what I needed :)

I'll try to update the patch following your directions.

source/blender/python/generic/bgl.h
67

I've used the same method as bytearray (https://github.com/python/cpython/blob/master/Objects/bytearrayobject.c#L55). It is used only to issue a traceback on release, there is not actually many implications.

Hi again,

I've been looking the code on bgl.c, just to see how easy it will be to add support for n-dimensional arrays. It's not hard, but I estimate that it will consume more time than what my work allows. I don't want to end up with a lot of bugs, so I would need to test it thoughtfully. Until I have the needed time, this patch is what I can do. I hope it will be useful to others as well.

Cheers.

Hi again!

Just one more question, could this patch be a candidate for inclusion in the main release?

Yep, Generally - all patches get reviewed for inclusion in main release.

Though this wont make it into 2.71

Ok. I'll wait eagerly to the new release ;) Meanwhile, here is a link of a project called "Insight" which makes use of this patch. It's a Python module which gives the ability to stream the video of any camera when the Game Engine is running. Hopefully, this will be useful for someone.

https://bitbucket.org/arco_group/insight

Cheers.

Haven't read up on pybuffers in a while but there's most likely some built-in method to manage the lifecycle on them.

source/blender/python/generic/bgl.c
612

Pretty sure throwing a system error is wrong.

The way it probably should work is manage the reference counts on the buffer as the views are created/destroyed so the GC never tries to call dealloc in the first place.

Campbell Barton (campbellbarton) requested changes to this revision.Feb 2 2015, 2:55 PM
Campbell Barton (campbellbarton) edited edge metadata.
This revision now requires changes to proceed.Feb 2 2015, 2:55 PM