Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly support numpy arrays in foreach_get and foreach_set #344

Closed
Andrej730 opened this issue Feb 12, 2025 · 1 comment · Fixed by #346
Closed

Explicitly support numpy arrays in foreach_get and foreach_set #344

Andrej730 opened this issue Feb 12, 2025 · 1 comment · Fixed by #346
Assignees

Comments

@Andrej730
Copy link
Contributor

Andrej730 commented Feb 12, 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 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.

I was thinking, can we extend seq argument in foreach_get / foreach_set with npt.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_/

Image

Test example:

import bpy
import numpy as np

mesh = 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)
@JonathanPlasse
Copy link
Contributor

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
Andrej730 added a commit to Andrej730/fake-bpy-module that referenced this issue Feb 16, 2025
@nutti nutti linked a pull request Feb 17, 2025 that will close this issue
Andrej730 added a commit to Andrej730/fake-bpy-module that referenced this issue Feb 17, 2025
Andrej730 added a commit to Andrej730/fake-bpy-module that referenced this issue Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants