Blender V5.0
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_volume_grid.hh"
11#include "BKE_volume_openvdb.hh"
12
13#include "NOD_rna_define.hh"
15
17#include "UI_resources.hh"
18
19#include "RNA_enum_types.hh"
20
22
24{
25 const bNode *node = b.node_or_null();
26 if (!node) {
27 return;
28 }
29
30 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
31
32 b.use_custom_socket_order();
33 b.allow_any_socket_order();
34 b.add_default_layout();
35 b.add_output<decl::Bool>("Is Valid")
36 .description("The new transform is valid and was successfully applied to the grid.");
37 b.add_input(data_type, "Grid")
38 .hide_value()
39 .structure_type(StructureType::Grid)
40 .is_default_link_socket();
41 b.add_output(data_type, "Grid").structure_type(StructureType::Grid).align_with_previous();
42 b.add_input<decl::Matrix>("Transform")
43 .description("The new transform from grid index space to object space.");
44}
45
46static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
47{
48 layout->use_property_split_set(true);
49 layout->use_property_decorate_set(false);
50 layout->prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
51}
52
53static std::optional<eNodeSocketDatatype> node_type_for_socket_type(const bNodeSocket &socket)
54{
55 switch (socket.type) {
56 case SOCK_FLOAT:
57 return SOCK_FLOAT;
58 case SOCK_BOOLEAN:
59 return SOCK_BOOLEAN;
60 case SOCK_INT:
61 return SOCK_INT;
62 case SOCK_VECTOR:
63 case SOCK_RGBA:
64 return SOCK_VECTOR;
65 default:
66 return std::nullopt;
67 }
68}
69
71{
72 const bNodeSocket &other_socket = params.other_socket();
73 const StructureType structure_type = other_socket.runtime->inferred_structure_type;
74 const bool is_grid = structure_type == StructureType::Grid;
75 const bool is_dynamic = structure_type == StructureType::Dynamic;
76 const eNodeSocketDatatype other_type = eNodeSocketDatatype(other_socket.type);
77
78 if (params.in_out() == SOCK_IN) {
79 if (is_grid || is_dynamic) {
80 const std::optional<eNodeSocketDatatype> data_type = node_type_for_socket_type(other_socket);
81 if (data_type) {
82 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
83 bNode &node = params.add_node("GeometryNodeSetGridTransform");
84 node.custom1 = *data_type;
85 params.update_and_connect_available_socket(node, "Grid");
86 });
87 }
88 }
89 if (!is_grid && params.node_tree().typeinfo->validate_link(other_type, SOCK_MATRIX)) {
90 params.add_item(IFACE_("Transform"), [](LinkSearchOpParams &params) {
91 bNode &node = params.add_node("GeometryNodeSetGridTransform");
92 params.update_and_connect_available_socket(node, "Transform");
93 });
94 }
95 }
96 else {
97 const std::optional<eNodeSocketDatatype> data_type = node_type_for_socket_type(other_socket);
98 if (data_type) {
99 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
100 bNode &node = params.add_node("GeometryNodeSetGridTransform");
101 node.custom1 = *data_type;
102 params.update_and_connect_available_socket(node, "Grid");
103 });
104 }
105 if (params.node_tree().typeinfo->validate_link(SOCK_BOOLEAN, other_type)) {
106 params.add_item(IFACE_("Is Valid"), [](LinkSearchOpParams &params) {
107 bNode &node = params.add_node("GeometryNodeSetGridTransform");
108 params.update_and_connect_available_socket(node, "Is Valid");
109 });
110 }
111 }
112}
113
115{
116#ifdef WITH_OPENVDB
117 bke::GVolumeGrid grid = params.extract_input<bke::GVolumeGrid>("Grid");
118 if (!grid) {
119 params.set_default_remaining_outputs();
120 return;
121 }
122
123 const float4x4 transform = params.extract_input<float4x4>("Transform");
124
125 try {
126 bke::VolumeGridData &grid_data = grid.get_for_write();
128 params.set_output("Is Valid", true);
129 }
130 catch (const openvdb::ArithmeticError & /*error*/) {
131 params.error_message_add(NodeWarningType::Error,
132 TIP_("Failed to set the new grid transform."));
133 params.set_output("Is Valid", false);
134 }
135
136 params.set_output("Grid", std::move(grid));
137#else
139#endif
140}
141
142static void node_init(bNodeTree * /*tree*/, bNode *node)
143{
144 node->custom1 = SOCK_FLOAT;
145}
146
147static void node_rna(StructRNA *srna)
148{
150 "data_type",
151 "Data Type",
152 "Node socket data type",
157}
158
159static void node_register()
160{
161 static blender::bke::bNodeType ntype;
162
163 geo_node_type_base(&ntype, "GeometryNodeSetGridTransform");
164 ntype.ui_name = "Set Grid Transform";
165 ntype.ui_description = "Set the transform for the grid from index space into object space.";
167 ntype.initfunc = node_init;
171 ntype.declare = node_declare;
173
174 node_rna(ntype.rna_ext.srna);
175}
177
178} // namespace blender::nodes::node_geo_set_grid_transform
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define TIP_(msgid)
#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
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void set_transform_matrix(VolumeGridData &grid, const float4x4 &matrix)
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
static std::optional< eNodeSocketDatatype > node_type_for_socket_type(const bNodeSocket &socket)
static void node_geo_exec(GeoNodeExecParams params)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
static void node_gather_link_search_ops(GatherLinkSearchOpParams &params)
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
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