Nodes: Add a callback to check for valid socket type

This adds a callback to bNodeTreeType to check which socket types are
valid for the tree type. Function has been implemented for the normal
tree types, and can be implemented for custom node trees with python,
by adding a `classmethod` to the tree. However, only builtin socket
types are supported.

This is relevant for T87049, but it also has the advantage that it is
now clear which node trees support which sockets. Previously this
was assumed to be known by all developers.

Differential Revision: https://developer.blender.org/D10938
This commit is contained in:
Wannes Malfait 2021-04-29 23:36:46 -05:00 committed by Hans Goudey
parent ddaeaa4b98
commit 47aca2b4c4
6 changed files with 76 additions and 1 deletions

View File

@ -410,6 +410,9 @@ typedef struct bNodeTreeType {
void (*node_add_init)(struct bNodeTree *ntree, struct bNode *bnode);
/* Check if the socket type is valid for this tree type. */
bool (*valid_socket_type)(enum eNodeSocketDatatype socket_type, struct bNodeTreeType *ntreetype);
/* RNA integration */
ExtensionRNA rna_ext;
} bNodeTreeType;

View File

@ -971,6 +971,32 @@ static void rna_NodeTree_get_from_context(
RNA_parameter_list_free(&list);
}
static bool rna_NodeTree_valid_socket_type(eNodeSocketDatatype socket_type,
bNodeTreeType *ntreetype)
{
extern FunctionRNA rna_NodeTree_valid_socket_type_func;
PointerRNA ptr;
ParameterList list;
FunctionRNA *func;
void *ret;
bool valid;
RNA_pointer_create(NULL, ntreetype->rna_ext.srna, NULL, &ptr); /* dummy */
func = &rna_NodeTree_valid_socket_type_func;
RNA_parameter_list_create(&list, &ptr, func);
RNA_parameter_set_lookup(&list, "type", &socket_type);
ntreetype->rna_ext.call(NULL, &ptr, func, &list);
RNA_parameter_get_lookup(&list, "valid", &ret);
valid = *(bool *)ret;
RNA_parameter_list_free(&list);
return valid;
}
static void rna_NodeTree_unregister(Main *UNUSED(bmain), StructRNA *type)
{
bNodeTreeType *nt = RNA_struct_blender_type_get(type);
@ -999,7 +1025,7 @@ static StructRNA *rna_NodeTree_register(Main *bmain,
bNodeTreeType *nt, dummynt;
bNodeTree dummyntree;
PointerRNA dummyptr;
int have_function[3];
int have_function[4];
/* setup dummy tree & tree type to store static properties in */
memset(&dummynt, 0, sizeof(bNodeTreeType));
@ -1045,6 +1071,7 @@ static StructRNA *rna_NodeTree_register(Main *bmain,
nt->poll = (have_function[0]) ? rna_NodeTree_poll : NULL;
nt->update = (have_function[1]) ? rna_NodeTree_update_reg : NULL;
nt->get_from_context = (have_function[2]) ? rna_NodeTree_get_from_context : NULL;
nt->valid_socket_type = (have_function[3]) ? rna_NodeTree_valid_socket_type : NULL;
ntreeTypeAdd(nt);
@ -11414,6 +11441,14 @@ static void rna_def_nodetree(BlenderRNA *brna)
parm = RNA_def_pointer(
func, "result_3", "ID", "From ID", "Original ID data-block selected from the context");
RNA_def_function_output(func, parm);
/* Check for support of a socket type. */
func = RNA_def_function(srna, "valid_socket_type", NULL);
RNA_def_function_ui_description(func, "Check if the socket type is valid for the node tree");
RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_REGISTER_OPTIONAL);
parm = RNA_def_enum(func, "type", node_socket_type_items, 0, "", "");
RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
RNA_def_function_return(func, RNA_def_boolean(func, "valid", false, "", ""));
}
static void rna_def_composite_nodetree(BlenderRNA *brna)

View File

@ -205,6 +205,12 @@ static void composite_node_add_init(bNodeTree *UNUSED(bnodetree), bNode *bnode)
}
}
static bool composite_node_tree_socket_type_valid(eNodeSocketDatatype socket_type,
bNodeTreeType *UNUSED(ntreetype))
{
return ELEM(socket_type, SOCK_FLOAT, SOCK_VECTOR, SOCK_RGBA);
}
bNodeTreeType *ntreeType_Composite;
void register_node_tree_type_cmp(void)
@ -227,6 +233,7 @@ void register_node_tree_type_cmp(void)
tt->update = update;
tt->get_from_context = composite_get_from_context;
tt->node_add_init = composite_node_add_init;
tt->valid_socket_type = composite_node_tree_socket_type_valid;
tt->rna_ext.srna = &RNA_CompositorNodeTree;

View File

@ -84,6 +84,21 @@ static void foreach_nodeclass(Scene *UNUSED(scene), void *calldata, bNodeClassCa
func(calldata, NODE_CLASS_LAYOUT, N_("Layout"));
}
static bool geometry_node_tree_socket_type_valid(eNodeSocketDatatype socket_type,
bNodeTreeType *UNUSED(ntreetype))
{
return ELEM(socket_type,
SOCK_FLOAT,
SOCK_VECTOR,
SOCK_RGBA,
SOCK_BOOLEAN,
SOCK_INT,
SOCK_STRING,
SOCK_OBJECT,
SOCK_GEOMETRY,
SOCK_COLLECTION);
}
void register_node_tree_type_geo(void)
{
bNodeTreeType *tt = ntreeType_Geometry = static_cast<bNodeTreeType *>(
@ -97,6 +112,7 @@ void register_node_tree_type_geo(void)
tt->update = geometry_node_tree_update;
tt->get_from_context = geometry_node_tree_get_from_context;
tt->foreach_nodeclass = foreach_nodeclass;
tt->valid_socket_type = geometry_node_tree_socket_type_valid;
ntreeTypeAdd(tt);
}

View File

@ -184,6 +184,12 @@ static bool shader_validate_link(bNodeTree *UNUSED(ntree), bNodeLink *link)
return true;
}
static bool shader_node_tree_socket_type_valid(eNodeSocketDatatype socket_type,
bNodeTreeType *UNUSED(ntreetype))
{
return ELEM(socket_type, SOCK_FLOAT, SOCK_VECTOR, SOCK_RGBA, SOCK_SHADER);
}
bNodeTreeType *ntreeType_Shader;
void register_node_tree_type_sh(void)
@ -205,6 +211,7 @@ void register_node_tree_type_sh(void)
tt->poll = shader_tree_poll;
tt->get_from_context = shader_get_from_context;
tt->validate_link = shader_validate_link;
tt->valid_socket_type = shader_node_tree_socket_type_valid;
tt->rna_ext.srna = &RNA_ShaderNodeTree;

View File

@ -152,6 +152,12 @@ static void update(bNodeTree *ntree)
}
}
static bool texture_node_tree_socket_type_valid(eNodeSocketDatatype socket_type,
bNodeTreeType *UNUSED(ntreetype))
{
return ELEM(socket_type, SOCK_FLOAT, SOCK_VECTOR, SOCK_RGBA);
}
bNodeTreeType *ntreeType_Texture;
void register_node_tree_type_tex(void)
@ -171,6 +177,7 @@ void register_node_tree_type_tex(void)
tt->local_sync = local_sync;
tt->local_merge = local_merge;
tt->get_from_context = texture_get_from_context;
tt->valid_socket_type = texture_node_tree_socket_type_valid;
tt->rna_ext.srna = &RNA_TextureNodeTree;