433 bool needs_relations_update_ =
false;
437 : bmain_(bmain), params_(params), relations_(bmain)
446 changed_ntrees.
append(ntree);
460 bool is_single_tree_update =
false;
462 if (root_ntrees.
size() == 1) {
468 update_result_by_tree_.add_new(ntree,
result);
469 if (!
result.interface_changed && !
result.output_changed) {
470 is_single_tree_update =
true;
474 if (!is_single_tree_update) {
476 for (
bNodeTree *ntree : ntrees_in_order) {
480 if (!update_result_by_tree_.contains(ntree)) {
482 update_result_by_tree_.add_new(ntree,
result);
486 if (
result.output_changed) {
491 if (
result.interface_changed) {
499 for (
const auto item : update_result_by_tree_.items()) {
503 this->reset_changed_flags(*ntree);
505 if (
result.interface_changed) {
507 relations_.ensure_modifier_users();
509 Object *
object = pair.first;
519 relations_.ensure_strip_users();
520 for (
const StripModifierPair &pair : relations_.get_strip_modifier_users(ntree)) {
521 Scene *scene = pair.first;
530 for (
const StripEffectPair &pair : relations_.get_compositor_effect_users(ntree)) {
534 relations_.ensure_scene_compositor_effects_users();
536 relations_.get_scene_compositor_effects_users(ntree))
543 if (
result.output_changed) {
548 ID &owner_or_self_id = owner_id ? *owner_id : ntree->
id;
549 if (params_.tree_changed_fn) {
550 params_.tree_changed_fn(*ntree, owner_or_self_id);
552 if (params_.tree_output_changed_fn &&
result.output_changed) {
553 params_.tree_output_changed_fn(*ntree, owner_or_self_id);
557 if (needs_relations_update_) {
568 enum class ToposortMark {
588 ToposortMarkMap marks;
589 for (
bNodeTree *ntree : trees_to_update) {
590 marks.add_new(ntree, ToposortMark::None);
592 for (
bNodeTree *ntree : trees_to_update) {
593 if (marks.lookup(ntree) == ToposortMark::None) {
594 const bool cycle_detected = !this->get_tree_update_order__visit_recursive(
595 ntree, marks, sorted_ntrees);
602 std::reverse(sorted_ntrees.
begin(), sorted_ntrees.
end());
604 return sorted_ntrees;
607 bool get_tree_update_order__visit_recursive(bNodeTree *ntree,
608 ToposortMarkMap &marks,
609 Vector<bNodeTree *> &sorted_ntrees)
611 ToposortMark &mark = marks.lookup(ntree);
612 if (mark == ToposortMark::Permanent) {
615 if (mark == ToposortMark::Temporary) {
620 mark = ToposortMark::Temporary;
622 for (
const TreeNodePair &pair : relations_.get_group_node_users(ntree)) {
623 this->get_tree_update_order__visit_recursive(pair.first, marks, sorted_ntrees);
625 sorted_ntrees.
append(ntree);
627 mark = ToposortMark::Permanent;
631 Set<bNodeTree *> get_trees_to_update(Span<bNodeTree *> root_ntrees)
633 relations_.ensure_group_node_users();
635 Set<bNodeTree *> reachable_trees;
636 Stack<bNodeTree *> trees_to_check = root_ntrees;
638 while (!trees_to_check.
is_empty()) {
639 bNodeTree *ntree = trees_to_check.
pop();
640 if (reachable_trees.
add(ntree)) {
641 for (
const TreeNodePair &pair : relations_.get_group_node_users(ntree)) {
642 trees_to_check.
push(pair.first);
647 return reachable_trees;
650 TreeUpdateResult update_tree(bNodeTree &ntree)
660 result.interface_changed =
true;
661 result.output_changed =
true;
664 if (this->update_panel_toggle_names(ntree)) {
665 result.interface_changed =
true;
668 this->update_socket_link_and_use(ntree);
669 this->update_individual_nodes(ntree);
670 this->update_internal_links(ntree);
671 this->update_generic_callback(ntree);
672 this->make_node_previews_dirty(ntree);
674 this->propagate_runtime_flags(ntree);
676 if (this->propagate_enum_definitions(ntree)) {
677 result.interface_changed =
true;
683 result.interface_changed =
true;
688 this->update_from_field_inference(ntree);
690 result.interface_changed =
true;
693 result.interface_changed =
true;
698 this->update_socket_shapes(ntree);
702 this->update_eval_dependencies(ntree);
705 result.output_changed = this->check_if_output_changed(ntree);
707 this->update_socket_link_and_use(ntree);
708 this->update_link_validation(ntree);
710 if (this->update_nested_node_refs(ntree)) {
711 result.interface_changed =
true;
719 result.interface_changed =
true;
722 if (
result.interface_changed) {
739 Set<int32_t> node_identifiers;
740 const Span<const bNode *> nodes = ntree.all_nodes();
742 const bNode &
node = *nodes[i];
752 void update_socket_link_and_use(bNodeTree &tree)
754 tree.ensure_topology_cache();
755 for (bNodeSocket *socket : tree.all_input_sockets()) {
756 if (socket->directly_linked_links().is_empty()) {
757 socket->link =
nullptr;
760 socket->link = socket->directly_linked_links()[0];
764 this->update_socket_used_tags(tree);
767 void update_socket_used_tags(bNodeTree &tree)
769 tree.ensure_topology_cache();
770 for (bNodeSocket *socket : tree.all_sockets()) {
771 const bool socket_is_linked = !socket->directly_linked_links().is_empty();
776 void update_individual_nodes(bNodeTree &ntree)
778 for (bNode *
node : ntree.all_nodes()) {
780 if (this->should_update_individual_node(ntree, *
node)) {
781 bke::bNodeType &ntype = *
node->typeinfo;
783 this->remove_unused_geometry_nodes_viewer_sockets(ntree, *
node);
792 else if (
node->is_undefined()) {
795 delete node->runtime->declaration;
796 node->runtime->declaration =
nullptr;
797 for (bNodeSocket &socket :
node->inputs) {
800 for (bNodeSocket &socket :
node->outputs) {
811 bool should_update_individual_node(
const bNodeTree &ntree,
const bNode &
node)
824 if (
node.is_group_input() ||
node.is_group_output()) {
840 void remove_unused_geometry_nodes_viewer_sockets(bNodeTree &ntree, bNode &viewer_node)
842 ntree.ensure_topology_cache();
843 Vector<int> item_indices_to_remove;
844 auto &
storage = *
static_cast<NodeGeometryViewer *
>(viewer_node.
storage);
846 const NodeGeometryViewerItem &item =
storage.items[i];
851 const bNodeSocket *socket = viewer_node.input_by_identifier(
UString(identifier_str));
855 if (!socket->is_directly_linked()) {
856 item_indices_to_remove.
append(i);
859 std::reverse(item_indices_to_remove.
begin(), item_indices_to_remove.
end());
860 for (
const int i : item_indices_to_remove) {
869 void update_internal_links(bNodeTree &ntree)
871 bke::node_tree_runtime::AllowUsingOutdatedInfo allow_outdated_info{ntree};
872 ntree.ensure_topology_cache();
873 for (bNode *
node : ntree.all_nodes()) {
874 if (!this->should_update_individual_node(ntree, *
node)) {
878 Vector<bNodeInternalLink> expected_internal_links;
879 for (
const bNodeSocket *output_socket :
node->output_sockets()) {
880 if (!output_socket->is_available()) {
883 if (output_socket->runtime->declaration &&
884 output_socket->runtime->declaration->no_mute_links)
888 const bNodeSocket *input_socket = this->find_internally_linked_input(ntree, output_socket);
889 if (input_socket ==
nullptr) {
893 expected_internal_links.
append(bNodeInternalLink{
894 const_cast<bNodeSocket *
>(input_socket),
const_cast<bNodeSocket *
>(output_socket)});
898 if (
node->runtime->internal_links.size() != expected_internal_links.
size()) {
899 this->update_internal_links_in_node(ntree, *
node, expected_internal_links);
903 const bool all_expected_internal_links_exist = std::all_of(
904 node->runtime->internal_links.begin(),
905 node->runtime->internal_links.end(),
906 [&](
const bNodeInternalLink &link) {
907 return expected_internal_links.as_span().contains(link);
910 if (all_expected_internal_links_exist) {
914 this->update_internal_links_in_node(ntree, *
node, expected_internal_links);
918 const bNodeSocket *find_internally_linked_input(
const bNodeTree &ntree,
919 const bNodeSocket *output_socket)
921 const bNode &
node = output_socket->owner_node();
922 if (
node.typeinfo->internally_linked_input) {
923 return node.typeinfo->internally_linked_input(ntree,
node, *output_socket);
926 const bNodeSocket *selected_socket =
nullptr;
927 int selected_priority = -1;
928 bool selected_is_linked =
false;
929 for (
const bNodeSocket *input_socket :
node.input_sockets()) {
930 if (!input_socket->is_available()) {
942 const bool is_linked = input_socket->is_directly_linked();
943 const bool is_preferred = priority > selected_priority || (is_linked && !selected_is_linked);
947 selected_socket = input_socket;
948 selected_priority = priority;
949 selected_is_linked = is_linked;
951 return selected_socket;
954 void update_internal_links_in_node(bNodeTree &ntree,
956 Span<bNodeInternalLink> internal_links)
959 node.runtime->internal_links.extend(internal_links);
963 void update_generic_callback(bNodeTree &ntree)
971 void make_node_previews_dirty(bNodeTree &ntree)
974 for (bNode *
node : ntree.all_nodes()) {
975 if (!
node->is_group()) {
978 if (bNodeTree *nested_tree =
reinterpret_cast<bNodeTree *
>(
node->id)) {
979 this->make_node_previews_dirty(*nested_tree);
984 void propagate_runtime_flags(
const bNodeTree &ntree)
986 ntree.ensure_topology_cache();
990 for (
const bNode *group_node : ntree.group_nodes()) {
991 const bNodeTree *group =
reinterpret_cast<bNodeTree *
>(group_node->id);
992 if (group !=
nullptr) {
999 for (
const UString idname : {
"ShaderNodeTexImage"_ustr,
"ShaderNodeTexEnvironment"_ustr}) {
1000 for (
const bNode *
node : ntree.nodes_by_type(idname)) {
1009 for (
const UString idname : {
"ShaderNodeOutputMaterial"_ustr,
1010 "ShaderNodeOutputLight"_ustr,
1011 "ShaderNodeOutputWorld"_ustr,
1012 "ShaderNodeOutputAOV"_ustr})
1014 const Span<const bNode *> nodes = ntree.nodes_by_type(idname);
1023 if (!ntree.nodes_by_type(
"GeometryNodeSimulationOutput"_ustr).is_empty()) {
1029 void update_from_field_inference(bNodeTree &ntree)
1034 for (bNode *
node : ntree.nodes_by_type(
"GeometryNodeBake"_ustr)) {
1035 NodeGeometryBake &
storage = *
static_cast<NodeGeometryBake *
>(
node->storage);
1037 const bNodeSocket &socket =
node->input_socket(i);
1038 NodeGeometryBakeItem &item =
storage.items[i];
1039 if (socket.may_be_field()) {
1047 const bool use_inferred_structure_type =
false)
1056 const StructureType display_structure_type = use_inferred_structure_type ?
1059 switch (display_structure_type) {
1060 case StructureType::Single:
1062 case StructureType::Dynamic:
1064 case StructureType::Field:
1066 case StructureType::Grid:
1068 case StructureType::List:
1075 void update_socket_shapes(bNodeTree &ntree)
1077 ntree.ensure_topology_cache();
1078 for (bNode *
node : ntree.all_nodes()) {
1079 if (
node->is_undefined()) {
1084 switch (
node->type_legacy) {
1092 for (bNodeSocket *socket :
node->input_sockets()) {
1095 for (bNodeSocket *socket :
node->output_sockets()) {
1101 const auto &
storage = *
static_cast<const NodeCombineBundle *
>(
node->storage);
1103 const NodeCombineBundleItem &item =
storage.items[i];
1104 bNodeSocket &socket =
node->input_socket(i);
1111 const auto &
storage = *
static_cast<const NodeSeparateBundle *
>(
node->storage);
1113 const NodeSeparateBundleItem &item =
storage.items[i];
1114 bNodeSocket &socket =
node->output_socket(i);
1124 const auto &
storage = *
static_cast<const NodeClosureOutput *
>(
1125 closure_output_node->storage);
1127 const NodeClosureInputItem &item =
storage.input_items.items[i];
1128 bNodeSocket &socket =
node->output_socket(i);
1136 const auto &
storage = *
static_cast<const NodeClosureOutput *
>(
node->storage);
1138 const NodeClosureOutputItem &item =
storage.output_items.items[i];
1139 bNodeSocket &socket =
node->input_socket(i);
1146 const auto &
storage = *
static_cast<const NodeEvaluateClosure *
>(
node->storage);
1148 const NodeEvaluateClosureInputItem &item =
storage.input_items.items[i];
1149 bNodeSocket &socket =
node->input_socket(i + 1);
1154 const NodeEvaluateClosureOutputItem &item =
storage.output_items.items[i];
1155 bNodeSocket &socket =
node->output_socket(i);
1163 for (bNodeSocket *socket :
node->input_sockets()) {
1166 for (bNodeSocket *socket :
node->output_sockets()) {
1170 if (
node->is_type(
"NodeGetBundleItem"_ustr)) {
1171 bNodeSocket &socket = *
node->output_by_identifier(
"Item"_ustr);
1172 const auto &
storage = *
static_cast<const NodeGetBundleItem *
>(
node->storage);
1176 else if (
node->is_type(
"NodeStoreBundleItem"_ustr)) {
1177 bNodeSocket &socket = *
node->input_by_identifier(
"Item"_ustr);
1178 const auto &
storage = *
static_cast<const NodeStoreBundleItem *
>(
node->storage);
1188 void update_eval_dependencies(bNodeTree &ntree)
1190 ntree.ensure_topology_cache();
1195 needs_relations_update_ =
true;
1197 std::move(new_deps));
1201 bool propagate_enum_definitions(bNodeTree &ntree)
1203 ntree.ensure_interface_cache();
1207 for (bNode *
node : ntree.toposort_right_to_left()) {
1208 const bool node_updated = this->should_update_individual_node(ntree, *
node);
1210 Vector<bNodeSocket *> locally_defined_enums;
1211 if (
node->is_type(
"GeometryNodeMenuSwitch"_ustr)) {
1212 bNodeSocket &enum_input =
node->input_socket(0);
1216 const NodeMenuSwitch &
storage = *
static_cast<NodeMenuSwitch *
>(
node->storage);
1217 const RuntimeNodeEnumItems *enum_items = this->create_runtime_enum_items(
1220 this->set_enum_ptr(*enum_input.default_value_typed<bNodeSocketValueMenu>(), enum_items);
1224 locally_defined_enums.
append(&enum_input);
1226 else if (!
node->is_group()) {
1228 for (bNodeSocket *input_socket :
node->input_sockets()) {
1229 if (!input_socket->is_available()) {
1235 const auto *socket_decl =
dynamic_cast<const nodes::decl::Menu *
>(
1240 this->set_enum_ptr(*input_socket->default_value_typed<bNodeSocketValueMenu>(),
1241 socket_decl->items.get());
1242 locally_defined_enums.
append(input_socket);
1247 for (bNodeSocket *socket :
node->input_sockets()) {
1249 !locally_defined_enums.
contains(socket))
1251 clear_enum_reference(*socket);
1254 for (bNodeSocket *socket :
node->output_sockets()) {
1256 clear_enum_reference(*socket);
1261 for (bNodeSocket *
output :
node->output_sockets()) {
1265 for (
const bNodeSocket *input :
output->directly_linked_sockets()) {
1266 if (!input->is_available() || input->type !=
SOCK_MENU) {
1269 this->update_socket_enum_definition(*
output->default_value_typed<bNodeSocketValueMenu>(),
1270 *input->default_value_typed<bNodeSocketValueMenu>());
1274 if (
node->is_group()) {
1276 if (
node->id ==
nullptr) {
1279 const bNodeTree *group_tree =
reinterpret_cast<bNodeTree *
>(
node->id);
1280 group_tree->ensure_interface_cache();
1282 for (
const int socket_i : group_tree->interface_inputs().index_range()) {
1283 bNodeSocket &input = *
node->input_sockets()[socket_i];
1284 const bNodeTreeInterfaceSocket &iosocket = *group_tree->interface_inputs()[socket_i];
1288 this->update_socket_enum_definition(
1289 *input.default_value_typed<bNodeSocketValueMenu>(),
1290 *
static_cast<bNodeSocketValueMenu *
>(iosocket.
socket_data));
1294 else if (
node->is_type(
"GeometryNodeMenuSwitch"_ustr)) {
1296 const bNodeSocket *
output =
node->output_sockets().first();
1297 for (bNodeSocket *input :
node->input_sockets().drop_front(1)) {
1299 this->update_socket_enum_definition(
1300 *input->default_value_typed<bNodeSocketValueMenu>(),
1301 *
output->default_value_typed<bNodeSocketValueMenu>());
1305 else if (
node->is_type(
"GeometryNodeForeachGeometryElementInput"_ustr)) {
1309 const IndexRange sockets =
node->input_sockets().index_range().drop_front(2);
1310 for (
const int socket_i : sockets) {
1311 bNodeSocket *input =
node->input_sockets()[socket_i];
1312 bNodeSocket *
output =
node->output_sockets()[socket_i];
1316 this->update_socket_enum_definition(
1317 *input->default_value_typed<bNodeSocketValueMenu>(),
1318 *
output->default_value_typed<bNodeSocketValueMenu>());
1327 for (bNodeSocket *input :
node->input_sockets()) {
1329 for (
const bNodeSocket *
output :
node->output_sockets()) {
1331 this->update_socket_enum_definition(
1332 *input->default_value_typed<bNodeSocketValueMenu>(),
1333 *
output->default_value_typed<bNodeSocketValueMenu>());
1342 const Span<bNode *> group_input_nodes = ntree.group_input_nodes();
1343 for (
const int interface_input_i : ntree.interface_inputs().index_range()) {
1344 const bNodeTreeInterfaceSocket &interface_socket =
1345 *ntree.interface_inputs()[interface_input_i];
1346 if (interface_socket.
socket_type != StringRef(
"NodeSocketMenu")) {
1349 const RuntimeNodeEnumItems *found_enum_items =
nullptr;
1350 bool found_conflict =
false;
1351 for (bNode *input_node : group_input_nodes) {
1352 const bNodeSocket &socket = input_node->output_socket(interface_input_i);
1353 const auto &socket_value = *socket.default_value_typed<bNodeSocketValueMenu>();
1354 if (socket_value.has_conflict()) {
1355 found_conflict =
true;
1358 if (found_enum_items ==
nullptr) {
1359 found_enum_items = socket_value.enum_items;
1361 else if (socket_value.enum_items !=
nullptr) {
1362 if (found_enum_items != socket_value.enum_items) {
1363 found_conflict =
true;
1368 if (found_conflict) {
1370 for (bNode *input_node : group_input_nodes) {
1371 bNodeSocket &socket = input_node->output_socket(interface_input_i);
1372 auto &socket_value = *socket.default_value_typed<bNodeSocketValueMenu>();
1373 if (socket_value.enum_items) {
1374 socket_value.enum_items->remove_user_and_delete_if_last();
1375 socket_value.enum_items =
nullptr;
1380 else if (found_enum_items !=
nullptr) {
1382 for (bNode *input_node : group_input_nodes) {
1383 bNodeSocket &socket = input_node->output_socket(interface_input_i);
1384 auto &socket_value = *socket.default_value_typed<bNodeSocketValueMenu>();
1385 if (socket_value.enum_items ==
nullptr) {
1387 socket_value.enum_items = found_enum_items;
1394 Vector<bNodeSocketValueMenu> interface_enum_items(ntree.interface_inputs().size(), {0});
1395 for (
const bNode *group_input_node : ntree.group_input_nodes()) {
1396 for (
const int socket_i : ntree.interface_inputs().index_range()) {
1397 const bNodeSocket &
output = *group_input_node->output_sockets()[socket_i];
1400 this->update_socket_enum_definition(interface_enum_items[socket_i],
1401 *
output.default_value_typed<bNodeSocketValueMenu>());
1407 bool changed =
false;
1408 for (
const int socket_i : ntree.interface_inputs().index_range()) {
1409 bNodeTreeInterfaceSocket &iosocket = *ntree.interface_inputs()[socket_i];
1411 bNodeSocketValueMenu &dst = *
static_cast<bNodeSocketValueMenu *
>(iosocket.
socket_data);
1412 const bNodeSocketValueMenu &src = interface_enum_items[socket_i];
1438 const RuntimeNodeEnumItems *create_runtime_enum_items(
const NodeEnumDefinition &enum_def)
1440 RuntimeNodeEnumItems *enum_items =
new RuntimeNodeEnumItems();
1442 for (
const int i : enum_def.items().index_range()) {
1443 const NodeEnumItem &src = enum_def.items()[i];
1444 RuntimeNodeEnumItem &dst = enum_items->
items[i];
1453 void clear_enum_reference(bNodeSocket &socket)
1456 bNodeSocketValueMenu &default_value = *socket.default_value_typed<bNodeSocketValueMenu>();
1457 this->reset_enum_ptr(default_value);
1458 default_value.
runtime_flag &= ~NODE_MENU_ITEMS_CONFLICT;
1461 void update_socket_enum_definition(bNodeSocketValueMenu &dst,
const bNodeSocketValueMenu &src)
1463 if (dst.has_conflict()) {
1469 if (src.has_conflict()) {
1471 this->reset_enum_ptr(dst);
1480 this->reset_enum_ptr(dst);
1485 void reset_enum_ptr(bNodeSocketValueMenu &dst)
1493 void set_enum_ptr(bNodeSocketValueMenu &dst,
const RuntimeNodeEnumItems *enum_items)
1505 static Array<ShaderNodeAncestorFlags> shader_tree_tag_inputs_by_ancestor(bNodeTree &ntree)
1507 Array<ShaderNodeAncestorFlags> input_ancestors(ntree.all_input_sockets().size(),
1510 for (
const bNode *
node : ntree.toposort_right_to_left()) {
1517 for (
const bNodeSocket *socket :
node->input_sockets()) {
1518 input_ancestors[socket->index_in_all_inputs()] =
1520 socket->identifier_ustr() ==
"Surface"_ustr) ?
1528 for (
const bNodeSocket *socket :
node->output_sockets()) {
1529 for (
const bNodeLink *link : socket->directly_linked_links()) {
1530 if (link->is_used()) {
1531 node_flags |= input_ancestors[link->tosock->index_in_all_inputs()];
1545 for (
const bNodeSocket *socket :
node->input_sockets()) {
1547 socket_flags = node_flags;
1549 "Diffuse Color"_ustr,
1550 "Glossy Color"_ustr,
1551 "Transmission Color"_ustr))
1558 return input_ancestors;
1561 static bool is_lighting_node(
const bNode &
node)
1563 switch (
node.type_legacy) {
1573 const NodeShaderVectTransform *nodeprop =
static_cast<const NodeShaderVectTransform *
>(
1582 static const char *shader_tree_link_error(bNodeLink &link,
1583 const Span<ShaderNodeAncestorFlags> input_ancestors)
1585 const bNode &from_node = *link.
fromnode;
1586 if (!is_lighting_node(from_node)) {
1591 return TIP_(
"Shader To RGB can't evaluate Lighting nodes");
1595 return TIP_(
"Light Accumulation can only be used for the Material Output Surface");
1603 "Lighting nodes must be connected to the Light sockets of a Light Accumulation node "
1604 "before reaching an output");
1608 "Lighting nodes can't be connected to the Color sockets of a Light Accumulation node");
1614 void update_link_validation(bNodeTree &ntree)
1616 const bNodeTreeZones *fallback_zones =
nullptr;
1624 for (bNodeLink &link : ntree.
links) {
1628 Array<ShaderNodeAncestorFlags> shader_input_ancestors;
1630 shader_input_ancestors = shader_tree_tag_inputs_by_ancestor(ntree);
1633 for (bNodeLink &link : ntree.
links) {
1634 if (!link.
fromsock->is_available() || !link.
tosock->is_available()) {
1635 link.
flag &= ~NODE_LINK_VALID;
1639 const bNodeSocketValueMenu *from_value =
1640 link.
fromsock->default_value_typed<bNodeSocketValueMenu>();
1641 const bNodeSocketValueMenu *to_value =
1642 link.
tosock->default_value_typed<bNodeSocketValueMenu>();
1643 if (from_value->has_conflict() || to_value->has_conflict()) {
1644 link.
flag &= ~NODE_LINK_VALID;
1647 NodeLinkError{
TIP_(
"Use node groups to reuse the same menu multiple times")});
1651 const bNode &from_node = *link.
fromnode;
1652 const bNode &to_node = *link.
tonode;
1656 link.
flag &= ~NODE_LINK_VALID;
1659 NodeLinkError{
TIP_(
"The links form a cycle which is not supported")});
1666 link.
flag &= ~NODE_LINK_VALID;
1671 TIP_(
"Conversion is not supported"),
1677 if (fallback_zones) {
1684 link.
flag &= ~NODE_LINK_VALID;
1686 NodeLinkKey{link}, NodeLinkError{
TIP_(
"Links can only go into a zone but not out")});
1690 if (
const char *error = this->get_structure_type_link_error(link)) {
1691 link.
flag &= ~NODE_LINK_VALID;
1696 if (
const char *error = shader_tree_link_error(link, shader_input_ancestors)) {
1697 link.
flag &= ~NODE_LINK_VALID;
1705 const char *get_structure_type_link_error(
const bNodeLink &link)
1707 const nodes::StructureType from_inferred_type =
1709 if (from_inferred_type == StructureType::Dynamic) {
1724 if (from_inferred_type == StructureType::Single) {
1727 return TIP_(
"Input expects a single value");
1733 if (
ELEM(from_inferred_type, StructureType::Single, StructureType::Field)) {
1736 return TIP_(
"Input expects a field or single value");
1742 if (from_inferred_type == StructureType::Grid) {
1745 return TIP_(
"Input expects a volume grid");
1751 if (from_inferred_type == StructureType::List) {
1754 return TIP_(
"Input expects a list");
1760 bool check_if_output_changed(
const bNodeTree &tree)
1762 tree.ensure_topology_cache();
1767 const Vector<const bNodeSocket *> tree_output_sockets = this->find_output_sockets(tree);
1769 const uint32_t new_topology_hash = this->get_combined_socket_topology_hash(
1770 tree, tree_output_sockets);
1781 for (
const FCurve &fcurve : adt->drivers) {
1782 const ChannelDriver *driver = fcurve.
driver;
1783 const StringRef expression = driver->
expression;
1799 if (old_topology_hash != new_topology_hash) {
1808 if (old_topology_hash == new_topology_hash) {
1813 if (!this->check_if_socket_outputs_changed_based_on_flags(tree, tree_output_sockets)) {
1820 Vector<const bNodeSocket *> find_output_sockets(
const bNodeTree &tree)
1822 Vector<const bNodeSocket *> sockets;
1823 for (
const bNode *
node : tree.all_nodes()) {
1824 if (!this->is_output_node(*
node)) {
1827 for (
const bNodeSocket *socket :
node->input_sockets()) {
1828 if (!
STREQ(socket->
idname,
"NodeSocketVirtual")) {
1836 bool is_output_node(
const bNode &
node)
const
1841 if (
node.is_group_output()) {
1844 if (
node.is_type(
"GeometryNodeWarning"_ustr)) {
1851 if (
node.is_group()) {
1852 const bNodeTree *node_group =
reinterpret_cast<const bNodeTree *
>(
node.id);
1853 if (node_group !=
nullptr &&
1866 uint32_t get_combined_socket_topology_hash(
const bNodeTree &tree,
1867 Span<const bNodeSocket *> sockets)
1869 if (tree.has_available_link_cycle()) {
1874 Array<uint32_t> hashes = this->get_socket_topology_hashes(tree, sockets);
1879 return combined_hash;
1882 Array<uint32_t> get_socket_topology_hashes(
const bNodeTree &tree,
1883 const Span<const bNodeSocket *> sockets)
1885 BLI_assert(!tree.has_available_link_cycle());
1886 Array<std::optional<uint32_t>> hash_by_socket_id(tree.all_sockets().size());
1887 Stack<const bNodeSocket *> sockets_to_check = sockets;
1889 auto get_socket_ptr_hash = [&](
const bNodeSocket &socket) {
1893 const bNodeTreeZones *zones = tree.zones();
1895 while (!sockets_to_check.
is_empty()) {
1896 const bNodeSocket &socket = *sockets_to_check.
peek();
1897 const bNode &
node = socket.owner_node();
1899 if (hash_by_socket_id[socket.index_in_tree()].has_value()) {
1900 sockets_to_check.
pop();
1906 if (socket.is_input()) {
1908 bool all_origins_computed =
true;
1909 bool get_value_from_origin =
false;
1910 Vector<const bNodeSocket *, 16> origin_sockets;
1911 for (
const bNodeLink *link : socket.directly_linked_links()) {
1912 if (link->is_muted()) {
1915 if (!link->is_available()) {
1922 if (zone->output_node_id ==
node.identifier) {
1923 if (
const bNode *input_node = zone->input_node()) {
1924 origin_sockets.
extend(input_node->input_sockets());
1929 for (
const bNodeSocket *origin_socket : origin_sockets) {
1930 const std::optional<uint32_t> origin_hash =
1931 hash_by_socket_id[origin_socket->index_in_tree()];
1932 if (origin_hash.has_value()) {
1933 if (get_value_from_origin || socket.
type != origin_socket->type) {
1934 socket_hash =
noise::hash(socket_hash, *origin_hash);
1938 socket_hash = *origin_hash;
1940 get_value_from_origin =
true;
1943 sockets_to_check.
push(origin_socket);
1944 all_origins_computed =
false;
1947 if (!all_origins_computed) {
1951 if (!get_value_from_origin) {
1952 socket_hash = get_socket_ptr_hash(socket);
1956 bool all_available_inputs_computed =
true;
1957 for (
const bNodeSocket *input_socket :
node.input_sockets()) {
1958 if (input_socket->is_available()) {
1959 if (!hash_by_socket_id[input_socket->index_in_tree()].has_value()) {
1960 sockets_to_check.
push(input_socket);
1961 all_available_inputs_computed =
false;
1965 if (!all_available_inputs_computed) {
1968 if (
node.is_reroute()) {
1969 socket_hash = *hash_by_socket_id[
node.input_socket(0).index_in_tree()];
1971 else if (
node.is_muted()) {
1972 const bNodeSocket *internal_input = socket.internal_link_input();
1973 if (internal_input ==
nullptr) {
1974 socket_hash = get_socket_ptr_hash(socket);
1977 socket_hash = *hash_by_socket_id[internal_input->index_in_tree()];
1981 socket_hash = get_socket_ptr_hash(socket);
1982 for (
const bNodeSocket *input_socket :
node.input_sockets()) {
1983 if (input_socket->is_available()) {
1984 const uint32_t input_socket_hash = *hash_by_socket_id[input_socket->index_in_tree()];
1985 socket_hash =
noise::hash(socket_hash, input_socket_hash);
1991 if (
node.is_type(
"ShaderNodeTexImage"_ustr) && socket.index() == 0) {
1993 const bNodeSocket &alpha_socket =
node.output_socket(1);
1995 if (alpha_socket.is_directly_linked()) {
2001 hash_by_socket_id[socket.index_in_tree()] = socket_hash;
2004 sockets_to_check.
pop();
2008 Array<uint32_t> hashes(sockets.
size());
2010 hashes[i] = *hash_by_socket_id[sockets[i]->index_in_tree()];
2019 bool check_if_socket_outputs_changed_based_on_flags(
const bNodeTree &tree,
2020 Span<const bNodeSocket *> sockets)
2023 Array<bool> pushed_by_socket_id(tree.all_sockets().size(),
false);
2024 Stack<const bNodeSocket *> sockets_to_check = sockets;
2026 for (
const bNodeSocket *socket : sockets) {
2027 pushed_by_socket_id[socket->index_in_tree()] =
true;
2030 while (!sockets_to_check.
is_empty()) {
2031 const bNodeSocket &socket = *sockets_to_check.
pop();
2032 const bNode &
node = socket.owner_node();
2037 bool change_affects_output =
true;
2039 change_affects_output =
false;
2042 change_affects_output =
false;
2045 change_affects_output =
false;
2047 if (change_affects_output) {
2051 if (socket.is_input()) {
2052 for (
const bNodeSocket *origin_socket : socket.directly_linked_sockets()) {
2053 bool &pushed = pushed_by_socket_id[origin_socket->index_in_tree()];
2055 sockets_to_check.
push(origin_socket);
2061 for (
const bNodeSocket *input_socket :
node.input_sockets()) {
2062 if (input_socket->is_available()) {
2063 bool &pushed = pushed_by_socket_id[input_socket->index_in_tree()];
2065 sockets_to_check.
push(input_socket);
2072 switch (
node.type_legacy) {
2076 const bNodeTreeZones *zones = tree.zones();
2084 for (
const bNodeSocket *input_socket : zone->
input_node()->input_sockets()) {
2085 if (input_socket->is_available()) {
2086 bool &pushed = pushed_by_socket_id[input_socket->index_in_tree()];
2088 sockets_to_check.
push(input_socket);
2098 if ((
node.is_type(
"ShaderNodeNormal"_ustr) ||
node.is_type(
"CompositorNodeNormal"_ustr)) &&
2099 socket.index() == 1)
2102 const bNodeSocket &normal_output =
node.output_socket(0);
2104 bool &pushed = pushed_by_socket_id[normal_output.index_in_tree()];
2106 sockets_to_check.
push(&normal_output);
2119 bool update_nested_node_refs(bNodeTree &ntree)
2121 ntree.ensure_topology_cache();
2124 Map<bNestedNodePath, int32_t> old_id_by_path;
2125 Set<int32_t> old_ids;
2126 for (
const bNestedNodeRef &ref : ntree.nested_node_refs_span()) {
2127 old_id_by_path.
add(ref.path, ref.id);
2128 old_ids.
add(ref.id);
2131 Vector<bNestedNodePath> nested_node_paths;
2134 for (
const bNestedNodePath &path : old_id_by_path.
keys()) {
2135 const bNode *
node = ntree.node_by_id(path.node_id);
2138 nested_node_paths.
append(path);
2145 for (
const UString idname : {
"GeometryNodeSimulationOutput"_ustr,
"GeometryNodeBake"_ustr}) {
2146 for (
const bNode *
node : ntree.nodes_by_type(idname)) {
2147 nested_node_paths.
append({
node->identifier, -1});
2152 for (
const bNode *
node : ntree.group_nodes()) {
2153 const bNodeTree *group =
reinterpret_cast<const bNodeTree *
>(
node->id);
2154 if (group ==
nullptr) {
2157 for (
const int i : group->nested_node_refs_span().index_range()) {
2159 nested_node_paths.
append({
node->identifier, child_ref.
id});
2166 Map<int32_t, bNestedNodePath> new_path_by_id;
2167 for (
const bNestedNodePath &path : nested_node_paths) {
2171 new_path_by_id.
add(old_id, path);
2182 new_path_by_id.
add(new_id, path);
2186 if (!this->nested_node_refs_changed(ntree, new_path_by_id)) {
2197 bNestedNodeRef *new_refs = MEM_new_array<bNestedNodeRef>(
size_t(new_path_by_id.
size()),
2200 for (
const auto item : new_path_by_id.
items()) {
2201 bNestedNodeRef &ref = new_refs[
index];
2203 ref.
path = item.value;
2213 bool nested_node_refs_changed(
const bNodeTree &ntree,
2214 const Map<int32_t, bNestedNodePath> &new_path_by_id)
2219 for (
const bNestedNodeRef &ref : ntree.nested_node_refs_span()) {
2220 if (!new_path_by_id.
contains(ref.id)) {
2227 void reset_changed_flags(bNodeTree &ntree)
2230 for (bNode *
node : ntree.all_nodes()) {
2232 node->runtime->update = 0;
2233 for (bNodeSocket &socket :
node->inputs) {
2236 for (bNodeSocket &socket :
node->outputs) {
2247 bool update_panel_toggle_names(bNodeTree &ntree)
2249 bool changed =
false;
2250 ntree.ensure_interface_cache();
2251 for (bNodeTreeInterfaceItem *item : ntree.interface_items()) {
2255 bNodeTreeInterfacePanel *panel =
reinterpret_cast<bNodeTreeInterfacePanel *
>(item);
2256 if (bNodeTreeInterfaceSocket *toggle_socket = panel->header_toggle_socket()) {
2257 if (!
STREQ(panel->
name_, toggle_socket->name_)) {
2258 MEM_SAFE_DELETE(toggle_socket->name_);