Geometry Nodes: Add an "Auto" option for Attribute Convert data type

Currently there is an "Auto" option for the domain, this commit adds a
similar option for "Auto" data type, that uses the data type from the
target attribute or the source attribute (in that order).

Ref T87347

Differential Revision: https://developer.blender.org/D10932
This commit is contained in:
Charlie Jolly 2021-04-30 08:11:45 -05:00 committed by Hans Goudey
parent 6cf28e98fb
commit 95d2d0d35a
Notes: blender-bot 2023-02-14 01:21:16 +01:00
Referenced by issue #87347, Add an "Auto" option for Attribute Convert data type
5 changed files with 39 additions and 20 deletions

View File

@ -1280,10 +1280,9 @@ typedef struct NodeAttributeSeparateXYZ {
typedef struct NodeAttributeConvert {
/* CustomDataType. */
uint8_t data_type;
char _pad[1];
int8_t data_type;
/* AttributeDomain. */
int16_t domain;
int8_t domain;
} NodeAttributeConvert;
typedef struct NodeGeometryMeshCircle {

View File

@ -237,6 +237,7 @@ extern const EnumPropertyItem rna_enum_curveprofile_preset_items[];
extern const EnumPropertyItem rna_enum_preference_section_items[];
extern const EnumPropertyItem rna_enum_attribute_type_items[];
extern const EnumPropertyItem rna_enum_attribute_type_with_auto_items[];
extern const EnumPropertyItem rna_enum_attribute_domain_items[];
extern const EnumPropertyItem rna_enum_attribute_domain_with_auto_items[];
extern const EnumPropertyItem *rna_enum_attribute_domain_itemf(struct ID *id, bool *r_free);

View File

@ -49,6 +49,19 @@ const EnumPropertyItem rna_enum_attribute_type_items[] = {
{0, NULL, 0, NULL, NULL},
};
const EnumPropertyItem rna_enum_attribute_type_with_auto_items[] = {
{CD_AUTO_FROM_NAME, "AUTO", 0, "Auto", ""},
{CD_PROP_FLOAT, "FLOAT", 0, "Float", "Floating-point value"},
{CD_PROP_INT32, "INT", 0, "Integer", "32-bit integer"},
{CD_PROP_FLOAT3, "FLOAT_VECTOR", 0, "Vector", "3D vector with floating-point values"},
{CD_PROP_COLOR, "FLOAT_COLOR", 0, "Color", "RGBA color with floating-point precisions"},
{CD_MLOOPCOL, "BYTE_COLOR", 0, "Byte Color", "RGBA color with 8-bit precision"},
{CD_PROP_STRING, "STRING", 0, "String", "Text string"},
{CD_PROP_BOOL, "BOOLEAN", 0, "Boolean", "True or false"},
{CD_PROP_FLOAT2, "FLOAT2", 0, "2D Vector", "2D vector with floating-point values"},
{0, NULL, 0, NULL, NULL},
};
const EnumPropertyItem rna_enum_attribute_domain_items[] = {
/* Not implement yet */
// {ATTR_DOMAIN_GEOMETRY, "GEOMETRY", 0, "Geometry", "Attribute on (whole) geometry"},

View File

@ -2047,6 +2047,7 @@ static void rna_GeometryNodeAttributeRandomize_data_type_update(Main *bmain,
static bool attribute_convert_type_supported(const EnumPropertyItem *item)
{
return ELEM(item->value,
CD_AUTO_FROM_NAME,
CD_PROP_FLOAT,
CD_PROP_FLOAT2,
CD_PROP_FLOAT3,
@ -2058,7 +2059,8 @@ static const EnumPropertyItem *rna_GeometryNodeAttributeConvert_type_itemf(
bContext *UNUSED(C), PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
{
*r_free = true;
return itemf_function_check(rna_enum_attribute_type_items, attribute_convert_type_supported);
return itemf_function_check(rna_enum_attribute_type_with_auto_items,
attribute_convert_type_supported);
}
static bool attribute_fill_type_supported(const EnumPropertyItem *item)
@ -9017,9 +9019,9 @@ static void def_geo_attribute_convert(StructRNA *srna)
RNA_def_struct_sdna_from(srna, "NodeAttributeConvert", "storage");
prop = RNA_def_property(srna, "data_type", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_items(prop, rna_enum_attribute_type_items);
RNA_def_property_enum_items(prop, rna_enum_attribute_type_with_auto_items);
RNA_def_property_enum_funcs(prop, NULL, NULL, "rna_GeometryNodeAttributeConvert_type_itemf");
RNA_def_property_enum_default(prop, CD_PROP_FLOAT);
RNA_def_property_enum_default(prop, CD_AUTO_FROM_NAME);
RNA_def_property_ui_text(prop, "Data Type", "The data type to save the result attribute with");
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_GeometryNode_socket_update");

View File

@ -35,8 +35,10 @@ static void geo_node_attribute_convert_layout(uiLayout *layout,
bContext *UNUSED(C),
PointerRNA *ptr)
{
uiItemR(layout, ptr, "domain", 0, "", ICON_NONE);
uiItemR(layout, ptr, "data_type", 0, "", ICON_NONE);
uiLayoutSetPropSep(layout, true);
uiLayoutSetPropDecorate(layout, false);
uiItemR(layout, ptr, "domain", 0, nullptr, ICON_NONE);
uiItemR(layout, ptr, "data_type", 0, IFACE_("Type"), ICON_NONE);
}
static void geo_node_attribute_convert_init(bNodeTree *UNUSED(tree), bNode *node)
@ -44,26 +46,27 @@ static void geo_node_attribute_convert_init(bNodeTree *UNUSED(tree), bNode *node
NodeAttributeConvert *data = (NodeAttributeConvert *)MEM_callocN(sizeof(NodeAttributeConvert),
__func__);
data->data_type = CD_PROP_FLOAT;
data->data_type = CD_AUTO_FROM_NAME;
data->domain = ATTR_DOMAIN_AUTO;
node->storage = data;
}
namespace blender::nodes {
static AttributeDomain get_result_domain(const GeometryComponent &component,
StringRef source_name,
StringRef result_name)
static AttributeMetaData get_result_domain_and_type(const GeometryComponent &component,
const StringRef source_name,
const StringRef result_name)
{
std::optional<AttributeMetaData> result_info = component.attribute_get_meta_data(result_name);
if (result_info) {
return result_info->domain;
return *result_info;
}
std::optional<AttributeMetaData> source_info = component.attribute_get_meta_data(source_name);
if (source_info) {
return source_info->domain;
return *source_info;
}
return ATTR_DOMAIN_POINT;
/* The node won't do anything in this case, but we still have to return a value. */
return AttributeMetaData{ATTR_DOMAIN_POINT, CD_PROP_BOOL};
}
static bool conversion_can_be_skipped(const GeometryComponent &component,
@ -92,13 +95,14 @@ static void attribute_convert_calc(GeometryComponent &component,
const GeoNodeExecParams &params,
const StringRef source_name,
const StringRef result_name,
const CustomDataType result_type,
const CustomDataType data_type,
const AttributeDomain domain)
{
const AttributeDomain result_domain = (domain == ATTR_DOMAIN_AUTO) ?
get_result_domain(
component, source_name, result_name) :
domain;
const AttributeMetaData auto_info = get_result_domain_and_type(
component, source_name, result_name);
const AttributeDomain result_domain = (domain == ATTR_DOMAIN_AUTO) ? auto_info.domain : domain;
const CustomDataType result_type = (data_type == CD_AUTO_FROM_NAME) ? auto_info.data_type :
data_type;
if (conversion_can_be_skipped(component, source_name, result_name, result_domain, result_type)) {
return;