Cleanup: Use abstraction for attribute math node input

Since creating the attribute node, a helper function has been added to
automatically get the input attribute or a constant value, depending on
the "input type" values for the node. This commit replaces the specific
implementation of that behavior with the new helper function.

The versioning is necessary since the node now has a "storage" struct.
This commit is contained in:
Hans Goudey 2020-12-17 14:43:01 -06:00
parent b10d8e330e
commit 23233fcf05
Notes: blender-bot 2023-02-14 05:36:11 +01:00
Referenced by issue #83989, Geometry Nodes: Attribute Math Node Always Adds Its Operands Regardless of the Operator Setting
4 changed files with 64 additions and 58 deletions

View File

@ -1428,5 +1428,26 @@ void blo_do_versions_290(FileData *fd, Library *UNUSED(lib), Main *bmain)
*/
{
/* Keep this block, even when empty. */
FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
if (ntree->type == NTREE_GEOMETRY) {
LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
if (node->type == GEO_NODE_ATTRIBUTE_MATH && node->storage == NULL) {
const int old_use_attibute_a = (1 << 0);
const int old_use_attibute_b = (1 << 1);
NodeAttributeMath *data = MEM_callocN(sizeof(NodeAttributeMath), "NodeAttributeMath");
data->operation = NODE_MATH_ADD;
data->input_type_a = (node->custom2 & old_use_attibute_a) ?
GEO_NODE_ATTRIBUTE_INPUT_ATTRIBUTE :
GEO_NODE_ATTRIBUTE_INPUT_FLOAT;
data->input_type_b = (node->custom2 & old_use_attibute_b) ?
GEO_NODE_ATTRIBUTE_INPUT_ATTRIBUTE :
GEO_NODE_ATTRIBUTE_INPUT_FLOAT;
node->storage = data;
}
}
}
}
FOREACH_NODETREE_END;
}
}

View File

