Blender V5.0
node_geo_grid_info.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 "BLI_math_matrix.hh"
8
10#include "BKE_volume_grid.hh"
11#include "BKE_volume_openvdb.hh"
12
13#include "NOD_rna_define.hh"
14#include "NOD_socket.hh"
16
18#include "UI_resources.hh"
19
20#include "RNA_enum_types.hh"
21
23
25{
26 const bNode *node = b.node_or_null();
27 if (!node) {
28 return;
29 }
30
31 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
32
33 b.add_input(data_type, "Grid").hide_value().structure_type(StructureType::Grid);
34
35 b.add_output<decl::Matrix>("Transform")
36 .description("Transform from grid index space to object space");
37 b.add_output(data_type, "Background Value").description("Default value outside of grid voxels");
38}
39
40static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
41{
42 layout->use_property_split_set(true);
43 layout->use_property_decorate_set(false);
44 layout->prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
45}
46
47static std::optional<eNodeSocketDatatype> node_type_for_socket_type(const bNodeSocket &socket)
48{
49 switch (socket.type) {
50 case SOCK_FLOAT:
51 return SOCK_FLOAT;
52 case SOCK_BOOLEAN:
53 return SOCK_BOOLEAN;
54 case SOCK_INT:
55 return SOCK_INT;
56 case SOCK_VECTOR:
57 case SOCK_RGBA:
58 return SOCK_VECTOR;
59 default:
60 return std::nullopt;
61 }
62}
63
65{
66 const bNodeSocket &other_socket = params.other_socket();
67 const StructureType structure_type = other_socket.runtime->inferred_structure_type;
68 const bool is_grid = structure_type == StructureType::Grid;
69 const bool is_dynamic = structure_type == StructureType::Dynamic;
70 const eNodeSocketDatatype other_type = eNodeSocketDatatype(other_socket.type);
71
72 if (params.in_out() == SOCK_IN) {
73 if (is_grid || is_dynamic) {
74 const std::optional<eNodeSocketDatatype> data_type = node_type_for_socket_type(other_socket);
75 if (data_type) {
76 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
77 bNode &node = params.add_node("GeometryNodeGridInfo");
78 node.custom1 = *data_type;
79 params.update_and_connect_available_socket(node, "Grid");
80 });
81 }
82 }
83 }
84 else {
85 if (params.node_tree().typeinfo->validate_link(SOCK_MATRIX, other_type)) {
86 params.add_item(IFACE_("Transform"), [](LinkSearchOpParams &params) {
87 bNode &node = params.add_node("GeometryNodeGridInfo");
88 params.update_and_connect_available_socket(node, "Transform");
89 });
90 }
91 const std::optional<eNodeSocketDatatype> data_type = node_type_for_socket_type(other_socket);
92 if (data_type) {
93 params.add_item(IFACE_("Background Value"), [data_type](LinkSearchOpParams &params) {
94 bNode &node = params.add_node("GeometryNodeGridInfo");
95 node.custom1 = *data_type;
96 params.update_and_connect_available_socket(node, "Background Value");
97 });
98 }
99 }
100}
101
103{
104#ifdef WITH_OPENVDB
105 const eNodeSocketDatatype data_type = eNodeSocketDatatype(params.node().custom1);
106
107 const auto grid = params.extract_input<bke::GVolumeGrid>("Grid");
108 if (!grid) {
109 params.set_default_remaining_outputs();
110 return;
111 }
112
113 bke::VolumeTreeAccessToken tree_token;
114 const std::shared_ptr<const openvdb::GridBase> vdb_grid = grid->grid_ptr(tree_token);
115 params.set_output("Transform", BKE_volume_transform_to_blender(vdb_grid->transform()));
116
118 *bke::socket_type_to_geo_nodes_base_cpp_type(data_type), [&](auto type_tag) {
119 using ValueT = decltype(type_tag);
120 using type_traits = typename bke::VolumeGridTraits<ValueT>;
121 using TreeType = typename type_traits::TreeType;
122 using GridType = openvdb::Grid<TreeType>;
123
124 if constexpr (!std::is_same_v<typename type_traits::BlenderType, void>) {
125 const std::shared_ptr<const GridType> vdb_typed_grid = openvdb::GridBase::grid<GridType>(
126 vdb_grid);
127 params.set_output("Background Value",
128 type_traits::to_blender(vdb_typed_grid->background()));
129 }
130 });
131#else
133#endif
134}
135
136static void node_init(bNodeTree * /*tree*/, bNode *node)
137{
138 node->custom1 = SOCK_FLOAT;
139}
140
141static void node_rna(StructRNA *srna)
142{
144 "data_type",
145 "Data Type",
146 "Node socket data type",
151}
152
153static void node_register()
154{
155 static blender::bke::bNodeType ntype;
156
157 geo_node_type_base(&ntype, "GeometryNodeGridInfo");
158 ntype.ui_name = "Grid Info";
159 ntype.ui_description = "Retrieve information about a volume grid";
160 ntype.nclass = NODE_CLASS_INPUT;
161 ntype.initfunc = node_init;
165 ntype.declare = node_declare;
167
168 node_rna(ntype.rna_ext.srna);
169}
171
172} // namespace blender::nodes::node_geo_grid_info_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:447
#define IFACE_(msgid)
@ SOCK_IN
eNodeSocketDatatype
@ SOCK_INT
@ SOCK_VECTOR
@ SOCK_BOOLEAN
@ SOCK_MATRIX
@ 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 convert_to_static_type(const CPPType &cpp_type, const Func &func)
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
const CPPType * socket_type_to_geo_nodes_base_cpp_type(eNodeSocketDatatype type)
Definition node.cc:5202
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
static void node_init(bNodeTree *, bNode *node)
static void node_rna(StructRNA *srna)
static std::optional< eNodeSocketDatatype > node_type_for_socket_type(const bNodeSocket &socket)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
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