Created
September 16, 2023 12:02
-
-
Save esnosy/50d3b2e01fc84ca3467e10437152a85f to your computer and use it in GitHub Desktop.
Ultra fast setting vertices locations of Blender mesh in Python, faster than foreach_set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import ctypes | |
def fast_foreach_set_vertices(mesh: bpy.types.Mesh, array: NDArray): | |
# Ultra fast vertices setting | |
# Thanks a ton!: https://blender.stackexchange.com/a/298488/53664 | |
# beware all things could go wrong here! | |
assert array.dtype == np.float32 | |
assert len(array.shape) == 1 | |
assert array.strides[0] == 4 # sizeof(float) | |
assert array.flags.c_contiguous and array.flags.aligned | |
assert array.nbytes == array.shape[0] * array.strides[0] | |
ptr = mesh.vertices[0].as_pointer() | |
ptr_as_float = ctypes.cast(ptr, ctypes.POINTER(ctypes.c_float)) | |
ctypes.memmove(ptr_as_float, array.ctypes.data, array.nbytes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment