Blender V5.0
node_geo_realize_instances.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
6
7#include "BKE_instances.hh"
8
10
12
14
16{
17 b.use_custom_socket_order();
18 b.allow_any_socket_order();
19 b.add_input<decl::Geometry>("Geometry")
20 .description("Geometry whose instances are (partially) realized");
21 b.add_output<decl::Geometry>("Geometry").propagate_all().align_with_previous();
22 b.add_input<decl::Bool>("Selection")
23 .default_value(true)
24 .hide_value()
25 .field_on_all()
26 .description("Which top-level instances to realize");
27 b.add_input<decl::Bool>("Realize All")
28 .default_value(true)
29 .field_on_all()
31 "Realize all levels of nested instances for a top-level instances. Overrides the value "
32 "of the Depth input");
33 b.add_input<decl::Int>("Depth").default_value(0).min(0).field_on_all().description(
34 "Number of levels of nested instances to realize for each top-level instance");
35}
36
38{
39 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
40 if (!geometry_set.has_instances()) {
41 params.set_output("Geometry", std::move(geometry_set));
42 return;
43 }
44
46
47 Field<bool> realize_all_field = params.extract_input<Field<bool>>("Realize All");
48 Field<int> depth_field = params.extract_input<Field<int>>("Depth");
49
50 static auto depth_override = mf::build::SI2_SO<int, bool, int>(
51 "depth_override", [](int depth, bool realize_all_field) {
52 return realize_all_field ? geometry::VariedDepthOptions::MAX_DEPTH : std::max(depth, 0);
53 });
54
55 Field<int> depth_field_overridden(FieldOperation::from(
56 depth_override, {std::move(depth_field), std::move(realize_all_field)}));
57
58 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
59
60 static auto selection_override = mf::build::SI2_SO<int, bool, bool>(
61 "selection_override",
62 [](int depth_override, bool selection) { return depth_override == 0 ? false : selection; });
63
64 Field<bool> selection_field_overrided(FieldOperation::from(
65 selection_override, {depth_field_overridden, std::move(selection_field)}));
66
67 const bke::Instances &instances = *geometry_set.get_instances();
68 const bke::InstancesFieldContext field_context(instances);
69 fn::FieldEvaluator evaluator(field_context, instances.instances_num());
70
71 const int evaluated_depth_index = evaluator.add(depth_field_overridden);
72 evaluator.set_selection(selection_field_overrided);
73 evaluator.evaluate();
74
75 geometry::VariedDepthOptions varied_depth_option;
76 varied_depth_option.depths = evaluator.get_evaluated<int>(evaluated_depth_index);
77 varied_depth_option.selection = evaluator.get_evaluated_selection_as_mask();
78
80 options.keep_original_ids = false;
81 options.realize_instance_attributes = true;
82 const NodeAttributeFilter attribute_filter = params.get_attribute_filter("Geometry");
83 options.attribute_filter = attribute_filter;
85 geometry_set, options, varied_depth_option);
86 for (const StringRef error : realize_result.errors) {
87 params.error_message_add(NodeWarningType::Error, error);
88 }
89 realize_result.geometry.name = geometry_set.name;
90 params.set_output("Geometry", std::move(realize_result.geometry));
91}
92
93static void node_register()
94{
95 static blender::bke::bNodeType ntype;
96
97 geo_node_type_base(&ntype, "GeometryNodeRealizeInstances", GEO_NODE_REALIZE_INSTANCES);
98 ntype.ui_name = "Realize Instances";
99 ntype.ui_description = "Convert instances into real geometry data";
100 ntype.enum_name_legacy = "REALIZE_INSTANCES";
102 ntype.declare = node_declare;
105}
107
108} // namespace blender::nodes::node_geo_realize_instances_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define GEO_NODE_REALIZE_INSTANCES
#define NOD_REGISTER_NODE(REGISTER_FUNC)
void set_selection(Field< bool > selection)
Definition FN_field.hh:383
int add(GField field, GVArray *varray_ptr)
Definition field.cc:751
IndexMask get_evaluated_selection_as_mask() const
Definition field.cc:817
const GVArray & get_evaluated(const int field_index) const
Definition FN_field.hh:448
static std::shared_ptr< FieldOperation > from(std::shared_ptr< const mf::MultiFunction > function, Vector< GField > inputs={})
Definition FN_field.hh:242
static void remember_deformed_positions_if_necessary(GeometrySet &geometry)
CCL_NAMESPACE_BEGIN struct Options options
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
static void error(const char *str)
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
RealizeInstancesResult realize_instances(bke::GeometrySet geometry_set, const RealizeInstancesOptions &options)
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
const Instances * get_instances() const
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362