[Bf-python] a lesson from numpy

Theo de Ridder theo.de.ridder at planet.nl
Thu Aug 30 22:03:19 CEST 2007


Last couple of weeks I experimented with numpy to enable scalable
interactive animations of swarms. The current BPY is not very suited  
for swarms.
Manipulating large amounts of individual objects on frame events is  
too slow,
and more efficient techniques like DupliVerts or ShapeKeys don't  
facilitate individual r/w access.
And the getData() of keyblock is copying data in a very clumsy and  
read-only way.

So I extended the Key.KeyBlock interface with getBuffer() returning a  
r/w Python buffer wrapping without copying the underlying keyverts.  
Then I could handle in numpy the different aspects like positions,  
rotations, and scales of all subjects as algebraic manipulation of  
multi-dimensional index matrices without having any loop in Python !

I did not incorporate yet lost buffers, but I think that a publish/ 
subscribe of buffers for this is more flexible than
using tracking IDs and forced 1-1 relations (pybuffers do have a slot  
to be made NULL when data is gone).
Using the standard pybuffer protocol for wrapping any contiguous  
memory-data is very simple, general, fast
and save, and removes the burden to do this specific for each Blender  
type. Specifying types and strides can
simply be postponed to the Python level.

Conclusion: when scalability is an issue like in swarms one should  
prefer direct adressable buffers
to iterator for loops and list/object conversions at a Python level.  
And numpy is amazingly suitable
to bring efficient manipulation of low-level Blender data at an  
elegant mathematical level.

Theo

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.blender.org/pipermail/bf-python/attachments/20070830/6d92f9e4/attachment.html>


More information about the Bf-python mailing list