Python: Attributes: Add domain_size function

This function returns the number of elements of a given domain.

Example:
```py
>>> attributes.domain_size('POINT')
500
```

Pull Request: https://projects.blender.org/blender/blender/pulls/125519
This commit is contained in:
Falk David
2024-07-29 11:14:50 +02:00
committed by Falk David
parent cd577f02b9
commit a1630792cf
3 changed files with 47 additions and 0 deletions

View File

@@ -111,6 +111,7 @@ struct CustomDataLayer *BKE_attribute_search_for_write(AttributeOwner &owner,
blender::bke::AttrDomain BKE_attribute_domain(const AttributeOwner &owner,
const struct CustomDataLayer *layer);
int BKE_attribute_domain_size(const AttributeOwner &owner, int domain);
int BKE_attribute_data_length(AttributeOwner &owner, struct CustomDataLayer *layer);
bool BKE_attribute_required(const AttributeOwner &owner, const char *name);
bool BKE_attribute_rename(AttributeOwner &owner,

View File

@@ -727,6 +727,12 @@ AttrDomain BKE_attribute_domain(const AttributeOwner &owner, const CustomDataLay
return AttrDomain(AttrDomain::Point);
}
int BKE_attribute_domain_size(const AttributeOwner &owner, const int domain)
{
const std::array<DomainInfo, ATTR_DOMAIN_NUM> info = get_domains(owner);
return info[domain].length;
}
int BKE_attribute_data_length(AttributeOwner &owner, CustomDataLayer *layer)
{
/* When in mesh editmode, attributes point to bmesh customdata layers, the attribute data is

View File

@@ -695,6 +695,12 @@ static void rna_AttributeGroup_update_active_color(Main * /*bmain*/,
}
}
static int rna_AttributeGroupID_domain_size(ID *id, const int domain)
{
AttributeOwner owner = AttributeOwner::from_id(id);
return BKE_attribute_domain_size(owner, domain);
}
static PointerRNA rna_AttributeGroupMesh_active_color_get(PointerRNA *ptr)
{
AttributeOwner owner = AttributeOwner::from_id(ptr->owner_id);
@@ -938,6 +944,13 @@ static void rna_AttributeGroupGreasePencilDrawing_active_index_range(
*softmax = *max;
}
static int rna_AttributeGroupGreasePencilDrawing_domain_size(GreasePencilDrawing *drawing,
const int domain)
{
AttributeOwner owner = AttributeOwner(AttributeOwnerType::GreasePencilDrawing, drawing);
return BKE_attribute_domain_size(owner, domain);
}
#else
static void rna_def_attribute_float(BlenderRNA *brna)
@@ -1498,6 +1511,19 @@ static void rna_def_attribute_group_id_common(StructRNA *srna)
"rna_AttributeGroupID_active_index_set",
"rna_AttributeGroupID_active_index_range");
RNA_def_property_update(prop, 0, "rna_AttributeGroup_update_active");
/* Domain Size */
func = RNA_def_function(srna, "domain_size", "rna_AttributeGroupID_domain_size");
RNA_def_function_ui_description(func, "Get the size of a given domain");
parm = RNA_def_enum(func,
"domain",
rna_enum_attribute_domain_items,
int(AttrDomain::Point),
"Domain",
"Type of element that attribute is stored on");
RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_REQUIRED);
parm = RNA_def_int(func, "size", 0, 0, INT_MAX, "Size", "Size of the domain", 0, INT_MAX);
RNA_def_function_return(func, parm);
}
static void rna_def_attribute_group_mesh(BlenderRNA *brna)
@@ -1660,6 +1686,20 @@ static void rna_def_attribute_group_grease_pencil_drawing(BlenderRNA *brna)
"rna_AttributeGroupGreasePencilDrawing_active_index_set",
"rna_AttributeGroupGreasePencilDrawing_active_index_range");
RNA_def_property_update(prop, 0, "rna_AttributeGroup_update_active");
/* Domain Size */
func = RNA_def_function(
srna, "domain_size", "rna_AttributeGroupGreasePencilDrawing_domain_size");
RNA_def_function_ui_description(func, "Get the size of a given domain");
parm = RNA_def_enum(func,
"domain",
rna_enum_attribute_domain_items,
int(AttrDomain::Point),
"Domain",
"Type of element that attribute is stored on");
RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_REQUIRED);
parm = RNA_def_int(func, "size", 0, 0, INT_MAX, "Size", "Size of the domain", 0, INT_MAX);
RNA_def_function_return(func, parm);
}
void rna_def_attributes_common(StructRNA *srna, const AttributeOwnerType type)