Blender V5.3
node_geo_get_named_grid.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "BKE_volume.hh"
8#include "BKE_volume_grid.hh"
9
10#include "BLT_translation.hh"
11
12#include "RNA_enum_types.hh"
13
14#include "NOD_rna_define.hh"
15
17#include "UI_resources.hh"
18
20
22{
23 b.add_input<decl::Geometry>("Volume"_ustr).description("Volume to take a named grid out of");
24 b.add_input<decl::String>("Name"_ustr).optional_label().is_volume_grid_name();
25 b.add_input<decl::Bool>("Remove"_ustr)
26 .default_value(true)
28
29 b.add_output<decl::Geometry>("Volume"_ustr);
30
31 const bNode *node = b.node_or_null();
32 if (!node) {
33
34 return;
35 }
36
37 b.add_output(eNodeSocketDatatype(node->custom1), "Grid"_ustr)
38 .structure_type(StructureType::Grid);
39}
40
41static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
42{
43 layout.use_property_split_set(true);
44 layout.use_property_decorate_set(false);
45 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
46}
47
48#ifdef WITH_OPENVDB
49static std::optional<SocketValueVariant> try_get_named_grid(
50 GeoNodeExecParams &params,
51 Volume &volume,
52 const StringRef grid_name,
53 const bool remove_grid,
54 const eNodeSocketDatatype desired_socket_type)
55{
56 const bke::VolumeGridData *grid = BKE_volume_grid_find(&volume, grid_name);
57 if (!grid) {
58 return std::nullopt;
59 }
60 const VolumeGridType stored_grid_type = grid->grid_type();
61 const std::optional<eNodeSocketDatatype> current_socket_type = bke::grid_type_to_socket_type(
62 stored_grid_type);
63 if (!current_socket_type) {
64 return std::nullopt;
65 }
66 /* Increment user count before removing from volume. */
67 grid->add_user();
68 if (remove_grid) {
69 BKE_volume_grid_remove(&volume, grid);
70 }
71 SocketValueVariant value_variant = SocketValueVariant::from(bke::GVolumeGrid(grid));
72 if (current_socket_type != desired_socket_type) {
73 std::optional<SocketValueVariant> converted_value = implicitly_convert_socket_value(
74 *bke::node_socket_type_find_static(*current_socket_type),
75 value_variant,
76 *bke::node_socket_type_find_static(desired_socket_type));
77 if (!converted_value) {
78 return std::nullopt;
79 }
80 params.error_message_add(NodeWarningType::Info, "Implicit grid type conversion");
81 value_variant = std::move(*converted_value);
82 }
83 return value_variant;
84}
85#endif
86
88{
89#ifdef WITH_OPENVDB
90 const bNode &node = params.node();
91 GeometrySet geometry_set = params.extract_input<GeometrySet>("Volume"_ustr);
92 const std::string grid_name = params.extract_input<std::string>("Name"_ustr);
93 const bool remove_grid = params.extract_input<bool>("Remove"_ustr);
94 const eNodeSocketDatatype socket_type = eNodeSocketDatatype(node.custom1);
95
96 if (Volume *volume = geometry_set.get_volume_for_write()) {
97 if (std::optional<SocketValueVariant> value_variant = try_get_named_grid(
98 params, *volume, grid_name, remove_grid, socket_type))
99 {
100 params.set_output("Grid"_ustr, std::move(*value_variant));
101 params.set_output("Volume"_ustr, std::move(geometry_set));
102 return;
103 }
104 }
105 if (!grid_name.empty()) {
107 "No supported grid found with the given name");
108 }
109 if (std::optional<VolumeGridType> grid_type = bke::socket_type_to_grid_type(socket_type)) {
110 params.set_output("Grid"_ustr, bke::GVolumeGrid(*grid_type));
111 params.set_output("Volume"_ustr, std::move(geometry_set));
112 return;
113 }
114 params.set_output("Volume"_ustr, std::move(geometry_set));
116
117#else
119#endif
120}
121
122static void node_init(bNodeTree * /*tree*/, bNode *node)
123{
124 node->custom1 = SOCK_FLOAT;
125}
126
127static void node_rna(StructRNA *srna)
128{
130 "data_type",
131 "Data Type",
132 "Node socket data type",
137}
138
139static void node_register()
140{
141 static bke::bNodeType ntype;
142
143 geo_node_type_base(&ntype, "GeometryNodeGetNamedGrid"_ustr, GEO_NODE_GET_NAMED_GRID);
144 ntype.ui_name = "Get Named Grid";
145 ntype.ui_description = "Get volume grid from a volume geometry with the specified name";
146 ntype.enum_name_legacy = "GET_NAMED_GRID";
148 ntype.declare = node_declare;
150 ntype.initfunc = node_init;
153
154 node_rna(ntype.rna_ext.srna);
155}
157
158} // namespace blender::nodes::node_geo_get_named_grid_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_GET_NAMED_GRID
Volume data-block.
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
std::optional< std::string > translation_context
static SocketValueVariant from(T &&value)
std::optional< VolumeGridType > socket_type_to_grid_type(eNodeSocketDatatype type)
Definition node.cc:6080
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
std::optional< eNodeSocketDatatype > grid_type_to_socket_type(VolumeGridType type)
Definition node.cc:6096
bNodeSocketType * node_socket_type_find_static(int type, int subtype=0)
Definition node.cc:2975
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
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)
std::optional< SocketValueVariant > implicitly_convert_socket_value(const bke::bNodeSocketType &from_type, const SocketValueVariant &from_value, const bke::bNodeSocketType &to_type)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams &params)
const bke::VolumeGridData * BKE_volume_grid_find(const Volume *volume, StringRef name)
void BKE_volume_grid_remove(Volume *volume, const bke::VolumeGridData *grid)
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)
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
const char * enum_name_legacy
Definition BKE_node.hh:274
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)