When I spoke at the Blender conference on creating an interactive cloth engine using python, I was surprised how many people knew nothing about Numpy. I’ve started to share some of the amazing things you can do with it inside of blender. Numpy lets you do things with python that can sometimes outperform comparable c code. Basic operations with Numpy are between 20 and 1000 times faster than typical python looping on big data. The code is also very clean and succinct.
I have four videos so far and I’m making more.
No commercials! No paying. Just sharing with the blender community as they’ve shared so much with me!
Thanks for this. I’ve used Numpy in some of my code, but felt I didn’t understand it enough to use it consistently, or know when using it would be beneficial.
I’ve been working on updating my code to use numpy and foreach for better performance, but have hit a snag. Any idea why this simplified version of setting uv images with foreach_set doesn’t work?
# get the active object and its UV
ob = bpy.context.scene.objects.active
uv = ob.data.uv_textures.active
# use the first loaded image for example
img = bpy.data.images[0]
# apply the image with a slow for loop (works)
for data in uv.data:
data.image = img
# apply the image with fast C loop (doesnt work)
uv.data.foreach_set('image', [img] * len(uv.data))
I’m not sure if there is a way to write an array full of objects to a blender collection. I don’t think I’ve ever gotten an array of images to work with foreach_set or foreach_get. I’ll look into it some more but if you find a solution let me know. So far I’ve only gotten it to work with bool, float, and int types. An image is an object with attributes. Maybe to use foreach_set for the image you just need an index array. Not sure. I couldn’t get it to work either.