From 300d499d62385f962d8e32204dea85602eb700cd Mon Sep 17 00:00:00 2001 From: Valtteri Koskivuori Date: Tue, 2 Jan 2024 21:47:10 +0200 Subject: [PATCH] blender: Use MemoryView in display_bitmap as well Way faster than the previous approach, going from ~0.25s to ~0.003s to display the bitmap. --- bindings/blender_init.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bindings/blender_init.py b/bindings/blender_init.py index 3c7a7aed..c9709553 100644 --- a/bindings/blender_init.py +++ b/bindings/blender_init.py @@ -405,15 +405,16 @@ def display_bitmap(self, bm): print("Grabbing float array from lib") start_first = time.time() float_count = bm.width * bm.height * bm.stride - floats = array('f', bm.data.float_ptr[:float_count]) + buffer_from_memory = ct.pythonapi.PyMemoryView_FromMemory + buffer_from_memory.restype = ct.py_object + buffer = buffer_from_memory(bm.data.float_ptr, 4 * float_count) end = time.time() print("Done, took {}s".format(end - start_first)) # We need to work around a bug in foreach_set(), where it gets the length of the first array dimension, instead of the whole array. print("Converting") start = time.time() - floats = np.asarray(floats).reshape(-1, 4) - + floats = np.frombuffer(buffer, np.float32).reshape(-1, 4) # Could also use memoryview, but that's only supported in Blender 4.0 and up # floats = memoryview(floats).cast('b').cast('f', (bm.width * bm.height, 4)) end = time.time()