Python API: Direct access to attribute arrays of meshes, curves, point clouds and GP drawings #122091
No reviewers
Labels
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Code Documentation
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Asset System
Module
Core
Module
Development Management
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#122091
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "SietseB/blender:core-rna-python-attribute-array"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This PR implements a Python API for direct access to attribute arrays of meshes, curves, point clouds and Grease Pencil drawings.
Current access to attribute arrays has its limitations, mainly because they are defined as property collections, which always return RNA property objects, not direct values.
This PR implements a more direct approach to lift these limitations.
Direct access to attributes
.attributes[name].data_array
Attribute type aware
All attribute type are automatically handled, so retrieving a
float3
attribute will return aVector(x, y, z)
, aColorGeometry4f
attribute will give a[r, g, b, a]
, etc.Foreach_get and foreach_set
.data_array.foreach_get(array)
.data_array.foreach_set(array)
Fill attribute
.data_array.fill(value)
.data_array.fill(value, indices)
Technical notes
BPy_AttributeArray
code in its own submodule inpython/generic
, similar to ID Properties Access.ColorGeometry4f
type instead offloat4
(and others) 34af3b796c.fill(value, indices)
97614244a1I'm not convinced it's worth it for us to maintain such an API outside of RNA. Some of these improvements would be more generally beneficial applied to RNA itself rather than just one custom type. The need for
RNA_struct_is_attribute_array
shows how this doesn't fit that well into existing design IMO. At least this should be discussed with core module members before moving forward further.There are a few more important aspects with RNA/Python access to attribute data. However we improve attribute access, these issues should be improved too.
CustomData
at some point with a more flexible/modern storage system. We should avoid building abstractions directly on top of CustomData directly in the meantime.The last three issues can be resolved by building on top of the C++ attribute API in
BKE_attribute.hh
rather than the CustomData system.@ -43,2 +43,4 @@
/** Shape key-block unique id reference. */
int uid;
/** Only for use in RNA and #bpy_rna: number of items in the layer data. */
int length;
I think it's important to avoid adding this. It should be retrieved from the "context" as necessary. That sort of redundant storage adds plenty of problems with syncing and invalid states.
The 'context' is the problem here. The function that creates the Python object only receives a
PointerRNA *ptr
, which contains anID owner_id
andvoid *data
. For meshes and curves, we can derive the length of the attribute array from theowner_id
, but for Grease Pencil we can't, because the geometry is in aDrawing
, which isn't anID
type.I see the problems you mention, of course, but I don't see a better solution for now. Any ideas, perhaps?
There was some discussion on improving the state of things regarding ownership of data pointed at by
PointerRNA
. See #122431I think storing the array length in the custom data layer is not that bad, if this was a C++ data type this would be a
Vector
and so it would have the size too. But if this is done, I think it requires some refactoring in customdata.cc to ensure any array pointer changes go along with size changes. Maybe affects some public API functions as well, where maybe it needs to then pass an Array instead of a pointer.Not quite.
ImplicitSharingInfo
serves as the ownership here.data
just serves as quick access, pointing to any contiguous array. At the very least it should be stored inCustomData
, notCustomDataLayer
.Anyway, improvements to
PointerRNA
should make this change unnecessary.I think you'd still have some way of freeing the data in a destructor, which for some data types requires the length. To me it's strange for a data structure to not know the length of its own array members, and instead passing it into various
CustomData_
API functions.ImplicitSharingInfo
handles the destruction itself. AllCustomData
should have to do is decrement the user count. But your overall point is fair.@ -0,0 +425,4 @@
char buffer_format_a;
char buffer_format_b;
std::string description;
std::function<PyObject *(void *, int)> get_attribute;
Use simpler types here: function pointers and
StringRef
will do the trick and will be faster. That said, I'm not sure about passing everything through a function pointer. The indirection seems unnecessarily complex. Why not just use a switch statement?@ -0,0 +429,4 @@
std::function<bool(void *, int, PyObject *)> set_attribute;
};
std::unordered_map<int, CustomDataTypeMapping> data_types{
Use
Map
in Blender code rather than the std library typesRepeating my comment from devtalk:
I think this can be implemented as a generic RNA feature for arrays rather than something attribute specific. It could define a multi dimensional array (like number of elements x 3), set it a vector or color subtype, and have the Python RNA wrapping recognize that.
Checkout
From your project repository, check out a new branch and test the changes.