Blender V5.3
node_geo_simulation.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 "BLI_string.hh"
6#include "BLI_string_utf8.hh"
7
10#include "BKE_context.hh"
11#include "BKE_curves.hh"
12#include "BKE_instances.hh"
13#include "BKE_modifier.hh"
15#include "BKE_screen.hh"
16
18
20
21#include "NOD_common.hh"
22#include "NOD_geo_bake.hh"
23#include "NOD_geo_simulation.hh"
25#include "NOD_socket.hh"
30
31#include "DNA_mesh_types.h"
33
34#include "ED_node.hh"
35
36#include "RNA_access.hh"
37#include "RNA_prototypes.hh"
38
39#include "BLT_translation.hh"
40
41#include "GEO_mix_geometries.hh"
42
43#include "WM_api.hh"
44
45#include "BLO_read_write.hh"
46
47#include "node_geometry_util.hh"
48
49namespace blender {
50
52
54 const Span<NodeSimulationItem> simulation_items,
55 const ComputeContext &compute_context,
56 bke::bake::BakeDataBlockMap *data_block_map,
57 bke::bake::BakeValues &&bake_values)
58{
60 for (const NodeSimulationItem &item : simulation_items) {
61 keys.append({item.identifier, item.socket_type});
62 }
63 Vector<SocketValueVariant> output_values = bake_values.to_runtime_values(
64 keys, compute_context, data_block_map);
65 bake_values.clear();
66 return output_values;
67}
68
69static void draw_simulation_state(const bContext *C,
70 ui::Layout &layout,
71 bNodeTree &ntree,
72 bNode &output_node)
73{
74 if (ui::Layout *panel = layout.panel(
75 C, "simulation_state_items", false, IFACE_("Simulation State")))
76 {
77 socket_items::ui::draw_items_list_with_operators<SimulationItemsAccessor>(
78 C, panel, ntree, output_node);
79 auto &storage = *static_cast<NodeGeometrySimulationOutput *>(output_node.storage);
80 socket_items::ui::draw_active_item_props<SimulationItemsAccessor>(
81 ntree, output_node, [&](PointerRNA *item_ptr) {
82 NodeSimulationItem &active_item = storage.items[storage.active_index];
83 const eNodeSocketDatatype socket_type = active_item.socket_type;
84 panel->use_property_split_set(true);
85 panel->use_property_decorate_set(false);
86 panel->prop(item_ptr, "socket_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
87 if (socket_type_supports_attributes(socket_type)) {
88 panel->prop(item_ptr, "attribute_domain", UI_ITEM_NONE, std::nullopt, ICON_NONE);
89 }
90 });
91 }
92}
93
95static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *current_node_ptr)
96{
97 bNodeTree &ntree = *reinterpret_cast<bNodeTree *>(current_node_ptr->owner_id);
98 bNode *current_node = static_cast<bNode *>(current_node_ptr->data);
99
100 const bke::bNodeTreeZones *zones = ntree.zones();
101 if (!zones) {
102 return;
103 }
104 const bke::bNodeTreeZone *zone = zones->get_zone_by_node(current_node->identifier);
105 if (!zone) {
106 return;
107 }
108 if (!zone->output_node_id) {
109 return;
110 }
111 bNode &output_node = const_cast<bNode &>(*zone->output_node());
112
113 BakeDrawContext ctx;
114 if (!get_bake_draw_context(C, output_node, ctx)) {
115 return;
116 }
118
119 draw_simulation_state(C, layout, ntree, output_node);
120
121 layout.use_property_split_set(true);
122 layout.use_property_decorate_set(false);
123
124 layout.enabled_set(ID_IS_EDITABLE(ctx.object));
125
126 {
127 ui::Layout &col = layout.column(false);
128 draw_bake_button_row(ctx, col, true);
129 if (const std::optional<std::string> bake_state_str = get_bake_state_string(ctx)) {
130 ui::Layout &row = col.row(true);
131 row.label(*bake_state_str, ICON_NONE);
132 }
133 }
134 draw_common_bake_settings(C, ctx, layout);
135 draw_data_blocks(C, layout, ctx.bake_rna);
136}
137
138namespace sim_input_node {
139
141
142class LazyFunctionForSimulationInputNode final : public LazyFunction {
143 const bNode &node_;
144 int32_t output_node_id_;
145 Span<NodeSimulationItem> simulation_items_;
146
147 public:
149 const bNode &node,
150 GeometryNodesLazyFunctionGraphInfo &own_lf_graph_info)
151 : node_(node)
152 {
153 debug_name_ = "Simulation Input";
154 output_node_id_ = node_storage(node).output_node_id;
155 const bNode &output_node = *node_tree.node_by_id(output_node_id_);
157 output_node.storage);
158 simulation_items_ = {storage.items, storage.items_num};
159
160 MutableSpan<int> lf_index_by_bsocket = own_lf_graph_info.mapping.lf_index_by_bsocket;
161 lf_index_by_bsocket[node.output_socket(0).index_in_tree()] = outputs_.append_and_get_index_as(
162 "Delta Time", CPPType::get<SocketValueVariant>());
163
164 for (const int i : simulation_items_.index_range()) {
165 const NodeSimulationItem &item = simulation_items_[i];
166 const bNodeSocket &input_bsocket = node.input_socket(i);
167 const bNodeSocket &output_bsocket = node.output_socket(i + 1);
168
169 lf_index_by_bsocket[input_bsocket.index_in_tree()] = inputs_.append_and_get_index_as(
170 item.name, CPPType::get<SocketValueVariant>(), lf::ValueUsage::Maybe);
171 lf_index_by_bsocket[output_bsocket.index_in_tree()] = outputs_.append_and_get_index_as(
173 }
174 }
175
176 void execute_impl(lf::Params &params, const lf::Context &context) const final
177 {
178 const GeoNodesUserData &user_data = *static_cast<const GeoNodesUserData *>(context.user_data);
179 if (!user_data.call_data->simulation_params) {
180 this->set_default_outputs(params);
181 return;
182 }
183 if (!user_data.call_data->self_object()) {
184 /* Self object is currently required for creating anonymous attribute names. */
185 this->set_default_outputs(params);
186 return;
187 }
188 std::optional<FoundNestedNodeID> found_id = find_nested_node_id(user_data, output_node_id_);
189 if (!found_id) {
190 this->set_default_outputs(params);
191 return;
192 }
193 if (found_id->is_in_loop || found_id->is_in_closure) {
194 this->set_default_outputs(params);
195 return;
196 }
197 SimulationZoneBehavior *zone_behavior = user_data.call_data->simulation_params->get(
198 found_id->id);
199 if (!zone_behavior) {
200 this->set_default_outputs(params);
201 return;
202 }
203 sim_input::Behavior &input_behavior = zone_behavior->input;
204 float delta_time = 0.0f;
205 if (auto *info = std::get_if<sim_input::UseCache>(&input_behavior)) {
206 delta_time = info->delta_time;
208 params, user_data, zone_behavior->data_block_map, std::move(info->values));
209 }
210 else if (std::get_if<sim_input::PassThrough>(&input_behavior)) {
211 delta_time = 0.0f;
212 this->pass_through(params, user_data, zone_behavior->data_block_map);
213 }
214 else {
216 }
217 if (!params.output_was_set(0)) {
218 params.set_output(0, SocketValueVariant(delta_time));
219 }
220 }
221
222 void set_default_outputs(lf::Params &params) const
223 {
225 }
226
227 void output_simulation_state(lf::Params &params,
228 const GeoNodesUserData &user_data,
229 bke::bake::BakeDataBlockMap *data_block_map,
230 bke::bake::BakeValues &&bake_values) const
231 {
233 simulation_items_, *user_data.compute_context, data_block_map, std::move(bake_values));
234 for (const int i : simulation_items_.index_range()) {
235 params.set_output(i + 1, std::move(output_values[i]));
236 }
237 }
238
239 void pass_through(lf::Params &params,
240 const GeoNodesUserData &user_data,
241 bke::bake::BakeDataBlockMap *data_block_map) const
242 {
243 /* Instead of outputting the initial values directly, convert them to a simulation state and
244 * then back. This ensures that some geometry processing happens on the data consistently (e.g.
245 * removing anonymous attributes). */
246 std::optional<bke::bake::BakeValues> bake_values = this->get_bake_values_from_inputs(
247 params, data_block_map);
248 if (!bake_values) {
249 /* Wait for inputs to be computed. */
250 return;
251 }
252 this->output_simulation_state(params, user_data, data_block_map, std::move(*bake_values));
253 }
254
255 std::optional<bke::bake::BakeValues> get_bake_values_from_inputs(
256 lf::Params &params, bke::bake::BakeDataBlockMap *data_block_map) const
257 {
258 Array<SocketValueVariant *> input_value_pointers(inputs_.size());
259 for (const int i : inputs_.index_range()) {
260 input_value_pointers[i] = params.try_get_input_data_ptr_or_request<SocketValueVariant>(i);
261 }
262 if (input_value_pointers.as_span().contains(nullptr)) {
263 /* Wait for inputs to be computed. */
264 return std::nullopt;
265 }
266 Vector<bke::bake::BakeValues::InputValue> input_values(simulation_items_.size());
267 for (const int i : inputs_.index_range()) {
268 const NodeSimulationItem &item = simulation_items_[i];
269 bke::bake::BakeValues::InputValue &input_value = input_values[i];
270 input_value.id = item.identifier;
271 input_value.name = item.name;
272 input_value.field_domain = AttrDomain(item.attribute_domain);
273 input_value.value = std::move(*input_value_pointers[i]);
274 }
275 return bke::bake::BakeValues::from_runtime_values(std::move(input_values), data_block_map);
276 }
277};
278
280{
281 b.use_custom_socket_order();
282 b.allow_any_socket_order();
283 b.add_output<decl::Float>("Delta Time"_ustr);
284
285 const bNode *node = b.node_or_null();
286 const bNodeTree *node_tree = b.tree_or_null();
287 if (ELEM(nullptr, node, node_tree)) {
288 return;
289 }
290
291 const bNode *output_node = node_tree->node_by_id(node_storage(*node).output_node_id);
292 if (!output_node) {
293 return;
294 }
295 const auto &output_storage = *static_cast<const NodeGeometrySimulationOutput *>(
296 output_node->storage);
297
298 for (const int i : IndexRange(output_storage.items_num)) {
299 const NodeSimulationItem &item = output_storage.items[i];
300 const eNodeSocketDatatype socket_type = item.socket_type;
301 if (socket_type == SOCK_GEOMETRY && i > 0) {
302 b.add_separator();
303 }
304 const UString name(item.name);
306 auto &input_decl = b.add_input(socket_type, name, identifier)
307 .socket_name_ptr(
308 &node_tree->id, *SimulationItemsAccessor::item_srna, &item, "name")
309 .structure_type(StructureType::Dynamic);
310 auto &output_decl = b.add_output(socket_type, name, identifier)
311 .align_with_previous()
312 .propagate_all({input_decl.index()})
313 .inferred_structure_type({input_decl.index()})
314 .structure_type(StructureType::Dynamic);
315 if (socket_type == SOCK_BUNDLE) {
316 dynamic_cast<decl::BundleBuilder &>(output_decl)
317 .pass_through_input_index(input_decl.index());
318 }
319 }
320 b.add_input<decl::Extend>(""_ustr, "__extend__"_ustr)
321 .structure_type(StructureType::Dynamic)
322 .custom_draw(socket_items::ui::draw_extend_socket_fn<SimulationItemsAccessor>());
323 b.add_output<decl::Extend>(""_ustr, "__extend__"_ustr)
324 .structure_type(StructureType::Dynamic)
325 .align_with_previous();
326}
327
328static void node_init(bNodeTree * /*tree*/, bNode *node)
329{
330 NodeGeometrySimulationInput *data = MEM_new<NodeGeometrySimulationInput>(__func__);
331 /* Needs to be initialized for the node to work. */
332 data->output_node_id = 0;
333 node->storage = data;
334}
335
336static void node_label(const bNodeTree * /*ntree*/,
337 const bNode * /*node*/,
338 char *label,
339 const int label_maxncpy)
340{
341 BLI_strncpy_utf8(label, CTX_IFACE_(BLT_I18NCONTEXT_ID_NODETREE, "Simulation"), label_maxncpy);
342}
343
345{
346 bNode *output_node = params.ntree.node_by_id(node_storage(params.node).output_node_id);
347 if (!output_node) {
348 return true;
349 }
351 params.ntree, params.node, *output_node, params.link);
352}
353
354static void node_register()
355{
356 static bke::bNodeType ntype;
357 geo_node_type_base(&ntype, "GeometryNodeSimulationInput"_ustr, GEO_NODE_SIMULATION_INPUT);
358 ntype.ui_name = "Simulation Input";
359 ntype.ui_description = "Input data for the simulation zone";
360 ntype.enum_name_legacy = "SIMULATION_INPUT";
362 ntype.initfunc = node_init;
363 ntype.declare = node_declare;
364 ntype.labelfunc = node_label;
366 ntype.gather_link_search_ops = nullptr;
367 ntype.no_muting = true;
370 "NodeGeometrySimulationInput",
374}
376
377} // namespace sim_input_node
378
380
382
383class LazyFunctionForSimulationOutputNode final : public LazyFunction {
384 const bNode &node_;
385 Span<NodeSimulationItem> simulation_items_;
386 int skip_input_index_;
392 int skip_inputs_offset_;
397 int solve_inputs_offset_;
398
399 public:
401 GeometryNodesLazyFunctionGraphInfo &own_lf_graph_info)
402 : node_(node)
403 {
404 debug_name_ = "Simulation Output";
405 const NodeGeometrySimulationOutput &storage = node_storage(node);
406 simulation_items_ = {storage.items, storage.items_num};
407
408 MutableSpan<int> lf_index_by_bsocket = own_lf_graph_info.mapping.lf_index_by_bsocket;
409
410 const bNodeSocket &skip_bsocket = node.input_socket(0);
411 skip_input_index_ = inputs_.append_and_get_index_as(
412 "Skip", CPPType::get<SocketValueVariant>(), lf::ValueUsage::Maybe);
413 lf_index_by_bsocket[skip_bsocket.index_in_tree()] = skip_input_index_;
414
415 skip_inputs_offset_ = inputs_.size();
416
417 /* Add the skip inputs that are linked to the simulation input node. */
418 for (const int i : simulation_items_.index_range()) {
419 const NodeSimulationItem &item = simulation_items_[i];
420 inputs_.append_as(item.name, CPPType::get<SocketValueVariant>(), lf::ValueUsage::Maybe);
421 }
422
423 solve_inputs_offset_ = inputs_.size();
424
425 /* Add the solve inputs that correspond to the simulation state inputs in the UI. */
426 for (const int i : simulation_items_.index_range()) {
427 const NodeSimulationItem &item = simulation_items_[i];
428 const bNodeSocket &input_bsocket = node.input_socket(i + 1);
429 const bNodeSocket &output_bsocket = node.output_socket(i);
430
431 lf_index_by_bsocket[input_bsocket.index_in_tree()] = inputs_.append_and_get_index_as(
432 item.name, CPPType::get<SocketValueVariant>(), lf::ValueUsage::Maybe);
433 lf_index_by_bsocket[output_bsocket.index_in_tree()] = outputs_.append_and_get_index_as(
435 }
436 }
437
438 void execute_impl(lf::Params &params, const lf::Context &context) const final
439 {
440 GeoNodesUserData &user_data = *static_cast<GeoNodesUserData *>(context.user_data);
441 GeoNodesLocalUserData &local_user_data = *static_cast<GeoNodesLocalUserData *>(
442 context.local_user_data);
443 if (!user_data.call_data->self_object()) {
444 /* The self object is currently required for generating anonymous attribute names. */
445 this->set_default_outputs(params);
446 return;
447 }
448 if (!user_data.call_data->simulation_params) {
449 if (eval_log::NodeTreeLogger *tree_logger = local_user_data.try_get_tree_logger(user_data)) {
450 tree_logger->node_warnings.append(
451 *tree_logger->allocator,
452 {node_.identifier,
453 {NodeWarningType::Error, TIP_("Simulation zone is not supported")}});
454 }
455 this->set_default_outputs(params);
456 return;
457 }
458 std::optional<FoundNestedNodeID> found_id = find_nested_node_id(user_data, node_.identifier);
459 if (!found_id || found_id->is_in_loop || found_id->is_in_closure) {
460 if (eval_log::NodeTreeLogger *tree_logger = local_user_data.try_get_tree_logger(user_data)) {
461 const StringRefNull message = TIP_("Simulation must not be in a loop or closure");
462 tree_logger->node_warnings.append(*tree_logger->allocator,
463 {node_.identifier, {NodeWarningType::Error, message}});
464 }
465 this->set_default_outputs(params);
466 return;
467 }
468 SimulationZoneBehavior *zone_behavior = user_data.call_data->simulation_params->get(
469 found_id->id);
470 if (!zone_behavior) {
471 this->set_default_outputs(params);
472 return;
473 }
474 sim_output::Behavior &output_behavior = zone_behavior->output;
475 if (auto *info = std::get_if<sim_output::ReadSingle>(&output_behavior)) {
476 this->output_cached_state(
477 params, user_data, zone_behavior->data_block_map, std::move(info->values));
478 }
479 else if (auto *info = std::get_if<sim_output::ReadInterpolated>(&output_behavior)) {
480 this->output_mixed_cached_state(params,
481 zone_behavior->data_block_map,
482 *user_data.compute_context,
483 std::move(info->prev_values),
484 std::move(info->next_values),
485 info->mix_factor);
486 }
487 else if (std::get_if<sim_output::PassThrough>(&output_behavior)) {
488 this->pass_through(params, user_data, zone_behavior->data_block_map);
489 }
490 else if (auto *info = std::get_if<sim_output::StoreNewState>(&output_behavior)) {
491 this->store_new_state(params, user_data, zone_behavior->data_block_map, *info);
492 }
493 else {
495 }
496 }
497
498 void set_default_outputs(lf::Params &params) const
499 {
501 }
502
503 void output_cached_state(lf::Params &params,
504 GeoNodesUserData &user_data,
505 bke::bake::BakeDataBlockMap *data_block_map,
506 bke::bake::BakeValues &&bake_values) const
507 {
509 for (const NodeSimulationItem &item : simulation_items_) {
510 keys.append({item.identifier, item.socket_type});
511 }
512 Vector<SocketValueVariant> output_values = bake_values.to_runtime_values(
513 keys, *user_data.compute_context, data_block_map);
514 bake_values.clear();
515 for (const int i : simulation_items_.index_range()) {
516 params.set_output(i, std::move(output_values[i]));
517 }
518 }
519
520 void output_mixed_cached_state(lf::Params &params,
521 bke::bake::BakeDataBlockMap *data_block_map,
522 const ComputeContext &compute_context,
523 bke::bake::BakeValues &&prev_bake_values,
524 bke::bake::BakeValues &&next_bake_values,
525 const float mix_factor) const
526 {
528 simulation_items_, compute_context, data_block_map, std::move(prev_bake_values));
530 simulation_items_, compute_context, data_block_map, std::move(next_bake_values));
531 if (mix_factor != 0.0f) {
532 for (const int i : simulation_items_.index_range()) {
533 geometry::mix_socket_values(output_values[i], next_values[i], mix_factor);
534 }
535 }
536 for (const int i : simulation_items_.index_range()) {
537 params.set_output(i, std::move(output_values[i]));
538 }
539 }
540
541 void pass_through(lf::Params &params,
542 GeoNodesUserData &user_data,
543 bke::bake::BakeDataBlockMap *data_block_map) const
544 {
545 std::optional<bke::bake::BakeValues> bake_values = this->get_bake_values_from_inputs(
546 params, data_block_map, true);
547 if (!bake_values) {
548 /* Wait for inputs to be computed. */
549 return;
550 }
552 simulation_items_, *user_data.compute_context, data_block_map, std::move(*bake_values));
553 for (const int i : simulation_items_.index_range()) {
554 params.set_output(i, std::move(output_values[i]));
555 }
556 }
557
558 void store_new_state(lf::Params &params,
559 GeoNodesUserData &user_data,
560 bke::bake::BakeDataBlockMap *data_block_map,
561 const sim_output::StoreNewState &info) const
562 {
563 const SocketValueVariant *skip_variant =
564 params.try_get_input_data_ptr_or_request<SocketValueVariant>(skip_input_index_);
565 if (skip_variant == nullptr) {
566 /* Wait for skip input to be computed. */
567 return;
568 }
569 const bool skip = skip_variant->copy_as<bool>();
570
571 /* Instead of outputting the values directly, convert them to a bake state and then back.
572 * This ensures that some geometry processing happens on the data consistently (e.g. removing
573 * anonymous attributes). */
574 std::optional<bke::bake::BakeValues> bake_values = this->get_bake_values_from_inputs(
575 params, data_block_map, skip);
576 if (!bake_values) {
577 /* Wait for inputs to be computed. */
578 return;
579 }
580 info.store_fn(*bake_values);
581 this->output_cached_state(params, user_data, data_block_map, std::move(*bake_values));
582 }
583
584 std::optional<bke::bake::BakeValues> get_bake_values_from_inputs(
585 lf::Params &params, bke::bake::BakeDataBlockMap *data_block_map, const bool skip) const
586 {
587 /* Choose which set of input parameters to use. The others are ignored. */
588 const int params_offset = skip ? skip_inputs_offset_ : solve_inputs_offset_;
589 Array<SocketValueVariant *> input_value_pointers(simulation_items_.size());
590 for (const int i : simulation_items_.index_range()) {
591 input_value_pointers[i] = params.try_get_input_data_ptr_or_request<SocketValueVariant>(
592 i + params_offset);
593 }
594 if (input_value_pointers.as_span().contains(nullptr)) {
595 /* Wait for inputs to be computed. */
596 return std::nullopt;
597 }
598
599 Vector<bke::bake::BakeValues::InputValue> input_values(simulation_items_.size());
600 for (const int i : simulation_items_.index_range()) {
601 const NodeSimulationItem &item = simulation_items_[i];
602 bke::bake::BakeValues::InputValue &input_value = input_values[i];
603 input_value.id = item.identifier;
604 input_value.name = item.name;
605 input_value.field_domain = AttrDomain(item.attribute_domain);
606 input_value.value = std::move(*input_value_pointers[i]);
607 }
608 return bke::bake::BakeValues::from_runtime_values(std::move(input_values), data_block_map);
609 }
610};
611
613{
614 b.use_custom_socket_order();
615 b.allow_any_socket_order();
616 b.add_input<decl::Bool>("Skip"_ustr)
617 .hide_value()
619 "Forward the output of the simulation input node directly to the output node and ignore "
620 "the nodes in the simulation zone");
621
622 const bNodeTree *tree = b.tree_or_null();
623 const bNode *node = b.node_or_null();
624 if (node == nullptr) {
625 return;
626 }
627
628 const NodeGeometrySimulationOutput &storage = node_storage(*node);
629
630 for (const int i : IndexRange(storage.items_num)) {
631 const NodeSimulationItem &item = storage.items[i];
632 const eNodeSocketDatatype socket_type = item.socket_type;
633 if (socket_type == SOCK_GEOMETRY && i > 0) {
634 b.add_separator();
635 }
636 const UString name(item.name);
638 auto &input_decl = b.add_input(socket_type, name, identifier)
639 .socket_name_ptr(
640 &tree->id, *SimulationItemsAccessor::item_srna, &item, "name")
641 .structure_type(StructureType::Dynamic);
642 auto &output_decl = b.add_output(socket_type, name, identifier)
643 .align_with_previous()
644 .propagate_all({input_decl.index()})
645 .inferred_structure_type({input_decl.index()})
646 .structure_type(StructureType::Dynamic);
647 if (socket_type == SOCK_BUNDLE) {
648 dynamic_cast<decl::BundleBuilder &>(output_decl)
649 .pass_through_input_index(input_decl.index());
650 }
651 }
652 b.add_input<decl::Extend>(""_ustr, "__extend__"_ustr)
653 .structure_type(StructureType::Dynamic)
654 .custom_draw(socket_items::ui::draw_extend_socket_fn<SimulationItemsAccessor>());
655 b.add_output<decl::Extend>(""_ustr, "__extend__"_ustr)
656 .structure_type(StructureType::Dynamic)
657 .align_with_previous();
658}
659
660static void node_init(bNodeTree * /*tree*/, bNode *node)
661{
662 NodeGeometrySimulationOutput *data = MEM_new<NodeGeometrySimulationOutput>(__func__);
663
664 data->next_identifier = 0;
665
666 data->items = MEM_new_array<NodeSimulationItem>(1, __func__);
667 data->items[0].name = BLI_strdup(DATA_("Geometry"));
668 data->items[0].socket_type = SOCK_GEOMETRY;
669 data->items[0].identifier = data->next_identifier++;
670 data->items_num = 1;
671
672 node->storage = data;
673}
674
676{
678 MEM_delete(reinterpret_cast<NodeGeometrySimulationOutput *>(node->storage));
679}
680
681static void node_copy_storage(bNodeTree * /*dst_tree*/, bNode *dst_node, const bNode *src_node)
682{
683 const NodeGeometrySimulationOutput &src_storage = node_storage(*src_node);
684 auto *dst_storage = MEM_new<NodeGeometrySimulationOutput>(__func__,
685 dna::shallow_copy(src_storage));
686 dst_node->storage = dst_storage;
687
689}
690
695
701
703{
704 BakeDrawContext ctx;
705 if (!get_bake_draw_context(&params.C, params.node, ctx)) {
706 return;
707 }
710 row.text = TIP_("Cannot bake in zone");
711 row.icon = ICON_STATUS_ERROR;
712 params.rows.append(std::move(row));
713 }
714 if (ctx.is_baked) {
716 row.text = get_baked_string(ctx);
717 params.rows.append(std::move(row));
718 }
719}
720
722{
723 const bNodeSocket &other_socket = params.other_socket();
725 return;
726 }
727 params.add_item_full_name(IFACE_("Simulation"), [](LinkSearchOpParams &params) {
728 bNode &input_node = params.add_node("GeometryNodeSimulationInput"_ustr);
729 bNode &output_node = params.add_node("GeometryNodeSimulationOutput"_ustr);
730 output_node.location[0] = 300;
731
732 auto &input_storage = *static_cast<NodeGeometrySimulationInput *>(input_node.storage);
733 input_storage.output_node_id = output_node.identifier;
734
736 const UString name(params.socket.name);
738 params.node_tree, output_node, params.socket.type, name.c_str());
741 if (params.socket.in_out == SOCK_IN) {
742 params.connect_available_socket(output_node, name);
743 }
744 else {
745 params.connect_available_socket(input_node, name);
746 }
747 params.node_tree.ensure_topology_cache();
749 input_node,
750 input_node.output_socket(1),
751 output_node,
752 output_node.input_socket(1));
753 });
754}
755
756static void node_blend_write(const bNodeTree & /*tree*/, const bNode &node, BlendWriter &writer)
757{
759}
760
765
766static void node_register()
767{
768 static bke::bNodeType ntype;
769
770 geo_node_type_base(&ntype, "GeometryNodeSimulationOutput"_ustr, GEO_NODE_SIMULATION_OUTPUT);
771 ntype.ui_name = "Simulation Output";
772 ntype.ui_description = "Output data from the simulation zone";
773 ntype.enum_name_legacy = "SIMULATION_OUTPUT";
775 ntype.initfunc = node_init;
776 ntype.declare = node_declare;
781 ntype.no_muting = true;
787 ntype, "NodeGeometrySimulationOutput", node_free_storage, node_copy_storage);
789}
790NOD_REGISTER_NODE(node_register)
791
792} // namespace sim_output_node
793
794} // namespace nodes::node_geo_simulation_cc
795
796namespace nodes {
797
798std::unique_ptr<LazyFunction> get_simulation_input_lazy_function(
799 const bNodeTree &node_tree,
800 const bNode &node,
801 GeometryNodesLazyFunctionGraphInfo &own_lf_graph_info)
802{
804 return std::make_unique<
806 node_tree, node, own_lf_graph_info);
807}
808
809std::unique_ptr<LazyFunction> get_simulation_output_lazy_function(
810 const bNode &node, GeometryNodesLazyFunctionGraphInfo &own_lf_graph_info)
811{
813 return std::make_unique<
815 node, own_lf_graph_info);
816}
817
818StructRNA **SimulationItemsAccessor::item_srna = &RNA_SimulationStateItem;
819
821{
822 writer->write_string(item.name);
823}
824
829
830} // namespace nodes
831
832Span<NodeSimulationItem> NodeGeometrySimulationOutput::items_span() const
833{
834 return Span<NodeSimulationItem>(items, items_num);
835}
836
837MutableSpan<NodeSimulationItem> NodeGeometrySimulationOutput::items_span()
838{
839 return MutableSpan<NodeSimulationItem>(items, items_num);
840}
841
842} // namespace blender
Low-level operations for curves.
#define NODE_CLASS_INTERFACE
Definition BKE_node.hh:490
#define GEO_NODE_SIMULATION_OUTPUT
#define GEO_NODE_SIMULATION_INPUT
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define BLT_I18NCONTEXT_ID_NODETREE
#define TIP_(msgid)
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define DATA_(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
#define BLI_assert_unreachable()
#define ELEM(...)
constexpr Span()=default
Span< T > as_span() const
Definition BLI_array.hh:254
static const CPPType & get()
constexpr IndexRange()=default
constexpr int64_t size() const
Definition BLI_span.hh:499
const char * c_str() const
void append(const T &value)
std::optional< int > output_node_id
const bNode * output_node() const
const bNodeTreeZone * get_zone_by_node(const int32_t node_id) const
static BakeValues from_runtime_values(Vector< InputValue > runtime_values, BakeDataBlockMap *data_block_map, bool is_for_cache=true)
void add_item_full_name(std::string name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
virtual SimulationZoneBehavior * get(const int zone_id) const =0
void pass_through(lf::Params &params, const GeoNodesUserData &user_data, bke::bake::BakeDataBlockMap *data_block_map) const
void execute_impl(lf::Params &params, const lf::Context &context) const final
LazyFunctionForSimulationInputNode(const bNodeTree &node_tree, const bNode &node, GeometryNodesLazyFunctionGraphInfo &own_lf_graph_info)
std::optional< bke::bake::BakeValues > get_bake_values_from_inputs(lf::Params &params, bke::bake::BakeDataBlockMap *data_block_map) const
void output_simulation_state(lf::Params &params, const GeoNodesUserData &user_data, bke::bake::BakeDataBlockMap *data_block_map, bke::bake::BakeValues &&bake_values) const
void output_mixed_cached_state(lf::Params &params, bke::bake::BakeDataBlockMap *data_block_map, const ComputeContext &compute_context, bke::bake::BakeValues &&prev_bake_values, bke::bake::BakeValues &&next_bake_values, const float mix_factor) 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 store_new_state(lf::Params &params, GeoNodesUserData &user_data, bke::bake::BakeDataBlockMap *data_block_map, const sim_output::StoreNewState &info) const
LazyFunctionForSimulationOutputNode(const bNode &node, GeometryNodesLazyFunctionGraphInfo &own_lf_graph_info)
std::optional< bke::bake::BakeValues > get_bake_values_from_inputs(lf::Params &params, bke::bake::BakeDataBlockMap *data_block_map, const bool skip) const
void output_cached_state(lf::Params &params, GeoNodesUserData &user_data, bke::bake::BakeDataBlockMap *data_block_map, bke::bake::BakeValues &&bake_values) const
#define storage(slot, qualifiers)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
Definition node.cc:4638
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
void mix_socket_values(bke::SocketValueVariant &a, const bke::SocketValueVariant &b, const float factor)
static void node_label(const bNodeTree *, const bNode *, char *label, const int label_maxncpy)
static bool node_insert_link(bke::NodeInsertLinkParams &params)
static void node_blend_write(const bNodeTree &, const bNode &node, BlendWriter &writer)
static void node_copy_storage(bNodeTree *, bNode *dst_node, const bNode *src_node)
static void node_extra_info(NodeExtraInfoParams &params)
static bool node_insert_link(bke::NodeInsertLinkParams &params)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_blend_read(bNodeTree &, bNode &node, BlendDataReader &reader)
static Vector< SocketValueVariant > get_output_values_from_bake_values(const Span< NodeSimulationItem > simulation_items, const ComputeContext &compute_context, bke::bake::BakeDataBlockMap *data_block_map, bke::bake::BakeValues &&bake_values)
static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *current_node_ptr)
static void draw_simulation_state(const bContext *C, ui::Layout &layout, bNodeTree &ntree, bNode &output_node)
std::variant< PassThrough, UseCache > Behavior
std::variant< PassThrough, StoreNewState, ReadSingle, ReadInterpolated, ReadError > Behavior
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)
std::unique_ptr< LazyFunction > get_simulation_output_lazy_function(const bNode &node, GeometryNodesLazyFunctionGraphInfo &own_lf_graph_info)
std::unique_ptr< LazyFunction > get_simulation_input_lazy_function(const bNodeTree &node_tree, const bNode &node, GeometryNodesLazyFunctionGraphInfo &own_lf_graph_info)
void draw_bake_button_row(const BakeDrawContext &ctx, ui::Layout &layout, bool is_in_sidebar=false)
void update_node_declaration_and_sockets(bNodeTree &ntree, bNode &node, Main *bmain)
void draw_data_blocks(const bContext *C, ui::Layout &layout, PointerRNA &bake_rna)
bool socket_type_supports_attributes(const eNodeSocketDatatype socket_type)
void draw_common_bake_settings(bContext *C, BakeDrawContext &ctx, ui::Layout &layout)
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)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
BMesh const char void * data
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
signed int int32_t
Definition BKE_text.h:78
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
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)
eNodeSocketDatatype socket_type
eNodeSocketDatatype type
eNodeTree_Type type
Defines a node type.
Definition BKE_node.hh:265
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
void(*) labelfunc(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:301
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
eval_log::NodeTreeLogger * try_get_tree_logger(const GeoNodesUserData &user_data) const
Vector< NodeExtraInfoRow > & rows
static void blend_read_data_item(BlendDataReader *reader, ItemT &item)
static bool supports_socket_type(const eNodeSocketDatatype socket_type, const int)
static std::string socket_identifier_for_item(const NodeSimulationItem &item)
static void blend_write_item(BlendWriter *writer, const ItemT &item)
std::function< void(bke::bake::BakeValues values)> store_fn
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)
PanelLayout panel(const bContext *C, StringRef idname, bool default_closed)
void active_set(bool active)
Layout & column(bool align)
Layout & row(bool align)