Blender V5.0
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").description("Volume to take a named grid out of");
24 b.add_input<decl::String>("Name").optional_label().is_volume_grid_name();
25 b.add_input<decl::Bool>("Remove").default_value(true).translation_context(
27
28 b.add_output<decl::Geometry>("Volume");
29
30 const bNode *node = b.node_or_null();
31 if (!node) {
32
33 return;
34 }
35
36 b.add_output(eNodeSocketDatatype(node->custom1), "Grid").structure_type(StructureType::Grid);
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
46#ifdef WITH_OPENVDB
47static std::optional<SocketValueVariant> try_get_named_grid(
49 Volume &volume,
50 const StringRef grid_name,
51 const bool remove_grid,
52 const eNodeSocketDatatype desired_socket_type)
53{
54 const bke::VolumeGridData *grid = BKE_volume_grid_find(&volume, grid_name);
55 if (!grid) {
56 return std::nullopt;
57 }
58 const VolumeGridType stored_grid_type = grid->grid_type();
59 const std::optional<eNodeSocketDatatype> current_socket_type = bke::grid_type_to_socket_type(
60 stored_grid_type);
61 if (!current_socket_type) {
62 return std::nullopt;
63 }
64 /* Increment user count before removing from volume. */
65 grid->add_user();
66 if (remove_grid) {
67 BKE_volume_grid_remove(&volume, grid);
68 }
69 SocketValueVariant value_variant = SocketValueVariant::From(bke::GVolumeGrid(grid));
70 if (current_socket_type != desired_socket_type) {
71 std::optional<SocketValueVariant> converted_value = implicitly_convert_socket_value(
72 *bke::node_socket_type_find_static(*current_socket_type),
73 value_variant,
74 *bke::node_socket_type_find_static(desired_socket_type));
75 if (!converted_value) {
76 return std::nullopt;
77 }
78 params.error_message_add(NodeWarningType::Info, "Implicit grid type conversion");
79 value_variant = std::move(*converted_value);
80 }
81 return value_variant;
82}
83#endif
84
86{
87#ifdef WITH_OPENVDB
88 const bNode &node = params.node();
89 GeometrySet geometry_set = params.extract_input<GeometrySet>("Volume");
90 const std::string grid_name = params.extract_input<std::string>("Name");
91 const bool remove_grid = params.extract_input<bool>("Remove");
92 const eNodeSocketDatatype socket_type = eNodeSocketDatatype(node.custom1);
93
94 if (Volume *volume = geometry_set.get_volume_for_write()) {
95 if (std::optional<SocketValueVariant> value_variant = try_get_named_grid(
96 params, *volume, grid_name, remove_grid, socket_type))
97 {
98 params.set_output("Grid", std::move(*value_variant));
99 params.set_output("Volume", std::move(geometry_set));
100 return;
101 }
102 }
103 if (!grid_name.empty()) {
104 params.error_message_add(NodeWarningType::Warning,
105 "No supported grid found with the given name");
106 }
107 if (std::optional<VolumeGridType> grid_type = bke::socket_type_to_grid_type(socket_type)) {
108 params.set_output("Grid", bke::GVolumeGrid(*grid_type));
109 params.set_output("Volume", std::move(geometry_set));
110 return;
111 }
112 params.set_output("Volume", std::move(geometry_set));
113 params.set_default_remaining_outputs();
114
115#else
117#endif
118}
119
120static void node_init(bNodeTree * /*tree*/, bNode *node)
121{
122 node->custom1 = SOCK_FLOAT;
123}
124
125static void node_rna(StructRNA *srna)
126{
128 "data_type",
129 "Data Type",
130 "Node socket data type",
135}
136
137static void node_register()
138{
139 static blender::bke::bNodeType ntype;
140
141 geo_node_type_base(&ntype, "GeometryNodeGetNamedGrid", GEO_NODE_GET_NAMED_GRID);
142 ntype.ui_name = "Get Named Grid";
143 ntype.ui_description = "Get volume grid from a volume geometry with the specified name";
144 ntype.enum_name_legacy = "GET_NAMED_GRID";
146 ntype.declare = node_declare;
148 ntype.initfunc = node_init;
151
152 node_rna(ntype.rna_ext.srna);
153}
155
156} // namespace blender::nodes::node_geo_get_named_grid_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define GEO_NODE_GET_NAMED_GRID
Volume data-block.
const blender::bke::VolumeGridData * BKE_volume_grid_find(const Volume *volume, blender::StringRef name)
void BKE_volume_grid_remove(Volume *volume, const blender::bke::VolumeGridData *grid)
VolumeGridType
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
eNodeSocketDatatype
@ SOCK_FLOAT
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
std::optional< std::string > translation_context
static SocketValueVariant From(T &&value)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
std::optional< VolumeGridType > socket_type_to_grid_type(eNodeSocketDatatype type)
Definition node.cc:5298
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
std::optional< eNodeSocketDatatype > grid_type_to_socket_type(VolumeGridType type)
Definition node.cc:5314
bNodeSocketType * node_socket_type_find_static(int type, int subtype=0)
Definition node.cc:2471
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static void node_layout(uiLayout *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)
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
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
const char * enum_name_legacy
Definition BKE_node.hh:247
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
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