Skip to content

Instantly share code, notes, and snippets.

@esnosy
Created September 16, 2023 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esnosy/50d3b2e01fc84ca3467e10437152a85f to your computer and use it in GitHub Desktop.
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
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