Blender V5.3
node_geo_grid_deactivate_voxels.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "BKE_volume_grid.hh"
8
9#include "NOD_rna_define.hh"
10#include "NOD_socket.hh"
12
13#include "RNA_enum_types.hh"
14
15#include "UI_interface.hh"
17#include "UI_resources.hh"
18
19#ifdef WITH_OPENVDB
22#endif
23
25
27
29{
30 b.use_custom_socket_order();
31 b.allow_any_socket_order();
32
33 b.add_default_layout();
34
35 const bNodeTree *tree = b.tree_or_null();
36 const bNode *node = b.node_or_null();
37 if (!node || !tree) {
38 return;
39 }
40 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
41
42 b.add_input(data_type, "Grid"_ustr).hide_value().structure_type(StructureType::Grid);
43 b.add_output(data_type, "Grid"_ustr)
44 .structure_type(StructureType::Grid)
45 .align_with_previous()
46 .propagate_references({1});
47
48 b.add_input<decl::Bool>("Selection"_ustr)
49 .default_value(true)
50 .hide_value()
51 .structure_type(StructureType::Field);
52}
53
54static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
55{
56 layout.use_property_split_set(true);
57 layout.use_property_decorate_set(false);
58 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
59}
60
61static std::optional<eNodeSocketDatatype> node_type_for_socket_type(const bNodeSocket &socket)
62{
63 switch (socket.type) {
64 case SOCK_FLOAT:
65 return SOCK_FLOAT;
66 case SOCK_BOOLEAN:
67 return SOCK_BOOLEAN;
68 case SOCK_INT:
69 return SOCK_INT;
70 case SOCK_VECTOR:
71 case SOCK_RGBA:
72 return SOCK_VECTOR;
73 default:
74 return std::nullopt;
75 }
76}
77
79{
80 const bNodeSocket &other_socket = params.other_socket();
81 const StructureType structure_type = other_socket.runtime->inferred_structure_type;
82 const eNodeSocketDatatype other_type = other_socket.type;
83
84 if (params.in_out() == SOCK_IN) {
85 if (ELEM(structure_type, StructureType::Grid, StructureType::Dynamic)) {
86 const std::optional<eNodeSocketDatatype> data_type = node_type_for_socket_type(other_socket);
87 if (data_type) {
88 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
89 bNode &node = params.add_node("GeometryNodeGridDeactivateVoxels"_ustr);
90 node.custom1 = *data_type;
91 params.update_and_connect_available_socket(node, "Grid"_ustr);
92 });
93 }
94 }
95 if (params.node_tree().typeinfo->validate_link(other_type, SOCK_BOOLEAN)) {
96 params.add_item(IFACE_("Selection"), [](LinkSearchOpParams &params) {
97 bNode &node = params.add_node("GeometryNodeGridDeactivateVoxels"_ustr);
98 params.connect_available_socket(node, "Selection"_ustr);
99 });
100 }
101 }
102 else {
103 if (ELEM(structure_type, StructureType::Grid, StructureType::Dynamic)) {
104 const std::optional<eNodeSocketDatatype> data_type = node_type_for_socket_type(other_socket);
105 if (data_type) {
106 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
107 bNode &node = params.add_node("GeometryNodeGridDeactivateVoxels"_ustr);
108 node.custom1 = *data_type;
109 params.update_and_connect_available_socket(node, "Grid"_ustr);
110 });
111 }
112 }
113 }
114}
115
116#ifdef WITH_OPENVDB
117
118BLI_NOINLINE static void process_leaf_node(const fn::Field<bool> &selection_field,
119 const openvdb::math::Transform &transform,
120 const volume_grid::LeafNodeMask &leaf_node_mask,
121 const openvdb::CoordBBox &leaf_bbox,
122 const volume_grid::GetVoxelsFn get_voxels_fn,
123 openvdb::GridBase &output_grid)
124{
125 AlignedBuffer<8192, 8> allocation_buffer;
126 ResourceScope scope(allocation_buffer);
127
129 IndexRange(volume_grid::LeafNodeMask::SIZE),
130 scope.allocator(),
131 [&](const int64_t i) { return leaf_node_mask.isOn(i); },
133
134 const openvdb::Coord any_voxel_in_leaf = leaf_bbox.min();
135 MutableSpan<openvdb::Coord> voxels = scope.allocator().allocate_array<openvdb::Coord>(
136 index_mask.min_array_size());
137 get_voxels_fn(voxels);
138
139 bke::VoxelFieldContext field_context{transform, voxels};
140 fn::FieldEvaluator evaluator{field_context, &index_mask};
141
142 MutableSpan<bool> selection = scope.allocator().allocate_array<bool>(
143 index_mask.min_array_size());
144 evaluator.add_with_destination(selection_field, selection);
145
146 evaluator.evaluate();
147
148 volume_grid::set_leaf_values_off(output_grid, any_voxel_in_leaf, selection);
149}
150
151BLI_NOINLINE static void process_voxels(const fn::Field<bool> &selection_field,
152 const openvdb::math::Transform &transform,
153 const Span<openvdb::Coord> voxels,
154 openvdb::GridBase &output_grid)
155{
156 const int64_t voxels_num = voxels.size();
157 AlignedBuffer<8192, 8> allocation_buffer;
158 ResourceScope scope(allocation_buffer);
159
160 bke::VoxelFieldContext field_context{transform, voxels};
161 fn::FieldEvaluator evaluator{field_context, voxels_num};
162
163 MutableSpan<bool> selection = scope.allocator().allocate_array<bool>(voxels_num);
164 evaluator.add_with_destination(selection_field, selection);
165 evaluator.evaluate();
166
167 volume_grid::set_grid_values_off(output_grid, selection, voxels);
168}
169
170BLI_NOINLINE static void process_tiles(const fn::Field<bool> &selection_field,
171 const openvdb::math::Transform &transform,
172 const Span<openvdb::CoordBBox> tiles,
173 openvdb::GridBase &output_grid)
174{
175 const int64_t tiles_num = tiles.size();
176 AlignedBuffer<8192, 8> allocation_buffer;
177 ResourceScope scope(allocation_buffer);
178
179 bke::TilesFieldContext field_context{transform, tiles};
180 fn::FieldEvaluator evaluator{field_context, tiles_num};
181
182 MutableSpan<bool> selection;
183 selection = scope.allocator().allocate_array<bool>(tiles_num);
184 evaluator.add_with_destination(selection_field, selection);
185 evaluator.evaluate();
186
187 volume_grid::set_tile_values_off(output_grid, selection, tiles);
188}
189
190#endif
191
193{
194#ifdef WITH_OPENVDB
195 bke::GVolumeGrid grid = params.extract_input<bke::GVolumeGrid>("Grid"_ustr);
196 if (!grid) {
197 params.error_message_add(NodeWarningType::Error, "The grid input is required");
199 return;
200 }
201
202 bke::VolumeTreeAccessToken tree_token;
203 openvdb::GridBase &grid_base = grid.get_for_write().grid_for_write(tree_token);
204
205 fn::Field<bool> selection_field = params.extract_input<fn::Field<bool>>("Selection"_ustr);
206 if (!selection_field.depends_on_input()) {
207 if (fn::evaluate_constant_field(selection_field)) {
208 /* Deactivate everything. */
209 grid_base.clear();
210 }
211 /* If selection_field evaluates to false keep the grid unmodified. */
212 }
213 else {
214 const openvdb::math::Transform &transform = grid_base.transform();
215 openvdb::MaskTree mask_tree;
216 volume_grid::to_typed_grid(grid_base,
217 [&](const auto &grid) { mask_tree.topologyUnion(grid.tree()); });
218
219 volume_grid::parallel_grid_topology_tasks(
220 mask_tree,
221 [&](const volume_grid::LeafNodeMask &leaf_node_mask,
222 const openvdb::CoordBBox &leaf_bbox,
223 const volume_grid::GetVoxelsFn get_voxels_fn) {
224 process_leaf_node(
225 selection_field, transform, leaf_node_mask, leaf_bbox, get_voxels_fn, grid_base);
226 },
227 [&](const Span<openvdb::Coord> voxels) {
228 process_voxels(selection_field, transform, voxels, grid_base);
229 },
231 process_tiles(selection_field, transform, tiles, grid_base);
232 });
233 }
234
235 params.set_output("Grid"_ustr, std::move(grid));
236#else
238#endif
239}
240
241static void node_init(bNodeTree * /*tree*/, bNode *node)
242{
243 node->custom1 = SOCK_FLOAT;
244}
245
246static void node_rna(StructRNA *srna)
247{
249 "data_type",
250 "Data Type",
251 "Node socket data type",
256}
257
258static void node_register()
259{
260 static blender::bke::bNodeType ntype;
261
262 geo_node_type_base(&ntype, "GeometryNodeGridDeactivateVoxels"_ustr);
263 ntype.ui_name = "Deactivate Voxels";
264 ntype.ui_description = "Deactivate selected voxels and tiles";
266 ntype.declare = node_declare;
267 ntype.initfunc = node_init;
272
273 node_rna(ntype.rna_ext.srna);
274}
276
277} // namespace blender::nodes::node_geo_grid_deactivate_voxels
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
#define BLI_NOINLINE
#define ELEM(...)
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
constexpr int64_t size() const
Definition BLI_span.hh:256
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
constexpr IndexRange()=default
nodes::StructureType inferred_structure_type
int add_with_destination(GField field, GVMutableArray dst)
bool depends_on_input() const
Definition FN_field.hh:513
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
void connect_available_socket(bNode &new_node, UString socket_name)
ccl_gpu_kernel_postfix ccl_global KernelWorkTile * tiles
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
constexpr Serial serial
void evaluate_constant_field(const GField &field, void *r_value)
static void node_gather_link_search_ops(GatherLinkSearchOpParams &params)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static std::optional< eNodeSocketDatatype > node_type_for_socket_type(const bNodeSocket &socket)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
const EnumPropertyItem * grid_socket_type_items_filter_fn(bContext *, PointerRNA *, PropertyRNA *, bool *r_free)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams &params)
__int64 int64_t
Definition BKE_text.h:90
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
eNodeSocketDatatype type
bke::bNodeSocketRuntime * runtime
bke::bNodeTreeType * typeinfo
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
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
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
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)