Blender V4.5
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 b.add_output<decl::Geometry>("Geometry").propagate_all().align_with_previous();
21 b.add_input<decl::Bool>("Selection")
22 .default_value(true)
23 .hide_value()
24 .field_on_all()
25 .description("Which top-level instances to realize");
26 b.add_input<decl::Bool>("Realize All")
27 .default_value(true)
28 .field_on_all()
30 "Realize all levels of nested instances for a top-level instances. Overrides the value "
31 "of the Depth input");
32 b.add_input<decl::Int>("Depth").default_value(0).min(0).field_on_all().description(
33 "Number of levels of nested instances to realize for each top-level instance");
34}
35
37{
38 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
39 if (!geometry_set.has_instances()) {
40 params.set_output("Geometry", std::move(geometry_set));
41 return;
42 }
43
45
46 Field<bool> realize_all_field = params.extract_input<Field<bool>>("Realize All");
47 Field<int> depth_field = params.extract_input<Field<int>>("Depth");
48
49 static auto depth_override = mf::build::SI2_SO<int, bool, int>(
50 "depth_override", [](int depth, bool realize_all_field) {
51 return realize_all_field ? geometry::VariedDepthOptions::MAX_DEPTH : std::max(depth, 0);
52 });
53
54 Field<int> depth_field_overridden(FieldOperation::Create(
55 depth_override, {std::move(depth_field), std::move(realize_all_field)}));
56
57 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
58
59 static auto selection_override = mf::build::SI2_SO<int, bool, bool>(
60 "selection_override",
61 [](int depth_override, bool selection) { return depth_override == 0 ? false : selection; });
62
63 Field<bool> selection_field_overrided(FieldOperation::Create(
64 selection_override, {depth_field_overridden, std::move(selection_field)}));
65
66 const bke::Instances &instances = *geometry_set.get_instances();
67 const bke::InstancesFieldContext field_context(instances);
68 fn::FieldEvaluator evaluator(field_context, instances.instances_num());
69
70 const int evaluated_depth_index = evaluator.add(depth_field_overridden);
71 evaluator.set_selection(selection_field_overrided);
72 evaluator.evaluate();
73
74 geometry::VariedDepthOptions varied_depth_option;
75 varied_depth_option.depths = evaluator.get_evaluated<int>(evaluated_depth_index);
76 varied_depth_option.selection = evaluator.get_evaluated_selection_as_mask();
77
79 options.keep_original_ids = false;
80 options.realize_instance_attributes = true;
81 const NodeAttributeFilter attribute_filter = params.get_attribute_filter("Geometry");
82 options.attribute_filter = attribute_filter;
84 geometry_set, options, varied_depth_option);
85 new_geometry_set.name = geometry_set.name;
86 params.set_output("Geometry", std::move(new_geometry_set));
87}
88
89static void node_register()
90{
91 static blender::bke::bNodeType ntype;
92
93 geo_node_type_base(&ntype, "GeometryNodeRealizeInstances", GEO_NODE_REALIZE_INSTANCES);
94 ntype.ui_name = "Realize Instances";
95 ntype.ui_description = "Convert instances into real geometry data";
96 ntype.enum_name_legacy = "REALIZE_INSTANCES";
98 ntype.declare = node_declare;
101}
103
104} // namespace blender::nodes::node_geo_realize_instances_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:447
#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 > Create(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]
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
bke::GeometrySet 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:226
std::string ui_description
Definition BKE_node.hh:232
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeDeclareFunction declare
Definition BKE_node.hh:355