Skip to content

Commit

Permalink
blender: Use MemoryView in display_bitmap as well
Browse files Browse the repository at this point in the history
Way faster than the previous approach, going from ~0.25s to ~0.003s to
display the bitmap.
  • Loading branch information
vkoskiv committed Jan 2, 2024
1 parent 6f3c45c commit 300d499
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions bindings/blender_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 300d499

Please sign in to comment.