Blender V5.3
inverse_eval.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <fmt/format.h>
10
15#include "NOD_partial_eval.hh"
17
19#include "BKE_context.hh"
20#include "BKE_library.hh"
21#include "BKE_modifier.hh"
22#include "BKE_node.hh"
24#include "BKE_node_runtime.hh"
27
28#include "BLI_map.hh"
29#include "BLI_math_euler.hh"
30#include "BLI_set.hh"
31#include "BLI_string.hh"
32
33#include "DEG_depsgraph.hh"
34
35#include "ED_node.hh"
36
37#include "RNA_access.hh"
38#include "RNA_path.hh"
39
40#include "MOD_nodes.hh"
41
42#include "ANIM_keyframing.hh"
43
45
46using namespace value_elem;
47
48std::optional<SocketValueVariant> convert_single_socket_value(const bNodeSocket &old_socket,
49 const bNodeSocket &new_socket,
50 const SocketValueVariant &old_value)
51{
52 const eNodeSocketDatatype old_type = old_socket.type;
53 const eNodeSocketDatatype new_type = new_socket.type;
54 if (old_type == new_type) {
55 return old_value;
56 }
57 const CPPType *old_cpp_type = old_socket.typeinfo->base_cpp_type;
58 const CPPType *new_cpp_type = new_socket.typeinfo->base_cpp_type;
59 if (!old_cpp_type || !new_cpp_type) {
60 return std::nullopt;
61 }
63 if (type_conversions.is_convertible(*old_cpp_type, *new_cpp_type)) {
64 const void *old_value_ptr = old_value.get_if(*old_cpp_type);
65 if (!old_value_ptr) {
66 return std::nullopt;
67 }
68 SocketValueVariant new_value;
69 void *new_value_ptr = new_value.allocate_single(*new_cpp_type);
70 type_conversions.convert_to_uninitialized(
71 *old_cpp_type, *new_cpp_type, old_value_ptr, new_value_ptr);
72 return new_value;
73 }
74 return std::nullopt;
75}
76
77static void evaluate_node_elem_upstream(const NodeInContext &ctx_node,
78 Vector<const bNodeSocket *> &r_modified_inputs,
80{
81 const bNode &node = *ctx_node.node;
82 const bke::bNodeType &ntype = *node.typeinfo;
83 if (!ntype.eval_inverse_elem) {
84 /* Node does not support inverse evaluation. */
85 return;
86 }
87 /* Build temporary map to be used by node evaluation function. */
88 Map<const bNodeSocket *, ElemVariant> elem_by_local_socket;
89 for (const bNodeSocket *output_socket : node.output_sockets()) {
90 if (const ElemVariant *elem = elem_by_socket.lookup_ptr({ctx_node.context, output_socket})) {
91 elem_by_local_socket.add(output_socket, *elem);
92 }
93 }
94 Vector<SocketElem> input_elems;
95 InverseElemEvalParams params{node, elem_by_local_socket, input_elems};
96 ntype.eval_inverse_elem(params);
97 /* Write back changed socket values to the map. */
98 for (const SocketElem &input_elem : input_elems) {
99 if (input_elem.elem) {
100 elem_by_socket.add({ctx_node.context, input_elem.socket}, input_elem.elem);
101 r_modified_inputs.append(input_elem.socket);
102 }
103 }
104}
105
106static bool propagate_socket_elem(const SocketInContext &ctx_from,
107 const SocketInContext &ctx_to,
108 Map<SocketInContext, ElemVariant> &elem_by_socket)
109{
110 const ElemVariant *from_elem = elem_by_socket.lookup_ptr(ctx_from);
111 if (!from_elem) {
112 return false;
113 }
114 /* Perform implicit conversion if necessary. */
115 const std::optional<ElemVariant> to_elem = convert_socket_elem(
116 *ctx_from.socket, *ctx_to.socket, *from_elem);
117 if (!to_elem || !*to_elem) {
118 return false;
119 }
120 elem_by_socket.lookup_or_add(ctx_to, *to_elem).merge(*to_elem);
121 return true;
122}
123
124static void get_input_elems_to_propagate(const NodeInContext &ctx_node,
126 Map<SocketInContext, ElemVariant> &elem_by_socket)
127{
128 for (const bNodeSocket *socket : ctx_node.node->input_sockets()) {
129 if (elem_by_socket.contains({ctx_node.context, socket})) {
130 r_sockets.append(socket);
131 }
132 }
133}
134
136 const SocketElem &initial_socket_elem)
137{
138 BLI_assert(!tree.has_available_link_cycle());
139
140 tree.ensure_topology_cache();
141
142 bke::ComputeContextCache compute_context_cache;
144 elem_by_socket.add({nullptr, initial_socket_elem.socket}, initial_socket_elem.elem);
145
147 {{nullptr, initial_socket_elem.socket}},
148 compute_context_cache,
149 /* Evaluate node. */
150 [&](const NodeInContext &ctx_node, Vector<const bNodeSocket *> &r_modified_inputs) {
151 evaluate_node_elem_upstream(ctx_node, r_modified_inputs, elem_by_socket);
152 },
153 /* Propagate value. */
154 [&](const SocketInContext &ctx_from, const SocketInContext &ctx_to) {
155 return propagate_socket_elem(ctx_from, ctx_to, elem_by_socket);
156 },
157 /* Get input sockets to propagate. */
158 [&](const NodeInContext &ctx_node, Vector<const bNodeSocket *> &r_sockets) {
159 get_input_elems_to_propagate(ctx_node, r_sockets, elem_by_socket);
160 });
161
163
164 for (const SocketInContext &ctx_socket : upstream_eval_targets.sockets) {
165 if (ctx_socket.context) {
166 /* Context should be empty because we only handle top-level sockets here. */
167 continue;
168 }
169 const ElemVariant *elem = elem_by_socket.lookup_ptr(ctx_socket);
170 if (!elem || !*elem) {
171 continue;
172 }
173 targets.input_sockets.append({ctx_socket.socket, *elem});
174 }
175
176 for (const NodeInContext ctx_node : upstream_eval_targets.value_nodes) {
177 if (ctx_node.context) {
178 /* Context should be empty because we only handle top-level nodes here. */
179 continue;
180 }
181 const bNodeSocket &socket = ctx_node.node->output_socket(0);
182 const ElemVariant *elem = elem_by_socket.lookup_ptr({nullptr, &socket});
183 if (!elem || !*elem) {
184 continue;
185 }
186 targets.value_nodes.append({ctx_node.node, *elem});
187 }
188
189 for (const int group_input_index : tree.interface_inputs().index_range()) {
191 tree.interface_inputs()[group_input_index]->socket_typeinfo()->type);
192 std::optional<ElemVariant> elem = get_elem_variant_for_socket_type(type);
193 if (!elem) {
194 continue;
195 }
196 /* Combine the elems from each group input node. */
197 for (const bNode *node : tree.group_input_nodes()) {
198 const bNodeSocket &socket = node->output_socket(group_input_index);
199 if (const ElemVariant *socket_elem = elem_by_socket.lookup_ptr({nullptr, &socket})) {
200 elem->merge(*socket_elem);
201 }
202 }
203 if (!*elem) {
204 continue;
205 }
206 targets.group_inputs.append({group_input_index, *elem});
207 }
208
209 return targets;
210}
211
213 const NodeInContext &ctx_node,
214 const Map<SocketInContext, ElemVariant> &elem_by_socket_filter,
215 Map<SocketInContext, ElemVariant> &elem_by_socket,
216 Vector<const bNodeSocket *> &r_outputs_to_propagate)
217{
218 const bNode &node = *ctx_node.node;
219 const bke::bNodeType &ntype = *node.typeinfo;
220 if (!ntype.eval_elem) {
221 return;
222 }
223 /* Build temporary map used by the node evaluation. */
224 Map<const bNodeSocket *, ElemVariant> elem_by_local_socket;
225 for (const bNodeSocket *input_socket : node.input_sockets()) {
226 if (const ElemVariant *elem = elem_by_socket.lookup_ptr({ctx_node.context, input_socket})) {
227 elem_by_local_socket.add(input_socket, *elem);
228 }
229 }
230 Vector<SocketElem> output_elems;
231 ElemEvalParams params{node, elem_by_local_socket, output_elems};
232 ntype.eval_elem(params);
233 /* Filter and store the outputs generated by the node evaluation. */
234 for (const SocketElem &output_elem : output_elems) {
235 if (output_elem.elem) {
236 if (const ElemVariant *elem_filter = elem_by_socket_filter.lookup_ptr(
237 {ctx_node.context, output_elem.socket}))
238 {
239 ElemVariant new_elem = *elem_filter;
240 new_elem.intersect(output_elem.elem);
241 elem_by_socket.add({ctx_node.context, output_elem.socket}, new_elem);
242 if (new_elem) {
243 r_outputs_to_propagate.append(output_elem.socket);
244 }
245 }
246 }
247 }
248}
249
251 const SocketInContext &ctx_from,
252 const SocketInContext &ctx_to,
253 const Map<SocketInContext, ElemVariant> &elem_by_socket_filter,
254 Map<SocketInContext, ElemVariant> &elem_by_socket)
255{
256 const ElemVariant *from_elem = elem_by_socket.lookup_ptr(ctx_from);
257 if (!from_elem) {
258 return false;
259 }
260 const ElemVariant *to_elem_filter = elem_by_socket_filter.lookup_ptr(ctx_to);
261 if (!to_elem_filter) {
262 return false;
263 }
264 const std::optional<ElemVariant> converted_elem = convert_socket_elem(
265 *ctx_from.socket, *ctx_to.socket, *from_elem);
266 if (!converted_elem) {
267 return false;
268 }
269 if (ctx_to.socket->is_multi_input()) {
270 ElemVariant added_elem = *converted_elem;
271 added_elem.intersect(*to_elem_filter);
272 elem_by_socket.lookup_or_add(ctx_to, added_elem).merge(added_elem);
273 return true;
274 }
275 ElemVariant to_elem = *to_elem_filter;
276 to_elem.intersect(*converted_elem);
277 elem_by_socket.add(ctx_to, to_elem);
278 return true;
279}
280
282 const ComputeContext &initial_context,
283 const SocketElem &initial_socket_elem,
284 FunctionRef<void(const ComputeContext &context)> foreach_context_fn,
285 FunctionRef<void(const ComputeContext &context,
286 const bNodeSocket &socket,
287 const ElemVariant &elem)> foreach_socket_fn)
288{
289 BLI_assert(initial_socket_elem.socket->is_input());
290 if (!initial_socket_elem.elem) {
291 return;
292 }
293 bke::ComputeContextCache compute_context_cache;
294 Map<SocketInContext, ElemVariant> upstream_elem_by_socket;
295 upstream_elem_by_socket.add({&initial_context, initial_socket_elem.socket},
296 initial_socket_elem.elem);
297
298 /* In a first pass, propagate upstream to find the upstream targets. */
300 {{&initial_context, initial_socket_elem.socket}},
301 compute_context_cache,
302 /* Evaluate node. */
303 [&](const NodeInContext &ctx_node, Vector<const bNodeSocket *> &r_modified_inputs) {
304 evaluate_node_elem_upstream(ctx_node, r_modified_inputs, upstream_elem_by_socket);
305 },
306 /* Propagate value. */
307 [&](const SocketInContext &ctx_from, const SocketInContext &ctx_to) {
308 return propagate_socket_elem(ctx_from, ctx_to, upstream_elem_by_socket);
309 },
310 /* Get input sockets to propagate. */
311 [&](const NodeInContext &ctx_node, Vector<const bNodeSocket *> &r_sockets) {
312 get_input_elems_to_propagate(ctx_node, r_sockets, upstream_elem_by_socket);
313 });
314
315 /* The upstream propagation may also follow node paths that don't end up in upstream targets.
316 * That can happen if there is a node on the path that does not support inverse evaluation. In
317 * this case, parts of the evaluation path has to be discarded again. This is done using a second
318 * pass. Now we start the evaluation at the discovered upstream targets and propagate the changed
319 * socket elements downstream. We only care about the sockets that have already been used by
320 * upstream evaluation, therefore the downstream evaluation is filtered. */
321
322 /* Gather all upstream evaluation targets to start downstream evaluation there. */
323 Vector<SocketInContext> initial_downstream_evaluation_sockets;
324 initial_downstream_evaluation_sockets.extend(upstream_eval_targets.sockets.begin(),
325 upstream_eval_targets.sockets.end());
326 initial_downstream_evaluation_sockets.extend(upstream_eval_targets.group_inputs.begin(),
327 upstream_eval_targets.group_inputs.end());
328 for (const NodeInContext &ctx_node : upstream_eval_targets.value_nodes) {
329 initial_downstream_evaluation_sockets.append(
330 {ctx_node.context, &ctx_node.node->output_socket(0)});
331 }
332
333 Map<SocketInContext, ElemVariant> final_elem_by_socket;
334 for (const SocketInContext &ctx_socket : initial_downstream_evaluation_sockets) {
335 final_elem_by_socket.add(ctx_socket, upstream_elem_by_socket.lookup(ctx_socket));
336 }
337
339 initial_downstream_evaluation_sockets,
340 compute_context_cache,
341 /* Evaluate node. */
342 [&](const NodeInContext &ctx_node, Vector<const bNodeSocket *> &r_outputs_to_propagate) {
344 ctx_node, upstream_elem_by_socket, final_elem_by_socket, r_outputs_to_propagate);
345 },
346 /* Propagate value. */
347 [&](const SocketInContext &ctx_from, const SocketInContext &ctx_to) {
349 ctx_from, ctx_to, upstream_elem_by_socket, final_elem_by_socket);
350 });
351
352 if (foreach_context_fn) {
353 Set<ComputeContextHash> handled_hashes;
354 for (const SocketInContext &ctx_socket : final_elem_by_socket.keys()) {
355 if (handled_hashes.add(ctx_socket.context->hash())) {
356 foreach_context_fn(*ctx_socket.context);
357 }
358 }
359 }
360 if (foreach_socket_fn) {
361 for (auto &&item : final_elem_by_socket.items()) {
362 foreach_socket_fn(*item.key.context, *item.key.socket, item.value);
363 }
364 }
365}
366
367using RNAValueVariant = std::variant<float, int, bool>;
368
370 ID &id,
372 const RNAValueVariant &value_variant)
373{
374 if (!ID_IS_EDITABLE(&id)) {
375 return false;
376 }
377
378 PointerRNA id_ptr = RNA_id_pointer_create(&id);
379 PointerRNA value_ptr;
380 PropertyRNA *prop;
381 int index;
382 if (!RNA_path_resolve_property_full(&id_ptr, rna_path.c_str(), &value_ptr, &prop, &index)) {
383 return false;
384 }
385
386 /* In the future, we could check if there is a driver on the property and propagate the change
387 * backwards through the driver. */
388
389 const PropertyType dst_type = RNA_property_type(prop);
390 const int array_len = RNA_property_array_length(&value_ptr, prop);
391
392 Scene *scene = CTX_data_scene(&C);
393 const bool only_when_keyed = animrig::is_keying_flag(scene, AUTOKEY_FLAG_INSERTAVAILABLE);
394
395 switch (dst_type) {
396 case PROP_FLOAT: {
397 float value = std::visit([](auto v) { return float(v); }, value_variant);
398 float soft_min, soft_max, step, precision;
399 RNA_property_float_ui_range(&value_ptr, prop, &soft_min, &soft_max, &step, &precision);
400 value = std::clamp(value, soft_min, soft_max);
401 if (array_len == 0) {
402 RNA_property_float_set(&value_ptr, prop, value);
403 RNA_property_update(&C, &value_ptr, prop);
405 &C, scene, &value_ptr, prop, 0, scene->r.cfra, only_when_keyed);
406 return true;
407 }
408 if (index >= 0 && index < array_len) {
409 RNA_property_float_set_index(&value_ptr, prop, index, value);
410 RNA_property_update(&C, &value_ptr, prop);
412 &C, scene, &value_ptr, prop, index, scene->r.cfra, only_when_keyed);
413 return true;
414 }
415 break;
416 }
417 case PROP_INT: {
418 int value = std::visit([](auto v) { return int(v); }, value_variant);
419 int soft_min, soft_max, step;
420 RNA_property_int_ui_range(&value_ptr, prop, &soft_min, &soft_max, &step);
421 value = std::clamp(value, soft_min, soft_max);
422 if (array_len == 0) {
423 RNA_property_int_set(&value_ptr, prop, value);
424 RNA_property_update(&C, &value_ptr, prop);
426 &C, scene, &value_ptr, prop, 0, scene->r.cfra, only_when_keyed);
427 return true;
428 }
429 if (index >= 0 && index < array_len) {
430 RNA_property_int_set_index(&value_ptr, prop, index, value);
431 RNA_property_update(&C, &value_ptr, prop);
433 &C, scene, &value_ptr, prop, index, scene->r.cfra, only_when_keyed);
434 return true;
435 }
436 break;
437 }
438 case PROP_BOOLEAN: {
439 const bool value = std::visit([](auto v) { return bool(v); }, value_variant);
440 if (array_len == 0) {
441 RNA_property_boolean_set(&value_ptr, prop, value);
442 RNA_property_update(&C, &value_ptr, prop);
444 &C, scene, &value_ptr, prop, 0, scene->r.cfra, only_when_keyed);
445 return true;
446 }
447 if (index >= 0 && index < array_len) {
448 RNA_property_boolean_set_index(&value_ptr, prop, index, value);
449 RNA_property_update(&C, &value_ptr, prop);
451 &C, scene, &value_ptr, prop, index, scene->r.cfra, only_when_keyed);
452 return true;
453 }
454 break;
455 }
456 default:
457 break;
458 };
459
460 return false;
461}
462
464 ID &id,
466 const float3 &value)
467{
468 bool any_success = false;
469 for (const int i : IndexRange(3)) {
470 const std::string rna_path_for_index = fmt::format("{}[{}]", rna_path, i);
471 any_success |= set_rna_property(C, id, rna_path_for_index, value[i]);
472 }
473 return any_success;
474}
475
477 bNodeSocket &socket,
478 const SocketValueVariant &value_variant)
479{
480 bNode &node = socket.owner_node();
481 bNodeTree &tree = socket.owner_tree();
482
483 const std::string default_value_rna_path = fmt::format(
484 "nodes[\"{}\"].inputs[{}].default_value", BLI_str_escape(node.name), socket.index());
485
486 switch (socket.type) {
487 case SOCK_FLOAT: {
488 const float value = value_variant.copy_as<float>();
489 return set_rna_property(C, tree.id, default_value_rna_path, value);
490 }
491 case SOCK_INT: {
492 const int value = value_variant.copy_as<int>();
493 return set_rna_property(C, tree.id, default_value_rna_path, value);
494 }
495 case SOCK_BOOLEAN: {
496 const bool value = value_variant.copy_as<bool>();
497 return set_rna_property(C, tree.id, default_value_rna_path, value);
498 }
499 case SOCK_VECTOR: {
500 const float3 value = value_variant.copy_as<float3>();
501 return set_rna_property_float3(C, tree.id, default_value_rna_path, value);
502 }
503 case SOCK_ROTATION: {
504 const math::Quaternion rotation = value_variant.copy_as<math::Quaternion>();
505 const float3 euler = float3(math::to_euler(rotation));
506 return set_rna_property_float3(C, tree.id, default_value_rna_path, euler);
507 }
508 default:
509 break;
510 }
511 return false;
512}
513
514static bool set_value_node_value(bContext &C, bNode &node, const SocketValueVariant &value_variant)
515{
516 bNodeTree &tree = node.owner_tree();
517
518 switch (node.type_legacy) {
519 case SH_NODE_VALUE: {
520 const float value = value_variant.copy_as<float>();
521 const std::string rna_path = fmt::format("nodes[\"{}\"].outputs[0].default_value",
522 BLI_str_escape(node.name));
523 return set_rna_property(C, tree.id, rna_path, value);
524 }
525 case FN_NODE_INPUT_INT: {
526 const int value = value_variant.copy_as<int>();
527 const std::string rna_path = fmt::format("nodes[\"{}\"].integer", BLI_str_escape(node.name));
528 return set_rna_property(C, tree.id, rna_path, value);
529 }
530 case FN_NODE_INPUT_BOOL: {
531 const bool value = value_variant.copy_as<bool>();
532 const std::string rna_path = fmt::format("nodes[\"{}\"].boolean", BLI_str_escape(node.name));
533 return set_rna_property(C, tree.id, rna_path, value);
534 }
536 const float3 value = value_variant.copy_as<float3>();
537 const std::string rna_path = fmt::format("nodes[\"{}\"].vector", BLI_str_escape(node.name));
538 return set_rna_property_float3(C, tree.id, rna_path, value);
539 }
541 const math::Quaternion rotation = value_variant.copy_as<math::Quaternion>();
542 const float3 euler = float3(math::to_euler(rotation));
543 const std::string rna_path = fmt::format("nodes[\"{}\"].rotation_euler",
544 BLI_str_escape(node.name));
545 return set_rna_property_float3(C, tree.id, rna_path, euler);
546 }
547 }
548 return false;
549}
550
552 Object &object,
554 const bNodeTreeInterfaceSocket &interface_socket,
555 const SocketValueVariant &value_variant)
556{
558
559 const std::string main_prop_rna_path = fmt::format(
560 "modifiers[\"{}\"].properties.inputs.{}.value",
562 interface_socket.identifier);
563
564 switch (interface_socket.socket_typeinfo()->type) {
565 case SOCK_FLOAT: {
566 const float value = value_variant.copy_as<float>();
567 return set_rna_property(C, object.id, main_prop_rna_path, value);
568 }
569 case SOCK_INT: {
570 const int value = value_variant.copy_as<int>();
571 return set_rna_property(C, object.id, main_prop_rna_path, value);
572 }
573 case SOCK_BOOLEAN: {
574 const bool value = value_variant.copy_as<bool>();
575 return set_rna_property(C, object.id, main_prop_rna_path, value);
576 }
577 case SOCK_VECTOR: {
578 const float3 value = value_variant.copy_as<float3>();
579 return set_rna_property_float3(C, object.id, main_prop_rna_path, value);
580 }
581 case SOCK_ROTATION: {
582 const math::Quaternion rotation = value_variant.copy_as<math::Quaternion>();
583 const float3 euler = float3(math::to_euler(rotation));
584 return set_rna_property_float3(C, object.id, main_prop_rna_path, euler);
585 }
586 default:
587 return false;
588 }
589}
590
591std::optional<SocketValueVariant> get_logged_socket_value(eval_log::NodeTreeLog &tree_log,
592 const bNodeSocket &socket)
593{
594 switch (socket.type) {
595 case SOCK_FLOAT: {
596 if (const std::optional<float> value = tree_log.find_primitive_socket_value<float>(socket)) {
597 return SocketValueVariant{*value};
598 }
599 break;
600 }
601 case SOCK_INT: {
602 if (const std::optional<int> value = tree_log.find_primitive_socket_value<int>(socket)) {
603 return SocketValueVariant{*value};
604 }
605 break;
606 }
607 case SOCK_BOOLEAN: {
608 if (const std::optional<bool> value = tree_log.find_primitive_socket_value<bool>(socket)) {
609 return SocketValueVariant{*value};
610 }
611 break;
612 }
613 case SOCK_VECTOR: {
614 if (const std::optional<float3> value = tree_log.find_primitive_socket_value<float3>(socket))
615 {
616 return SocketValueVariant{*value};
617 }
618 break;
619 }
620 case SOCK_ROTATION: {
621 if (const std::optional<math::Quaternion> value =
623 {
624 return SocketValueVariant{*value};
625 }
626 break;
627 }
628 case SOCK_MATRIX: {
629 if (const std::optional<float4x4> value = tree_log.find_primitive_socket_value<float4x4>(
630 socket))
631 {
632 return SocketValueVariant{*value};
633 }
634 break;
635 }
636 default:
637 break;
638 }
639 return std::nullopt;
640}
641
643 const NodeInContext &ctx_node,
646 Vector<const bNodeSocket *> &r_modified_inputs)
647{
648 const bNode &node = *ctx_node.node;
649 const ComputeContext *context = ctx_node.context;
650 const bke::bNodeType &ntype = *node.typeinfo;
651 if (!ntype.eval_inverse) {
652 /* Node does not support inverse evaluation. */
653 return;
654 }
655 if (!context) {
656 /* We need a context here to access the tree log. */
657 return;
658 }
659 eval_log::NodeTreeLog &tree_log = eval_log.get_tree_log(context->hash());
660 tree_log.ensure_socket_values();
661
662 /* Build a temporary map of old socket values for the node evaluation. */
664 for (const bNodeSocket *socket : node.input_sockets()) {
665 if (!socket->is_available()) {
666 continue;
667 }
668 /* Retrieve input socket values from the log. */
669 if (const std::optional<SocketValueVariant> value = get_logged_socket_value(tree_log, *socket))
670 {
671 old_socket_values.add(socket, *value);
672 }
673 }
674 for (const bNodeSocket *socket : node.output_sockets()) {
675 if (!socket->is_available()) {
676 continue;
677 }
678 /* First check if there is an updated socket value for an output socket. */
679 if (const SocketValueVariant *value = value_by_socket.lookup_ptr({context, socket})) {
680 old_socket_values.add(socket, *value);
681 }
682 /* If not, retrieve the output socket value from the log. */
683 else if (const std::optional<SocketValueVariant> value = get_logged_socket_value(tree_log,
684 *socket))
685 {
686 old_socket_values.add(socket, *value);
687 }
688 }
689
691 InverseEvalParams params{node, old_socket_values, updated_socket_values};
692 ntype.eval_inverse(params);
693 /* Write back new socket values. */
694 for (auto &&item : updated_socket_values.items()) {
695 const bNodeSocket &socket = *item.key;
696 value_by_socket.add({context, &socket}, std::move(item.value));
697 r_modified_inputs.append(&socket);
698 }
699}
700
702 Object &object,
705 const Span<SocketToUpdate> sockets_to_update)
706{
707 nmd.node_group->ensure_topology_cache();
708
709 bke::ComputeContextCache compute_context_cache;
711
712 Vector<SocketInContext> initial_sockets;
713
714 /* Gather starting values for the backpropagation. */
715 for (const SocketToUpdate &socket_to_update : sockets_to_update) {
716 if (socket_to_update.multi_input_link) {
717 BLI_assert(socket_to_update.multi_input_link->tosock == socket_to_update.socket);
718 const std::optional<SocketValueVariant> converted_value = convert_single_socket_value(
719 *socket_to_update.socket,
720 *socket_to_update.multi_input_link->fromsock,
721 socket_to_update.new_value);
722 if (!converted_value) {
723 continue;
724 }
725 value_by_socket.add({socket_to_update.context, socket_to_update.multi_input_link->fromsock},
726 *converted_value);
727 }
728 else {
729 value_by_socket.add({socket_to_update.context, socket_to_update.socket},
730 socket_to_update.new_value);
731 }
732 }
733
734 if (value_by_socket.is_empty()) {
735 return false;
736 }
737
738 for (const SocketInContext &ctx_socket : value_by_socket.keys()) {
739 initial_sockets.append(ctx_socket);
740 }
741
742 /* Actually backpropagate the socket values as far as possible in the node tree. */
744 initial_sockets,
745 compute_context_cache,
746 /* Evaluate node. */
747 [&](const NodeInContext &ctx_node, Vector<const bNodeSocket *> &r_modified_inputs) {
749 ctx_node, eval_log, value_by_socket, r_modified_inputs);
750 },
751 /* Propagate value. */
752 [&](const SocketInContext &ctx_from, const SocketInContext &ctx_to) {
753 const SocketValueVariant *from_value = value_by_socket.lookup_ptr(ctx_from);
754 if (!from_value) {
755 return false;
756 }
757 const std::optional<SocketValueVariant> converted_value = convert_single_socket_value(
758 *ctx_from.socket, *ctx_to.socket, *from_value);
759 if (!converted_value) {
760 return false;
761 }
762 value_by_socket.add(ctx_to, std::move(*converted_value));
763 return true;
764 },
765 /* Get input sockets to propagate. */
766 [&](const NodeInContext &ctx_node, Vector<const bNodeSocket *> &r_sockets) {
767 for (const bNodeSocket *socket : ctx_node.node->input_sockets()) {
768 if (value_by_socket.contains({ctx_node.context, socket})) {
769 r_sockets.append(socket);
770 }
771 }
772 });
773
774 bool any_success = false;
775 /* Set new values for sockets. */
776 for (const SocketInContext &ctx_socket : upstream_eval_targets.sockets) {
777 if (const SocketValueVariant *value = value_by_socket.lookup_ptr(ctx_socket)) {
778 bNodeSocket &socket_mutable = const_cast<bNodeSocket &>(*ctx_socket.socket);
779 any_success |= set_socket_value(C, socket_mutable, *value);
780 }
781 }
782 /* Set new values for value nodes. */
783 for (const NodeInContext &ctx_node : upstream_eval_targets.value_nodes) {
784 if (const SocketValueVariant *value = value_by_socket.lookup_ptr(
785 {ctx_node.context, &ctx_node.node->output_socket(0)}))
786 {
787 bNode &node_mutable = const_cast<bNode &>(*ctx_node.node);
788 any_success |= set_value_node_value(C, node_mutable, *value);
789 }
790 }
791 /* Set new values for modifier inputs. */
792 const bke::DataBlockComputeContext data_block_context{nullptr, object.id};
793 const bke::GeometryNodesModifierComputeContext modifier_context{&data_block_context, nmd};
794 for (const bNode *group_input_node : nmd.node_group->group_input_nodes()) {
795 for (const bNodeSocket *socket : group_input_node->output_sockets().drop_back(1)) {
796 if (const SocketValueVariant *value = value_by_socket.lookup_ptr(
797 {&modifier_context, socket}))
798 {
799 any_success |= set_modifier_value(
800 C, object, nmd, *nmd.node_group->interface_inputs()[socket->index()], *value);
801 }
802 }
803 }
804
805 return any_success;
806}
807
809 const bNode &node,
812 : socket_values_(socket_values), updated_socket_values_(updated_socket_values), node(node)
813{
814}
815
816} // namespace blender::nodes::inverse_eval
Functions to insert, delete or modify keyframes.
#define FN_NODE_INPUT_VECTOR
#define SH_NODE_VALUE
#define FN_NODE_INPUT_INT
#define FN_NODE_INPUT_BOOL
#define FN_NODE_INPUT_ROTATION
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define C
Definition RandGen.cpp:29
constexpr IndexRange()=default
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
KeyIterator keys() const &
Definition BLI_map.hh:875
bool is_empty() const
Definition BLI_map.hh:986
bool contains(const Key &key) const
Definition BLI_map.hh:353
ItemIterator items() const &
Definition BLI_map.hh:902
Value & lookup_or_add(const Key &key, const Value &value)
Definition BLI_map.hh:588
bool add(const Key &key)
Definition BLI_set.hh:248
void append(const T &value)
void extend(Span< T > array)
void convert_to_uninitialized(const CPPType &from_type, const CPPType &to_type, const void *from_value, void *to_value) const
bool is_convertible(const CPPType &from_type, const CPPType &to_type) const
void * allocate_single(const CPPType &type)
std::optional< T > find_primitive_socket_value(const bNodeSocket &query_socket)
InverseEvalParams(const bNode &node, const Map< const bNodeSocket *, bke::SocketValueVariant > &socket_values, Map< const bNodeSocket *, bke::SocketValueVariant > &updated_socket_values)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
bool autokeyframe_property(bContext *C, Scene *scene, PointerRNA *ptr, PropertyRNA *prop, int rnaindex, float cfra, bool only_if_property_keyed)
bool is_keying_flag(const Scene *scene, eKeying_Flag flag)
const DataTypeConversions & get_implicit_type_conversions()
QuaternionBase< float > Quaternion
Euler3Base< T > to_euler(const AxisAngleBase< T, AngleT > &axis_angle, EulerOrder order)
static void evaluate_node_elem_upstream(const NodeInContext &ctx_node, Vector< const bNodeSocket * > &r_modified_inputs, Map< SocketInContext, ElemVariant > &elem_by_socket)
static void backpropagate_socket_values_through_node(const NodeInContext &ctx_node, eval_log::NodesEvalLog &eval_log, Map< SocketInContext, SocketValueVariant > &value_by_socket, Vector< const bNodeSocket * > &r_modified_inputs)
LocalInverseEvalTargets find_local_inverse_eval_targets(const bNodeTree &tree, const SocketElem &initial_socket_elem)
void foreach_element_on_inverse_eval_path(const ComputeContext &initial_context, const SocketElem &initial_socket_elem, FunctionRef< void(const ComputeContext &context)> foreach_context_fn, FunctionRef< void(const ComputeContext &context, const bNodeSocket &socket, const ElemVariant &elem)> foreach_socket_fn)
std::optional< SocketValueVariant > get_logged_socket_value(eval_log::NodeTreeLog &tree_log, const bNodeSocket &socket)
static bool set_socket_value(bContext &C, bNodeSocket &socket, const SocketValueVariant &value_variant)
static bool set_modifier_value(bContext &C, Object &object, NodesModifierData &nmd, const bNodeTreeInterfaceSocket &interface_socket, const SocketValueVariant &value_variant)
static bool propagate_value_elem_filtered(const SocketInContext &ctx_from, const SocketInContext &ctx_to, const Map< SocketInContext, ElemVariant > &elem_by_socket_filter, Map< SocketInContext, ElemVariant > &elem_by_socket)
bool backpropagate_socket_values(bContext &C, Object &object, NodesModifierData &nmd, eval_log::NodesEvalLog &eval_log, const Span< SocketToUpdate > sockets_to_update)
static void get_input_elems_to_propagate(const NodeInContext &ctx_node, Vector< const bNodeSocket * > &r_sockets, Map< SocketInContext, ElemVariant > &elem_by_socket)
static bool set_rna_property_float3(bContext &C, ID &id, const StringRefNull rna_path, const float3 &value)
static void evaluate_node_elem_downstream_filtered(const NodeInContext &ctx_node, const Map< SocketInContext, ElemVariant > &elem_by_socket_filter, Map< SocketInContext, ElemVariant > &elem_by_socket, Vector< const bNodeSocket * > &r_outputs_to_propagate)
std::optional< SocketValueVariant > convert_single_socket_value(const bNodeSocket &old_socket, const bNodeSocket &new_socket, const SocketValueVariant &old_value)
static bool set_value_node_value(bContext &C, bNode &node, const SocketValueVariant &value_variant)
static bool set_rna_property(bContext &C, ID &id, const StringRefNull rna_path, const RNAValueVariant &value_variant)
std::variant< float, int, bool > RNAValueVariant
static bool propagate_socket_elem(const SocketInContext &ctx_from, const SocketInContext &ctx_to, Map< SocketInContext, ElemVariant > &elem_by_socket)
UpstreamEvalTargets eval_upstream(const Span< SocketInContext > initial_sockets, bke::ComputeContextCache &compute_context_cache, FunctionRef< void(const NodeInContext &ctx_node, Vector< const bNodeSocket * > &r_modified_inputs)> evaluate_node_fn, FunctionRef< bool(const SocketInContext &ctx_from, const SocketInContext &ctx_to)> propagate_value_fn, FunctionRef< void(const NodeInContext &ctx_node, Vector< const bNodeSocket * > &r_sockets)> get_inputs_to_propagate_fn)
void eval_downstream(const Span< SocketInContext > initial_sockets, bke::ComputeContextCache &compute_context_cache, FunctionRef< void(const NodeInContext &ctx_node, Vector< const bNodeSocket * > &r_outputs_to_propagate)> evaluate_node_fn, FunctionRef< bool(const SocketInContext &ctx_from, const SocketInContext &ctx_to)> propagate_value_fn)
std::optional< ElemVariant > get_elem_variant_for_socket_type(const eNodeSocketDatatype type)
Definition value_elem.cc:13
std::optional< ElemVariant > convert_socket_elem(const bNodeSocket &old_socket, const bNodeSocket &new_socket, const ElemVariant &old_elem)
Definition value_elem.cc:33
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
PointerRNA RNA_id_pointer_create(ID *id)
void RNA_property_float_set(PointerRNA *ptr, PropertyRNA *prop, float value)
ATTR_WARN_UNUSED_RESULT const BMVert * v
MatBase< float, 4, 4 > float4x4
void RNA_property_int_set(PointerRNA *ptr, PropertyRNA *prop, int value)
PropertyType RNA_property_type(PropertyRNA *prop)
@ PROP_BOOLEAN
Definition RNA_types.hh:210
void DEG_id_tag_update(ID *id, unsigned int flags)
char char size_t BLI_str_escape(char *__restrict dst, StringRef src, size_t dst_maxncpy) ATTR_NONNULL(1)
void RNA_property_int_ui_range(PointerRNA *ptr, PropertyRNA *prop, int *softmin, int *softmax, int *step)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void RNA_property_boolean_set(PointerRNA *ptr, PropertyRNA *prop, bool value)
Scene * CTX_data_scene(const bContext *C)
void RNA_property_int_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, int value)
void RNA_property_float_ui_range(PointerRNA *ptr, PropertyRNA *prop, float *softmin, float *softmax, float *step, float *precision)
void RNA_property_float_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, float value)
@ AUTOKEY_FLAG_INSERTAVAILABLE
VecBase< float, 3 > float3
void RNA_property_boolean_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, bool value)
bool RNA_path_resolve_property_full(const PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop, int *r_index)
Definition rna_path.cc:609
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct RenderData r
bke::bNodeSocketType * typeinfo
eNodeSocketDatatype type
eNodeTree_Type type
const CPPType * base_cpp_type
Definition BKE_node.hh:210
Defines a node type.
Definition BKE_node.hh:265
NodeInverseElemEvalFunction eval_inverse_elem
Definition BKE_node.hh:434
NodeInverseEvalFunction eval_inverse
Definition BKE_node.hh:441
NodeElemEvalFunction eval_elem
Definition BKE_node.hh:428
void intersect(const ElemVariant &other)