You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Though we did included typing.Buffer as possible argument in #257 those methods still cannot accept numpy arrays.
typing.Buffer requires __buffer__ method to be implemented on class and in Python 3.11 though it wasn't exposed to Python (e.g. np.eye(3).__buffer__ will result in AttributeError: 'numpy.ndarray' object has no attribute '__buffer__') it was implemented internally in C-extension and buffer copy does work.
array.array had a similar thing and what they did, they virtually implemented __buffer__ method in the stub file (python/typeshed#10225) - basically just added to to .pyi though accessing it will result in AttributeError too. Though it might not be entirely correct, it probably what users are expecting from Buffer protocol.
I've created an issue in numpy (numpy/numpy#26783) and submitted a PR (numpy/numpy#26784) to do the similar thing but it wasn't accepted to avoid confusion with virtual method floating around.
importbpyimportnumpyasnpmesh=bpy.data.meshes[0]
verts_= []
mesh.vertices.foreach_get("co", verts_)
verts=np.empty(len(mesh.vertices) *3, dtype="f")
# Argument of type "NDArray[Any]" cannot be assigned to parameter "seq" of type "MutableSequence[bool] | MutableSequence[int] | MutableSequence[float] | Buffer" in function "foreach_get"mesh.vertices.foreach_get("co", verts)
The text was updated successfully, but these errors were encountered:
I guess it is ok, numpy is always installed in Blender.
People will have to install numpy as a dev dependency for Pyright not to complain about unknown something when in strict mode.
Andrej730
added a commit
to Andrej730/fake-bpy-module
that referenced
this issue
Feb 16, 2025
Though we did included
typing.Buffer
as possible argument in #257 those methods still cannot accept numpy arrays.typing.Buffer
requires__buffer__
method to be implemented on class and in Python 3.11 though it wasn't exposed to Python (e.g.np.eye(3).__buffer__
will result inAttributeError: 'numpy.ndarray' object has no attribute '__buffer__'
) it was implemented internally in C-extension and buffer copy does work.array.array
had a similar thing and what they did, they virtually implemented__buffer__
method in the stub file (python/typeshed#10225) - basically just added to to .pyi though accessing it will result inAttributeError
too. Though it might not be entirely correct, it probably what users are expecting fromBuffer
protocol.I've created an issue in numpy (numpy/numpy#26783) and submitted a PR (numpy/numpy#26784) to do the similar thing but it wasn't accepted to avoid confusion with virtual method floating around.
I was thinking, can we extend
seq
argument inforeach_get
/foreach_set
withnpt.NDArray
explicitly? Numpy seems to be the most convenient container for those methods as it does support very efficient buffer copy - https://b3d.interplanety.org/en/optimizing-the-speed-of-data-access-using-foreach_/Test example:
The text was updated successfully, but these errors were encountered: