Blender V5.0
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
8
9#include "NOD_rna_define.hh"
11
13#include "UI_resources.hh"
14
15#include "RNA_enum_types.hh"
16
18
20{
21 const bNode *node = b.node_or_null();
22 if (!node) {
23 return;
24 }
25
26 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
27
28 b.use_custom_socket_order();
29 b.allow_any_socket_order();
30 b.add_default_layout();
31 b.add_input(data_type, "Grid")
32 .hide_value()
33 .structure_type(StructureType::Grid)
34 .is_default_link_socket();
35 b.add_output(data_type, "Grid").structure_type(StructureType::Grid).align_with_previous();
36 b.add_input(data_type, "Background").structure_type(StructureType::Single);
37}
38
39static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
40{
41 layout->use_property_split_set(true);
42 layout->use_property_decorate_set(false);
43 layout->prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
44}
45
46static std::optional<eNodeSocketDatatype> node_type_for_socket_type(const bNodeSocket &socket)
47{
48 switch (socket.type) {
49 case SOCK_FLOAT:
50 return SOCK_FLOAT;
51 case SOCK_BOOLEAN:
52 return SOCK_BOOLEAN;
53 case SOCK_INT:
54 return SOCK_INT;
55 case SOCK_VECTOR:
56 case SOCK_RGBA:
57 return SOCK_VECTOR;
58 default:
59 return std::nullopt;
60 }
61}
62
64{
65 const std::optional<eNodeSocketDatatype> data_type = node_type_for_socket_type(
66 params.other_socket());
67 if (!data_type) {
68 return;
69 }
70 const bNodeSocket &other_socket = params.other_socket();
71 const StructureType structure_type = other_socket.runtime->inferred_structure_type;
72 const bool is_grid = structure_type == StructureType::Grid;
73 const bool is_dynamic = structure_type == StructureType::Dynamic;
74
75 if (params.in_out() == SOCK_IN) {
76 if (is_grid || is_dynamic) {
77 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
78 bNode &node = params.add_node("GeometryNodeSetGridBackground");
79 node.custom1 = *data_type;
80 params.update_and_connect_available_socket(node, "Grid");
81 });
82 }
83 if (!is_grid || is_dynamic) {
84 params.add_item(IFACE_("Background"), [data_type](LinkSearchOpParams &params) {
85 bNode &node = params.add_node("GeometryNodeSetGridBackground");
86 node.custom1 = *data_type;
87 params.update_and_connect_available_socket(node, "Background");
88 });
89 }
90 }
91 else {
92 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
93 bNode &node = params.add_node("GeometryNodeSetGridBackground");
94 node.custom1 = *data_type;
95 params.update_and_connect_available_socket(node, "Grid");
96 });
97 }
98}
99
101{
102#ifdef WITH_OPENVDB
103 bke::GVolumeGrid grid = params.extract_input<bke::GVolumeGrid>("Grid");
104 if (!grid) {
105 params.set_default_remaining_outputs();
106 return;
107 }
108
109 const auto background = params.extract_input<bke::SocketValueVariant>("Background");
110
111 bke::VolumeTreeAccessToken tree_token;
112 openvdb::GridBase &grid_base = grid.get_for_write().grid_for_write(tree_token);
113 bke::volume_grid::set_grid_background(grid_base, background.get_single_ptr());
114
115 params.set_output("Grid", std::move(grid));
116#else
118#endif
119}
120
121static void node_init(bNodeTree * /*tree*/, bNode *node)
122{
123 node->custom1 = SOCK_FLOAT;
124}
125
126static void node_rna(StructRNA *srna)
127{
129 "data_type",
130 "Data Type",
131 "Node socket data type",
136}
137
138static void node_register()
139{
140 static blender::bke::bNodeType ntype;
141
142 geo_node_type_base(&ntype, "GeometryNodeSetGridBackground");
143 ntype.ui_name = "Set Grid Background";
144 ntype.ui_description = "Set the background value used for inactive voxels and tiles";
146 ntype.initfunc = node_init;
150 ntype.declare = node_declare;
152
153 node_rna(ntype.rna_ext.srna);
154}
156
157} // namespace blender::nodes::node_geo_set_grid_background
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define IFACE_(msgid)
@ SOCK_IN
eNodeSocketDatatype
@ SOCK_INT
@ SOCK_VECTOR
@ SOCK_BOOLEAN
@ SOCK_FLOAT
@ SOCK_RGBA
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
static void node_layout(uiLayout *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)
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
StructRNA * srna
bNodeSocketRuntimeHandle * runtime
int16_t custom1
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:378
NodeDeclareFunction declare
Definition BKE_node.hh:362
void use_property_decorate_set(bool is_sep)
void use_property_split_set(bool value)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
PointerRNA * ptr
Definition wm_files.cc:4238