using numpy arrays with pyglet

313 views
Skip to first unread message

tamas

unread,
Aug 31, 2008, 10:57:28 PM8/31/08
to pyglet...@googlegroups.com
Hi all,

I've been interested in using numpy arrays with the pyglet.graphics
api. One of my motivations to use numpy arrays over the ctypes c
arrays is the nice item access that comes with numpy arrays.

The unobtrusive approach is just to just use a numpy array that
references the same memory of the c arrays returned by vertex list
properties (I found some useful discussion related to this at
http://projects.scipy.org/pipermail/numpy-discussion/2006-July/009438.html
that uses the buffer interface to share the memory). This works just
fine, although then you'll need to do some extra work to shape the
array to reflex the vertex/attribute counts, and then update it as the
properties of the vertex list change (resizing, allocation, etc).

I've attached some code (and example usage) to use numpy arrays with
pyglet.graphics. Essentially the 'as_numpy_array' function is all you
need to manipulate pyglets vertex data as numpy arrays. But based on
Alex's suggestion
(http://groups.google.com/group/pyglet-users/msg/92a658460c2547d8),
this sketch implements versions of the relevant classes in
pyglet.graphics. I implemented simple subclasses of the classes in
pyglet.graphics.vertexbuffer to return numpy array views of the
allocated data, as well as a subclass of
pyglet.graphics.vertexdomain.VertexList whose vertexattribute property
descriptors reshape and transpose their numpy arrays to reflect the
vertex/attribute counts, i.e for 4 vertices with the format 'v3f':

((x1, x2, x3, x4),
(y1, y2, y3, y4),
(z1, z2, z3, z4))

Using these classes with pyglet.graphics in the normal way (i.e
pyglet.graphics.vertex_list, pyglet.graphics.Batch.add/add_indexed)
involves hacking the module by replacing some of its classes and
functions (see the 'install' function), although hooks in
pyglet.graphics some time in the future would be nice :)

-tamas

nverts.py
nverts_example.py

tamas

unread,
Sep 1, 2008, 11:56:18 AM9/1/08
to pyglet...@googlegroups.com
sorry, the above nverts.py breaks with VBO support (admittedly i had
only tested this on hardware only supporting opengl < 1.5 until just
now). I've attached a corrected version.
-tamas
nverts.py

tamas

unread,
Sep 21, 2008, 4:23:53 PM9/21/08
to pyglet...@googlegroups.com
I've been looking for another solution that doesn't swap out code in
pyglet.graphics, and doesn't break the vertex list interface for
existing layers, like pyglet.sprite.

Here's another version that uses a subclassed Batch to return custom
VertexList and IndexedVertexList objects that support numpy arrays for
vertex attributes. The numpy arrays are made available though an
attribute 'n' of the vertex list (i.e vlist.n.vertices instead of
vlist.vertices) so it doesn't change the existing interface.

>>> b = nverts2.Batch()
>>> v = b.add(4, 0, None, 'v3f')
# normal c_array access
>>> v.vertices[:]
# numpy ndarray access
>>> v.n.vertices

Any one have thoughts on designing an interface to Numpy ndarray views
with pyglet.graphics?

nverts2.py
Reply all
Reply to author
Forward
0 new messages