Refactor: Replace BKE_attributes_supported with C++ API

This removes `BKE_attributes_supported`.
Instead, a static method `from_id`
is added to the `AttributeAccessor` class that constructs
the accessor from the given ID. If this fails, `std::nullopt`
is returned.

Pull Request: https://projects.blender.org/blender/blender/pulls/124245
This commit is contained in:
Falk David 2024-07-05 18:41:49 +02:00 committed by Falk David
parent 5360722449
commit f23ade5ac4
5 changed files with 36 additions and 19 deletions

@ -18,7 +18,8 @@
namespace blender::bke { namespace blender::bke {
enum class AttrDomain : int8_t; enum class AttrDomain : int8_t;
} class AttributeAccessor;
} // namespace blender::bke
struct CustomData; struct CustomData;
struct CustomDataLayer; struct CustomDataLayer;
struct ID; struct ID;
@ -73,8 +74,6 @@ class AttributeOwner {
/* Attributes. */ /* Attributes. */
bool BKE_attributes_supported(const AttributeOwner &owner);
/** /**
* Create a new attribute layer. * Create a new attribute layer.
*/ */

@ -477,6 +477,11 @@ class AttributeAccessor {
{ {
} }
/**
* Construct an #AttributeAccessor from an ID.
*/
static std::optional<AttributeAccessor> from_id(const ID &id);
/** /**
* \return True, when the attribute is available. * \return True, when the attribute is available.
*/ */

@ -188,17 +188,6 @@ static std::optional<blender::bke::MutableAttributeAccessor> get_attribute_acces
} // namespace blender::bke } // namespace blender::bke
bool BKE_attributes_supported(const AttributeOwner &owner)
{
const std::array<DomainInfo, ATTR_DOMAIN_NUM> info = get_domains(owner);
for (const int domain : IndexRange(ATTR_DOMAIN_NUM)) {
if (info[domain].customdata) {
return true;
}
}
return false;
}
static bool bke_attribute_rename_if_exists(AttributeOwner &owner, static bool bke_attribute_rename_if_exists(AttributeOwner &owner,
const char *old_name, const char *old_name,
const char *new_name, const char *new_name,

@ -5,12 +5,17 @@
#include <utility> #include <utility>
#include "BKE_attribute_math.hh" #include "BKE_attribute_math.hh"
#include "BKE_curves.hh"
#include "BKE_customdata.hh" #include "BKE_customdata.hh"
#include "BKE_deform.hh" #include "BKE_deform.hh"
#include "BKE_geometry_set.hh" #include "BKE_geometry_set.hh"
#include "BKE_type_conversions.hh" #include "BKE_type_conversions.hh"
#include "DNA_ID.h"
#include "DNA_grease_pencil_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h" #include "DNA_meshdata_types.h"
#include "DNA_pointcloud_types.h"
#include "BLI_array_utils.hh" #include "BLI_array_utils.hh"
#include "BLI_color.hh" #include "BLI_color.hh"
@ -582,6 +587,23 @@ static GVArray try_adapt_data_type(GVArray varray, const CPPType &to_type)
return conversions.try_convert(std::move(varray), to_type); return conversions.try_convert(std::move(varray), to_type);
} }
std::optional<AttributeAccessor> AttributeAccessor::from_id(const ID &id)
{
switch (GS(id.name)) {
case ID_ME:
return reinterpret_cast<const Mesh &>(id).attributes();
case ID_PT:
return reinterpret_cast<const PointCloud &>(id).attributes();
case ID_CV:
return reinterpret_cast<const Curves &>(id).geometry.wrap().attributes();
case ID_GP:
return reinterpret_cast<const GreasePencil &>(id).attributes();
default:
return {};
}
return {};
}
GAttributeReader AttributeAccessor::lookup(const AttributeIDRef &attribute_id, GAttributeReader AttributeAccessor::lookup(const AttributeIDRef &attribute_id,
const std::optional<AttrDomain> domain, const std::optional<AttrDomain> domain,
const std::optional<eCustomDataType> data_type) const const std::optional<eCustomDataType> data_type) const

@ -211,15 +211,17 @@ bool attribute_set_poll(bContext &C, const ID &object_data)
static bool geometry_attributes_poll(bContext *C) static bool geometry_attributes_poll(bContext *C)
{ {
using namespace blender::bke;
const Object *ob = object::context_object(C); const Object *ob = object::context_object(C);
const Main *bmain = CTX_data_main(C); const Main *bmain = CTX_data_main(C);
ID *data = (ob) ? static_cast<ID *>(ob->data) : nullptr; if (!ob || !BKE_id_is_editable(bmain, &ob->id)) {
AttributeOwner owner = AttributeOwner::from_id(data);
if (!owner.is_valid()) {
return false; return false;
} }
return (ob && BKE_id_is_editable(bmain, &ob->id) && data && BKE_id_is_editable(bmain, data)) && const ID *data = (ob) ? static_cast<const ID *>(ob->data) : nullptr;
BKE_attributes_supported(owner); if (!data || !BKE_id_is_editable(bmain, data)) {
return false;
}
return AttributeAccessor::from_id(*data).has_value();
} }
static bool geometry_attributes_remove_poll(bContext *C) static bool geometry_attributes_remove_poll(bContext *C)