Blender V5.3
node_geo_grid_mean.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 "NOD_rna_define.hh"
6#include "NOD_socket.hh"
8
9#include "RNA_enum_types.hh"
10
11#include "node_geometry_util.hh"
12
14#include "UI_resources.hh"
15
16#include "BKE_volume_grid.hh"
18#include "BKE_volume_openvdb.hh"
19
20#ifdef WITH_OPENVDB
21# include "openvdb/tools/Filter.h"
22#endif
23
25
27{
28 b.use_custom_socket_order();
29 b.allow_any_socket_order();
30 b.add_default_layout();
31 const bNode *node = b.node_or_null();
32 if (!node) {
33 return;
34 }
35 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
36 b.add_input(data_type, "Grid"_ustr).hide_value().structure_type(StructureType::Grid);
37 b.add_output(data_type, "Grid"_ustr).structure_type(StructureType::Grid).align_with_previous();
38
39 b.add_input<decl::Int>("Width"_ustr)
40 .default_value(1)
41 .min(0)
42 .max(10)
43 .structure_type(StructureType::Single)
44 .description("Filter kernel radius in voxels");
45
46 b.add_input<decl::Int>("Iterations"_ustr)
47 .default_value(1)
48 .min(0)
49 .max(100)
50 .structure_type(StructureType::Single)
51 .description("Number of iterations to apply the filter");
52}
53
54static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
55{
56 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
57}
58
59static std::optional<eNodeSocketDatatype> node_type_for_socket_type(const bNodeSocket &socket)
60{
61 switch (socket.type) {
62 case SOCK_FLOAT:
63 return SOCK_FLOAT;
64 case SOCK_INT:
65 return SOCK_INT;
66 case SOCK_VECTOR:
67 case SOCK_RGBA:
68 return SOCK_VECTOR;
69 default:
70 return std::nullopt;
71 }
72}
73
75{
76 const bNodeSocket &other_socket = params.other_socket();
77 const StructureType structure_type = other_socket.runtime->inferred_structure_type;
78 const bool is_grid = structure_type == StructureType::Grid;
79 const bool is_dynamic = structure_type == StructureType::Dynamic;
80
81 const std::optional<eNodeSocketDatatype> data_type = node_type_for_socket_type(other_socket);
82 if (!data_type) {
83 return;
84 }
85
86 if (params.in_out() == SOCK_IN && (is_grid || is_dynamic)) {
87 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
88 bNode &node = params.add_node("GeometryNodeGridMean"_ustr);
89 node.custom1 = *data_type;
90 params.update_and_connect_available_socket(node, "Grid"_ustr);
91 });
92 }
93 else if (params.in_out() == SOCK_OUT) {
94 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
95 bNode &node = params.add_node("GeometryNodeGridMean"_ustr);
96 node.custom1 = *data_type;
97 params.update_and_connect_available_socket(node, "Grid"_ustr);
98 });
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 int width = params.extract_input<int>("Width"_ustr);
112 const int iterations = params.extract_input<int>("Iterations"_ustr);
113 if (width <= 0 || iterations <= 0) {
114 params.set_output("Grid"_ustr, std::move(grid));
115 return;
116 }
117
118 bke::VolumeTreeAccessToken tree_token;
119 openvdb::GridBase &grid_base = grid.get_for_write().grid_for_write(tree_token);
120
121 bke::volume_grid::to_typed_grid(grid_base, [&](auto &typed_grid) {
122 openvdb::tools::Filter filter(typed_grid);
123 filter.mean(width, iterations);
124 });
125
126 params.set_output("Grid"_ustr, std::move(grid));
127#else
129#endif
130}
131
132static void node_init(bNodeTree * /*tree*/, bNode *node)
133{
134 node->custom1 = SOCK_FLOAT;
135}
136
138 PointerRNA * /*ptr*/,
139 PropertyRNA * /*prop*/,
140 bool *r_free)
141{
142 *r_free = true;
144 [](const EnumPropertyItem &item) -> bool {
146 item.value != SOCK_BOOLEAN;
147 });
148}
149
150static void node_rna(StructRNA *srna)
151{
153 "data_type",
154 "Data Type",
155 "Node socket data type",
160}
161
162static void node_register()
163{
164 static blender::bke::bNodeType ntype;
165
166 geo_node_type_base(&ntype, "GeometryNodeGridMean"_ustr);
167 ntype.ui_name = "Grid Mean";
168 ntype.ui_description =
169 "Apply mean (box) filter smoothing to a voxel. The mean value from surrounding voxels in a "
170 "box-shape defined by the radius replaces the voxel value.";
172 ntype.initfunc = node_init;
176 ntype.declare = node_declare;
178
179 node_rna(ntype.rna_ext.srna);
180}
182
183} // namespace blender::nodes::node_geo_grid_mean_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
nodes::StructureType inferred_structure_type
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 update_and_connect_available_socket(bNode &new_node, UString socket_name)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static std::optional< eNodeSocketDatatype > node_type_for_socket_type(const bNodeSocket &socket)
static void node_declare(NodeDeclarationBuilder &b)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static const EnumPropertyItem * grid_mean_socket_type_items_filter_fn(bContext *, PointerRNA *, PropertyRNA *, bool *r_free)
static void node_init(bNodeTree *, bNode *node)
static void node_gather_link_search_ops(GatherLinkSearchOpParams &params)
static void node_rna(StructRNA *srna)
static void node_geo_exec(GeoNodeExecParams params)
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)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams &params)
bool socket_type_supports_grids(const eNodeSocketDatatype socket_type)
const EnumPropertyItem * enum_items_filter(const EnumPropertyItem *original_item_array, FunctionRef< bool(const EnumPropertyItem &item)> fn)
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
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)