Blender V5.3
node_geo_merge_layers.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
8#include "GEO_merge_layers.hh"
9
10#include "BKE_grease_pencil.hh"
11
12#include "NOD_rna_define.hh"
13
14#include "RNA_enum_types.hh"
15
17#include "UI_resources.hh"
18
20
22
27
29{
30 b.use_custom_socket_order();
31 b.allow_any_socket_order();
32 b.add_input<decl::Geometry>("Grease Pencil"_ustr)
34 .description("Grease Pencil data to merge layers of");
35 b.add_output<decl::Geometry>("Grease Pencil"_ustr)
36 .propagate_all_geometry()
37 .align_with_previous();
38 b.add_input<decl::Bool>("Selection"_ustr)
39 .default_value(true)
40 .hide_value()
41 .evaluated_geometry_field();
42 b.add_input<decl::Menu>("Mode"_ustr)
45 b.add_input<decl::Int>("Group ID"_ustr)
46 .hide_value()
47 .evaluated_geometry_field()
48 .usage_by_single_menu(int(MergeLayerMode::ByID));
49}
50
51static void node_init(bNodeTree * /*tree*/, bNode *node)
52{
53 /* Still used for forward compatibility. */
54 node->storage = MEM_new<NodeGeometryMergeLayers>(__func__);
55}
56
57static GreasePencil *merge_by_name(const GreasePencil &src_grease_pencil,
58 const GeoNodeExecParams &params,
59 const AttributeFilter &attribute_filter)
60{
61 using namespace bke::greasepencil;
62 const Field<bool> selection_field = params.get_input<Field<bool>>("Selection"_ustr);
63
64 bke::GreasePencilFieldContext field_context{src_grease_pencil};
65 FieldEvaluator field_evaluator{field_context, src_grease_pencil.layers().size()};
66 field_evaluator.add(selection_field);
67 field_evaluator.evaluate();
69 src_grease_pencil, field_evaluator.get_evaluated<bool>(0), attribute_filter);
70}
71
72static GroupedSpan<int> get_layers_map_by_id(const GreasePencil &src_grease_pencil,
73 const GeoNodeExecParams &params,
74 Array<int> &r_offsets,
75 Array<int> &r_indices)
76{
77 using namespace bke::greasepencil;
78
79 const int old_layers_num = src_grease_pencil.layers().size();
80
81 const Field<bool> selection_field = params.get_input<Field<bool>>("Selection"_ustr);
82 const Field<int> group_id_field = params.get_input<Field<int>>("Group ID"_ustr);
83
84 bke::GreasePencilFieldContext field_context{src_grease_pencil};
85 FieldEvaluator field_evaluator{field_context, old_layers_num};
86 field_evaluator.add(selection_field);
87 field_evaluator.add(group_id_field);
88 field_evaluator.evaluate();
89 const VArray<bool> selection = field_evaluator.get_evaluated<bool>(0);
90 const VArray<int> group_ids = field_evaluator.get_evaluated<int>(1);
91
92 Array<int> layer_to_group(old_layers_num);
93 Map<int, int> id_to_group_index;
94 int groups_num = 0;
95 for (const int i : IndexRange(old_layers_num)) {
96 if (selection[i]) {
97 layer_to_group[i] = id_to_group_index.lookup_or_add_cb(group_ids[i],
98 [&]() { return groups_num++; });
99 }
100 else {
101 layer_to_group[i] = groups_num++;
102 }
103 }
105 layer_to_group, groups_num, r_offsets, r_indices);
106}
107
109 const GeoNodeExecParams &params,
110 const AttributeFilter &attribute_filter)
111{
112 using namespace bke::greasepencil;
113
114 const GreasePencil *src_grease_pencil = geometry.get_grease_pencil();
115 if (!src_grease_pencil) {
116 return;
117 }
118 const int old_layers_num = src_grease_pencil->layers().size();
119
120 GreasePencil *new_grease_pencil;
121 switch (MergeLayerMode(params.get_input<MergeLayerMode>("Mode"_ustr))) {
123 new_grease_pencil = merge_by_name(*src_grease_pencil, params, attribute_filter);
124 break;
125 }
127 Array<int> offsets;
128 Array<int> all_indices;
129 const GroupedSpan<int> layers_map = get_layers_map_by_id(
130 *src_grease_pencil, params, offsets, all_indices);
131 const int new_layers_num = layers_map.size();
132 if (old_layers_num == new_layers_num) {
133 return;
134 }
135 new_grease_pencil = geometry::merge_layers(*src_grease_pencil, layers_map, attribute_filter);
136 break;
137 }
138 }
139
140 geometry.replace_grease_pencil(new_grease_pencil);
141}
142
144{
145 GeometrySet main_geometry = params.extract_input<GeometrySet>("Grease Pencil"_ustr);
146 const NodeAttributeFilter attribute_filter = params.get_attribute_filter("Grease Pencil"_ustr);
147
149 merge_layers(geometry, params, attribute_filter);
150 });
151
152 params.set_output("Grease Pencil"_ustr, std::move(main_geometry));
153}
154
155static void node_register()
156{
157 static bke::bNodeType ntype;
158
159 geo_node_type_base(&ntype, "GeometryNodeMergeLayers"_ustr, GEO_NODE_MERGE_LAYERS);
160 ntype.ui_name = "Merge Layers";
161 ntype.ui_description = "Join groups of Grease Pencil layers into one";
162 ntype.enum_name_legacy = "MERGE_LAYERS";
164 ntype.declare = node_declare;
165 ntype.initfunc = node_init;
168 ntype, "NodeGeometryMergeLayers", node_free_standard_storage, node_copy_standard_storage);
170}
172
173} // namespace blender::nodes::node_geo_merge_layers_cc
Low-level operations for grease pencil.
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_MERGE_LAYERS
#define NOD_REGISTER_NODE(REGISTER_FUNC)
constexpr IndexRange()=default
Value & lookup_or_add_cb(const Key &key, const CreateValueF &create_value)
Definition BLI_map.hh:620
int add(GField field, GVArray *varray_ptr)
const GVArray & get_evaluated(const int field_index) const
NodeAttributeFilter get_attribute_filter(const UString output_identifier) const
T extract_input(const UString identifier)
T get_input(const UString identifier) const
void set_output(const UString identifier, T &&value)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
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
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
GreasePencil * merge_layers_by_name(const GreasePencil &src_grease_pencil, const VArray< bool > &selection, const bke::AttributeFilter &attribute_filter)
GreasePencil * merge_layers(const GreasePencil &src_grease_pencil, GroupedSpan< int > layers_to_merge, const bke::AttributeFilter &attribute_filter)
static void node_init(bNodeTree *, bNode *node)
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
static GroupedSpan< int > get_layers_map_by_id(const GreasePencil &src_grease_pencil, const GeoNodeExecParams &params, Array< int > &r_offsets, Array< int > &r_indices)
static void merge_layers(GeometrySet &geometry, const GeoNodeExecParams &params, const AttributeFilter &attribute_filter)
NODE_STORAGE_FUNCS(NodeGeometryMergeLayers)
static GreasePencil * merge_by_name(const GreasePencil &src_grease_pencil, const GeoNodeExecParams &params, const AttributeFilter &attribute_filter)
GroupedSpan< int > build_groups_from_indices(Span< int > indices, int groups_num, Array< int > &offset_data, Array< int > &index_data)
const EnumPropertyItem rna_enum_node_grease_pencil_merge_mode_items[]
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
@ GEO_NODE_MERGE_LAYERS_BY_ID
@ GEO_NODE_MERGE_LAYERS_BY_NAME
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
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(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393