Blender V5.3
node_geo_grid_clip.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
5#include "BKE_volume_grid.hh"
8
9#include "NOD_rna_define.hh"
11
12#include "RNA_enum_types.hh"
13
15#include "UI_resources.hh"
16
17#include "node_geometry_util.hh"
18
19#ifdef WITH_OPENVDB
20# include <openvdb/tools/Clip.h>
21#endif
22
24
26{
27 const bNode *node = b.node_or_null();
28 if (!node) {
29 return;
30 }
31
32 b.use_custom_socket_order();
33 b.allow_any_socket_order();
34 b.add_default_layout();
35
36 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
37 b.add_input(data_type, "Grid"_ustr).hide_value().structure_type(StructureType::Grid);
38 b.add_output(data_type, "Grid"_ustr).structure_type(StructureType::Grid).align_with_previous();
39
40 b.add_input<decl::Int>("Min X"_ustr)
41 .default_value(0)
42 .structure_type(StructureType::Single)
43 .description("Minimum X index of the clipping bounding box");
44 b.add_input<decl::Int>("Min Y"_ustr)
45 .default_value(0)
46 .structure_type(StructureType::Single)
47 .description("Minimum Y index of the clipping bounding box");
48 b.add_input<decl::Int>("Min Z"_ustr)
49 .default_value(0)
50 .structure_type(StructureType::Single)
51 .description("Minimum Z index of the clipping bounding box");
52 b.add_input<decl::Int>("Max X"_ustr)
53 .default_value(32)
54 .structure_type(StructureType::Single)
55 .description("Maximum X index of the clipping bounding box");
56 b.add_input<decl::Int>("Max Y"_ustr)
57 .default_value(32)
58 .structure_type(StructureType::Single)
59 .description("Maximum Y index of the clipping bounding box");
60 b.add_input<decl::Int>("Max Z"_ustr)
61 .default_value(32)
62 .structure_type(StructureType::Single)
63 .description("Maximum Z index of the clipping bounding box");
64}
65
66static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
67{
68 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
69}
70
71static std::optional<eNodeSocketDatatype> node_type_for_socket_type(const bNodeSocket &socket)
72{
73 switch (socket.type) {
74 case SOCK_FLOAT:
75 return SOCK_FLOAT;
76 case SOCK_BOOLEAN:
77 return SOCK_BOOLEAN;
78 case SOCK_INT:
79 return SOCK_INT;
80 case SOCK_VECTOR:
81 case SOCK_RGBA:
82 return SOCK_VECTOR;
83 default:
84 return std::nullopt;
85 }
86}
87
89{
90 const std::optional<eNodeSocketDatatype> data_type = node_type_for_socket_type(
91 params.other_socket());
92 if (!data_type) {
93 return;
94 }
95 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
96 bNode &node = params.add_node("GeometryNodeGridClip"_ustr);
97 node.custom1 = *data_type;
98 params.update_and_connect_available_socket(node, "Grid"_ustr);
99 });
100}
101
103{
104#ifdef WITH_OPENVDB
105 bke::GVolumeGrid grid = params.extract_input<bke::GVolumeGrid>("Grid"_ustr);
106 if (!grid) {
108 return;
109 }
110
111 const int3 min_index = int3(params.extract_input<int>("Min X"_ustr),
112 params.extract_input<int>("Min Y"_ustr),
113 params.extract_input<int>("Min Z"_ustr));
114 const int3 max_index = int3(params.extract_input<int>("Max X"_ustr),
115 params.extract_input<int>("Max Y"_ustr),
116 params.extract_input<int>("Max Z"_ustr));
117
118 bke::VolumeTreeAccessToken tree_token;
119 openvdb::GridBase &grid_base = grid.get_for_write().grid_for_write(tree_token);
120
121 const openvdb::CoordBBox coord_bbox(openvdb::Coord(min_index.x, min_index.y, min_index.z),
122 openvdb::Coord(max_index.x, max_index.y, max_index.z));
123
124 bke::volume_grid::to_typed_grid(grid_base, [&](auto &typed_grid) {
125 auto active_bbox = typed_grid.evalActiveVoxelBoundingBox();
126 if (active_bbox.empty()) {
127 return;
128 }
129 active_bbox.intersect(coord_bbox);
130 typed_grid.clip(active_bbox);
131 });
132
133 params.set_output("Grid"_ustr, std::move(grid));
134#else
136#endif
137}
138
139static void node_init(bNodeTree * /*tree*/, bNode *node)
140{
141 node->custom1 = SOCK_FLOAT;
142}
143
144static void node_rna(StructRNA *srna)
145{
147 "data_type",
148 "Data Type",
149 "Node socket data type",
154}
155
156static void node_register()
157{
158 static bke::bNodeType ntype;
159 geo_node_type_base(&ntype, "GeometryNodeGridClip"_ustr);
160 ntype.ui_name = "Clip Grid";
161 ntype.ui_description =
162 "Deactivate grid voxels outside minimum and maximum coordinates, setting them to the "
163 "background value.";
165 ntype.declare = node_declare;
167 ntype.initfunc = node_init;
171 node_rna(ntype.rna_ext.srna);
172}
174
175} // namespace blender::nodes::node_geo_grid_clip_cc
#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
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static void node_geo_exec(GeoNodeExecParams params)
static void node_rna(StructRNA *srna)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
static std::optional< eNodeSocketDatatype > node_type_for_socket_type(const bNodeSocket &socket)
static void node_gather_link_search_ops(GatherLinkSearchOpParams &params)
static void node_init(bNodeTree *, bNode *node)
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)
VecBase< int32_t, 3 > int3
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
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 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)