Blender V5.3
node_geo_set_grid_background.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "BKE_node_runtime.hh"
9
10#include "NOD_rna_define.hh"
12
14#include "UI_resources.hh"
15
16#include "RNA_enum_types.hh"
17
19
21{
22 const bNode *node = b.node_or_null();
23 if (!node) {
24 return;
25 }
26
27 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
28
29 b.use_custom_socket_order();
30 b.allow_any_socket_order();
31 b.add_default_layout();
32 b.add_input(data_type, "Grid"_ustr)
33 .hide_value()
34 .structure_type(StructureType::Grid)
35 .is_default_link_socket();
36 b.add_output(data_type, "Grid"_ustr).structure_type(StructureType::Grid).align_with_previous();
37 b.add_input(data_type, "Background"_ustr).structure_type(StructureType::Single);
38 b.add_input<decl::Bool>("Update Inactive"_ustr)
39 .description("Override all values stored for inactive voxels as well");
40}
41
42static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
43{
44 layout.use_property_split_set(true);
45 layout.use_property_decorate_set(false);
46 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
47}
48
49static std::optional<eNodeSocketDatatype> node_type_for_socket_type(const bNodeSocket &socket)
50{
51 switch (socket.type) {
52 case SOCK_FLOAT:
53 return SOCK_FLOAT;
54 case SOCK_BOOLEAN:
55 return SOCK_BOOLEAN;
56 case SOCK_INT:
57 return SOCK_INT;
58 case SOCK_VECTOR:
59 case SOCK_RGBA:
60 return SOCK_VECTOR;
61 default:
62 return std::nullopt;
63 }
64}
65
67{
68 const std::optional<eNodeSocketDatatype> data_type = node_type_for_socket_type(
69 params.other_socket());
70 if (!data_type) {
71 return;
72 }
73 const bNodeSocket &other_socket = params.other_socket();
74 const StructureType structure_type = other_socket.runtime->inferred_structure_type;
75 const bool is_grid = structure_type == StructureType::Grid;
76 const bool is_dynamic = structure_type == StructureType::Dynamic;
77
78 if (params.in_out() == SOCK_IN) {
79 if (is_grid || is_dynamic) {
80 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
81 bNode &node = params.add_node("GeometryNodeSetGridBackground"_ustr);
82 node.custom1 = *data_type;
83 params.update_and_connect_available_socket(node, "Grid"_ustr);
84 });
85 }
86 if (!is_grid || is_dynamic) {
87 params.add_item(IFACE_("Background"), [data_type](LinkSearchOpParams &params) {
88 bNode &node = params.add_node("GeometryNodeSetGridBackground"_ustr);
89 node.custom1 = *data_type;
90 params.update_and_connect_available_socket(node, "Background"_ustr);
91 });
92 }
93 }
94 else {
95 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
96 bNode &node = params.add_node("GeometryNodeSetGridBackground"_ustr);
97 node.custom1 = *data_type;
98 params.update_and_connect_available_socket(node, "Grid"_ustr);
99 });
100 }
101}
102
104{
105#ifdef WITH_OPENVDB
106 bke::GVolumeGrid grid = params.extract_input<bke::GVolumeGrid>("Grid"_ustr);
107 if (!grid) {
109 return;
110 }
111
112 auto background_variant = params.extract_input<bke::SocketValueVariant>("Background"_ustr);
113 background_variant.ensure_type(*grid->cpp_type());
114 const GPointer background = background_variant.get();
115
116 const bool update_inactive = params.get_input<bool>("Update Inactive"_ustr);
117
118 bke::VolumeTreeAccessToken tree_token;
119 openvdb::GridBase &grid_base = grid.get_for_write().grid_for_write(tree_token);
120 bke::volume_grid::set_grid_background(grid_base, background);
121
122 if (update_inactive) {
123 bke::volume_grid::set_inactive_values(grid_base, background);
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
137static void node_rna(StructRNA *srna)
138{
140 "data_type",
141 "Data Type",
142 "Node socket data type",
147}
148
149static void node_register()
150{
151 static bke::bNodeType ntype;
152
153 geo_node_type_base(&ntype, "GeometryNodeSetGridBackground"_ustr);
154 ntype.ui_name = "Set Grid Background";
155 ntype.ui_description = "Set the background value used for inactive voxels and tiles";
157 ntype.initfunc = node_init;
161 ntype.declare = node_declare;
164
165 node_rna(ntype.rna_ext.srna);
166}
168
169} // namespace blender::nodes::node_geo_set_grid_background
#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
const void * get() const
nodes::StructureType inferred_structure_type
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
T extract_input(const UString identifier)
T get_input(const UString identifier) const
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 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)
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)
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
static constexpr int _160
Definition BKE_node.hh:246
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)