Blender V5.3
node_geo_set_grid_transform.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 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"
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.use_custom_socket_order();
34 b.allow_any_socket_order();
35 b.add_default_layout();
36 b.add_output<decl::Bool>("Is Valid"_ustr)
37 .description("The new transform is valid and was successfully applied to the grid.");
38 b.add_input(data_type, "Grid"_ustr)
39 .hide_value()
40 .structure_type(StructureType::Grid)
41 .is_default_link_socket();
42 b.add_output(data_type, "Grid"_ustr).structure_type(StructureType::Grid).align_with_previous();
43 b.add_input<decl::Matrix>("Transform"_ustr)
44 .description("The new transform from grid index space to object space.");
45}
46
47static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
48{
49 layout.use_property_split_set(true);
50 layout.use_property_decorate_set(false);
51 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
52}
53
54static std::optional<eNodeSocketDatatype> node_type_for_socket_type(const bNodeSocket &socket)
55{
56 switch (socket.type) {
57 case SOCK_FLOAT:
58 return SOCK_FLOAT;
59 case SOCK_BOOLEAN:
60 return SOCK_BOOLEAN;
61 case SOCK_INT:
62 return SOCK_INT;
63 case SOCK_VECTOR:
64 case SOCK_RGBA:
65 return SOCK_VECTOR;
66 default:
67 return std::nullopt;
68 }
69}
70
72{
73 const bNodeSocket &other_socket = params.other_socket();
74 const StructureType structure_type = other_socket.runtime->inferred_structure_type;
75 const bool is_grid = structure_type == StructureType::Grid;
76 const bool is_dynamic = structure_type == StructureType::Dynamic;
77 const eNodeSocketDatatype other_type = other_socket.type;
78
79 if (params.in_out() == SOCK_IN) {
80 if (is_grid || is_dynamic) {
81 const std::optional<eNodeSocketDatatype> data_type = node_type_for_socket_type(other_socket);
82 if (data_type) {
83 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
84 bNode &node = params.add_node("GeometryNodeSetGridTransform"_ustr);
85 node.custom1 = *data_type;
86 params.update_and_connect_available_socket(node, "Grid"_ustr);
87 });
88 }
89 }
90 if (!is_grid && params.node_tree().typeinfo->validate_link(other_type, SOCK_MATRIX)) {
91 params.add_item(IFACE_("Transform"), [](LinkSearchOpParams &params) {
92 bNode &node = params.add_node("GeometryNodeSetGridTransform"_ustr);
93 params.update_and_connect_available_socket(node, "Transform"_ustr);
94 });
95 }
96 }
97 else {
98 const std::optional<eNodeSocketDatatype> data_type = node_type_for_socket_type(other_socket);
99 if (data_type) {
100 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
101 bNode &node = params.add_node("GeometryNodeSetGridTransform"_ustr);
102 node.custom1 = *data_type;
103 params.update_and_connect_available_socket(node, "Grid"_ustr);
104 });
105 }
106 if (params.node_tree().typeinfo->validate_link(SOCK_BOOLEAN, other_type)) {
107 params.add_item(IFACE_("Is Valid"), [](LinkSearchOpParams &params) {
108 bNode &node = params.add_node("GeometryNodeSetGridTransform"_ustr);
109 params.update_and_connect_available_socket(node, "Is Valid"_ustr);
110 });
111 }
112 }
113}
114
116{
117#ifdef WITH_OPENVDB
118 bke::GVolumeGrid grid = params.extract_input<bke::GVolumeGrid>("Grid"_ustr);
119 if (!grid) {
121 return;
122 }
123
124 const float4x4 transform = params.extract_input<float4x4>("Transform"_ustr);
125
126 try {
127 bke::VolumeGridData &grid_data = grid.get_for_write();
129 params.set_output("Is Valid"_ustr, true);
130 }
131 catch (const openvdb::ArithmeticError & /*error*/) {
133 TIP_("Failed to set the new grid transform."));
134 params.set_output("Is Valid"_ustr, false);
135 }
136
137 params.set_output("Grid"_ustr, std::move(grid));
138#else
140#endif
141}
142
143static void node_init(bNodeTree * /*tree*/, bNode *node)
144{
145 node->custom1 = SOCK_FLOAT;
146}
147
148static void node_rna(StructRNA *srna)
149{
151 "data_type",
152 "Data Type",
153 "Node socket data type",
158}
159
160static void node_register()
161{
162 static bke::bNodeType ntype;
163
164 geo_node_type_base(&ntype, "GeometryNodeSetGridTransform"_ustr);
165 ntype.ui_name = "Set Grid Transform";
166 ntype.ui_description = "Set the transform for the grid from index space into object space.";
168 ntype.initfunc = node_init;
172 ntype.declare = node_declare;
174
175 node_rna(ntype.rna_ext.srna);
176}
178
179} // namespace blender::nodes::node_geo_set_grid_transform
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define TIP_(msgid)
#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)
void error_message_add(const NodeWarningType type, StringRef message) const
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 set_transform_matrix(VolumeGridData &grid, const float4x4 &matrix)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static std::optional< eNodeSocketDatatype > node_type_for_socket_type(const bNodeSocket &socket)
static void node_geo_exec(GeoNodeExecParams params)
static void node_gather_link_search_ops(GatherLinkSearchOpParams &params)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
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)
MatBase< float, 4, 4 > float4x4
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)