Blender V5.0
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
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").description("Volume geometry to add a grid to");
27 b.add_output<decl::Geometry>("Volume").align_with_previous();
28 b.add_input<decl::String>("Name").optional_label().is_volume_grid_name();
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 (params.other_socket().type == SOCK_GEOMETRY) {
43 params.add_item(IFACE_("Volume"), [](LinkSearchOpParams &params) {
44 bNode &node = params.add_node("GeometryNodeStoreNamedGrid");
45 params.update_and_connect_available_socket(node, "Volume");
46 });
47 }
48 if (params.in_out() == SOCK_IN) {
49 if (params.other_socket().type == SOCK_STRING) {
50 params.add_item(IFACE_("Name"), [](LinkSearchOpParams &params) {
51 bNode &node = params.add_node("GeometryNodeStoreNamedGrid");
52 params.update_and_connect_available_socket(node, "Name");
53 });
54 }
55 if (const std::optional<VolumeGridType> data_type = bke::socket_type_to_grid_type(
56 eNodeSocketDatatype(params.other_socket().type)))
57 {
58 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
59 bNode &node = params.add_node("GeometryNodeStoreNamedGrid");
60 node.custom1 = *data_type;
61 params.update_and_connect_available_socket(node, "Grid");
62 });
63 }
64 }
65}
66
67static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
68{
69 layout->use_property_split_set(true);
70 layout->use_property_decorate_set(false);
71 layout->prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
72}
73
74static void node_init(bNodeTree * /*tree*/, bNode *node)
75{
77}
78
79#ifdef WITH_OPENVDB
80
81static void try_store_grid(GeoNodeExecParams params, Volume &volume)
82{
83 const std::string grid_name = params.extract_input<std::string>("Name");
84
85 bke::GVolumeGrid grid = params.extract_input<bke::GVolumeGrid>("Grid");
86 if (!grid) {
87 return;
88 }
89
90 if (const bke::VolumeGridData *existing_grid = BKE_volume_grid_find(&volume, grid_name)) {
91 BKE_volume_grid_remove(&volume, existing_grid);
92 }
93 grid.get_for_write().set_name(grid_name);
94 grid->add_user();
95 BKE_volume_grid_add(&volume, grid.get());
96}
97
98static void node_geo_exec(GeoNodeExecParams params)
99{
100 GeometrySet geometry_set = params.extract_input<GeometrySet>("Volume");
101 Volume *volume = geometry_set.get_volume_for_write();
102 if (!volume) {
103 volume = BKE_id_new_nomain<Volume>("Store Named Grid Output");
104 geometry_set.replace_volume(volume);
105 }
106
107 try_store_grid(params, *volume);
108
109 params.set_output("Volume", geometry_set);
110}
111
112#else /* WITH_OPENVDB */
113
118
119#endif /* WITH_OPENVDB */
120
121static void node_rna(StructRNA *srna)
122{
124 "data_type",
125 "Data Type",
126 "Type of grid data",
131}
132
133static void node_register()
134{
135 static blender::bke::bNodeType ntype;
136
137 geo_node_type_base(&ntype, "GeometryNodeStoreNamedGrid", GEO_NODE_STORE_NAMED_GRID);
138 ntype.ui_name = "Store Named Grid";
139 ntype.ui_description = "Store grid data in a volume geometry with the specified name";
140 ntype.enum_name_legacy = "STORE_NAMED_GRID";
142 ntype.declare = node_declare;
145 ntype.initfunc = node_init;
148
149 node_rna(ntype.rna_ext.srna);
150}
152
153} // namespace blender::nodes::node_geo_store_named_grid_cc
void * BKE_id_new_nomain(short type, const char *name)
Definition lib_id.cc:1519
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#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
struct Volume Volume
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
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
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
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: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
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