Blender V5.3
node_geo_bake.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <fmt/format.h>
6
7#include "NOD_geo_bake.hh"
13
15#include "UI_resources.hh"
16
17#include "BLI_path_utils.hh"
18#include "BLI_string_utf8.hh"
19
22#include "BKE_context.hh"
23#include "BKE_global.hh"
24#include "BKE_library.hh"
25#include "BKE_main.hh"
26#include "BKE_screen.hh"
27
28#include "ED_node.hh"
29
30#include "DNA_modifier_types.h"
31
32#include "RNA_access.hh"
33#include "RNA_prototypes.hh"
34
35#include "MOD_nodes.hh"
36
37#include "WM_api.hh"
38
39#include "BLO_read_write.hh"
40
41#include "GEO_mix_geometries.hh"
42
43#include "node_geometry_util.hh"
44
45namespace blender {
46
48
49namespace bake = bke::bake;
50
52
54{
55 b.use_custom_socket_order();
56 b.allow_any_socket_order();
57
58 b.add_default_layout();
59
60 const bNodeTree *ntree = b.tree_or_null();
61 const bNode *node = b.node_or_null();
62 if (!node) {
63 return;
64 }
65 const NodeGeometryBake &storage = node_storage(*node);
66
67 for (const int i : IndexRange(storage.items_num)) {
68 const NodeGeometryBakeItem &item = storage.items[i];
69 const eNodeSocketDatatype socket_type = item.socket_type;
70 const UString name(item.name);
72 auto &input_decl = b.add_input(socket_type, name, identifier)
73 .socket_name_ptr(
74 &ntree->id, *BakeItemsAccessor::item_srna, &item, "name");
75 auto &output_decl = b.add_output(socket_type, name, identifier).align_with_previous();
76 if (socket_type_supports_attributes(socket_type)) {
78 output_decl.inferred_structure_type({input_decl.index()});
79 }
80 }
81 input_decl.structure_type(StructureType::Dynamic);
82 output_decl.structure_type(StructureType::Dynamic);
83 if (socket_type == SOCK_BUNDLE) {
84 dynamic_cast<decl::BundleBuilder &>(output_decl)
85 .pass_through_input_index(input_decl.index());
86 }
87 }
88 b.add_input<decl::Extend>(""_ustr, "__extend__"_ustr)
89 .structure_type(StructureType::Dynamic)
90 .custom_draw(socket_items::ui::draw_extend_socket_fn<BakeItemsAccessor>());
91 b.add_output<decl::Extend>(""_ustr, "__extend__"_ustr)
92 .structure_type(StructureType::Dynamic)
93 .align_with_previous();
94}
95
96static void node_init(bNodeTree * /*tree*/, bNode *node)
97{
98 NodeGeometryBake *data = MEM_new<NodeGeometryBake>(__func__);
99 node->storage = data;
100}
101
103{
105 MEM_delete(reinterpret_cast<NodeGeometryBake *>(node->storage));
106}
107
108static void node_copy_storage(bNodeTree * /*tree*/, bNode *dst_node, const bNode *src_node)
109{
110 const NodeGeometryBake &src_storage = node_storage(*src_node);
111 auto *dst_storage = MEM_new<NodeGeometryBake>(__func__, dna::shallow_copy(src_storage));
112 dst_node->storage = dst_storage;
113
115}
116
118{
120 params.ntree, params.node, params.node, params.link);
121}
122
123static void draw_bake_items(const bContext *C, ui::Layout &layout, PointerRNA node_ptr)
124{
125 bNodeTree &tree = *reinterpret_cast<bNodeTree *>(node_ptr.owner_id);
126 bNode &node = *static_cast<bNode *>(node_ptr.data);
127 NodeGeometryBake &storage = node_storage(node);
128
129 if (ui::Layout *panel = layout.panel(C, "bake_items", false, IFACE_("Bake Items"))) {
130 socket_items::ui::draw_items_list_with_operators<BakeItemsAccessor>(C, panel, tree, node);
131 socket_items::ui::draw_active_item_props<BakeItemsAccessor>(
132 tree, node, [&](PointerRNA *item_ptr) {
133 const NodeGeometryBakeItem &active_item = storage.items[storage.active_index];
134 const eNodeSocketDatatype socket_type = active_item.socket_type;
135 panel->use_property_split_set(true);
136 panel->use_property_decorate_set(false);
137 panel->prop(item_ptr, "socket_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
138 if (socket_type_supports_attributes(socket_type)) {
139 panel->prop(item_ptr, "attribute_domain", UI_ITEM_NONE, std::nullopt, ICON_NONE);
140 panel->prop(item_ptr, "is_attribute", UI_ITEM_NONE, std::nullopt, ICON_NONE);
141 }
142 });
143 }
144}
145
150
156 private:
157 Mutex mutex_;
159
160 public:
162 {
163 std::lock_guard lock{mutex_};
164 return map_.lookup_default(key, nullptr);
165 }
166
167 void try_add(ID &id) override
168 {
169 std::lock_guard lock{mutex_};
170 map_.add(bake::BakeDataBlockID(id), &id);
171 }
172};
173
174class LazyFunctionForBakeNode final : public LazyFunction {
175 const bNode &node_;
176 Span<NodeGeometryBakeItem> bake_items_;
177
178 public:
180 : node_(node)
181 {
182 debug_name_ = "Bake";
183 const NodeGeometryBake &storage = node_storage(node);
184 bake_items_ = {storage.items, storage.items_num};
185
186 MutableSpan<int> lf_index_by_bsocket = lf_graph_info.mapping.lf_index_by_bsocket;
187
188 for (const int i : bake_items_.index_range()) {
189 const NodeGeometryBakeItem &item = bake_items_[i];
190 const bNodeSocket &input_bsocket = node.input_socket(i);
191 const bNodeSocket &output_bsocket = node.output_socket(i);
192 lf_index_by_bsocket[input_bsocket.index_in_tree()] = inputs_.append_and_get_index_as(
193 item.name, CPPType::get<SocketValueVariant>(), lf::ValueUsage::Maybe);
194 lf_index_by_bsocket[output_bsocket.index_in_tree()] = outputs_.append_and_get_index_as(
196 }
197 }
198
199 void execute_impl(lf::Params &params, const lf::Context &context) const final
200 {
201 GeoNodesUserData &user_data = *static_cast<GeoNodesUserData *>(context.user_data);
202 GeoNodesLocalUserData &local_user_data = *static_cast<GeoNodesLocalUserData *>(
203 context.local_user_data);
204 if (!user_data.call_data->self_object()) {
205 /* The self object is currently required for generating anonymous attribute names. */
206 this->set_default_outputs(params);
207 return;
208 }
209 if (!user_data.call_data->bake_params) {
210 this->set_default_outputs(params);
211 return;
212 }
213 std::optional<FoundNestedNodeID> found_id = find_nested_node_id(user_data, node_.identifier);
214 if (!found_id) {
215 this->set_default_outputs(params);
216 return;
217 }
218 if (found_id->is_in_loop || found_id->is_in_closure) {
219 DummyDataBlockMap data_block_map;
220 this->pass_through(params, user_data, &data_block_map);
221 return;
222 }
223 BakeNodeBehavior *behavior = user_data.call_data->bake_params->get(found_id->id);
224 if (!behavior) {
225 this->set_default_outputs(params);
226 return;
227 }
228 if (auto *info = std::get_if<sim_output::ReadSingle>(&behavior->behavior)) {
229 this->output_cached_state(params, user_data, behavior->data_block_map, info->values);
230 }
231 else if (auto *info = std::get_if<sim_output::ReadInterpolated>(&behavior->behavior)) {
232 this->output_mixed_cached_state(params,
233 behavior->data_block_map,
234 *user_data.compute_context,
235 info->prev_values,
236 info->next_values,
237 info->mix_factor);
238 }
239 else if (std::get_if<sim_output::PassThrough>(&behavior->behavior)) {
240 this->pass_through(params, user_data, behavior->data_block_map);
241 }
242 else if (auto *info = std::get_if<sim_output::StoreNewState>(&behavior->behavior)) {
243 this->store(params, user_data, behavior->data_block_map, *info);
244 }
245 else if (auto *info = std::get_if<sim_output::ReadError>(&behavior->behavior)) {
246 if (eval_log::NodeTreeLogger *tree_logger = local_user_data.try_get_tree_logger(user_data)) {
247 tree_logger->node_warnings.append(
248 *tree_logger->allocator, {node_.identifier, {NodeWarningType::Error, info->message}});
249 }
250 this->set_default_outputs(params);
251 }
252 else {
254 }
255 }
256
257 void set_default_outputs(lf::Params &params) const
258 {
260 }
261
262 void pass_through(lf::Params &params,
263 GeoNodesUserData &user_data,
264 bke::bake::BakeDataBlockMap *data_block_map) const
265 {
266 std::optional<bake::BakeValues> bake_values = this->get_bake_values_from_inputs(
267 params, data_block_map, false);
268 if (!bake_values) {
269 /* Wait for inputs to be computed. */
270 return;
271 }
273 std::move(*bake_values), data_block_map, *user_data.compute_context);
274 for (const int i : bake_items_.index_range()) {
275 params.set_output(i, std::move(output_values[i]));
276 }
277 }
278
279 void store(lf::Params &params,
280 GeoNodesUserData &user_data,
281 bke::bake::BakeDataBlockMap *data_block_map,
282 const sim_output::StoreNewState &info) const
283 {
284 std::optional<bake::BakeValues> bake_values = this->get_bake_values_from_inputs(
285 params, data_block_map);
286 if (!bake_values) {
287 /* Wait for inputs to be computed. */
288 return;
289 }
290 this->output_cached_state(params, user_data, data_block_map, *bake_values);
291 info.store_fn(std::move(*bake_values));
292 }
293
294 void output_cached_state(lf::Params &params,
295 GeoNodesUserData &user_data,
296 bke::bake::BakeDataBlockMap *data_block_map,
297 const bake::BakeValues &bake_values) const
298 {
300 bake_values, data_block_map, *user_data.compute_context);
301 for (const int i : bake_items_.index_range()) {
302 params.set_output(i, std::move(values[i]));
303 }
304 }
305
306 void output_mixed_cached_state(lf::Params &params,
307 bke::bake::BakeDataBlockMap *data_block_map,
308 const ComputeContext &compute_context,
309 const bake::BakeValues &prev_bake_values,
310 const bake::BakeValues &next_bake_values,
311 const float mix_factor) const
312 {
314 prev_bake_values, data_block_map, compute_context);
316 next_bake_values, data_block_map, compute_context);
317 if (mix_factor != 0.0f) {
318 for (const int i : bake_items_.index_range()) {
319 geometry::mix_socket_values(output_values[i], next_values[i], mix_factor);
320 }
321 }
322 for (const int i : bake_items_.index_range()) {
323 params.set_output(i, std::move(output_values[i]));
324 }
325 }
326
327 std::optional<bake::BakeValues> get_bake_values_from_inputs(
328 lf::Params &params,
329 bke::bake::BakeDataBlockMap *data_block_map,
330 const bool is_for_cache = true) const
331 {
332 Array<bke::SocketValueVariant *> input_value_pointers(bake_items_.size());
333 for (const int i : bake_items_.index_range()) {
334 input_value_pointers[i] = params.try_get_input_data_ptr_or_request<bke::SocketValueVariant>(
335 i);
336 }
337 if (input_value_pointers.as_span().contains(nullptr)) {
338 /* Wait for inputs to be computed. */
339 return std::nullopt;
340 }
341
342 Vector<bake::BakeValues::InputValue> bake_input_values(bake_items_.size());
343 for (const int i : bake_items_.index_range()) {
344 const NodeGeometryBakeItem &item = bake_items_[i];
345 bake::BakeValues::InputValue &bake_input_value = bake_input_values[i];
346 bake_input_value.id = item.identifier;
347 bake_input_value.name = item.name;
348 bake_input_value.field_domain = AttrDomain(item.attribute_domain);
349 bake_input_value.value = std::move(*input_value_pointers[i]);
350 }
351
353 std::move(bake_input_values), data_block_map, is_for_cache);
354 }
355
357 bke::bake::BakeDataBlockMap *data_block_map,
358 const ComputeContext &compute_context) const
359 {
361 for (const NodeGeometryBakeItem &item : bake_items_) {
362 keys.append({item.identifier, item.socket_type});
363 }
364 return bake_values.to_runtime_values(keys, compute_context, data_block_map);
365 }
366
368 const Object &self_object,
369 const ComputeContext &compute_context,
370 const NodeGeometryBakeItem &item,
371 const CPPType &type) const
372 {
373 std::string attribute_name = bke::hash_to_anonymous_attribute_name(
374 compute_context.hash(), self_object.id.name, node_.identifier, item.identifier);
375 std::string socket_inspection_name = make_anonymous_attribute_socket_inspection_string(
376 node_.label_or_name(), item.name);
377 return ImplicitSharingPtr<AttributeFieldInput>(MEM_new<AttributeFieldInput>(
378 __func__, std::move(attribute_name), type, std::move(socket_inspection_name)));
379 }
380};
381
383{
384 BakeDrawContext ctx;
385 if (!get_bake_draw_context(&params.C, params.node, ctx)) {
386 return;
387 }
390 row.text = TIP_("Cannot bake in zone");
391 row.icon = ICON_STATUS_ERROR;
392 params.rows.append(std::move(row));
393 }
394 if (ctx.is_baked) {
396 row.text = get_baked_string(ctx);
397 params.rows.append(std::move(row));
398 }
399}
400
401static void node_layout(ui::Layout &layout, bContext *C, PointerRNA *ptr)
402{
403 BakeDrawContext ctx;
404 const bNode &node = *static_cast<const bNode *>(ptr->data);
405 if (!get_bake_draw_context(C, node, ctx)) {
406 return;
407 }
409 layout.enabled_set(ID_IS_EDITABLE(ctx.object));
410 ui::Layout &col = layout.column(false);
411 {
412 ui::Layout &row = col.row(true);
413 row.enabled_set(!ctx.is_baked);
414 row.prop(&ctx.bake_rna, "bake_mode", ui::ITEM_R_EXPAND, IFACE_("Mode"), ICON_NONE);
415 }
416 draw_bake_button_row(ctx, col);
417}
418
419static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *ptr)
420{
421 draw_bake_items(C, layout, *ptr);
422
423 BakeDrawContext ctx;
424 const bNode &node = *static_cast<const bNode *>(ptr->data);
425 if (!get_bake_draw_context(C, node, ctx)) {
426 return;
427 }
428
430 layout.enabled_set(ID_IS_EDITABLE(ctx.object));
431
432 {
433 ui::Layout &col = layout.column(false);
434 {
435 ui::Layout &row = col.row(true);
436 row.enabled_set(!ctx.is_baked);
437 row.prop(&ctx.bake_rna, "bake_mode", ui::ITEM_R_EXPAND, IFACE_("Mode"), ICON_NONE);
438 }
439
440 draw_bake_button_row(ctx, col, true);
441 if (const std::optional<std::string> bake_state_str = get_bake_state_string(ctx)) {
442 ui::Layout &row = col.row(true);
443 row.label(*bake_state_str, ICON_NONE);
444 }
445 }
446
447 draw_common_bake_settings(C, ctx, layout);
448 draw_data_blocks(C, layout, ctx.bake_rna);
449}
450
452{
453 const eNodeSocketDatatype type = params.other_socket().type;
455 return;
456 }
457 params.add_item(
458 IFACE_("Value"),
459 [type](LinkSearchOpParams &params) {
460 bNode &node = params.add_node("GeometryNodeBake"_ustr);
462 params.node_tree, node, type, params.socket.name);
463 params.update_and_connect_available_socket(node, UString(params.socket.name));
464 },
465 -1);
466}
467
469 const bNode &node,
470 const bNodeSocket &output_socket)
471{
472 /* Internal links should always map corresponding input and output sockets. */
473 return node.input_by_identifier(output_socket.identifier_ustr());
474}
475
476static void node_blend_write(const bNodeTree & /*tree*/, const bNode &node, BlendWriter &writer)
477{
479}
480
485
486static void node_register()
487{
488 static bke::bNodeType ntype;
489 geo_node_type_base(&ntype, "GeometryNodeBake"_ustr, GEO_NODE_BAKE);
490 ntype.ui_name = "Bake";
491 ntype.ui_description = "Cache the incoming data so that it can be used without recomputation";
492 ntype.enum_name_legacy = "BAKE";
494 ntype.declare = node_declare;
496 ntype.initfunc = node_init;
507}
508NOD_REGISTER_NODE(node_register)
509
510} // namespace nodes::node_geo_bake_cc
511
512namespace nodes {
513
515{
517 r_ctx.node = &node;
518 r_ctx.snode = CTX_wm_space_node(C);
519 if (!r_ctx.snode) {
520 return false;
521 }
522 std::optional<ed::space_node::ObjectAndModifier> object_and_modifier =
524 if (!object_and_modifier) {
525 return false;
526 }
527 r_ctx.object = object_and_modifier->object;
528 r_ctx.nmd = object_and_modifier->nmd;
529 const std::optional<FoundNestedNodeID> bake_id = ed::space_node::find_nested_node_id_in_root(
530 *r_ctx.snode, *r_ctx.node);
531 if (!bake_id) {
532 return false;
533 }
534 r_ctx.is_bakeable_in_current_context = !bake_id->is_in_loop && !bake_id->is_in_closure;
535 r_ctx.bake = nullptr;
536 for (const NodesModifierBake &iter_bake : Span(r_ctx.nmd->bakes, r_ctx.nmd->bakes_num)) {
537 if (iter_bake.id == bake_id->id) {
538 r_ctx.bake = &iter_bake;
539 break;
540 }
541 }
542 if (!r_ctx.bake) {
543 return false;
544 }
545
547 const_cast<ID &>(r_ctx.object->id),
548 RNA_NodesModifier,
549 const_cast<NodesModifierData *>(r_ctx.nmd));
551 modifier_ptr, RNA_NodesModifierBake, const_cast<NodesModifierBake *>(r_ctx.bake));
552 if (r_ctx.nmd->runtime->cache) {
553 const bke::bake::ModifierCache &cache = *r_ctx.nmd->runtime->cache;
554 std::lock_guard lock{cache.mutex};
555 if (const std::unique_ptr<bke::bake::BakeNodeCache> *node_cache_ptr =
556 cache.bake_cache_by_id.lookup_ptr(bake_id->id))
557 {
558 const bke::bake::BakeNodeCache &node_cache = **node_cache_ptr;
559 if (!node_cache.bake.frames.is_empty()) {
560 const int first_frame = node_cache.bake.frames.first()->frame.frame();
561 const int last_frame = node_cache.bake.frames.last()->frame.frame();
562 r_ctx.baked_range = IndexRange(first_frame, last_frame - first_frame + 1);
563 }
564 }
565 else if (const std::unique_ptr<bke::bake::SimulationNodeCache> *node_cache_ptr =
566 cache.simulation_cache_by_id.lookup_ptr(bake_id->id))
567 {
568 const bke::bake::SimulationNodeCache &node_cache = **node_cache_ptr;
569 if (!node_cache.bake.frames.is_empty() &&
571 {
572 const int first_frame = node_cache.bake.frames.first()->frame.frame();
573 const int last_frame = node_cache.bake.frames.last()->frame.frame();
574 r_ctx.baked_range = IndexRange(first_frame, last_frame - first_frame + 1);
575 }
576 }
577 }
578 const Scene *scene = CTX_data_scene(C);
580 *scene, *r_ctx.object, *r_ctx.nmd, r_ctx.bake->id);
581 r_ctx.bake_still = node.type_legacy == GEO_NODE_BAKE &&
583 r_ctx.is_baked = r_ctx.baked_range.has_value();
584 r_ctx.bake_target = bke::bake::get_node_bake_target(*r_ctx.object, *r_ctx.nmd, r_ctx.bake->id);
585
586 return true;
587}
588
589std::string get_baked_string(const BakeDrawContext &ctx)
590{
591 if (ctx.bake_still && ctx.baked_range->size() == 1) {
592 return fmt::format(fmt::runtime(RPT_("Baked Frame {}")), ctx.baked_range->first());
593 }
594 return fmt::format(
595 fmt::runtime(RPT_("Baked {} - {}")), ctx.baked_range->first(), ctx.baked_range->last());
596}
597
598std::optional<std::string> get_bake_state_string(const BakeDrawContext &ctx)
599{
600 if (G.is_rendering) {
601 /* Avoid accessing data that is generated while baking. */
602 return std::nullopt;
603 }
604 if (ctx.is_baked) {
605 const std::string baked_str = get_baked_string(ctx);
607 BLI_str_format_byte_unit(size_str, ctx.bake->bake_size, true);
608 if (ctx.bake->packed) {
609 return fmt::format(fmt::runtime(RPT_("{} ({} packed)")), baked_str, size_str);
610 }
611 return fmt::format(fmt::runtime(RPT_("{} ({} on disk)")), baked_str, size_str);
612 }
613 if (ctx.frame_range.has_value()) {
614 if (!ctx.bake_still) {
615 return fmt::format(
616 fmt::runtime(RPT_("Frames {} - {}")), ctx.frame_range->first(), ctx.frame_range->last());
617 }
618 }
619 return std::nullopt;
620}
621
622void draw_bake_button_row(const BakeDrawContext &ctx, ui::Layout &layout, const bool is_in_sidebar)
623{
624 ui::Layout &col = layout.column(true);
625 ui::Layout &row = col.row(true);
626 {
627 const char *bake_label = IFACE_("Bake");
628 if (is_in_sidebar) {
629 bake_label = ctx.bake_target == NODES_MODIFIER_BAKE_TARGET_DISK ? IFACE_("Bake to Disk") :
630 IFACE_("Bake Packed");
631 }
632
633 PointerRNA ptr = row.op("OBJECT_OT_geometry_node_bake_single",
634 bake_label,
635 ICON_NONE,
639 RNA_string_set(&ptr, "modifier_name", ctx.nmd->modifier.name);
640 RNA_int_set(&ptr, "bake_id", ctx.bake->id);
641 }
642 {
643 ui::Layout &subrow = row.row(true);
644 subrow.active_set(ctx.is_baked);
645 if (is_in_sidebar) {
646 if (ctx.is_baked && !G.is_rendering) {
647 if (ctx.bake->packed) {
648 PointerRNA ptr = subrow.op("OBJECT_OT_geometry_node_bake_unpack_single",
649 "",
650 ICON_PACKAGE,
654 RNA_string_set(&ptr, "modifier_name", ctx.nmd->modifier.name);
655 RNA_int_set(&ptr, "bake_id", ctx.bake->id);
656 }
657 else {
658 PointerRNA ptr = subrow.op("OBJECT_OT_geometry_node_bake_pack_single",
659 "",
660 ICON_UGLYPACKAGE,
664 RNA_string_set(&ptr, "modifier_name", ctx.nmd->modifier.name);
665 RNA_int_set(&ptr, "bake_id", ctx.bake->id);
666 }
667 }
668 else {
669 /* If the data is not yet baked, still show the icon based on the derived bake target. */
670 const int icon = ctx.bake_target == NODES_MODIFIER_BAKE_TARGET_DISK ? ICON_UGLYPACKAGE :
671 ICON_PACKAGE;
672 PointerRNA ptr = subrow.op("OBJECT_OT_geometry_node_bake_pack_single",
673 "",
674 icon,
677 }
678 }
679 {
680 PointerRNA ptr = subrow.op("OBJECT_OT_geometry_node_bake_delete_single",
681 "",
682 ICON_TRASH,
686 RNA_string_set(&ptr, "modifier_name", ctx.nmd->modifier.name);
687 RNA_int_set(&ptr, "bake_id", ctx.bake->id);
688 }
689 }
690}
691
693{
694 layout.use_property_split_set(true);
695 layout.use_property_decorate_set(false);
696
697 ui::Layout &settings_col = layout.column(false);
698 settings_col.active_set(!ctx.is_baked);
699 {
700 ui::Layout &col = settings_col.column(true);
701 col.prop(&ctx.bake_rna, "bake_target", UI_ITEM_NONE, std::nullopt, ICON_NONE);
702 ui::Layout &subcol = col.column(true);
704 subcol.prop(&ctx.bake_rna, "use_custom_path", UI_ITEM_NONE, IFACE_("Custom Path"), ICON_NONE);
705 ui::Layout &subsubcol = subcol.column(true);
706 const bool use_custom_path = ctx.bake->flag & NODES_MODIFIER_BAKE_CUSTOM_PATH;
707 subsubcol.active_set(use_custom_path);
708 Main *bmain = CTX_data_main(C);
709 auto bake_path = bke::bake::get_node_bake_path(*bmain, *ctx.object, *ctx.nmd, ctx.bake->id);
710
711 char placeholder_path[FILE_MAX] = "";
712 if (StringRef(ctx.bake->directory).is_empty() &&
713 !(ctx.bake->flag & NODES_MODIFIER_BAKE_CUSTOM_PATH) && bake_path.has_value() &&
714 bake_path->bake_dir.has_value())
715 {
716 STRNCPY(placeholder_path, bake_path->bake_dir->c_str());
718 BLI_path_rel(placeholder_path, BKE_main_blendfile_path(bmain));
719 }
720 }
721
722 subsubcol.prop(&ctx.bake_rna,
723 RNA_struct_find_property(&ctx.bake_rna, "directory"),
724 -1,
725 0,
727 IFACE_("Path"),
728 ICON_NONE,
729 placeholder_path);
730 }
731 {
732 ui::Layout &col = settings_col.column(true);
733 col.prop(&ctx.bake_rna,
734 "use_custom_simulation_frame_range",
736 IFACE_("Custom Range"),
737 ICON_NONE);
738 ui::Layout &subcol = col.column(true);
740 subcol.prop(&ctx.bake_rna, "frame_start", UI_ITEM_NONE, IFACE_("Start"), ICON_NONE);
741 subcol.prop(&ctx.bake_rna, "frame_end", UI_ITEM_NONE, IFACE_("End"), ICON_NONE);
742 }
743}
744
745static void draw_bake_data_block_list_item(uiList * /*ui_list*/,
746 const bContext * /*C*/,
747 ui::Layout &layout,
748 PointerRNA * /*idataptr*/,
749 PointerRNA *itemptr,
750 int /*icon*/,
751 PointerRNA * /*active_dataptr*/,
752 const char * /*active_propname*/,
753 int /*index*/,
754 int /*flt_flag*/)
755{
756 auto &data_block = *static_cast<NodesModifierDataBlock *>(itemptr->data);
757 ui::Layout &row = layout.row(true);
758
759 std::string name;
760 if (StringRef(data_block.lib_name).is_empty()) {
761 name = data_block.id_name;
762 }
763 else {
764 name = fmt::format("{} [{}]", data_block.id_name, data_block.lib_name);
765 }
766
767 row.prop(itemptr, "id", UI_ITEM_NONE, name, ICON_NONE);
768}
769
770void draw_data_blocks(const bContext *C, ui::Layout &layout, PointerRNA &bake_rna)
771{
772 static const uiListType *data_block_list = []() {
774 STRNCPY_UTF8(list->idname, "DATA_UL_nodes_modifier_data_blocks");
777 return list;
778 }();
779
780 PointerRNA data_blocks_ptr = RNA_pointer_create_discrete(
781 bake_rna.owner_id, RNA_NodesModifierBakeDataBlocks, bake_rna.data);
782
783 if (ui::Layout *panel = layout.panel(
784 C, "data_block_references", true, IFACE_("Data-Block References")))
785 {
787 C,
788 data_block_list->idname,
789 "",
790 &bake_rna,
791 "data_blocks",
792 &data_blocks_ptr,
793 "active_index",
794 nullptr,
795 3,
796 5,
799 }
800}
801
802std::unique_ptr<LazyFunction> get_bake_lazy_function(
803 const bNode &node, GeometryNodesLazyFunctionGraphInfo &lf_graph_info)
804{
805 namespace file_ns = nodes::node_geo_bake_cc;
806 BLI_assert(node.type_legacy == GEO_NODE_BAKE);
807 return std::make_unique<file_ns::LazyFunctionForBakeNode>(node, lf_graph_info);
808}
809
810StructRNA **BakeItemsAccessor::item_srna = &RNA_NodeGeometryBakeItem;
811
813{
814 writer->write_string(item.name);
815}
816
818{
819 BLO_read_string(reader, &item.name);
820}
821
822}; // namespace nodes
823
824} // namespace blender
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_SIMULATION_OUTPUT
#define GEO_NODE_BAKE
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define FILE_MAX
#define BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE
Definition BLI_string.hh:31
#define STRNCPY_UTF8(dst, src)
#define RPT_(msgid)
#define TIP_(msgid)
#define IFACE_(msgid)
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
volatile int lock
#define BLI_assert_unreachable()
#define ELEM(...)
constexpr Span()=default
Span< T > as_span() const
Definition BLI_array.hh:254
static const CPPType & get()
const ComputeContextHash & hash() const
constexpr IndexRange()=default
UString()=default
void append(const T &value)
static BakeValues from_runtime_values(Vector< InputValue > runtime_values, BakeDataBlockMap *data_block_map, bool is_for_cache=true)
Vector< SocketValueVariant > to_runtime_values(const Span< OutputKey > keys, const ComputeContext &compute_context, BakeDataBlockMap *data_block_map) const
constexpr bool is_empty() const
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
virtual BakeNodeBehavior * get(const int id) const =0
std::optional< bake::BakeValues > get_bake_values_from_inputs(lf::Params &params, bke::bake::BakeDataBlockMap *data_block_map, const bool is_for_cache=true) const
LazyFunctionForBakeNode(const bNode &node, GeometryNodesLazyFunctionGraphInfo &lf_graph_info)
void store(lf::Params &params, GeoNodesUserData &user_data, bke::bake::BakeDataBlockMap *data_block_map, const sim_output::StoreNewState &info) const
Vector< SocketValueVariant > bake_to_output_values(const bake::BakeValues &bake_values, bke::bake::BakeDataBlockMap *data_block_map, const ComputeContext &compute_context) const
void output_cached_state(lf::Params &params, GeoNodesUserData &user_data, bke::bake::BakeDataBlockMap *data_block_map, const bake::BakeValues &bake_values) const
ImplicitSharingPtr< AttributeFieldInput > make_attribute_field(const Object &self_object, const ComputeContext &compute_context, const NodeGeometryBakeItem &item, const CPPType &type) const
void pass_through(lf::Params &params, GeoNodesUserData &user_data, bke::bake::BakeDataBlockMap *data_block_map) const
void execute_impl(lf::Params &params, const lf::Context &context) const final
void output_mixed_cached_state(lf::Params &params, bke::bake::BakeDataBlockMap *data_block_map, const ComputeContext &compute_context, const bake::BakeValues &prev_bake_values, const bake::BakeValues &next_bake_values, const float mix_factor) const
#define storage(slot, qualifiers)
std::optional< NodesModifierBakeTarget > get_node_bake_target(const Object &object, const NodesModifierData &nmd, int node_id)
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)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
std::string hash_to_anonymous_attribute_name(Args &&...args)
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
std::optional< ObjectAndModifier > get_geometry_nodes_modifier_for_node_editor(const SpaceNode &snode)
std::optional< nodes::FoundNestedNodeID > find_nested_node_id_in_root(const SpaceNode &snode, const bNode &node)
void mix_socket_values(bke::SocketValueVariant &a, const bke::SocketValueVariant &b, const float factor)
static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *ptr)
static void draw_bake_items(const bContext *C, ui::Layout &layout, PointerRNA node_ptr)
static bool node_insert_link(bke::NodeInsertLinkParams &params)
static void node_blend_write(const bNodeTree &, const bNode &node, BlendWriter &writer)
static void node_extra_info(NodeExtraInfoParams &params)
static void node_layout(ui::Layout &layout, bContext *C, PointerRNA *ptr)
static void node_free_storage(bNode *node)
static void node_blend_read(bNodeTree &, bNode &node, BlendDataReader &reader)
static const bNodeSocket * node_internally_linked_input(const bNodeTree &, const bNode &node, const bNodeSocket &output_socket)
static void node_copy_storage(bNodeTree *, bNode *dst_node, const bNode *src_node)
static void node_declare(NodeDeclarationBuilder &b)
static void node_init(bNodeTree *, bNode *node)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
void blend_write(BlendWriter *writer, const bNode &node)
void blend_read_data(BlendDataReader *reader, bNode &node)
void copy_array(const bNode &src_node, bNode &dst_node)
Accessor::ItemT * add_item_with_socket_type_and_name(bNodeTree &ntree, bNode &node, const eNodeSocketDatatype socket_type, const char *name, std::optional< int > dimensions=std::nullopt)
bool try_add_item_via_any_extend_socket(bNodeTree &ntree, bNode &extend_node, bNode &storage_node, bNodeLink &link, const std::optional< StringRef > socket_identifier=std::nullopt, typename Accessor::ItemT **r_new_item=nullptr)
bool get_bake_draw_context(const bContext *C, const bNode &node, BakeDrawContext &r_ctx)
void draw_bake_button_row(const BakeDrawContext &ctx, ui::Layout &layout, bool is_in_sidebar=false)
void draw_data_blocks(const bContext *C, ui::Layout &layout, PointerRNA &bake_rna)
static void draw_bake_data_block_list_item(uiList *, const bContext *, ui::Layout &layout, PointerRNA *, PointerRNA *itemptr, int, PointerRNA *, const char *, int, int)
bool socket_type_supports_attributes(const eNodeSocketDatatype socket_type)
void draw_common_bake_settings(bContext *C, BakeDrawContext &ctx, ui::Layout &layout)
std::string make_anonymous_attribute_socket_inspection_string(const bNodeSocket &socket)
std::unique_ptr< LazyFunction > get_bake_lazy_function(const bNode &node, GeometryNodesLazyFunctionGraphInfo &lf_graph_info)
std::optional< FoundNestedNodeID > find_nested_node_id(const GeoNodesUserData &user_data, const int node_id)
void set_default_remaining_node_outputs(lf::Params &params, const bNode &node)
std::optional< std::string > get_bake_state_string(const BakeDrawContext &ctx)
std::string get_baked_string(const BakeDrawContext &ctx)
void template_uilist(Layout *layout, const bContext *C, const char *listtype_name, const char *list_id, PointerRNA *dataptr, StringRefNull propname, PointerRNA *active_dataptr, StringRefNull active_propname, const char *item_dyntip_propname, int rows, int maxrows, int layout_type, enum TemplateListFlags flags)
BMesh Iterators.
void BLI_str_format_byte_unit(char dst[BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE], long long int bytes, bool base_10) ATTR_NONNULL(1)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
char * STRNCPY(char(&dst)[N], const char *src)
@ NODES_MODIFIER_BAKE_CUSTOM_PATH
@ NODES_MODIFIER_BAKE_CUSTOM_SIMULATION_FRAME_RANGE
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
Global G
Definition blender.cc:57
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BMesh const char void * data
@ NODES_MODIFIER_BAKE_MODE_STILL
void WM_operator_properties_id_lookup_set_from_id(PointerRNA *ptr, const ID *id)
bool void BLI_path_rel(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
SpaceNode * CTX_wm_space_node(const bContext *C)
@ NODES_MODIFIER_BAKE_TARGET_DISK
bool WM_uilisttype_add(uiListType *ult)
Main * CTX_data_main(const bContext *C)
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
std::mutex Mutex
Definition BLI_mutex.hh:51
PointerRNA RNA_pointer_create_id_subdata(ID &id, StructRNA *type, void *data)
PointerRNA RNA_pointer_create_with_parent(const PointerRNA &parent, StructRNA *type, void *data)
Scene * CTX_data_scene(const bContext *C)
@ GEO_NODE_BAKE_ITEM_IS_ATTRIBUTE
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
void write_string(const char *data)
char name[258]
Definition DNA_ID.h:433
eNodeSocketDatatype socket_type
NodesModifierPackedBake * packed
NodesModifierBakeMode bake_mode
NodesModifierRuntime * runtime
std::shared_ptr< bke::bake::ModifierCache > cache
Definition MOD_nodes.hh:64
eNodeSocketDatatype type
eNodeTree_Type type
Defines a node type.
Definition BKE_node.hh:265
NodeInternallyLinkedInputFunction internally_linked_input
Definition BKE_node.hh:415
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
NodeBlendWriteFunction blend_write_storage_content
Definition BKE_node.hh:421
void(*) register_operators()
Definition BKE_node.hh:448
std::string ui_description
Definition BKE_node.hh:271
NodeBlendDataReadFunction blend_data_read_storage_content
Definition BKE_node.hh:422
NodeExtraInfoFunction get_extra_info
Definition BKE_node.hh:412
void(*) draw_buttons_ex(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:292
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
bool(*) insert_link(NodeInsertLinkParams &params)
Definition BKE_node.hh:364
Map< int, std::unique_ptr< SimulationNodeCache > > simulation_cache_by_id
Map< int, std::unique_ptr< BakeNodeCache > > bake_cache_by_id
Vector< std::unique_ptr< FrameCache > > frames
std::optional< NodesModifierBakeTarget > bake_target
std::optional< IndexRange > frame_range
const NodesModifierData * nmd
std::optional< IndexRange > baked_range
const NodesModifierBake * bake
static bool supports_socket_type(const eNodeSocketDatatype socket_type, const int ntree_type)
static void blend_write_item(BlendWriter *writer, const ItemT &item)
static std::string socket_identifier_for_item(const NodeGeometryBakeItem &item)
static void blend_read_data_item(BlendDataReader *reader, ItemT &item)
eval_log::NodeTreeLogger * try_get_tree_logger(const GeoNodesUserData &user_data) const
Vector< NodeExtraInfoRow > & rows
ID * lookup_or_remember_missing(const bake::BakeDataBlockID &key) override
std::function< void(bke::bake::BakeValues values)> store_fn
char idname[BKE_ST_MAXNAME]
void enabled_set(bool enabled)
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
void label(StringRef name, int icon)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
PanelLayout panel(const bContext *C, StringRef idname, bool default_closed)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
void active_set(bool active)
Layout & column(bool align)
Layout & row(bool align)