Blender V5.3
MOD_nodes.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstring>
10#include <fmt/format.h>
11#include <sstream>
12#include <string>
13#include <xxhash.h>
14
15#include "MEM_guardedalloc.h"
16
17#include "BLI_listbase.hh"
19#include "BLI_set.hh"
20#include "BLI_string.hh"
21#include "BLI_utildefines.hh"
22
23#include "DNA_array_utils.hh"
25#include "DNA_curves_types.h"
26#include "DNA_material_types.h"
27#include "DNA_mesh_types.h"
28#include "DNA_modifier_types.h"
29#include "DNA_node_types.h"
30#include "DNA_object_types.h"
32#include "DNA_scene_types.h"
33#include "DNA_screen_types.h"
34#include "DNA_space_types.h"
35#include "DNA_view3d_types.h"
37
42#include "BKE_customdata.hh"
43#include "BKE_global.hh"
44#include "BKE_idprop.hh"
45#include "BKE_lib_id.hh"
46#include "BKE_lib_query.hh"
47#include "BKE_main.hh"
48#include "BKE_mesh.hh"
49#include "BKE_modifier.hh"
51#include "BKE_node_runtime.hh"
53#include "BKE_object.hh"
54#include "BKE_packedFile.hh"
55#include "BKE_pointcloud.hh"
56#include "BKE_screen.hh"
57#include "BKE_workspace.hh"
58
59#include "BLO_read_write.hh"
60
63#include "UI_resources.hh"
64
65#include "BLT_translation.hh"
66
67#include "WM_api.hh"
68#include "WM_types.hh"
69
70#include "RNA_access.hh"
71#include "RNA_enum_types.hh"
72#include "RNA_path.hh"
73#include "RNA_prototypes.hh"
74
78
79#include "MOD_modifiertypes.hh"
80#include "MOD_nodes.hh"
81#include "MOD_ui_common.hh"
82
83#include "ED_node.hh"
84#include "ED_object.hh"
85#include "ED_screen.hh"
86#include "ED_undo.hh"
87#include "ED_viewer_path.hh"
88
89#include "NOD_dependencies.hh"
96
97namespace blender {
98
99namespace lf = fn::lazy_function;
100namespace bake = bke::bake;
101
102static void init_data(ModifierData *md)
103{
104 NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
105
108 nmd->runtime = MEM_new<NodesModifierRuntime>(__func__);
109 nmd->runtime->cache = std::make_shared<bake::ModifierCache>();
110}
111
114{
117 if (ID *id = IDP_ID_get(property)) {
118 deps.add_generic_id_full(id);
119 }
120 });
121}
122
123/* We don't know exactly what attributes from the other object we will need. */
129
131 Collection &collection)
132{
133 DEG_add_collection_geometry_relation(ctx->node, &collection, "Nodes Modifier");
135}
136
138 Object &object,
140{
141 if (info.transform) {
142 DEG_add_object_relation(ctx->node, &object, DEG_OB_COMP_TRANSFORM, "Nodes Modifier");
143 }
144 if (&object == ctx->object) {
145 return;
146 }
147 if (info.geometry) {
148 const bool is_just_collection_instance = object.type == OB_EMPTY &&
149 object.instance_collection != nullptr &&
150 object.modifiers.is_empty();
151 if (is_just_collection_instance) {
152 add_collection_relation(ctx, *object.instance_collection);
153 }
154 else if (DEG_object_has_geometry_component(&object)) {
155 DEG_add_object_relation(ctx->node, &object, DEG_OB_COMP_GEOMETRY, "Nodes Modifier");
157 }
158 }
159 if (object.type == OB_CAMERA) {
160 if (info.camera_parameters) {
161 DEG_add_object_relation(ctx->node, &object, DEG_OB_COMP_PARAMETERS, "Nodes Modifier");
162 }
163 }
164 if (object.type == OB_ARMATURE) {
165 if (info.pose) {
166 DEG_add_object_relation(ctx->node, &object, DEG_OB_COMP_EVAL_POSE, "Nodes Modifier");
167 }
168 }
169}
170
172{
173 NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
174 if (nmd->node_group == nullptr) {
175 return;
176 }
177 if (ID_MISSING(nmd->node_group)) {
178 return;
179 }
180
181 DEG_add_node_tree_output_relation(ctx->node, nmd->node_group, "Nodes Modifier");
182
184
185 /* Create dependencies to data-blocks referenced by the settings in the modifier. */
186 find_dependencies_from_settings(*nmd, eval_deps);
187
188 for (const NodesModifierBake &bake : Span(nmd->bakes, nmd->bakes_num)) {
189 for (const NodesModifierDataBlock &data_block : Span(bake.data_blocks, bake.data_blocks_num)) {
190 if (data_block.id) {
191 eval_deps.add_generic_id_full(data_block.id);
192 }
193 }
194 }
195
196 for (ID *id : eval_deps.ids.values()) {
197 switch (ID_Type(GS(id->name))) {
198 case ID_OB: {
199 Object *object = reinterpret_cast<Object *>(id);
201 ctx, *object, eval_deps.objects_info.lookup_default(object->id.session_uid, {}));
202 break;
203 }
204 case ID_GR: {
205 Collection *collection = reinterpret_cast<Collection *>(id);
206 add_collection_relation(ctx, *collection);
207 break;
208 }
209 case ID_IM:
210 case ID_TE: {
211 DEG_add_generic_id_relation(ctx->node, id, "Nodes Modifier");
212 break;
213 }
214 case ID_VF: {
215 DEG_add_vfont_relation(ctx->node, reinterpret_cast<VFont *>(id), "Nodes Modifier");
216 break;
217 }
218 case ID_MA: {
219 /* Purposefully don't add relations for materials. While there are material sockets,
220 * the pointers are only passed around as handles rather than dereferenced. */
221 break;
222 }
223 default: {
224 /* Other types don't need depsgraph dependencies currently. */
225 break;
226 }
227 }
228 }
229
230 if (eval_deps.needs_own_transform) {
231 DEG_add_depends_on_transform_relation(ctx->node, "Nodes Modifier");
232 }
233 if (eval_deps.needs_active_camera) {
234 DEG_add_scene_camera_relation(ctx->node, ctx->scene, DEG_OB_COMP_TRANSFORM, "Nodes Modifier");
235 }
236 /* Active camera is a scene parameter that can change, so we need a relation for that, too. */
237 if (eval_deps.needs_active_camera || eval_deps.needs_scene_render_params) {
238 DEG_add_scene_relation(ctx->node, ctx->scene, DEG_SCENE_COMP_PARAMETERS, "Nodes Modifier");
239 }
240}
241
242static bool depends_on_time(Scene * /*scene*/, ModifierData *md)
243{
244 const NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
245 const bNodeTree *tree = nmd->node_group;
246 if (tree == nullptr) {
247 return false;
248 }
249 if (ID_MISSING(tree)) {
250 return false;
251 }
252 for (const NodesModifierBake &bake : Span(nmd->bakes, nmd->bakes_num)) {
253 if (bake.bake_mode == NODES_MODIFIER_BAKE_MODE_ANIMATION) {
254 return true;
255 }
256 }
258 return eval_deps.time_dependent;
259}
260
261static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
262{
263 NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
264 walk(user_data, ob, reinterpret_cast<ID **>(&nmd->node_group), IDWALK_CB_USER);
265
266 if (nmd->settings_legacy.properties) {
267 /* Also walk legacy properties because they are still used at runtime before post linking
268 * versioning. */
271 walk(user_data, ob, reinterpret_cast<ID **>(&id_prop->data.pointer), IDWALK_CB_USER);
272 });
273 }
274
275 for (NodesModifierBake &bake : MutableSpan(nmd->bakes, nmd->bakes_num)) {
276 for (NodesModifierDataBlock &data_block : MutableSpan(bake.data_blocks, bake.data_blocks_num))
277 {
278 walk(user_data, ob, &data_block.id, IDWALK_CB_USER);
279 }
280 }
281}
282
283static void foreach_tex_link(ModifierData *md, Object *ob, TexWalkFunc walk, void *user_data)
284{
285 PointerRNA ptr = RNA_pointer_create_discrete(&ob->id, RNA_Modifier, md);
286 PropertyRNA *prop = RNA_struct_find_property(&ptr, "texture");
287 walk(user_data, ob, md, &ptr, prop);
288}
289
290static bool is_disabled(const Scene * /*scene*/, ModifierData *md, bool /*use_render_params*/)
291{
292 NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
293
294 if (nmd->node_group == nullptr) {
295 return true;
296 }
297
298 return false;
299}
300
302{
303 if (!DEG_is_active(ctx->depsgraph)) {
304 return false;
305 }
306 if ((ctx->flag & MOD_APPLY_ORCO) != 0) {
307 return false;
308 }
309 return true;
310}
311
313{
314 if (!nmd.runtime->cache) {
315 if (nmd.bakes_num == 0) {
316 return;
317 }
318 nmd.runtime->cache = std::make_shared<bake::ModifierCache>();
319 }
320 bake::ModifierCache &modifier_cache = *nmd.runtime->cache;
321 std::lock_guard lock{modifier_cache.mutex};
322
323 Set<int> existing_bake_ids;
324 for (const NodesModifierBake &bake : Span{nmd.bakes, nmd.bakes_num}) {
325 existing_bake_ids.add(bake.id);
326 }
327
328 auto remove_predicate = [&](auto item) { return !existing_bake_ids.contains(item.key); };
329
330 modifier_cache.bake_cache_by_id.remove_if(remove_predicate);
331 modifier_cache.simulation_cache_by_id.remove_if(remove_predicate);
332}
333
335{
336 Map<int, NodesModifierBake *> old_bake_by_id;
338 old_bake_by_id.add(bake.id, &bake);
339 }
340
341 Vector<int> new_bake_ids;
342 if (nmd.node_group && !ID_MISSING(nmd.node_group)) {
343 for (const bNestedNodeRef &ref : nmd.node_group->nested_node_refs_span()) {
344 const bNode *node = nmd.node_group->find_nested_node(ref.id);
345 if (node) {
347 new_bake_ids.append(ref.id);
348 }
349 }
350 else if (old_bake_by_id.contains(ref.id)) {
351 /* Keep baked data in case linked data is missing so that it still exists when the linked
352 * data has been found. */
353 new_bake_ids.append(ref.id);
354 }
355 }
356 }
357
358 NodesModifierBake *new_bake_data = MEM_new_array<NodesModifierBake>(new_bake_ids.size(),
359 __func__);
360 for (const int i : new_bake_ids.index_range()) {
361 const int id = new_bake_ids[i];
362 NodesModifierBake *old_bake = old_bake_by_id.lookup_default(id, nullptr);
363 NodesModifierBake &new_bake = new_bake_data[i];
364 if (old_bake) {
365 new_bake = *old_bake;
366 /* The ownership of this data was moved to `new_bake`. */
367 old_bake->directory = nullptr;
368 old_bake->data_blocks = nullptr;
369 old_bake->data_blocks_num = 0;
370 old_bake->packed = nullptr;
371 }
372 else {
373 new_bake.id = id;
374 new_bake.frame_start = 1;
375 new_bake.frame_end = 100;
377 }
378 }
379
380 for (NodesModifierBake &old_bake : MutableSpan(nmd.bakes, nmd.bakes_num)) {
381 nodes_modifier_bake_destruct(&old_bake, true);
382 }
383 MEM_SAFE_DELETE(nmd.bakes);
384
385 nmd.bakes = new_bake_data;
386 nmd.bakes_num = new_bake_ids.size();
387
389}
390
391static void update_system_properties(Main &bmain, Object &object, NodesModifierData &nmd)
392{
393 if (!nmd.modifier.system_properties) {
395 bke::idprop::create_group("NodesModifierProperties").release();
396 }
397 if (!nmd.node_group || ID_MISSING(nmd.node_group)) {
398 return;
399 }
401 &object.id, RNA_NodesModifierProperties, &nmd);
403}
404
406{
407 update_system_properties(bmain, *object, *nmd);
409 nmd->runtime->usage_cache.reset();
410
412}
413
414NodesModifierBake *NodesModifierData::find_bake(const int id)
415{
416 return const_cast<NodesModifierBake *>(std::as_const(*this).find_bake(id));
417}
418
419const NodesModifierBake *NodesModifierData::find_bake(const int id) const
420{
421 for (const NodesModifierBake &bake : Span{this->bakes, this->bakes_num}) {
422 if (bake.id == id) {
423 return &bake;
424 }
425 }
426 return nullptr;
427}
428
435 const ComputeContext &final_compute_context,
436 const int final_node_id,
437 const NodesModifierData &nmd,
438 nodes::GeoNodesSideEffectNodes &r_side_effect_nodes)
439{
440 if (nmd.node_group == nullptr || ID_MISSING(nmd.node_group)) {
441 return;
442 }
443
444 Vector<const ComputeContext *> compute_context_vec;
445 for (const ComputeContext *c = &final_compute_context; c; c = c->parent()) {
446 compute_context_vec.append(c);
447 }
448 std::reverse(compute_context_vec.begin(), compute_context_vec.end());
449
450 const auto *modifier_compute_context =
451 dynamic_cast<const bke::GeometryNodesModifierComputeContext *>(compute_context_vec[1]);
452 if (modifier_compute_context == nullptr) {
453 return;
454 }
455 if (modifier_compute_context->modifier_uid() != nmd.modifier.persistent_uid) {
456 return;
457 }
458
459 const bNodeTree *current_tree = nmd.node_group;
460 const bke::bNodeTreeZone *current_zone = nullptr;
461
462 /* Write side effect nodes to a new map and only if everything succeeds, move the nodes to the
463 * caller. This is easier than changing r_side_effect_nodes directly and then undoing changes in
464 * case of errors. */
465 nodes::GeoNodesSideEffectNodes local_side_effect_nodes;
466 for (const ComputeContext *compute_context_generic : compute_context_vec.as_span().drop_front(2))
467 {
468 const bke::bNodeTreeZones *current_zones = current_tree->zones();
469 if (current_zones == nullptr) {
470 return;
471 }
472 const nodes::GeometryNodesLazyFunctionGraphInfo *lf_graph_info =
474 if (lf_graph_info == nullptr) {
475 return;
476 }
477 const ComputeContextHash &parent_compute_context_hash =
478 compute_context_generic->parent()->hash();
479 if (const auto *compute_context = dynamic_cast<const bke::SimulationZoneComputeContext *>(
480 compute_context_generic))
481 {
482 const bke::bNodeTreeZone *simulation_zone = current_zones->get_zone_by_node(
483 compute_context->output_node_id());
484 if (simulation_zone == nullptr) {
485 return;
486 }
487 if (simulation_zone->parent_zone != current_zone) {
488 return;
489 }
490 const lf::FunctionNode *lf_zone_node = lf_graph_info->mapping.zone_node_map.lookup_default(
491 *simulation_zone->output_node_id, nullptr);
492 if (lf_zone_node == nullptr) {
493 return;
494 }
495 const lf::FunctionNode *lf_simulation_output_node =
497 *simulation_zone->output_node_id, nullptr);
498 if (lf_simulation_output_node == nullptr) {
499 return;
500 }
501 local_side_effect_nodes.nodes_by_context.add(parent_compute_context_hash, lf_zone_node);
502 /* By making the simulation output node a side-effect-node, we can ensure that the simulation
503 * runs when it contains an active viewer. */
504 local_side_effect_nodes.nodes_by_context.add(compute_context_generic->hash(),
505 lf_simulation_output_node);
506
507 current_zone = simulation_zone;
508 }
509 else if (const auto *compute_context = dynamic_cast<const bke::RepeatZoneComputeContext *>(
510 compute_context_generic))
511 {
512 const bke::bNodeTreeZone *repeat_zone = current_zones->get_zone_by_node(
513 compute_context->output_node_id());
514 if (repeat_zone == nullptr) {
515 return;
516 }
517 if (repeat_zone->parent_zone != current_zone) {
518 return;
519 }
520 const lf::FunctionNode *lf_zone_node = lf_graph_info->mapping.zone_node_map.lookup_default(
521 *repeat_zone->output_node_id, nullptr);
522 if (lf_zone_node == nullptr) {
523 return;
524 }
525 local_side_effect_nodes.nodes_by_context.add(parent_compute_context_hash, lf_zone_node);
526 local_side_effect_nodes.iterations_by_iteration_zone.add(
527 {parent_compute_context_hash, compute_context->output_node_id()},
528 compute_context->iteration());
529 current_zone = repeat_zone;
530 }
531 else if (const auto *compute_context =
533 compute_context_generic))
534 {
535 const bke::bNodeTreeZone *foreach_zone = current_zones->get_zone_by_node(
536 compute_context->output_node_id());
537 if (foreach_zone == nullptr) {
538 return;
539 }
540 if (foreach_zone->parent_zone != current_zone) {
541 return;
542 }
543 const lf::FunctionNode *lf_zone_node = lf_graph_info->mapping.zone_node_map.lookup_default(
544 *foreach_zone->output_node_id, nullptr);
545 if (lf_zone_node == nullptr) {
546 return;
547 }
548 local_side_effect_nodes.nodes_by_context.add(parent_compute_context_hash, lf_zone_node);
549 local_side_effect_nodes.iterations_by_iteration_zone.add(
550 {parent_compute_context_hash, compute_context->output_node_id()},
551 compute_context->index());
552 current_zone = foreach_zone;
553 }
554 else if (const auto *compute_context = dynamic_cast<const bke::GroupNodeComputeContext *>(
555 compute_context_generic))
556 {
557 const bNode *group_node = current_tree->node_by_id(compute_context->node_id());
558 if (group_node == nullptr) {
559 return;
560 }
561 if (group_node->id == nullptr) {
562 return;
563 }
564 if (group_node->is_muted()) {
565 return;
566 }
567 if (current_zone != current_zones->get_zone_by_node(group_node->identifier)) {
568 return;
569 }
570 const lf::FunctionNode *lf_group_node = lf_graph_info->mapping.group_node_map.lookup_default(
571 group_node->identifier, nullptr);
572 if (lf_group_node == nullptr) {
573 return;
574 }
575 local_side_effect_nodes.nodes_by_context.add(parent_compute_context_hash, lf_group_node);
576 current_tree = reinterpret_cast<const bNodeTree *>(group_node->id);
577 current_zone = nullptr;
578 }
579 else if (const auto *compute_context =
580 dynamic_cast<const bke::EvaluateClosureComputeContext *>(compute_context_generic))
581 {
582 const bNode *evaluate_node = current_tree->node_by_id(compute_context->node_id());
583 if (!evaluate_node) {
584 return;
585 }
586 if (evaluate_node->is_muted()) {
587 return;
588 }
589 if (current_zone != current_zones->get_zone_by_node(evaluate_node->identifier)) {
590 return;
591 }
592 const std::optional<nodes::ClosureSourceLocation> &source_location =
593 compute_context->closure_source_location();
594 if (!source_location) {
595 return;
596 }
597 if (!source_location->tree->zones()) {
598 return;
599 }
600 const lf::FunctionNode *lf_evaluate_node =
602 evaluate_node->identifier, nullptr);
603 if (!lf_evaluate_node) {
604 return;
605 }
606 /* The tree may sometimes be original and sometimes evaluated, depending on the source of the
607 * compute context. */
608 const bNodeTree *eval_closure_tree = DEG_is_evaluated(source_location->tree) ?
609 source_location->tree :
610 reinterpret_cast<const bNodeTree *>(
612 ctx.depsgraph, &source_location->tree->id));
613 const bNode *closure_output_node = eval_closure_tree->node_by_id(
614 source_location->closure_output_node_id);
615 if (!closure_output_node) {
616 return;
617 }
618 local_side_effect_nodes.nodes_by_context.add(parent_compute_context_hash, lf_evaluate_node);
619 current_tree = eval_closure_tree;
620 current_zone = eval_closure_tree->zones()->get_zone_by_node(closure_output_node->identifier);
621 }
622 else {
623 return;
624 }
625 }
626 const bNode *final_node = current_tree->node_by_id(final_node_id);
627 if (final_node == nullptr) {
628 return;
629 }
630 const nodes::GeometryNodesLazyFunctionGraphInfo *lf_graph_info =
632 if (lf_graph_info == nullptr) {
633 return;
634 }
635 const bke::bNodeTreeZones *tree_zones = current_tree->zones();
636 if (tree_zones == nullptr) {
637 return;
638 }
639 if (tree_zones->get_zone_by_node(final_node_id) != current_zone) {
640 return;
641 }
642 const lf::FunctionNode *lf_node =
643 lf_graph_info->mapping.possible_side_effect_node_map.lookup_default(final_node_id, nullptr);
644 if (lf_node == nullptr) {
645 return;
646 }
647 local_side_effect_nodes.nodes_by_context.add(final_compute_context.hash(), lf_node);
648
649 /* Successfully found all side effect nodes for the viewer path. */
650 for (const auto item : local_side_effect_nodes.nodes_by_context.items()) {
651 r_side_effect_nodes.nodes_by_context.add_multiple(item.key, item.value);
652 }
653 for (const auto item : local_side_effect_nodes.iterations_by_iteration_zone.items()) {
654 r_side_effect_nodes.iterations_by_iteration_zone.add_multiple(item.key, item.value);
655 }
656}
657
659 const ViewerPath &viewer_path,
660 const NodesModifierData &nmd,
661 const ModifierEvalContext &ctx,
662 nodes::GeoNodesSideEffectNodes &r_side_effect_nodes)
663{
664 const std::optional<ed::viewer_path::ViewerPathForGeometryNodesViewer> parsed_path =
666 if (!parsed_path.has_value()) {
667 return;
668 }
669 if (parsed_path->object != DEG_get_original(ctx.object)) {
670 return;
671 }
672 if (parsed_path->modifier_uid != nmd.modifier.persistent_uid) {
673 return;
674 }
675
676 bke::ComputeContextCache compute_context_cache;
677 const ComputeContext *object_context = &compute_context_cache.for_data_block(
678 nullptr, parsed_path->object->id);
679 const ComputeContext *current = &compute_context_cache.for_geometry_nodes_modifier(
680 object_context, nmd);
681 for (const ViewerPathElem *elem : parsed_path->node_path) {
683 *elem, compute_context_cache, current);
684 if (!current) {
685 return;
686 }
687 }
688
689 try_add_side_effect_node(ctx, *current, parsed_path->viewer_node_id, nmd, r_side_effect_nodes);
690}
691
693 const ModifierEvalContext &ctx,
694 const NodesModifierData &nmd,
695 const int root_nested_node_id,
696 nodes::GeoNodesSideEffectNodes &r_side_effect_nodes)
697{
698 bke::ComputeContextCache compute_context_cache;
699 const ComputeContext *object_context = &compute_context_cache.for_data_block(nullptr,
700 ctx.object->id);
701 const ComputeContext *compute_context = &compute_context_cache.for_geometry_nodes_modifier(
702 object_context, nmd);
703
704 int nested_node_id = root_nested_node_id;
705 const bNodeTree *tree = nmd.node_group;
706 while (true) {
707 const bNestedNodeRef *ref = tree->find_nested_node_ref(nested_node_id);
708 if (!ref) {
709 return;
710 }
711 const bNode *node = tree->node_by_id(ref->path.node_id);
712 if (!node) {
713 return;
714 }
715 const bke::bNodeTreeZones *zones = tree->zones();
716 if (!zones) {
717 return;
718 }
719 if (zones->get_zone_by_node(node->identifier) != nullptr) {
720 /* Only top level nodes are allowed here. */
721 return;
722 }
723 if (node->is_group()) {
724 if (!node->id) {
725 return;
726 }
727 compute_context = &compute_context_cache.for_group_node(
728 compute_context, node->identifier, tree);
729 tree = reinterpret_cast<const bNodeTree *>(node->id);
730 nested_node_id = ref->path.id_in_node;
731 }
732 else {
733 try_add_side_effect_node(ctx, *compute_context, ref->path.node_id, nmd, r_side_effect_nodes);
734 return;
735 }
736 }
737}
738
744 const ModifierEvalContext &ctx,
745 nodes::GeoNodesSideEffectNodes &r_side_effect_nodes)
746{
747 if (!nmd.runtime->cache) {
748 return;
749 }
750 if (!DEG_is_active(ctx.depsgraph)) {
751 /* Only the active depsgraph can bake. */
752 return;
753 }
754 bake::ModifierCache &modifier_cache = *nmd.runtime->cache;
755 for (const bNestedNodeRef &ref : nmd.node_group->nested_node_refs_span()) {
756 if (!modifier_cache.requested_bakes.contains(ref.id)) {
757 continue;
758 }
759 find_side_effect_nodes_for_nested_node(ctx, nmd, ref.id, r_side_effect_nodes);
760 }
761}
762
764 const NodesModifierData &nmd,
765 const ModifierEvalContext &ctx,
766 const wmWindowManager &wm,
767 nodes::GeoNodesSideEffectNodes &r_side_effect_nodes,
768 Set<ComputeContextHash> &r_verbose_log_contexts)
769{
770 Object *object_orig = DEG_get_original(ctx.object);
771 const NodesModifierData &nmd_orig = *reinterpret_cast<const NodesModifierData *>(
772 BKE_modifier_get_original(ctx.object, const_cast<ModifierData *>(&nmd.modifier)));
773 bke::ComputeContextCache compute_context_cache;
775 *object_orig,
776 nmd_orig,
777 wm,
778 compute_context_cache,
779 [&](const ComputeContext &compute_context,
780 const bNode &gizmo_node,
781 const bNodeSocket &gizmo_socket) {
783 ctx, compute_context, gizmo_node.identifier, nmd, r_side_effect_nodes);
784 r_verbose_log_contexts.add(compute_context.hash());
785
787 compute_context, gizmo_node, gizmo_socket, [&](const ComputeContext &node_context) {
788 /* Make sure that all intermediate sockets are logged. This is necessary to be able
789 * to evaluate the nodes in reverse for the gizmo. */
790 r_verbose_log_contexts.add(node_context.hash());
791 });
792 });
793}
794
796 const ModifierEvalContext &ctx,
797 nodes::GeoNodesSideEffectNodes &r_side_effect_nodes,
798 Set<ComputeContextHash> &r_verbose_log_contexts)
799{
800 Main *bmain = DEG_get_bmain(ctx.depsgraph);
801 wmWindowManager *wm = bmain->wm.first();
802 if (wm == nullptr) {
803 return;
804 }
805 for (const wmWindow &window : wm->windows) {
807 const WorkSpace *workspace = BKE_workspace_active_get(window.workspace_hook);
808 find_side_effect_nodes_for_viewer_path(workspace->viewer_path, nmd, ctx, r_side_effect_nodes);
809 for (const ScrArea &area : screen->areabase) {
810 const SpaceLink *sl = area.spacedata.first_as<SpaceLink>();
811 if (sl == nullptr) {
812 continue;
813 }
814 if (sl->spacetype == SPACE_SPREADSHEET) {
815 const SpaceSpreadsheet &sspreadsheet = *reinterpret_cast<const SpaceSpreadsheet *>(sl);
817 sspreadsheet.geometry_id.viewer_path, nmd, ctx, r_side_effect_nodes);
818 }
819 if (sl->spacetype == SPACE_VIEW3D) {
820 const View3D &v3d = *reinterpret_cast<const View3D *>(sl);
821 find_side_effect_nodes_for_viewer_path(v3d.viewer_path, nmd, ctx, r_side_effect_nodes);
822 }
823 }
824 }
825
826 find_side_effect_nodes_for_baking(nmd, ctx, r_side_effect_nodes);
828 nmd, ctx, *wm, r_side_effect_nodes, r_verbose_log_contexts);
829}
830
832 const ModifierEvalContext &ctx,
833 Set<ComputeContextHash> &r_socket_log_contexts)
834{
835 Main *bmain = DEG_get_bmain(ctx.depsgraph);
836 wmWindowManager *wm = bmain->wm.first();
837 if (wm == nullptr) {
838 return;
839 }
840 bke::ComputeContextCache compute_context_cache;
841 for (const wmWindow &window : wm->windows) {
843 for (const ScrArea &area : screen->areabase) {
844 const SpaceLink *sl = area.spacedata.first_as<SpaceLink>();
845 if (sl == nullptr) [[unlikely]] {
846 continue;
847 }
848 if (sl->spacetype == SPACE_NODE) {
849 const SpaceNode &snode = *reinterpret_cast<const SpaceNode *>(sl);
850 if (snode.edittree == nullptr || snode.edittree->type != NTREE_GEOMETRY) {
851 continue;
852 }
854 continue;
855 }
858 snode, compute_context_cache);
859 for (const ComputeContextHash &hash : hash_by_zone.values()) {
860 r_socket_log_contexts.add(hash);
861 }
862 }
863 }
864 }
865}
866
869 Mutex mutex_;
870
871 public:
874
876 {
877 if (ID *id = this->old_mappings.lookup_default(key, nullptr)) {
878 return id;
879 }
880 if (this->old_mappings.contains(key)) {
881 /* Don't allow overwriting old mappings. */
882 return nullptr;
883 }
884 std::lock_guard lock{mutex_};
885 return this->new_mappings.lookup_or_add(key, nullptr);
886 }
887
888 void try_add(ID &id) override
889 {
890 bake::BakeDataBlockID key{id};
891 if (this->old_mappings.contains(key)) {
892 return;
893 }
894 std::lock_guard lock{mutex_};
895 this->new_mappings.add_overwrite(std::move(key), &id);
896 }
897
898 private:
899 ID *lookup_in_map(Map<bake::BakeDataBlockID, ID *> &map,
900 const bake::BakeDataBlockID &key,
901 const std::optional<ID_Type> &type)
902 {
903 ID *id = map.lookup_default(key, nullptr);
904 if (!id) {
905 return nullptr;
906 }
907 if (type && GS(id->name) != *type) {
908 return nullptr;
909 }
910 return id;
911 }
912};
913
914namespace sim_input = nodes::sim_input;
915namespace sim_output = nodes::sim_output;
916
918 std::optional<int> prev;
919 std::optional<int> current;
920 std::optional<int> next;
921};
922
924 const Span<std::unique_ptr<bake::FrameCache>> frame_caches, const SubFrame frame)
925{
926 BakeFrameIndices frame_indices;
927 if (!frame_caches.is_empty()) {
928 const int first_future_frame_index = binary_search::first_if(
929 frame_caches,
930 [&](const std::unique_ptr<bake::FrameCache> &value) { return value->frame > frame; });
931 frame_indices.next = (first_future_frame_index == frame_caches.size()) ?
932 std::nullopt :
933 std::optional<int>(first_future_frame_index);
934 if (first_future_frame_index > 0) {
935 const int index = first_future_frame_index - 1;
936 if (frame_caches[index]->frame < frame) {
937 frame_indices.prev = index;
938 }
939 else {
940 BLI_assert(frame_caches[index]->frame == frame);
941 frame_indices.current = index;
942 if (index > 0) {
943 frame_indices.prev = index - 1;
944 }
945 }
946 }
947 }
948 return frame_indices;
949}
950
951static void ensure_bake_loaded(bake::NodeBakeCache &bake_cache, bake::FrameCache &frame_cache)
952{
953 if (!frame_cache.values.is_empty()) {
954 return;
955 }
956 if (!frame_cache.meta_data_source.has_value()) {
957 return;
958 }
959 if (bake_cache.memory_blob_reader) {
960 if (const auto *meta_buffer = std::get_if<Span<std::byte>>(&*frame_cache.meta_data_source)) {
961 const std::string meta_str{reinterpret_cast<const char *>(meta_buffer->data()),
962 size_t(meta_buffer->size())};
963 std::istringstream meta_stream{meta_str};
964 std::optional<bake::BakeValues> bake_state = bake::deserialize_bake(
965 meta_stream, *bake_cache.memory_blob_reader, *bake_cache.blob_sharing);
966 if (!bake_state.has_value()) {
967 return;
968 }
969 frame_cache.values = std::move(*bake_state);
970 return;
971 }
972 }
973 if (!bake_cache.blobs_dir) {
974 return;
975 }
976 const auto *meta_path = std::get_if<std::string>(&*frame_cache.meta_data_source);
977 if (!meta_path) {
978 return;
979 }
980 bake::DiskBlobReader blob_reader{*bake_cache.blobs_dir};
981 fstream meta_file{*meta_path};
982 std::optional<bake::BakeValues> bake_values = bake::deserialize_bake(
983 meta_file, blob_reader, *bake_cache.blob_sharing);
984 if (!bake_values.has_value()) {
985 return;
986 }
987 frame_cache.values = std::move(*bake_values);
988}
989
991 bake::NodeBakeCache &bake_cache,
992 const Main &bmain,
993 const Object &object,
994 const NodesModifierData &nmd,
995 const int id)
996{
997 if (bake.packed) {
998 if (bake.packed->meta_files_num == 0) {
999 return false;
1000 }
1001 bake_cache.reset();
1003 for (const NodesModifierBakeFile &meta_file :
1004 Span{bake.packed->meta_files, bake.packed->meta_files_num})
1005 {
1006 const std::optional<SubFrame> frame = bake::file_name_to_frame(meta_file.name);
1007 if (!frame) {
1008 return false;
1009 }
1010 if (!file_by_frame.add(*frame, &meta_file)) {
1011 /* Can only have on file per (sub)frame. */
1012 return false;
1013 }
1014 }
1015
1016 /* Make sure frames processed in the right order. */
1017 Vector<SubFrame> frames;
1018 frames.extend(file_by_frame.keys().begin(), file_by_frame.keys().end());
1019 std::ranges::sort(frames);
1020
1021 for (const SubFrame &frame : frames) {
1022 const NodesModifierBakeFile &meta_file = *file_by_frame.lookup(frame);
1023 auto frame_cache = std::make_unique<bake::FrameCache>();
1024 frame_cache->frame = frame;
1025 frame_cache->meta_data_source = meta_file.data();
1026 bake_cache.frames.append(std::move(frame_cache));
1027 }
1028
1029 bake_cache.memory_blob_reader = std::make_unique<bake::MemoryBlobReader>();
1030 for (const NodesModifierBakeFile &blob_file :
1031 Span{bake.packed->blob_files, bake.packed->blob_files_num})
1032 {
1033 bake_cache.memory_blob_reader->add(blob_file.name, blob_file.data());
1034 }
1035 bake_cache.blob_sharing = std::make_unique<bake::BlobReadSharing>();
1036 return true;
1037 }
1038
1039 std::optional<bake::BakePath> bake_path = bake::get_node_bake_path(bmain, object, nmd, id);
1040 if (!bake_path) {
1041 return false;
1042 }
1043 Vector<bake::MetaFile> meta_files = bake::find_sorted_meta_files(bake_path->meta_dir);
1044 if (meta_files.is_empty()) {
1045 return false;
1046 }
1047 bake_cache.reset();
1048 for (const bake::MetaFile &meta_file : meta_files) {
1049 auto frame_cache = std::make_unique<bake::FrameCache>();
1050 frame_cache->frame = meta_file.frame;
1051 frame_cache->meta_data_source = meta_file.path;
1052 bake_cache.frames.append(std::move(frame_cache));
1053 }
1054 bake_cache.blobs_dir = bake_path->blobs_dir;
1055 bake_cache.blob_sharing = std::make_unique<bake::BlobReadSharing>();
1056 return true;
1057}
1058
1060 private:
1061 static constexpr float max_delta_frames = 1.0f;
1062
1063 const NodesModifierData &nmd_;
1064 const ModifierEvalContext &ctx_;
1065 const Main *bmain_;
1066 const Scene *scene_;
1067 SubFrame current_frame_;
1068 bool use_frame_cache_;
1069 bool depsgraph_is_active_;
1070 bake::ModifierCache *modifier_cache_;
1071 float fps_;
1072 bool has_invalid_simulation_ = false;
1073
1074 public:
1079
1081
1083 : nmd_(nmd), ctx_(ctx)
1084 {
1085 const Depsgraph *depsgraph = ctx_.depsgraph;
1086 bmain_ = DEG_get_bmain(depsgraph);
1087 current_frame_ = DEG_get_ctime(depsgraph);
1088 const Scene *scene = DEG_get_input_scene(depsgraph);
1089 scene_ = scene;
1090 use_frame_cache_ = ctx_.object->flag & OB_FLAG_USE_SIMULATION_CACHE;
1091 depsgraph_is_active_ = DEG_is_active(depsgraph);
1092 modifier_cache_ = nmd.runtime->cache.get();
1093 fps_ = scene->frames_per_second();
1094
1095 if (!modifier_cache_) {
1096 return;
1097 }
1098 std::lock_guard lock{modifier_cache_->mutex};
1099 if (depsgraph_is_active_) {
1100 /* Invalidate data on user edits. */
1102 for (std::unique_ptr<bake::SimulationNodeCache> &node_cache :
1103 modifier_cache_->simulation_cache_by_id.values())
1104 {
1105 if (node_cache->cache_status != bake::CacheStatus::Baked) {
1106 node_cache->cache_status = bake::CacheStatus::Invalid;
1107 if (!node_cache->bake.frames.is_empty()) {
1108 if (node_cache->bake.frames.last()->frame == current_frame_) {
1109 /* Remove the last (which is the current) cached frame so that it is simulated
1110 * again. */
1111 node_cache->bake.frames.pop_last();
1112 }
1113 }
1114 }
1115 }
1116 }
1118 }
1119 for (const std::unique_ptr<bake::SimulationNodeCache> &node_cache_ptr :
1120 modifier_cache_->simulation_cache_by_id.values())
1121 {
1122 const bake::SimulationNodeCache &node_cache = *node_cache_ptr;
1123 if (node_cache.cache_status == bake::CacheStatus::Invalid) {
1124 has_invalid_simulation_ = true;
1125 break;
1126 }
1127 }
1128 }
1129
1131 {
1132 for (auto item : modifier_cache_->simulation_cache_by_id.items()) {
1133 const int id = item.key;
1134 bake::SimulationNodeCache &node_cache = *item.value;
1135 if (node_cache.cache_status != bake::CacheStatus::Invalid) {
1136 continue;
1137 }
1138 const std::optional<IndexRange> sim_frame_range = bake::get_node_bake_frame_range(
1139 *scene_, *ctx_.object, nmd_, id);
1140 if (!sim_frame_range.has_value()) {
1141 continue;
1142 }
1143 const SubFrame start_frame{int(sim_frame_range->start())};
1144 if (current_frame_ <= start_frame) {
1145 node_cache.reset();
1146 }
1147 if (!node_cache.bake.frames.is_empty() &&
1148 current_frame_ < node_cache.bake.frames.first()->frame)
1149 {
1150 node_cache.reset();
1151 }
1152 }
1153 }
1154
1155 nodes::SimulationZoneBehavior *get(const int zone_id) const override
1156 {
1157 if (!modifier_cache_) {
1158 return nullptr;
1159 }
1160 std::lock_guard lock{modifier_cache_->mutex};
1161 return &this->data_by_zone_id
1162 .lookup_or_add_cb(zone_id,
1163 [&]() {
1164 auto data = std::make_unique<DataPerZone>();
1165 data->behavior.data_block_map = &data->data_block_map;
1167 zone_id, data->behavior, data->data_block_map);
1168 return data;
1169 })
1170 ->behavior;
1171 }
1172
1173 void init_simulation_info(const int zone_id,
1174 nodes::SimulationZoneBehavior &zone_behavior,
1175 NodesModifierBakeDataBlockMap &data_block_map) const
1176 {
1177 bake::SimulationNodeCache &node_cache =
1178 *modifier_cache_->simulation_cache_by_id.lookup_or_add_cb(
1179 zone_id, []() { return std::make_unique<bake::SimulationNodeCache>(); });
1180 const NodesModifierBake &bake = *nmd_.find_bake(zone_id);
1181 const IndexRange sim_frame_range = *bake::get_node_bake_frame_range(
1182 *scene_, *ctx_.object, nmd_, zone_id);
1183 const SubFrame sim_start_frame{int(sim_frame_range.first())};
1184 const SubFrame sim_end_frame{int(sim_frame_range.last())};
1185
1186 if (!modifier_cache_->requested_bakes.contains(zone_id)) {
1187 /* Try load baked data. */
1188 if (!node_cache.bake.failed_finding_bake) {
1189 if (node_cache.cache_status != bake::CacheStatus::Baked) {
1190 if (try_find_baked_data(bake, node_cache.bake, *bmain_, *ctx_.object, nmd_, zone_id)) {
1191 node_cache.cache_status = bake::CacheStatus::Baked;
1192 }
1193 else {
1194 node_cache.bake.failed_finding_bake = true;
1195 }
1196 }
1197 }
1198 }
1199
1200 /* If there are no baked frames, we don't need keep track of the data-blocks. */
1201 if (!node_cache.bake.frames.is_empty() || node_cache.prev_cache.has_value()) {
1202 for (const NodesModifierDataBlock &data_block : Span{bake.data_blocks, bake.data_blocks_num})
1203 {
1204 data_block_map.old_mappings.add(data_block, data_block.id);
1205 }
1206 }
1207
1208 const BakeFrameIndices frame_indices = get_bake_frame_indices(node_cache.bake.frames,
1209 current_frame_);
1210 if (node_cache.cache_status == bake::CacheStatus::Baked) {
1211 this->read_from_cache(frame_indices, node_cache, zone_behavior);
1212 return;
1213 }
1214 if (use_frame_cache_) {
1215 /* If the depsgraph is active, we allow creating new simulation states. Otherwise, the access
1216 * is read-only. */
1217 if (depsgraph_is_active_) {
1218 if (node_cache.bake.frames.is_empty()) {
1219 if (current_frame_ < sim_start_frame || current_frame_ > sim_end_frame) {
1220 /* Outside of simulation frame range, so ignore the simulation if there is no cache. */
1221 this->input_pass_through(zone_behavior);
1222 this->output_pass_through(zone_behavior);
1223 return;
1224 }
1225 /* Initialize the simulation. */
1226 if (current_frame_ > sim_start_frame || has_invalid_simulation_) {
1227 node_cache.cache_status = bake::CacheStatus::Invalid;
1228 }
1229 this->input_pass_through(zone_behavior);
1230 this->output_store_frame_cache(node_cache, zone_behavior);
1231 return;
1232 }
1233 if (frame_indices.prev && !frame_indices.current && !frame_indices.next &&
1234 current_frame_ <= sim_end_frame)
1235 {
1236 /* Read the previous frame's data and store the newly computed simulation state. */
1237 auto &use_cache_info = zone_behavior.input.emplace<sim_input::UseCache>();
1238 const bake::FrameCache &prev_frame_cache = *node_cache.bake.frames[*frame_indices.prev];
1239 const float real_delta_frames = float(current_frame_) - float(prev_frame_cache.frame);
1240 if (real_delta_frames != 1) {
1241 node_cache.cache_status = bake::CacheStatus::Invalid;
1242 }
1243 const float delta_frames = std::min(max_delta_frames, real_delta_frames);
1244 use_cache_info.delta_time = delta_frames / fps_;
1245 use_cache_info.values = prev_frame_cache.values;
1246 this->output_store_frame_cache(node_cache, zone_behavior);
1247 return;
1248 }
1249 }
1250 this->read_from_cache(frame_indices, node_cache, zone_behavior);
1251 return;
1252 }
1253
1254 /* When there is no per-frame cache, check if there is a previous state. */
1255 if (node_cache.prev_cache) {
1256 if (node_cache.prev_cache->frame < current_frame_) {
1257 /* Do a simulation step. */
1258 const float delta_frames = std::min(
1259 max_delta_frames, float(current_frame_) - float(node_cache.prev_cache->frame));
1260 auto &use_cache_info = zone_behavior.input.emplace<sim_input::UseCache>();
1261 use_cache_info.delta_time = delta_frames / fps_;
1262 use_cache_info.values = std::move(node_cache.prev_cache->values);
1263 this->store_as_prev_items(node_cache, zone_behavior);
1264 return;
1265 }
1266 if (node_cache.prev_cache->frame == current_frame_) {
1267 /* Just read from the previous state if the frame has not changed. */
1268 auto &use_cache_info = zone_behavior.input.emplace<sim_input::UseCache>();
1269 use_cache_info.delta_time = 0.0f;
1270 use_cache_info.values = node_cache.prev_cache->values;
1271 auto &read_single_info = zone_behavior.output.emplace<sim_output::ReadSingle>();
1272 read_single_info.values = node_cache.prev_cache->values;
1273 return;
1274 }
1275 if (!depsgraph_is_active_) {
1276 /* There is no previous state, and it's not possible to initialize the simulation because
1277 * the depsgraph is not active. */
1278 zone_behavior.input.emplace<sim_input::PassThrough>();
1279 zone_behavior.output.emplace<sim_output::PassThrough>();
1280 return;
1281 }
1282 /* Reset the simulation when the scene time moved backwards. */
1283 node_cache.prev_cache.reset();
1284 }
1285 zone_behavior.input.emplace<sim_input::PassThrough>();
1286 if (depsgraph_is_active_) {
1287 /* Initialize the simulation. */
1288 this->store_as_prev_items(node_cache, zone_behavior);
1289 }
1290 else {
1291 zone_behavior.output.emplace<sim_output::PassThrough>();
1292 }
1293 }
1294
1296 {
1297 zone_behavior.input.emplace<sim_input::PassThrough>();
1298 }
1299
1301 {
1302 zone_behavior.output.emplace<sim_output::PassThrough>();
1303 }
1304
1306 nodes::SimulationZoneBehavior &zone_behavior) const
1307 {
1308 auto &store_new_state_info = zone_behavior.output.emplace<sim_output::StoreNewState>();
1309 store_new_state_info.store_fn =
1310 [simulation_cache = modifier_cache_,
1311 node_cache = &node_cache,
1312 current_frame = current_frame_](bke::bake::BakeValues values) {
1313 std::lock_guard lock{simulation_cache->mutex};
1314 auto frame_cache = std::make_unique<bake::FrameCache>();
1315 frame_cache->frame = current_frame;
1316 frame_cache->values = std::move(values);
1317 node_cache->bake.frames.append(std::move(frame_cache));
1318 };
1319 }
1320
1322 nodes::SimulationZoneBehavior &zone_behavior) const
1323 {
1324 auto &store_new_state_info = zone_behavior.output.emplace<sim_output::StoreNewState>();
1325 store_new_state_info.store_fn =
1326 [simulation_cache = modifier_cache_,
1327 node_cache = &node_cache,
1328 current_frame = current_frame_](bke::bake::BakeValues values) {
1329 std::lock_guard lock{simulation_cache->mutex};
1330 if (!node_cache->prev_cache) {
1331 node_cache->prev_cache.emplace();
1332 }
1333 node_cache->prev_cache->values = std::move(values);
1334 node_cache->prev_cache->frame = current_frame;
1335 };
1336 }
1337
1338 void read_from_cache(const BakeFrameIndices &frame_indices,
1339 bake::SimulationNodeCache &node_cache,
1340 nodes::SimulationZoneBehavior &zone_behavior) const
1341 {
1342 if (frame_indices.prev) {
1343 auto &use_cache_info = zone_behavior.input.emplace<sim_input::UseCache>();
1344 bake::FrameCache &frame_cache = *node_cache.bake.frames[*frame_indices.prev];
1345 const float delta_frames = std::min(max_delta_frames,
1346 float(current_frame_) - float(frame_cache.frame));
1347 use_cache_info.delta_time = delta_frames / fps_;
1348 use_cache_info.values = frame_cache.values;
1349 }
1350 else {
1351 zone_behavior.input.emplace<sim_input::PassThrough>();
1352 }
1353 if (frame_indices.current) {
1354 this->read_single(*frame_indices.current, node_cache, zone_behavior);
1355 }
1356 else if (frame_indices.next) {
1357 if (frame_indices.prev) {
1358 this->read_interpolated(
1359 *frame_indices.prev, *frame_indices.next, node_cache, zone_behavior);
1360 }
1361 else {
1362 this->output_pass_through(zone_behavior);
1363 }
1364 }
1365 else if (frame_indices.prev) {
1366 this->read_single(*frame_indices.prev, node_cache, zone_behavior);
1367 }
1368 else {
1369 this->output_pass_through(zone_behavior);
1370 }
1371 }
1372
1373 void read_single(const int frame_index,
1374 bake::SimulationNodeCache &node_cache,
1375 nodes::SimulationZoneBehavior &zone_behavior) const
1376 {
1377 bake::FrameCache &frame_cache = *node_cache.bake.frames[frame_index];
1378 ensure_bake_loaded(node_cache.bake, frame_cache);
1379 auto &read_single_info = zone_behavior.output.emplace<sim_output::ReadSingle>();
1380 read_single_info.values = frame_cache.values;
1381 }
1382
1383 void read_interpolated(const int prev_frame_index,
1384 const int next_frame_index,
1385 bake::SimulationNodeCache &node_cache,
1386 nodes::SimulationZoneBehavior &zone_behavior) const
1387 {
1388 bake::FrameCache &prev_frame_cache = *node_cache.bake.frames[prev_frame_index];
1389 bake::FrameCache &next_frame_cache = *node_cache.bake.frames[next_frame_index];
1390 ensure_bake_loaded(node_cache.bake, prev_frame_cache);
1391 ensure_bake_loaded(node_cache.bake, next_frame_cache);
1392 auto &read_interpolated_info = zone_behavior.output.emplace<sim_output::ReadInterpolated>();
1393 read_interpolated_info.mix_factor = (float(current_frame_) - float(prev_frame_cache.frame)) /
1394 (float(next_frame_cache.frame) -
1395 float(prev_frame_cache.frame));
1396 read_interpolated_info.prev_values = prev_frame_cache.values;
1397 read_interpolated_info.next_values = next_frame_cache.values;
1398 }
1399};
1400
1402 private:
1403 const NodesModifierData &nmd_;
1404 const ModifierEvalContext &ctx_;
1405 Main *bmain_;
1406 SubFrame current_frame_;
1407 bake::ModifierCache *modifier_cache_;
1408 bool depsgraph_is_active_;
1409
1410 public:
1415
1417
1419 : nmd_(nmd), ctx_(ctx)
1420 {
1421 const Depsgraph *depsgraph = ctx_.depsgraph;
1422 current_frame_ = DEG_get_ctime(depsgraph);
1423 modifier_cache_ = nmd.runtime->cache.get();
1424 depsgraph_is_active_ = DEG_is_active(depsgraph);
1425 bmain_ = DEG_get_bmain(depsgraph);
1426 }
1427
1428 nodes::BakeNodeBehavior *get(const int id) const override
1429 {
1430 if (!modifier_cache_) {
1431 return nullptr;
1432 }
1433 std::lock_guard lock{modifier_cache_->mutex};
1434 return &this->data_by_node_id
1435 .lookup_or_add_cb(id,
1436 [&]() {
1437 auto data = std::make_unique<DataPerNode>();
1438 data->behavior.data_block_map = &data->data_block_map;
1439 this->init_bake_behavior(
1440 id, data->behavior, data->data_block_map);
1441 return data;
1442 })
1443 ->behavior;
1444 return nullptr;
1445 }
1446
1447 private:
1448 void init_bake_behavior(const int id,
1449 nodes::BakeNodeBehavior &behavior,
1450 NodesModifierBakeDataBlockMap &data_block_map) const
1451 {
1452 bake::BakeNodeCache &node_cache = *modifier_cache_->bake_cache_by_id.lookup_or_add_cb(
1453 id, []() { return std::make_unique<bake::BakeNodeCache>(); });
1454 const NodesModifierBake &bake = *nmd_.find_bake(id);
1455
1456 for (const NodesModifierDataBlock &data_block : Span{bake.data_blocks, bake.data_blocks_num}) {
1457 data_block_map.old_mappings.add(data_block, data_block.id);
1458 }
1459
1460 if (depsgraph_is_active_) {
1461 if (modifier_cache_->requested_bakes.contains(id)) {
1462 /* This node is baked during the current evaluation. */
1463 auto &store_info = behavior.behavior.emplace<sim_output::StoreNewState>();
1464 store_info.store_fn = [modifier_cache = modifier_cache_,
1465 node_cache = &node_cache,
1466 current_frame = current_frame_](bake::BakeValues values) {
1467 std::lock_guard lock{modifier_cache->mutex};
1468 auto frame_cache = std::make_unique<bake::FrameCache>();
1469 frame_cache->frame = current_frame;
1470 frame_cache->values = std::move(values);
1471 auto &frames = node_cache->bake.frames;
1472 const int insert_index = binary_search::first_if(
1473 frames, [&](const std::unique_ptr<bake::FrameCache> &frame_cache) {
1474 return frame_cache->frame > current_frame;
1475 });
1476 frames.insert(insert_index, std::move(frame_cache));
1477 };
1478 return;
1479 }
1480 }
1481
1482 /* Try load baked data. */
1483 if (node_cache.bake.frames.is_empty()) {
1484 if (!node_cache.bake.failed_finding_bake) {
1485 if (!try_find_baked_data(bake, node_cache.bake, *bmain_, *ctx_.object, nmd_, id)) {
1486 node_cache.bake.failed_finding_bake = true;
1487 }
1488 }
1489 }
1490
1491 if (node_cache.bake.frames.is_empty()) {
1492 behavior.behavior.emplace<sim_output::PassThrough>();
1493 return;
1494 }
1495 const BakeFrameIndices frame_indices = get_bake_frame_indices(node_cache.bake.frames,
1496 current_frame_);
1497 if (frame_indices.current) {
1498 this->read_single(*frame_indices.current, node_cache, behavior);
1499 return;
1500 }
1501 if (frame_indices.prev && frame_indices.next) {
1502 this->read_interpolated(*frame_indices.prev, *frame_indices.next, node_cache, behavior);
1503 return;
1504 }
1505 if (frame_indices.prev) {
1506 this->read_single(*frame_indices.prev, node_cache, behavior);
1507 return;
1508 }
1509 if (frame_indices.next) {
1510 this->read_single(*frame_indices.next, node_cache, behavior);
1511 return;
1512 }
1514 }
1515
1516 void read_single(const int frame_index,
1517 bake::BakeNodeCache &node_cache,
1518 nodes::BakeNodeBehavior &behavior) const
1519 {
1520 bake::FrameCache &frame_cache = *node_cache.bake.frames[frame_index];
1521 ensure_bake_loaded(node_cache.bake, frame_cache);
1522 if (this->check_read_error(frame_cache, behavior)) {
1523 return;
1524 }
1525 auto &read_single_info = behavior.behavior.emplace<sim_output::ReadSingle>();
1526 read_single_info.values = frame_cache.values;
1527 }
1528
1529 void read_interpolated(const int prev_frame_index,
1530 const int next_frame_index,
1531 bake::BakeNodeCache &node_cache,
1532 nodes::BakeNodeBehavior &behavior) const
1533 {
1534 bake::FrameCache &prev_frame_cache = *node_cache.bake.frames[prev_frame_index];
1535 bake::FrameCache &next_frame_cache = *node_cache.bake.frames[next_frame_index];
1536 ensure_bake_loaded(node_cache.bake, prev_frame_cache);
1537 ensure_bake_loaded(node_cache.bake, next_frame_cache);
1538 if (this->check_read_error(prev_frame_cache, behavior) ||
1539 this->check_read_error(next_frame_cache, behavior))
1540 {
1541 return;
1542 }
1543 auto &read_interpolated_info = behavior.behavior.emplace<sim_output::ReadInterpolated>();
1544 read_interpolated_info.mix_factor = (float(current_frame_) - float(prev_frame_cache.frame)) /
1545 (float(next_frame_cache.frame) -
1546 float(prev_frame_cache.frame));
1547 read_interpolated_info.prev_values = prev_frame_cache.values;
1548 read_interpolated_info.next_values = next_frame_cache.values;
1549 }
1550
1551 [[nodiscard]] bool check_read_error(const bake::FrameCache &frame_cache,
1552 nodes::BakeNodeBehavior &behavior) const
1553 {
1554 if (frame_cache.meta_data_source && frame_cache.values.is_empty()) {
1555 auto &read_error_info = behavior.behavior.emplace<sim_output::ReadError>();
1556 read_error_info.message = RPT_("Cannot load the baked data");
1557 return true;
1558 }
1559 return false;
1560 }
1561};
1562
1565 const Span<bake::BakeDataBlockID> missing,
1566 FunctionRef<ID *(const bake::BakeDataBlockID &)> get_data_block)
1567{
1568 const int old_num = bake.data_blocks_num;
1569 const int new_num = old_num + missing.size();
1570 bake.data_blocks = reinterpret_cast<NodesModifierDataBlock *>(
1571 MEM_realloc_zeroed(bake.data_blocks, sizeof(NodesModifierDataBlock) * new_num));
1572 for (const int i : missing.index_range()) {
1573 NodesModifierDataBlock &data_block = bake.data_blocks[old_num + i];
1574 const bke::bake::BakeDataBlockID &key = missing[i];
1575
1576 data_block.id_name = BLI_strdup(key.id_name.c_str());
1577 if (!key.lib_name.empty()) {
1578 data_block.lib_name = BLI_strdup(key.lib_name.c_str());
1579 }
1580 data_block.id_type = int(key.type);
1581 ID *id = get_data_block(key);
1582 if (id) {
1583 data_block.id = id;
1584 }
1585 }
1586 bake.data_blocks_num = new_num;
1587}
1588
1589void nodes_modifier_data_block_destruct(NodesModifierDataBlock *data_block, const bool do_id_user)
1590{
1591 MEM_SAFE_DELETE(data_block->id_name);
1592 MEM_SAFE_DELETE(data_block->lib_name);
1593 if (do_id_user) {
1594 id_us_min(data_block->id);
1595 }
1596}
1597
1605 NodesModifierData &nmd_eval,
1606 NodesModifierData &nmd_orig,
1607 NodesModifierSimulationParams &simulation_params,
1608 NodesModifierBakeParams &bake_params)
1609{
1610 Depsgraph *depsgraph = ctx.depsgraph;
1611 Main *bmain = DEG_get_bmain(depsgraph);
1612
1613 struct DataPerBake {
1614 bool reset_first = false;
1616 };
1617 Map<int, DataPerBake> writeback_data;
1618 for (auto item : simulation_params.data_by_zone_id.items()) {
1619 DataPerBake data;
1620 NodesModifierBake &bake = *nmd_eval.find_bake(item.key);
1621 if (item.value->data_block_map.old_mappings.size() < bake.data_blocks_num) {
1622 data.reset_first = true;
1623 }
1624 if (bake::SimulationNodeCache *node_cache = nmd_eval.runtime->cache->get_simulation_node_cache(
1625 item.key))
1626 {
1627 /* Only writeback if the bake node has actually baked anything. */
1628 if (!node_cache->bake.frames.is_empty() || node_cache->prev_cache.has_value()) {
1629 data.new_mappings = std::move(item.value->data_block_map.new_mappings);
1630 }
1631 }
1632 if (data.reset_first || !data.new_mappings.is_empty()) {
1633 writeback_data.add(item.key, std::move(data));
1634 }
1635 }
1636 for (auto item : bake_params.data_by_node_id.items()) {
1637 if (bake::BakeNodeCache *node_cache = nmd_eval.runtime->cache->get_bake_node_cache(item.key)) {
1638 /* Only writeback if the bake node has actually baked anything. */
1639 if (!node_cache->bake.frames.is_empty()) {
1640 DataPerBake data;
1641 data.new_mappings = std::move(item.value->data_block_map.new_mappings);
1642 writeback_data.add(item.key, std::move(data));
1643 }
1644 }
1645 }
1646
1647 if (writeback_data.is_empty()) {
1648 /* Nothing to do. */
1649 return;
1650 }
1651
1653 *depsgraph,
1654 [object_eval = ctx.object,
1655 bmain,
1656 &nmd_orig,
1657 &nmd_eval,
1658 writeback_data = std::move(writeback_data)]() {
1659 for (auto item : writeback_data.items()) {
1660 const int bake_id = item.key;
1661 DataPerBake data = item.value;
1662
1663 NodesModifierBake &bake_orig = *nmd_orig.find_bake(bake_id);
1664 NodesModifierBake &bake_eval = *nmd_eval.find_bake(bake_id);
1665
1666 if (data.reset_first) {
1667 /* Reset data-block list on original data. */
1668 dna::array::clear<NodesModifierDataBlock>(&bake_orig.data_blocks,
1669 &bake_orig.data_blocks_num,
1670 &bake_orig.active_data_block,
1671 [](NodesModifierDataBlock *data_block) {
1672 nodes_modifier_data_block_destruct(
1673 data_block, true);
1674 });
1675 /* Reset data-block list on evaluated data. */
1676 dna::array::clear<NodesModifierDataBlock>(&bake_eval.data_blocks,
1677 &bake_eval.data_blocks_num,
1678 &bake_eval.active_data_block,
1679 [](NodesModifierDataBlock *data_block) {
1680 nodes_modifier_data_block_destruct(
1681 data_block, false);
1682 });
1683 }
1684
1685 Vector<bake::BakeDataBlockID> sorted_new_mappings;
1686 sorted_new_mappings.extend(data.new_mappings.keys().begin(),
1687 data.new_mappings.keys().end());
1688 bool needs_reevaluation = false;
1689 /* Add new data block mappings to the original modifier. This may do a name lookup in
1690 * bmain to find the data block if there is not faster way to get it. */
1691 add_missing_data_block_mappings(
1692 bake_orig, sorted_new_mappings, [&](const bake::BakeDataBlockID &key) -> ID * {
1693 ID *id_orig = nullptr;
1694 if (ID *id_eval = data.new_mappings.lookup_default(key, nullptr)) {
1695 id_orig = DEG_get_original(id_eval);
1696 }
1697 else {
1698 needs_reevaluation = true;
1699 id_orig = BKE_libblock_find_name_and_library(
1700 bmain, short(key.type), key.id_name.c_str(), key.lib_name.c_str());
1701 }
1702 if (id_orig) {
1703 id_us_plus(id_orig);
1704 }
1705 return id_orig;
1706 });
1707 /* Add new data block mappings to the evaluated modifier. In most cases this makes it so
1708 * the evaluated modifier is in the same state as if it were copied from the updated
1709 * original again. The exception is when a missing data block was found that is not in
1710 * the depsgraph currently. */
1711 add_missing_data_block_mappings(
1712 bake_eval, sorted_new_mappings, [&](const bake::BakeDataBlockID &key) -> ID * {
1713 return data.new_mappings.lookup_default(key, nullptr);
1714 });
1715
1716 if (needs_reevaluation) {
1717 Object *object_orig = DEG_get_original(object_eval);
1718 DEG_id_tag_update(&object_orig->id, ID_RECALC_GEOMETRY);
1719 DEG_relations_tag_update(bmain);
1720 }
1721 }
1722 });
1723}
1724
1726 const ModifierEvalContext *ctx,
1727 bke::GeometrySet &geometry_set)
1728{
1729 NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
1730 if (nmd->node_group == nullptr) {
1731 return;
1732 }
1733 NodesModifierData *nmd_orig = reinterpret_cast<NodesModifierData *>(
1735 if (ID_MISSING(nmd_orig->node_group)) {
1736 return;
1737 }
1738
1739 const bNodeTree &tree = *nmd->node_group;
1740
1741 tree.ensure_topology_cache();
1742 const bNode *output_node = tree.group_output_node();
1743 if (output_node == nullptr) {
1744 BKE_modifier_set_error(ctx->object, md, "Node group must have a group output node");
1745 geometry_set.clear();
1746 return;
1747 }
1748
1749 Span<const bNodeSocket *> group_outputs = output_node->input_sockets().drop_back(1);
1750 if (group_outputs.is_empty()) {
1751 BKE_modifier_set_error(ctx->object, md, "Node group must have an output socket");
1752 geometry_set.clear();
1753 return;
1754 }
1755
1756 const bNodeSocket *first_output_socket = group_outputs[0];
1757 if (!STREQ(first_output_socket->idname, "NodeSocketGeometry")) {
1758 BKE_modifier_set_error(ctx->object, md, "Node group's first output must be a geometry");
1759 geometry_set.clear();
1760 return;
1761 }
1762
1763 const nodes::GeometryNodesLazyFunctionGraphInfo *lf_graph_info =
1765 if (lf_graph_info == nullptr) {
1766 BKE_modifier_set_error(ctx->object, md, "Cannot evaluate node group");
1767 geometry_set.clear();
1768 return;
1769 }
1770
1771 bool use_orig_index_verts = false;
1772 bool use_orig_index_edges = false;
1773 bool use_orig_index_faces = false;
1774 if (const Mesh *mesh = geometry_set.get_mesh()) {
1775 use_orig_index_verts = CustomData_has_layer(&mesh->vert_data, CD_ORIGINDEX);
1776 use_orig_index_edges = CustomData_has_layer(&mesh->edge_data, CD_ORIGINDEX);
1777 use_orig_index_faces = CustomData_has_layer(&mesh->face_data, CD_ORIGINDEX);
1778 }
1779
1780 nodes::GeoNodesCallData call_data;
1781
1782 nodes::GeoNodesModifierData modifier_eval_data{};
1783 modifier_eval_data.depsgraph = ctx->depsgraph;
1784 modifier_eval_data.self_object = ctx->object;
1785 auto eval_log = std::make_unique<nodes::eval_log::NodesEvalLog>();
1786 call_data.modifier_data = &modifier_eval_data;
1787
1788 NodesModifierSimulationParams simulation_params(*nmd, *ctx);
1789 call_data.simulation_params = &simulation_params;
1790 NodesModifierBakeParams bake_params{*nmd, *ctx};
1791 call_data.bake_params = &bake_params;
1792
1793 Set<ComputeContextHash> verbose_log_contexts;
1794 if (logging_enabled(ctx)) {
1795 call_data.eval_log = eval_log.get();
1796
1797 find_verbose_log_contexts(*nmd, *ctx, verbose_log_contexts);
1798 call_data.verbose_log_contexts = &verbose_log_contexts;
1799 }
1800
1801 nodes::GeoNodesSideEffectNodes side_effect_nodes;
1802 find_side_effect_nodes(*nmd, *ctx, side_effect_nodes, verbose_log_contexts);
1803 call_data.side_effect_nodes = &side_effect_nodes;
1804
1805 bke::DataBlockComputeContext data_block_compute_context{nullptr, ctx->object->id};
1806 bke::GeometryNodesModifierComputeContext modifier_compute_context{&data_block_compute_context,
1807 *nmd};
1808
1809 PointerRNA md_ptr = RNA_pointer_create_discrete(&ctx->object->id, RNA_NodesModifier, md);
1810 PointerRNA properties_ptr = RNA_pointer_get(&md_ptr, "properties");
1811
1813 tree, properties_ptr, modifier_compute_context, call_data, std::move(geometry_set));
1814
1815 if (logging_enabled(ctx)) {
1816 nmd_orig->runtime->eval_log = std::move(eval_log);
1817 }
1818
1819 if (DEG_is_active(ctx->depsgraph) && !(ctx->flag & MOD_APPLY_TO_ORIGINAL)) {
1820 add_data_block_items_writeback(*ctx, *nmd, *nmd_orig, simulation_params, bake_params);
1821 }
1822
1823 if (use_orig_index_verts || use_orig_index_edges || use_orig_index_faces) {
1824 if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
1825 /* Add #CD_ORIGINDEX layers if they don't exist already. This is required because the
1826 * #eModifierTypeFlag_SupportsMapping flag is set. If the layers did not exist before, it is
1827 * assumed that the output mesh does not have a mapping to the original mesh. */
1828 if (use_orig_index_verts) {
1829 CustomData_add_layer(&mesh->vert_data, CD_ORIGINDEX, CD_SET_DEFAULT, mesh->verts_num);
1830 }
1831 if (use_orig_index_edges) {
1832 CustomData_add_layer(&mesh->edge_data, CD_ORIGINDEX, CD_SET_DEFAULT, mesh->edges_num);
1833 }
1834 if (use_orig_index_faces) {
1835 CustomData_add_layer(&mesh->face_data, CD_ORIGINDEX, CD_SET_DEFAULT, mesh->faces_num);
1836 }
1837 }
1838 }
1839}
1840
1842{
1845
1846 modifyGeometry(md, ctx, geometry_set);
1847
1848 bke::MeshComponent &mesh_component = geometry_set.get_component_for_write<bke::MeshComponent>();
1849 if (mesh_component.get() != mesh) {
1850 /* If this is the same as the input mesh, it's not necessary to make a copy of it even if it's
1851 * not owned by the geometry set. That's because we know that the caller manages the ownership
1852 * of the mesh. */
1853 mesh_component.ensure_owns_direct_data();
1854 }
1855 Mesh *new_mesh = mesh_component.release();
1856 if (new_mesh == nullptr) {
1857 return BKE_mesh_new_nomain(0, 0, 0, 0);
1858 }
1859 return new_mesh;
1860}
1861
1863 const ModifierEvalContext *ctx,
1864 bke::GeometrySet *geometry_set)
1865{
1866 modifyGeometry(md, ctx, *geometry_set);
1867}
1868
1870{
1871 if (!nmd.node_group || ID_MISSING(nmd.node_group)) {
1872 this->reset();
1873 return;
1874 }
1875 const bNodeTree &tree = *nmd.node_group;
1876 tree.ensure_interface_cache();
1877 tree.ensure_topology_cache();
1878
1880 const_cast<ID *>(&object.id), RNA_NodesModifier, const_cast<NodesModifierData *>(&nmd));
1881 PointerRNA properties_ptr = RNA_pointer_get(&nmd_ptr, "properties");
1882
1883 ResourceScope scope;
1884 const Vector<nodes::InferenceValue> group_input_values =
1885 nodes::get_geometry_nodes_input_inference_values(tree, properties_ptr, scope);
1886
1887 /* Compute the hash of the input values. This has to be done every time currently, because there
1888 * is no reliable callback yet that is called any of the modifier properties changes. */
1889 XXH3_state_t *state = XXH3_createState();
1890 XXH3_64bits_reset(state);
1891 BLI_SCOPED_DEFER([&]() { XXH3_freeState(state); });
1892 for (const int input_i : IndexRange(nmd.node_group->interface_inputs().size())) {
1893 const nodes::InferenceValue &value = group_input_values[input_i];
1894 XXH3_64bits_update(state, &input_i, sizeof(input_i));
1895 if (value.is_primitive_value()) {
1896 const void *value_ptr = value.get_primitive_ptr();
1897 const bNodeTreeInterfaceSocket &io_socket = *nmd.node_group->interface_inputs()[input_i];
1898 const CPPType &base_type = *io_socket.socket_typeinfo()->base_cpp_type;
1899 uint64_t value_hash = base_type.hash_or_fallback(value_ptr, 0);
1900 XXH3_64bits_update(state, &value_hash, sizeof(value_hash));
1901 }
1902 }
1903 const uint64_t new_input_values_hash = XXH3_64bits_digest(state);
1904 if (new_input_values_hash == input_values_hash_) {
1905 if (this->inputs.size() == tree.interface_inputs().size() &&
1906 this->outputs.size() == tree.interface_outputs().size())
1907 {
1908 /* The cache is up to date, so return early. */
1909 return;
1910 }
1911 }
1912 /* Compute the new usage inference result. */
1913 this->inputs.reinitialize(tree.interface_inputs().size());
1914 this->outputs.reinitialize(tree.interface_outputs().size());
1916 tree, group_input_values, inputs, outputs);
1917 input_values_hash_ = new_input_values_hash;
1918}
1919
1921{
1922 input_values_hash_ = 0;
1923 this->inputs = {};
1924 this->outputs = {};
1925}
1926
1927static void panel_draw(const bContext *C, Panel *panel)
1928{
1929 ui::Layout &layout = *panel->layout;
1930 PointerRNA *modifier_ptr = modifier_panel_get_property_pointers(panel, nullptr);
1931 nodes::draw_geometry_nodes_modifier_ui(*C, modifier_ptr, layout);
1932}
1933
1934static void panel_register(ARegionType *region_type)
1935{
1937}
1938
1939static void blend_write(BlendWriter *writer, const ID * /*id_owner*/, const ModifierData *md)
1940{
1941 const NodesModifierData *nmd = reinterpret_cast<const NodesModifierData *>(md);
1942
1943 writer->write_struct(nmd);
1944
1945 writer->write_string(nmd->bake_directory);
1946
1947 if (nmd->settings_legacy.properties != nullptr) {
1948 /* Write legacy settings for forward compatibility. Created by
1949 * #create_legacy_geometry_nodes_properties. */
1951 }
1952
1953 writer->write_struct_array(nmd->bakes_num, nmd->bakes);
1954 for (const NodesModifierBake &bake : Span(nmd->bakes, nmd->bakes_num)) {
1955 writer->write_string(bake.directory);
1956
1957 writer->write_struct_array(bake.data_blocks_num, bake.data_blocks);
1958 for (const NodesModifierDataBlock &item : Span(bake.data_blocks, bake.data_blocks_num)) {
1959 writer->write_string(item.id_name);
1960 writer->write_string(item.lib_name);
1961 }
1962 if (bake.packed) {
1963 writer->write_struct(bake.packed);
1964 writer->write_struct_array(bake.packed->meta_files_num, bake.packed->meta_files);
1965 writer->write_struct_array(bake.packed->blob_files_num, bake.packed->blob_files);
1966 const auto write_bake_file = [&](const NodesModifierBakeFile &bake_file) {
1967 writer->write_string(bake_file.name);
1968 if (bake_file.packed_file) {
1969 BKE_packedfile_blend_write(writer, bake_file.packed_file);
1970 }
1971 };
1972 for (const NodesModifierBakeFile &meta_file :
1973 Span{bake.packed->meta_files, bake.packed->meta_files_num})
1974 {
1975 write_bake_file(meta_file);
1976 }
1977 for (const NodesModifierBakeFile &blob_file :
1978 Span{bake.packed->blob_files, bake.packed->blob_files_num})
1979 {
1980 write_bake_file(blob_file);
1981 }
1982 }
1983 }
1984}
1985
1986static void blend_read(BlendDataReader *reader, ModifierData *md)
1987{
1988 NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
1989 BLO_read_string(reader, &nmd->bake_directory);
1990 if (nmd->node_group == nullptr || nmd->modifier.system_properties) {
1991 /* Don't bother reading old settings when modifier system properties are available. */
1992 nmd->settings_legacy.properties = nullptr;
1993 }
1994 else {
1997 }
1998
1999 BLO_read_array_and_validate_size(reader, &nmd->bakes, &nmd->bakes_num);
2000
2001 if (nmd->bakes_num > 0 && nmd->bakes == nullptr) {
2002 /* This case generally shouldn't be allowed to happen. However, there is a bug report with a
2003 * corrupted .blend file (#123974) that triggers this case. Unfortunately, it's not clear how
2004 * that could have happened. For now, handle this case more gracefully in release builds, while
2005 * still crashing in debug builds. */
2006 nmd->bakes_num = 0;
2008 }
2009
2010 for (NodesModifierBake &bake : MutableSpan(nmd->bakes, nmd->bakes_num)) {
2011 BLO_read_string(reader, &bake.directory);
2012
2013 BLO_read_array_and_validate_size(reader, &bake.data_blocks, &bake.data_blocks_num);
2014 for (NodesModifierDataBlock &data_block : MutableSpan(bake.data_blocks, bake.data_blocks_num))
2015 {
2016 BLO_read_string(reader, &data_block.id_name);
2017 BLO_read_string(reader, &data_block.lib_name);
2018 }
2019
2021 if (bake.packed) {
2023 reader, &bake.packed->meta_files, &bake.packed->meta_files_num);
2025 reader, &bake.packed->blob_files, &bake.packed->blob_files_num);
2026 const auto read_bake_file = [&](NodesModifierBakeFile &bake_file) {
2027 BLO_read_string(reader, &bake_file.name);
2028 if (bake_file.packed_file) {
2029 BKE_packedfile_blend_read(reader, &bake_file.packed_file, "");
2030 }
2031 };
2032 for (NodesModifierBakeFile &meta_file :
2033 MutableSpan{bake.packed->meta_files, bake.packed->meta_files_num})
2034 {
2035 read_bake_file(meta_file);
2036 }
2037 for (NodesModifierBakeFile &blob_file :
2038 MutableSpan{bake.packed->blob_files, bake.packed->blob_files_num})
2039 {
2040 read_bake_file(blob_file);
2041 }
2042 }
2043 }
2044
2045 nmd->runtime = MEM_new<NodesModifierRuntime>(__func__);
2046 nmd->runtime->cache = std::make_shared<bake::ModifierCache>();
2047}
2048
2049static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
2050{
2051 const NodesModifierData *nmd = reinterpret_cast<const NodesModifierData *>(md);
2052 NodesModifierData *tnmd = reinterpret_cast<NodesModifierData *>(target);
2053
2054 BKE_modifier_copydata_generic(md, target, flag);
2055
2056 if (nmd->bakes) {
2057 tnmd->bakes = MEM_dupalloc(nmd->bakes);
2058 for (const int i : IndexRange(nmd->bakes_num)) {
2059 NodesModifierBake &bake = tnmd->bakes[i];
2060 if (bake.directory) {
2061 bake.directory = BLI_strdup(bake.directory);
2062 }
2063 if (bake.data_blocks) {
2064 bake.data_blocks = MEM_dupalloc(bake.data_blocks);
2065 for (const int i : IndexRange(bake.data_blocks_num)) {
2066 NodesModifierDataBlock &data_block = bake.data_blocks[i];
2067 if (data_block.id_name) {
2068 data_block.id_name = BLI_strdup(data_block.id_name);
2069 }
2070 if (data_block.lib_name) {
2071 data_block.lib_name = BLI_strdup(data_block.lib_name);
2072 }
2073 }
2074 }
2076 }
2077 }
2078
2079 tnmd->runtime = MEM_new<NodesModifierRuntime>(__func__);
2080
2082 /* Share the simulation cache between the original and evaluated modifier. */
2083 tnmd->runtime->cache = nmd->runtime->cache;
2084 /* Keep bake path in the evaluated modifier. */
2085 tnmd->bake_directory = nmd->bake_directory ? BLI_strdup(nmd->bake_directory) : nullptr;
2086 }
2087 else {
2088 tnmd->runtime->cache = std::make_shared<bake::ModifierCache>();
2089 /* Clear the bake path when duplicating. */
2090 tnmd->bake_directory = nullptr;
2091 }
2092}
2093
2095 const NodesModifierBake &bake_src)
2096{
2097 BLI_assert(ELEM(bake_dst.packed, bake_src.packed, nullptr));
2098 if (!bake_src.packed) {
2099 return;
2100 }
2101
2102 bake_dst.packed = MEM_dupalloc(bake_src.packed);
2103 const auto copy_bake_files_inplace = [](NodesModifierBakeFile **bake_files,
2104 const int bake_files_num) {
2105 if (!*bake_files) {
2106 return;
2107 }
2108 *bake_files = MEM_dupalloc(*bake_files);
2109 for (NodesModifierBakeFile &bake_file : MutableSpan{*bake_files, bake_files_num}) {
2110 bake_file.name = BLI_strdup_null(bake_file.name);
2111 if (bake_file.packed_file) {
2112 bake_file.packed_file = BKE_packedfile_duplicate(bake_file.packed_file);
2113 }
2114 }
2115 };
2116 copy_bake_files_inplace(&bake_dst.packed->meta_files, bake_dst.packed->meta_files_num);
2117 copy_bake_files_inplace(&bake_dst.packed->blob_files, bake_dst.packed->blob_files_num);
2118}
2119
2121{
2122 const auto free_packed_files = [](NodesModifierBakeFile *files, const int files_num) {
2123 for (NodesModifierBakeFile &file : MutableSpan{files, files_num}) {
2124 MEM_SAFE_DELETE(file.name);
2125 if (file.packed_file) {
2126 BKE_packedfile_free(file.packed_file);
2127 }
2128 }
2129 MEM_SAFE_DELETE(files);
2130 };
2131 free_packed_files(packed_bake->meta_files, packed_bake->meta_files_num);
2132 free_packed_files(packed_bake->blob_files, packed_bake->blob_files_num);
2133 MEM_SAFE_DELETE(packed_bake);
2134}
2135
2137{
2138 MEM_SAFE_DELETE(bake->directory);
2139
2140 for (NodesModifierDataBlock &data_block : MutableSpan(bake->data_blocks, bake->data_blocks_num))
2141 {
2142 nodes_modifier_data_block_destruct(&data_block, do_id_user);
2143 }
2144 MEM_SAFE_DELETE(bake->data_blocks);
2145
2146 if (bake->packed) {
2148 }
2149}
2150
2151static void free_data(ModifierData *md)
2152{
2153 NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
2154 for (NodesModifierBake &bake : MutableSpan(nmd->bakes, nmd->bakes_num)) {
2156 }
2157 MEM_SAFE_DELETE(nmd->bakes);
2158
2159 MEM_SAFE_DELETE(nmd->bake_directory);
2160 MEM_delete(nmd->runtime);
2161}
2162
2163static void required_data_mask(ModifierData * /*md*/, CustomData_MeshMasks *r_cddata_masks)
2164{
2165 /* We don't know what the node tree will need. If there are vertex groups, it is likely that the
2166 * node tree wants to access them. */
2167 r_cddata_masks->vmask |= CD_MASK_MDEFORMVERT;
2168 r_cddata_masks->vmask |= CD_MASK_PROP_ALL;
2169}
2170
2172 /*idname*/ "GeometryNodes",
2173 /*name*/ N_("GeometryNodes"),
2174 /*struct_name*/ "NodesModifierData",
2175 /*struct_size*/ sizeof(NodesModifierData),
2176 /*srna*/ &RNA_NodesModifier,
2178 /*flags*/
2182 /*icon*/ ICON_GEOMETRY_NODES,
2183
2184 /*copy_data*/ copy_data,
2185
2186 /*deform_verts*/ nullptr,
2187 /*deform_matrices*/ nullptr,
2188 /*deform_verts_EM*/ nullptr,
2189 /*deform_matrices_EM*/ nullptr,
2190 /*modify_mesh*/ modify_mesh,
2191 /*modify_geometry_set*/ modify_geometry_set,
2192
2193 /*init_data*/ init_data,
2194 /*required_data_mask*/ required_data_mask,
2195 /*free_data*/ free_data,
2196 /*is_disabled*/ is_disabled,
2197 /*update_depsgraph*/ update_depsgraph,
2198 /*depends_on_time*/ depends_on_time,
2199 /*depends_on_normals*/ nullptr,
2200 /*foreach_ID_link*/ foreach_ID_link,
2201 /*foreach_tex_link*/ foreach_tex_link,
2202 /*free_runtime_data*/ nullptr,
2203 /*panel_register*/ panel_register,
2204 /*blend_write*/ blend_write,
2205 /*blend_read*/ blend_read,
2206 /*foreach_cache*/ nullptr,
2207 /*foreach_working_space_color*/ nullptr,
2208};
2209
2210} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
#define IDP_BlendDataRead(reader, prop)
#define GEO_NODE_SIMULATION_OUTPUT
#define GEO_NODE_BAKE
General operations, lookup, etc. for blender objects.
General operations for point clouds.
#define BLI_SCOPED_DEFER(function_to_defer)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define RPT_(msgid)
#define ID_MISSING(_id)
Definition DNA_ID.h:695
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
volatile int lock
#define BLI_assert_unreachable()
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define ELEM(...)
#define STREQ(a, b)
#define MEM_realloc_zeroed(vmemh, len)
#define CD_MASK_MDEFORMVERT
#define CD_MASK_PROP_ALL
constexpr Span()=default
uint64_t hash_or_fallback(const void *value, uint64_t fallback_hash) const
const ComputeContext * parent() const
const ComputeContextHash & hash() const
constexpr int64_t first() const
constexpr int64_t last(const int64_t n=0) const
SubIterator begin() const
Definition BLI_map.hh:768
SubIterator end() const
Definition BLI_map.hh:778
bool add_overwrite(const Key &key, const Value &value)
Definition BLI_map.hh:325
ValueIterator values() const &
Definition BLI_map.hh:884
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
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
Value & lookup_or_add_cb(const Key &key, const CreateValueF &create_value)
Definition BLI_map.hh:620
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
Value & lookup_or_add(const Key &key, const Value &value)
Definition BLI_map.hh:588
ID * lookup_or_remember_missing(const bake::BakeDataBlockID &key) override
Definition MOD_nodes.cc:875
Map< bake::BakeDataBlockID, ID * > old_mappings
Definition MOD_nodes.cc:872
Map< bake::BakeDataBlockID, ID * > new_mappings
Definition MOD_nodes.cc:873
NodesModifierBakeParams(NodesModifierData &nmd, const ModifierEvalContext &ctx)
Map< int, std::unique_ptr< DataPerNode > > data_by_node_id
nodes::BakeNodeBehavior * get(const int id) const override
NodesModifierSimulationParams(NodesModifierData &nmd, const ModifierEvalContext &ctx)
void input_pass_through(nodes::SimulationZoneBehavior &zone_behavior) const
void output_store_frame_cache(bake::SimulationNodeCache &node_cache, nodes::SimulationZoneBehavior &zone_behavior) const
void store_as_prev_items(bake::SimulationNodeCache &node_cache, nodes::SimulationZoneBehavior &zone_behavior) const
void read_single(const int frame_index, bake::SimulationNodeCache &node_cache, nodes::SimulationZoneBehavior &zone_behavior) const
void read_interpolated(const int prev_frame_index, const int next_frame_index, bake::SimulationNodeCache &node_cache, nodes::SimulationZoneBehavior &zone_behavior) const
void read_from_cache(const BakeFrameIndices &frame_indices, bake::SimulationNodeCache &node_cache, nodes::SimulationZoneBehavior &zone_behavior) const
void output_pass_through(nodes::SimulationZoneBehavior &zone_behavior) const
Map< int, std::unique_ptr< DataPerZone > > data_by_zone_id
nodes::SimulationZoneBehavior * get(const int zone_id) const override
void init_simulation_info(const int zone_id, nodes::SimulationZoneBehavior &zone_behavior, NodesModifierBakeDataBlockMap &data_block_map) const
Array< nodes::socket_usage_inference::SocketUsage > outputs
Definition MOD_nodes.hh:45
void ensure(const Object &object, const NodesModifierData &nmd)
Array< nodes::socket_usage_inference::SocketUsage > inputs
Definition MOD_nodes.hh:44
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
int64_t size() const
void append(const T &value)
bool is_empty() const
IndexRange index_range() const
void extend(Span< T > array)
Span< T > as_span() const
const GeometryNodesModifierComputeContext & for_geometry_nodes_modifier(const ComputeContext *parent, const NodesModifierData &nmd)
const GroupNodeComputeContext & for_group_node(const ComputeContext *parent, int32_t node_id, const bNodeTree *tree=nullptr)
const DataBlockComputeContext & for_data_block(const ComputeContext *parent, const uint32_t orig_session_uid, const ID *id=nullptr)
std::optional< int > output_node_id
const bNodeTreeZone * get_zone_by_node(const int32_t node_id) const
static Map< const bke::bNodeTreeZone *, ComputeContextHash > get_context_hash_by_zone_for_node_editor(const SpaceNode &snode, bke::ComputeContextCache &compute_context_cache)
Definition eval_log.cc:972
const int state
int64_t first_if(const Range &range, Predicate &&predicate)
std::optional< BakePath > get_node_bake_path(const Main &bmain, const Object &object, const NodesModifierData &nmd, int node_id)
std::optional< IndexRange > get_node_bake_frame_range(const Scene &scene, const Object &object, const NodesModifierData &nmd, int node_id)
std::optional< BakeValues > deserialize_bake(std::istream &stream, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing)
std::optional< SubFrame > file_name_to_frame(StringRef file_name)
Vector< MetaFile > find_sorted_meta_files(StringRefNull meta_dir)
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
void add(Depsgraph &depsgraph, std::function< void()> fn)
bool node_editor_is_for_geometry_nodes_modifier(const SpaceNode &snode, const Object &object, const NodesModifierData &nmd)
std::optional< ViewerPathForGeometryNodesViewer > parse_geometry_nodes_viewer(const ViewerPath &viewer_path)
const ComputeContext * compute_context_for_viewer_path_elem(const ViewerPathElem &elem, bke::ComputeContextCache &compute_context_cache, const ComputeContext *parent_compute_context)
void foreach_active_gizmo_in_modifier(const Object &object, const NodesModifierData &nmd, const wmWindowManager &wm, bke::ComputeContextCache &compute_context_cache, const ForeachGizmoInModifierFn fn)
void foreach_compute_context_on_gizmo_path(const ComputeContext &gizmo_context, const bNode &gizmo_node, const bNodeSocket &gizmo_socket, FunctionRef< void(const ComputeContext &context)> fn)
void infer_group_interface_usage(const bNodeTree &group, const Span< InferenceValue > group_input_values, const MutableSpan< SocketUsage > r_input_usages, const std::optional< MutableSpan< SocketUsage > > r_output_usages)
Vector< InferenceValue > get_geometry_nodes_input_inference_values(const bNodeTree &btree, const PointerRNA &properties_ptr, ResourceScope &scope)
EvalDependencies gather_eval_dependencies_recursive(const bNodeTree &ntree)
const std::shared_ptr< const GeometryNodesLazyFunctionGraphInfo > & ensure_geometry_nodes_lazy_function_graph(const bNodeTree &btree)
void draw_geometry_nodes_modifier_ui(const bContext &C, PointerRNA *modifier_ptr, ui::Layout &layout)
bke::GeometrySet execute_geometry_nodes_on_geometry(const bNodeTree &btree, const PointerRNA &properties_ptr, const ComputeContext &base_compute_context, GeoNodesCallData &call_data, bke::GeometrySet input_geometry)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static bool logging_enabled(const ModifierEvalContext *ctx)
Definition MOD_nodes.cc:301
static void find_side_effect_nodes_for_baking(const NodesModifierData &nmd, const ModifierEvalContext &ctx, nodes::GeoNodesSideEffectNodes &r_side_effect_nodes)
Definition MOD_nodes.cc:743
static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
void BKE_packedfile_blend_write(BlendWriter *writer, const PackedFile *pf)
unsigned __int64 uint64_t
Definition BKE_text.h:91
@ NODES_MODIFIER_PANEL_WARNINGS
static void blend_write(BlendWriter *writer, const ID *, const ModifierData *md)
Definition MOD_bevel.cc:454
void BKE_modifier_set_error(const Object *ob, ModifierData *md, const char *format,...) ATTR_PRINTF_FORMAT(3
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void DEG_add_generic_id_relation(DepsNodeHandle *node_handle, ID *id, const char *description)
PackedFile * BKE_packedfile_duplicate(const PackedFile *pf_src)
bool DEG_is_active(const Depsgraph *depsgraph)
Definition depsgraph.cc:324
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
static void find_verbose_log_contexts(const NodesModifierData &nmd, const ModifierEvalContext &ctx, Set< ComputeContextHash > &r_socket_log_contexts)
Definition MOD_nodes.cc:831
static bool depends_on_time(Scene *, ModifierData *)
Definition MOD_build.cc:46
void nodes_modifier_packed_bake_copy(NodesModifierBake &bake_dst, const NodesModifierBake &bake_src)
@ OB_FLAG_USE_SIMULATION_CACHE
void IDP_BlendWrite(BlendWriter *writer, const IDProperty *prop)
Definition idprop.cc:1696
void IDP_foreach_property(IDProperty *id_property_root, int type_filter, FunctionRef< void(IDProperty *id_property)> callback)
static void init_data(ModifierData *md)
void(*)(void *user_data, Object *ob, ID **idpoin, LibraryForeachIDCallbackFlag cb_flag) IDWalkFunc
static void update_depsgraph(tGraphSliderOp *gso)
void MOD_nodes_update_interface(Main &bmain, Object *object, NodesModifierData *nmd)
Definition MOD_nodes.cc:405
static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
void BKE_packedfile_free(PackedFile *pf)
ID * DEG_get_evaluated_id(const Depsgraph *depsgraph, ID *id)
void DEG_add_object_relation(DepsNodeHandle *node_handle, Object *object, eDepsObjectComponentType component, const char *description)
bScreen * BKE_workspace_active_screen_get(const WorkSpaceInstanceHook *hook) GETTER_ATTRS
Definition workspace.cc:684
ModifierTypeInfo modifierType_Nodes
void DEG_add_collection_geometry_relation(DepsNodeHandle *node_handle, Collection *collection, const char *description)
void DEG_add_depends_on_transform_relation(DepsNodeHandle *node_handle, const char *description)
static BakeFrameIndices get_bake_frame_indices(const Span< std::unique_ptr< bake::FrameCache > > frame_caches, const SubFrame frame)
Definition MOD_nodes.cc:923
@ LIB_ID_COPY_SET_COPIED_ON_WRITE
void DEG_add_node_tree_output_relation(DepsNodeHandle *node_handle, bNodeTree *node_tree, const char *description)
static void add_missing_data_block_mappings(NodesModifierBake &bake, const Span< bake::BakeDataBlockID > missing, FunctionRef< ID *(const bake::BakeDataBlockID &)> get_data_block)
WorkSpace * BKE_workspace_active_get(WorkSpaceInstanceHook *hook) GETTER_ATTRS
Definition workspace.cc:633
static void required_data_mask(ModifierData *, CustomData_MeshMasks *r_cddata_masks)
static void modify_geometry_set(ModifierData *md, const ModifierEvalContext *ctx, bke::GeometrySet *geometry_set)
static void find_side_effect_nodes_for_viewer_path(const ViewerPath &viewer_path, const NodesModifierData &nmd, const ModifierEvalContext &ctx, nodes::GeoNodesSideEffectNodes &r_side_effect_nodes)
Definition MOD_nodes.cc:658
static void try_add_side_effect_node(const ModifierEvalContext &ctx, const ComputeContext &final_compute_context, const int final_node_id, const NodesModifierData &nmd, nodes::GeoNodesSideEffectNodes &r_side_effect_nodes)
Definition MOD_nodes.cc:434
void nodes_modifier_data_block_destruct(NodesModifierDataBlock *data_block, const bool do_id_user)
void nodes_modifier_bake_destruct(NodesModifierBake *bake, const bool do_id_user)
static const CustomData_MeshMasks dependency_data_mask
Definition MOD_nodes.cc:124
Main * DEG_get_bmain(const Depsgraph *graph)
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
static void free_data(ModifierData *md)
@ MOD_APPLY_TO_ORIGINAL
@ NODES_MODIFIER_BAKE_MODE_ANIMATION
@ NODES_MODIFIER_BAKE_MODE_STILL
static void find_dependencies_from_settings(const NodesModifierData &nmd, nodes::EvalDependencies &deps)
Definition MOD_nodes.cc:112
static void update_bakes_from_node_group(NodesModifierData &nmd)
Definition MOD_nodes.cc:334
static void panel_register(ARegionType *region_type)
Mesh * BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int corners_num)
static void add_data_block_items_writeback(const ModifierEvalContext &ctx, NodesModifierData &nmd_eval, NodesModifierData &nmd_orig, NodesModifierSimulationParams &simulation_params, NodesModifierBakeParams &bake_params)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
static Mesh * modify_mesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh)
Definition MOD_array.cc:873
@ eModifierTypeFlag_SupportsMapping
@ eModifierTypeFlag_AcceptsGreasePencil
@ eModifierTypeFlag_AcceptsCVs
@ eModifierTypeFlag_EnableInEditmode
@ eModifierTypeFlag_SupportsEditmode
@ eModifierTypeFlag_AcceptsMesh
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
static void foreach_tex_link(ModifierData *md, Object *ob, TexWalkFunc walk, void *user_data)
void BLO_read_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size, const int elems=1)
static void find_side_effect_nodes_for_active_gizmos(const NodesModifierData &nmd, const ModifierEvalContext &ctx, const wmWindowManager &wm, nodes::GeoNodesSideEffectNodes &r_side_effect_nodes, Set< ComputeContextHash > &r_verbose_log_contexts)
Definition MOD_nodes.cc:763
static void modifyGeometry(ModifierData *md, const ModifierEvalContext *ctx, bke::GeometrySet &geometry_set)
static void blend_read(BlendDataReader *, ModifierData *md)
static void find_side_effect_nodes(const NodesModifierData &nmd, const ModifierEvalContext &ctx, nodes::GeoNodesSideEffectNodes &r_side_effect_nodes, Set< ComputeContextHash > &r_verbose_log_contexts)
Definition MOD_nodes.cc:795
T * DEG_get_original(T *id)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
static void remove_outdated_bake_caches(NodesModifierData &nmd)
Definition MOD_nodes.cc:312
static void update_system_properties(Main &bmain, Object &object, NodesModifierData &nmd)
Definition MOD_nodes.cc:391
void RNA_ensure_and_sync_system_properties(Main &bmain, PointerRNA &ptr, IDProperty &idprops)
std::mutex Mutex
Definition BLI_mutex.hh:51
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
void DEG_add_collection_geometry_customdata_mask(DepsNodeHandle *node_handle, Collection *collection, const CustomData_MeshMasks *masks)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
ID_Type GS(const char *name)
Definition DNA_ID.h:410
static void ensure_bake_loaded(bake::NodeBakeCache &bake_cache, bake::FrameCache &frame_cache)
Definition MOD_nodes.cc:951
Scene * DEG_get_input_scene(const Depsgraph *graph)
bool DEG_object_has_geometry_component(Object *object)
static void find_side_effect_nodes_for_nested_node(const ModifierEvalContext &ctx, const NodesModifierData &nmd, const int root_nested_node_id, nodes::GeoNodesSideEffectNodes &r_side_effect_nodes)
Definition MOD_nodes.cc:692
static void panel_draw(const bContext *, Panel *panel)
static bool try_find_baked_data(const NodesModifierBake &bake, bake::NodeBakeCache &bake_cache, const Main &bmain, const Object &object, const NodesModifierData &nmd, const int id)
Definition MOD_nodes.cc:990
char * BLI_strdup_null(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_MALLOC
void BKE_packedfile_blend_read(BlendDataReader *reader, PackedFile **pf_p, StringRefNull filepath)
void DEG_add_scene_camera_relation(DepsNodeHandle *node_handle, Scene *scene, eDepsObjectComponentType component, const char *description)
void nodes_modifier_packed_bake_free(NodesModifierPackedBake *packed_bake)
void DEG_add_scene_relation(DepsNodeHandle *node_handle, Scene *scene, eDepsSceneComponentType component, const char *description)
static bool is_disabled(const Scene *, ModifierData *md, bool)
bool DEG_is_evaluated(const T *id)
ModifierData * BKE_modifier_get_original(const Object *object, ModifierData *md)
void(*)(void *user_data, Object *ob, ModifierData *md, const PointerRNA *ptr, PropertyRNA *texture_prop) TexWalkFunc
void id_us_min(ID *id)
Definition lib_id.cc:441
float DEG_get_ctime(const Depsgraph *graph)
void DEG_add_vfont_relation(DepsNodeHandle *handle, VFont *vfont, const char *description)
static void add_object_relation(const ModifierUpdateDepsgraphContext *ctx, Object &object, const nodes::EvalDependencies::ObjectDependencyInfo &info)
Definition MOD_nodes.cc:137
@ IDP_TYPE_FILTER_ID
static void add_collection_relation(const ModifierUpdateDepsgraphContext *ctx, Collection &collection)
Definition MOD_nodes.cc:130
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
void * CustomData_add_layer(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem)
void DEG_add_customdata_mask(DepsNodeHandle *handle, Object *object, const CustomData_MeshMasks *masks)
void BKE_modifier_copydata_generic(const ModifierData *md, ModifierData *md_dst, int flag)
#define hash
Definition noise_c.cc:156
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
std::optional< int > current
Definition MOD_nodes.cc:919
std::optional< int > prev
Definition MOD_nodes.cc:918
std::optional< int > next
Definition MOD_nodes.cc:920
void write_string(const char *data)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct_array(const int64_t array_size, const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
char name[258]
Definition DNA_ID.h:433
ListBaseT< wmWindowManager > wm
Definition BKE_main.hh:415
struct IDProperty * system_properties
NodesModifierBakeDataBlockMap data_block_map
NodesModifierDataBlock * data_blocks
NodesModifierPackedBake * packed
NodesModifierBakeMode bake_mode
struct NodesModifierSettings settings_legacy
NodesModifierRuntime * runtime
NodesModifierUsageInferenceCache usage_cache
Definition MOD_nodes.hh:69
std::shared_ptr< bke::bake::ModifierCache > cache
Definition MOD_nodes.hh:64
std::shared_ptr< nodes::eval_log::NodesEvalLog > eval_log
Definition MOD_nodes.hh:58
ui::Layout * layout
ListBaseT< SpaceLink > spacedata
struct bNodeTree * edittree
SpreadsheetTableIDGeometry geometry_id
eNodeTree_Type type
ListBaseT< struct ScrArea > areabase
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
static GeometrySet from_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
const Mesh * get_mesh() const
std::optional< std::variant< std::string, Span< std::byte > > > meta_data_source
Map< int, std::unique_ptr< SimulationNodeCache > > simulation_cache_by_id
Map< int, std::unique_ptr< BakeNodeCache > > bake_cache_by_id
std::unique_ptr< MemoryBlobReader > memory_blob_reader
Vector< std::unique_ptr< FrameCache > > frames
std::unique_ptr< BlobReadSharing > blob_sharing
Map< uint32_t, ObjectDependencyInfo > objects_info
const Set< ComputeContextHash > * verbose_log_contexts
const GeoNodesSideEffectNodes * side_effect_nodes
MultiValueMap< std::pair< ComputeContextHash, int32_t >, int > iterations_by_iteration_zone
MultiValueMap< ComputeContextHash, const lf::FunctionNode * > nodes_by_context
std::function< void(bke::bake::BakeValues values)> store_fn
struct WorkSpaceInstanceHook * workspace_hook
#define N_(msgid)