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