[Guide] Make your addons faster with Numpy

https://www.labri.fr/perso/nrougier/from-python-to-numpy/

If you’re interested in making great performing addons, I found this great resource that I thought was good to share. Now, Blender doesn’t include Scipy but there are many good points about just Numpy to justify a read.

It also shows the great possibilities that a good Python API and support give applications.

1 Like

Good Stuff!

This is great stuff! It’s also a little above my head at this stage in my python experience.

Can anybody post some examples using some of these techniques modifying actual Blender data that is much faster than with bpy?

One in particular that would be interesting to me would be going through all the fcurves of a fairly large selection of objects, retrieving all the keypoints and applying a numeric offset to only the selected keys…

Or maybe applying some kind of proximity deformation effect to the verts of a mesh to simulate a volume preserving collision with the ground or faces of another object, almost like a fake soft body effect.

Some Blender specific examples would go a long way in helping people start speeding up their scripts.

Anyway, really good find. Thanks for posting!

@pacermike: here’s one I made a while ago: https://github.com/amb/blender-scripts/blob/master/mesh_curves.py

read_* and calc_normals functions are the most relevant parts. Unfortunately it’s not very readable, but that the price you pay when you start optimizing. But the speedup was definitely worth it.

Starting with simple Numpy tutorials around the net would probably be a good idea. Reading data from Blender into Numpy arrays is quite straight forward. The biggest hurdle is understanding and mastering how Numpy itself works.

foreach_get and foreach_set is a way you can transfer data fast from Blender to Numpy and back.

@ Ambi

interesting
you could also use the Cuda python
but not certain it is worth the time for small task

can you explain how or what the little code snippet you did do ?

wold be nice to see more examples using numpy !

Numpy with array is very powerfull for doing Matrix math
or solving N dimen equations!
or even using Complex numbers which would be great for helping making Group nodes!

thanks
happy bl

@ambi:

Thanks for this! I’ll start digging in :slight_smile:

@Ricky: The linked addon calculates mesh curvatures and saves them into vertex colors. It averages the neighbouring vertex normals differences for each vertex.

so to make it work you need a curve object selected !

thanks for feedback

happy bl

I will definitely read this. I wish blender had build in PIL and scipy. Numpy is cool, but not always enough.
I wonder where do I poke blender devs to include both of above into master.