Blender V5.3
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#include "UI_resources.hh"
15
16#include "NOD_rna_define.hh"
17
19
21{
22 b.use_custom_socket_order();
23 b.allow_any_socket_order();
24 b.add_input<decl::Geometry>("Geometry"_ustr)
25 .description("Geometry whose instances are (partially) realized");
26 b.add_output<decl::Geometry>("Geometry"_ustr).propagate_all_geometry().align_with_previous();
27 b.add_input<decl::Bool>("Selection"_ustr)
28 .default_value(true)
29 .hide_value()
30 .evaluated_geometry_field()
31 .description("Which top-level instances to realize");
32 b.add_input<decl::Bool>("Realize All"_ustr)
33 .default_value(true)
34 .evaluated_geometry_field()
36 "Realize all levels of nested instances for a top-level instances. Overrides the value "
37 "of the Depth input");
38 b.add_input<decl::Int>("Depth"_ustr)
39 .default_value(0)
40 .min(0)
41 .evaluated_geometry_field()
42 .description("Number of levels of nested instances to realize for each top-level instance");
43 b.add_input<decl::Bool>("Preserve Normals"_ustr)
44 .default_value(false)
45 .description("Preserve the apparent face orientation of mirrored mesh instances");
46}
47
48static void node_layout_ex(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
49{
50 layout.prop(ptr, "realize_to_point_domain", UI_ITEM_NONE, std::nullopt, ICON_NONE);
51}
52
54{
55 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry"_ustr);
56 if (!geometry_set.has_instances()) {
57 params.set_output("Geometry"_ustr, std::move(geometry_set));
58 return;
59 }
60
62
63 Field<bool> realize_all_field = params.extract_input<Field<bool>>("Realize All"_ustr);
64 Field<int> depth_field = params.extract_input<Field<int>>("Depth"_ustr);
65 const bNode &node = params.node();
66 const bool realize_to_point_domain = node.custom1 & GEO_NODE_REALIZE_TO_POINT_DOMAIN;
67
68 static auto depth_override = mf::build::SI2_SO<int, bool, int>(
69 "depth_override", [](int depth, bool realize_all_field) {
70 return realize_all_field ? geometry::VariedDepthOptions::MAX_DEPTH : std::max(depth, 0);
71 });
72
73 Field<int> depth_field_overridden(FieldOperation::from(
74 depth_override, {std::move(depth_field), std::move(realize_all_field)}));
75
76 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection"_ustr);
77
78 static auto selection_override = mf::build::SI2_SO<int, bool, bool>(
79 "selection_override",
80 [](int depth_override, bool selection) { return depth_override == 0 ? false : selection; });
81
82 Field<bool> selection_field_overrided(FieldOperation::from(
83 selection_override, {depth_field_overridden, std::move(selection_field)}));
84
85 const bke::Instances &instances = *geometry_set.get_instances();
86 const bke::InstancesFieldContext field_context(instances);
87 fn::FieldEvaluator evaluator(field_context, instances.instances_num());
88
89 const int evaluated_depth_index = evaluator.add(depth_field_overridden);
90 evaluator.set_selection(selection_field_overrided);
91 evaluator.evaluate();
92
93 geometry::VariedDepthOptions varied_depth_option;
94 varied_depth_option.depths = evaluator.get_evaluated<int>(evaluated_depth_index);
95 varied_depth_option.selection = evaluator.get_evaluated_selection_as_mask();
96
98 options.keep_original_ids = false;
99 options.realize_instance_attributes = true;
100 options.realize_to_point_domain = realize_to_point_domain;
101 options.preserve_normals = params.extract_input<bool>("Preserve Normals"_ustr);
102 const NodeAttributeFilter attribute_filter = params.get_attribute_filter("Geometry"_ustr);
103 options.attribute_filter = attribute_filter;
105 geometry_set, options, varied_depth_option);
106 for (const StringRef error : realize_result.errors) {
108 }
109 realize_result.geometry.set_name(geometry_set.name());
110 params.set_output("Geometry"_ustr, std::move(realize_result.geometry));
111}
112
113static void node_rna(StructRNA *srna)
114{
116 "realize_to_point_domain",
117 "Realize to Point Domain",
118 "Propagate instance attributes to the point domain rather than the curve "
119 "domain. This property exists for compatibility with 5.0 and earlier.",
121}
122
123static void node_register()
124{
125 static bke::bNodeType ntype;
126
127 geo_node_type_base(&ntype, "GeometryNodeRealizeInstances"_ustr, GEO_NODE_REALIZE_INSTANCES);
128 ntype.ui_name = "Realize Instances";
129 ntype.ui_description = "Convert instances into real geometry data";
130 ntype.enum_name_legacy = "REALIZE_INSTANCES";
132 ntype.declare = node_declare;
136 node_rna(ntype.rna_ext.srna);
137}
139
140} // namespace blender::nodes::node_geo_realize_instances_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_REALIZE_INSTANCES
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_boolean_accessors(member, flag)
#define UI_ITEM_NONE
void set_selection(Field< bool > selection)
int add(GField field, GVArray *varray_ptr)
IndexMask get_evaluated_selection_as_mask() const
const GVArray & get_evaluated(const int field_index) const
static FieldOperationPtr from(std::shared_ptr< const mf::MultiFunction > fn, Vector< GField > inputs)
Definition field.cc:252
void error_message_add(const NodeWarningType type, StringRef message) const
NodeAttributeFilter get_attribute_filter(const UString output_identifier) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
static void remember_deformed_positions_if_necessary(GeometrySet &geometry)
CCL_NAMESPACE_BEGIN struct Options options
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
RealizeInstancesResult realize_instances(bke::GeometrySet geometry_set, const RealizeInstancesOptions &options)
static void node_geo_exec(GeoNodeExecParams params)
static void node_layout_ex(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
PropertyRNA * RNA_def_node_boolean(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const BooleanRNAAccessors accessors, std::optional< bool > default_value, bool allow_animation)
@ GEO_NODE_REALIZE_TO_POINT_DOMAIN
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
StringRefNull name() const
void set_name(std::string name)
const Instances * get_instances() const
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) draw_buttons_ex(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:292
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
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)