From e0d40471364aafca967b6ebd5266ae7d7f78dee4 Mon Sep 17 00:00:00 2001 From: Dalai Felinto Date: Wed, 23 Jul 2014 11:33:29 -0300 Subject: Bake-API: Test for cyclic node connection If the active image node contributes to the final material shader (meaning it's either directly or indirectly connected to an Output Node) the user will receive an alert about circular dependency. Similar to what we do for Blender internal the baking will still happen, but the user will receive the alert which should prevent the image saving to happen if the result was not intentional. Core function to check for node output written by Lukas Toenne. Reviewers: lukastoenne, campbellbarton Differential Revision: https://developer.blender.org/D673 --- source/blender/nodes/intern/node_common.c | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'source/blender/nodes') diff --git a/source/blender/nodes/intern/node_common.c b/source/blender/nodes/intern/node_common.c index ae834f9e7cc..c58c9c902ec 100644 --- a/source/blender/nodes/intern/node_common.c +++ b/source/blender/nodes/intern/node_common.c @@ -336,6 +336,40 @@ void ntree_update_reroute_nodes(bNodeTree *ntree) node_reroute_inherit_type_recursive(ntree, node); } +static bool node_is_connected_to_output_recursive(bNodeTree *ntree, bNode *node) +{ + bNodeLink *link; + + /* avoid redundant checks, and infinite loops in case of cyclic node links */ + if (node->done) + return false; + node->done = 1; + + /* main test, done before child loop so it catches output nodes themselves as well */ + if (node->typeinfo->nclass == NODE_CLASS_OUTPUT && node->flag & NODE_DO_OUTPUT) + return true; + + /* test all connected nodes, first positive find is sufficient to return true */ + for (link = ntree->links.first; link; link = link->next) { + if (link->fromnode == node) { + if (node_is_connected_to_output_recursive(ntree, link->tonode)) + return true; + } + } + return false; +} + +bool BKE_node_is_connected_to_output(bNodeTree *ntree, bNode *node) +{ + bNode *tnode; + + /* clear flags */ + for (tnode = ntree->nodes.first; tnode; tnode = tnode->next) + tnode->done = 0; + + return node_is_connected_to_output_recursive(ntree, node); +} + void BKE_node_tree_unlink_id(ID *id, struct bNodeTree *ntree) { bNode *node; -- cgit v1.2.3