Blender V4.5
node_geo_store_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_lib_id.hh"
8#include "BKE_volume.hh"
9#include "BKE_volume_grid.hh"
10
11#include "RNA_enum_types.hh"
12
13#include "NOD_rna_define.hh"
15
16#include "UI_interface.hh"
17#include "UI_resources.hh"
18
20
22{
23 b.use_custom_socket_order();
24 b.allow_any_socket_order();
25 b.add_default_layout();
26 b.add_input<decl::Geometry>("Volume");
27 b.add_output<decl::Geometry>("Volume").align_with_previous();
28 b.add_input<decl::String>("Name").hide_label();
29
30 const bNode *node = b.node_or_null();
31 if (!node) {
32 return;
33 }
34
35 b.add_input(*bke::grid_type_to_socket_type(VolumeGridType(node->custom1)), "Grid")
36 .hide_value()
37 .structure_type(StructureType::Grid);
38}
39
41{
42 if (!USER_EXPERIMENTAL_TEST(&U, use_new_volume_nodes)) {
43 return;
44 }
45 if (params.other_socket().type == SOCK_GEOMETRY) {
46 params.add_item(IFACE_("Volume"), [](LinkSearchOpParams &params) {
47 bNode &node = params.add_node("GeometryNodeStoreNamedGrid");
48 params.update_and_connect_available_socket(node, "Volume");
49 });
50 }
51 if (params.in_out() == SOCK_IN) {
52 if (params.other_socket().type == SOCK_STRING) {
53 params.add_item(IFACE_("Name"), [](LinkSearchOpParams &params) {
54 bNode &node = params.add_node("GeometryNodeStoreNamedGrid");
55 params.update_and_connect_available_socket(node, "Name");
56 });
57 }
58 if (const std::optional<VolumeGridType> data_type = bke::socket_type_to_grid_type(
59 eNodeSocketDatatype(params.other_socket().type)))
60 {
61 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
62 bNode &node = params.add_node("GeometryNodeStoreNamedGrid");
63 node.custom1 = *data_type;
64 params.update_and_connect_available_socket(node, "Grid");
65 });
66 }
67 }
68}
69
70static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
71{
72 uiLayoutSetPropSep(layout, true);
73 uiLayoutSetPropDecorate(layout, false);
74 layout->prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
75}
76
77static void node_init(bNodeTree * /*tree*/, bNode *node)
78{
80}
81
82#ifdef WITH_OPENVDB
83
84static void try_store_grid(GeoNodeExecParams params, Volume &volume)
85{
86 const std::string grid_name = params.extract_input<std::string>("Name");
87
88 bke::GVolumeGrid grid = params.extract_input<bke::GVolumeGrid>("Grid");
89 if (!grid) {
90 return;
91 }
92
93 if (const bke::VolumeGridData *existing_grid = BKE_volume_grid_find(&volume, grid_name)) {
94 BKE_volume_grid_remove(&volume, existing_grid);
95 }
96 grid.get_for_write().set_name(grid_name);
97 grid->add_user();
98 BKE_volume_grid_add(&volume, grid.get());
99}
100
101static void node_geo_exec(GeoNodeExecParams params)
102{
103 GeometrySet geometry_set = params.extract_input<GeometrySet>("Volume");
104 Volume *volume = geometry_set.get_volume_for_write();
105 if (!volume) {
106 volume = BKE_id_new_nomain<Volume>("Store Named Grid Output");
107 geometry_set.replace_volume(volume);
108 }
109
110 try_store_grid(params, *volume);
111
112 params.set_output("Volume", geometry_set);
113}
114
115#else /* WITH_OPENVDB */
116
121
122#endif /* WITH_OPENVDB */
123
124static void node_rna(StructRNA *srna)
125{
127 "data_type",
128 "Data Type",
129 "Type of grid data",
134}
135
136static void node_register()
137{
138 static blender::bke::bNodeType ntype;
139
140 geo_node_type_base(&ntype, "GeometryNodeStoreNamedGrid", GEO_NODE_STORE_NAMED_GRID);
141 ntype.ui_name = "Store Named Grid";
142 ntype.ui_description = "Store grid data in a volume geometry with the specified name";
143 ntype.enum_name_legacy = "STORE_NAMED_GRID";
145 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_store_named_grid_cc
void * BKE_id_new_nomain(short type, const char *name)
Definition lib_id.cc:1500
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:447
#define GEO_NODE_STORE_NAMED_GRID
Volume data-block.
void BKE_volume_grid_add(Volume *volume, const blender::bke::VolumeGridData &grid)
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
@ VOLUME_GRID_FLOAT
#define IFACE_(msgid)
@ SOCK_IN
eNodeSocketDatatype
@ SOCK_GEOMETRY
@ SOCK_STRING
#define USER_EXPERIMENTAL_TEST(userdef, member)
struct Volume Volume
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
#define UI_ITEM_NONE
void uiLayoutSetPropDecorate(uiLayout *layout, bool is_sep)
#define U
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
std::optional< VolumeGridType > socket_type_to_grid_type(eNodeSocketDatatype type)
Definition node.cc:5487
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
std::optional< eNodeSocketDatatype > grid_type_to_socket_type(VolumeGridType type)
Definition node.cc:5503
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
static void search_link_ops(GatherLinkSearchOpParams &params)
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_data_type_socket_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_volume_grid_data_type_items[]
Definition rna_volume.cc:25
StructRNA * srna
Definition RNA_types.hh:909
void replace_volume(Volume *volume, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
Volume * get_volume_for_write()
int16_t custom1
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:277
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
const char * enum_name_legacy
Definition BKE_node.hh:235
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:247
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:371
NodeDeclareFunction declare
Definition BKE_node.hh:355
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:4226