diff options
author | Jacques Lucke <jacques@blender.org> | 2022-07-08 17:16:56 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2022-07-08 17:16:56 +0300 |
commit | b876ce2a4a4638142439a7cf265a0780491ae4cc (patch) | |
tree | 871d71eb6d1cf215869fc941c831c81bcacc6433 /source/blender/editors/geometry/geometry_attributes.cc | |
parent | f391e8f316bd29b700cef874a59cf3b64203d70c (diff) |
Geometry Nodes: new geometry attribute API
Currently, there are two attribute API. The first, defined in `BKE_attribute.h` is
accessible from RNA and C code. The second is implemented with `GeometryComponent`
and is only accessible in C++ code. The second is widely used, but only being
accessible through the `GeometrySet` API makes it awkward to use, and even impossible
for types that don't correspond directly to a geometry component like `CurvesGeometry`.
This patch adds a new attribute API, designed to replace the `GeometryComponent`
attribute API now, and to eventually replace or be the basis of the other one.
The basic idea is that there is an `AttributeAccessor` class that allows code to
interact with a set of attributes owned by some geometry. The accessor itself has
no ownership. `AttributeAccessor` is a simple type that can be passed around by
value. That makes it easy to return it from functions and to store it in containers.
For const-correctness, there is also a `MutableAttributeAccessor` that allows
changing individual and can add or remove attributes.
Currently, `AttributeAccessor` is composed of two pointers. The first is a pointer
to the owner of the attribute data. The second is a pointer to a struct with
function pointers, that is similar to a virtual function table. The functions
know how to access attributes on the owner.
The actual attribute access for geometries is still implemented with the `AttributeProvider`
pattern, which makes it easy to support different sources of attributes on a
geometry and simplifies dealing with built-in attributes.
There are different ways to get an attribute accessor for a geometry:
* `GeometryComponent.attributes()`
* `CurvesGeometry.attributes()`
* `bke::mesh_attributes(const Mesh &)`
* `bke::pointcloud_attributes(const PointCloud &)`
All of these also have a `_for_write` variant that returns a `MutabelAttributeAccessor`.
Differential Revision: https://developer.blender.org/D15280
Diffstat (limited to 'source/blender/editors/geometry/geometry_attributes.cc')
-rw-r--r-- | source/blender/editors/geometry/geometry_attributes.cc | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/source/blender/editors/geometry/geometry_attributes.cc b/source/blender/editors/geometry/geometry_attributes.cc index c7e782b7b89..4cf14334ac7 100644 --- a/source/blender/editors/geometry/geometry_attributes.cc +++ b/source/blender/editors/geometry/geometry_attributes.cc @@ -282,8 +282,7 @@ static int geometry_attribute_convert_exec(bContext *C, wmOperator *op) RNA_enum_get(op->ptr, "mode")); Mesh *mesh = reinterpret_cast<Mesh *>(ob_data); - MeshComponent mesh_component; - mesh_component.replace(mesh, GeometryOwnershipType::Editable); + bke::MutableAttributeAccessor attributes = bke::mesh_attributes_for_write(*mesh); /* General conversion steps are always the same: * 1. Convert old data to right domain and data type. @@ -301,33 +300,33 @@ static int geometry_attribute_convert_exec(bContext *C, wmOperator *op) return OPERATOR_CANCELLED; } - GVArray src_varray = mesh_component.attribute_get_for_read(name, dst_domain, dst_type); + GVArray src_varray = attributes.lookup_or_default(name, dst_domain, dst_type); const CPPType &cpp_type = src_varray.type(); void *new_data = MEM_malloc_arrayN(src_varray.size(), cpp_type.size(), __func__); src_varray.materialize_to_uninitialized(new_data); - mesh_component.attribute_try_delete(name); - mesh_component.attribute_try_create(name, dst_domain, dst_type, AttributeInitMove(new_data)); + attributes.remove(name); + attributes.add(name, dst_domain, dst_type, blender::bke::AttributeInitMove(new_data)); break; } case ConvertAttributeMode::UVMap: { MLoopUV *dst_uvs = static_cast<MLoopUV *>( MEM_calloc_arrayN(mesh->totloop, sizeof(MLoopUV), __func__)); - VArray<float2> src_varray = mesh_component.attribute_get_for_read<float2>( + VArray<float2> src_varray = attributes.lookup_or_default<float2>( name, ATTR_DOMAIN_CORNER, {0.0f, 0.0f}); for (const int i : IndexRange(mesh->totloop)) { copy_v2_v2(dst_uvs[i].uv, src_varray[i]); } - mesh_component.attribute_try_delete(name); + attributes.remove(name); CustomData_add_layer_named( &mesh->ldata, CD_MLOOPUV, CD_ASSIGN, dst_uvs, mesh->totloop, name.c_str()); break; } case ConvertAttributeMode::VertexGroup: { Array<float> src_weights(mesh->totvert); - VArray<float> src_varray = mesh_component.attribute_get_for_read<float>( + VArray<float> src_varray = attributes.lookup_or_default<float>( name, ATTR_DOMAIN_POINT, 0.0f); src_varray.materialize(src_weights); - mesh_component.attribute_try_delete(name); + attributes.remove(name); bDeformGroup *defgroup = BKE_object_defgroup_new(ob, name.c_str()); const int defgroup_index = BLI_findindex(BKE_id_defgroup_list_get(&mesh->id), defgroup); @@ -652,15 +651,16 @@ bool ED_geometry_attribute_convert(Mesh *mesh, return false; } - MeshComponent mesh_component; - mesh_component.replace(mesh, GeometryOwnershipType::Editable); - GVArray src_varray = mesh_component.attribute_get_for_read(name, new_domain, new_type); + blender::bke::MutableAttributeAccessor attributes = blender::bke::mesh_attributes_for_write( + *mesh); + + GVArray src_varray = attributes.lookup_or_default(name, new_domain, new_type); const CPPType &cpp_type = src_varray.type(); void *new_data = MEM_malloc_arrayN(src_varray.size(), cpp_type.size(), __func__); src_varray.materialize_to_uninitialized(new_data); - mesh_component.attribute_try_delete(name); - mesh_component.attribute_try_create(name, new_domain, new_type, AttributeInitMove(new_data)); + attributes.remove(name); + attributes.add(name, new_domain, new_type, blender::bke::AttributeInitMove(new_data)); int *active_index = BKE_id_attributes_active_index_p(&mesh->id); if (*active_index > 0) { |