@ -1085,6 +1085,17 @@ typedef struct NodeAttributeCompare {
char _pad[5];
} NodeAttributeCompare;
typedef struct NodeAttributeMath {
/* e.g. NODE_MATH_ADD. */
uint8_t operation;
/* GeometryNodeAttributeInputMode */
uint8_t input_type_a;
uint8_t input_type_b;
char _pad[5];
} NodeAttributeMath;
typedef struct NodeAttributeMix {
/* e.g. MA_RAMP_BLEND. */
uint8_t blend_type;
@ -1499,11 +1510,6 @@ typedef enum GeometryNodeTriangulateQuads {
GEO_NODE_TRIANGULATE_QUAD_SHORTEDGE = 3,
} GeometryNodeTriangulateQuads;
typedef enum GeometryNodeUseAttributeFlag {
GEO_NODE_USE_ATTRIBUTE_A = (1 << 0),
GEO_NODE_USE_ATTRIBUTE_B = (1 << 1),
} GeometryNodeUseAttributeFlag;
typedef enum GeometryNodePointInstanceType {
GEO_NODE_POINT_INSTANCE_TYPE_OBJECT = 0,
GEO_NODE_POINT_INSTANCE_TYPE_COLLECTION = 1,

View File

@ -426,18 +426,6 @@ static const EnumPropertyItem rna_node_geometry_triangulate_ngon_method_items[]
{0, NULL, 0, NULL, NULL},
};
static const EnumPropertyItem rna_node_geometry_attribute_input_a_items[] = {
{0, "FLOAT", 0, "Float", ""},
{GEO_NODE_USE_ATTRIBUTE_A, "ATTRIBUTE", 0, "Attribute", ""},
{0, NULL, 0, NULL, NULL},
};
static const EnumPropertyItem rna_node_geometry_attribute_input_b_items[] = {
{0, "FLOAT", 0, "Float", ""},
{GEO_NODE_USE_ATTRIBUTE_B, "ATTRIBUTE", 0, "Attribute", ""},
{0, NULL, 0, NULL, NULL},
};
# define ITEM_ATTRIBUTE \
{ \
GEO_NODE_ATTRIBUTE_INPUT_ATTRIBUTE, "ATTRIBUTE", 0, "Attribute", "" \
@ -8454,8 +8442,10 @@ static void def_geo_attribute_math(StructRNA *srna)
{
PropertyRNA *prop;
RNA_def_struct_sdna_from(srna, "NodeAttributeMath", "storage");
prop = RNA_def_property(srna, "operation", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_sdna(prop, NULL, "custom1");
RNA_def_property_enum_sdna(prop, NULL, "operation");
RNA_def_property_enum_items(prop, rna_enum_node_math_items);
RNA_def_property_enum_funcs(prop, NULL, NULL, "rna_GeometryNodeAttributeMath_operation_itemf");
RNA_def_property_enum_default(prop, NODE_MATH_ADD);
@ -8463,14 +8453,14 @@ static void def_geo_attribute_math(StructRNA *srna)
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update");
prop = RNA_def_property(srna, "input_type_a", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_bitflag_sdna(prop, NULL, "custom2");
RNA_def_property_enum_items(prop, rna_node_geometry_attribute_input_a_items);
RNA_def_property_enum_bitflag_sdna(prop, NULL, "input_type_a");
RNA_def_property_enum_items(prop, rna_node_geometry_attribute_input_type_items_float);
RNA_def_property_ui_text(prop, "Input Type A", "");
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_socket_update");
prop = RNA_def_property(srna, "input_type_b", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_bitflag_sdna(prop, NULL, "custom2");
RNA_def_property_enum_items(prop, rna_node_geometry_attribute_input_b_items);
RNA_def_property_enum_bitflag_sdna(prop, NULL, "input_type_b");
RNA_def_property_enum_items(prop, rna_node_geometry_attribute_input_type_items_float);
RNA_def_property_ui_text(prop, "Input Type B", "");
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_socket_update");
}

View File

@ -30,9 +30,9 @@
static bNodeSocketTemplate geo_node_attribute_math_in[] = {
{SOCK_GEOMETRY, N_("Geometry")},
{SOCK_STRING, N_("Attribute A")},
{SOCK_STRING, N_("A")},
{SOCK_FLOAT, N_("A"), 0.0f, 0.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX},
{SOCK_STRING, N_("Attribute B")},
{SOCK_STRING, N_("B")},
{SOCK_FLOAT, N_("B"), 0.0f, 0.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX},
{SOCK_STRING, N_("Result")},
{-1, ""},
@ -45,27 +45,27 @@ static bNodeSocketTemplate geo_node_attribute_math_out[] = {
static void geo_node_attribute_math_init(bNodeTree *UNUSED(tree), bNode *node)
{
node->custom1 = NODE_MATH_ADD;
node->custom2 = GEO_NODE_USE_ATTRIBUTE_A | GEO_NODE_USE_ATTRIBUTE_B;
}
NodeAttributeMath *data = (NodeAttributeMath *)MEM_callocN(sizeof(NodeAttributeMath),
"NodeAttributeMath");
static void geo_node_attribute_math_update(bNodeTree *UNUSED(ntree), bNode *node)
{
bNodeSocket *sock_attribute_a = (bNodeSocket *)BLI_findlink(&node->inputs, 1);
bNodeSocket *sock_float_a = sock_attribute_a->next;
bNodeSocket *sock_attribute_b = sock_float_a->next;
bNodeSocket *sock_float_b = sock_attribute_b->next;
GeometryNodeUseAttributeFlag flag = static_cast<GeometryNodeUseAttributeFlag>(node->custom2);
nodeSetSocketAvailability(sock_attribute_a, flag & GEO_NODE_USE_ATTRIBUTE_A);
nodeSetSocketAvailability(sock_attribute_b, flag & GEO_NODE_USE_ATTRIBUTE_B);
nodeSetSocketAvailability(sock_float_a, !(flag & GEO_NODE_USE_ATTRIBUTE_A));
nodeSetSocketAvailability(sock_float_b, !(flag & GEO_NODE_USE_ATTRIBUTE_B));
data->operation = NODE_MATH_ADD;
data->input_type_a = GEO_NODE_ATTRIBUTE_INPUT_ATTRIBUTE;
data->input_type_b = GEO_NODE_ATTRIBUTE_INPUT_ATTRIBUTE;
node->storage = data;
}
namespace blender::nodes {
static void geo_node_attribute_math_update(bNodeTree *UNUSED(ntree), bNode *node)
{
NodeAttributeMath *node_storage = (NodeAttributeMath *)node->storage;
update_attribute_input_socket_availabilities(
*node, "A", (GeometryNodeAttributeInputMode)node_storage->input_type_a);
update_attribute_input_socket_availabilities(
*node, "B", (GeometryNodeAttributeInputMode)node_storage->input_type_b);
}
static void do_math_operation(const FloatReadAttribute &input_a,
const FloatReadAttribute &input_b,
FloatWriteAttribute result,
@ -112,23 +112,10 @@ static void attribute_math_calc(GeometryComponent &component, const GeoNodeExecP
return;
}
GeometryNodeUseAttributeFlag flag = static_cast<GeometryNodeUseAttributeFlag>(node.custom2);
auto get_input_attribute = [&](GeometryNodeUseAttributeFlag use_flag,
StringRef attribute_socket_identifier,
StringRef value_socket_identifier) {
if (flag & use_flag) {
const std::string attribute_name = params.get_input<std::string>(
attribute_socket_identifier);
return component.attribute_try_get_for_read(attribute_name, result_domain, result_type);
}
const float value = params.get_input<float>(value_socket_identifier);
return component.attribute_get_constant_for_read(result_domain, result_type, &value);
};
ReadAttributePtr attribute_a = get_input_attribute(GEO_NODE_USE_ATTRIBUTE_A, "Attribute A", "A");
ReadAttributePtr attribute_b = get_input_attribute(GEO_NODE_USE_ATTRIBUTE_B, "Attribute B", "B");
ReadAttributePtr attribute_a = params.get_input_attribute(
"A", component, result_domain, result_type, nullptr);
ReadAttributePtr attribute_b = params.get_input_attribute(
"B", component, result_domain, result_type, nullptr);
if (!attribute_a || !attribute_b) {
/* Attribute wasn't found. */
return;
@ -161,7 +148,9 @@ void register_node_type_geo_attribute_math()
geo_node_type_base(&ntype, GEO_NODE_ATTRIBUTE_MATH, "Attribute Math", NODE_CLASS_ATTRIBUTE, 0);
node_type_socket_templates(&ntype, geo_node_attribute_math_in, geo_node_attribute_math_out);
ntype.geometry_node_execute = blender::nodes::geo_node_attribute_math_exec;
node_type_update(&ntype, geo_node_attribute_math_update);
node_type_update(&ntype, blender::nodes::geo_node_attribute_math_update);
node_type_init(&ntype, geo_node_attribute_math_init);
node_type_storage(
&ntype, "NodeAttributeCompare", node_free_standard_storage, node_copy_standard_storage);
nodeRegisterType(&ntype);
}