Blender V5.3
node_geo_field_to_grid.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
7
12
14#include "UI_resources.hh"
15
16#include "RNA_enum_types.hh"
17#include "RNA_prototypes.hh"
18
19#include "BLO_read_write.hh"
20
21#ifdef WITH_OPENVDB
23#endif
24
25namespace blender {
26
28
31
32namespace grid = bke::volume_grid;
33
35{
36 b.use_custom_socket_order();
37 b.allow_any_socket_order();
38 b.add_default_layout();
39
40 const bNodeTree *tree = b.tree_or_null();
41 const bNode *node = b.node_or_null();
42 if (!node || !tree) {
43 return;
44 }
45 const GeometryNodeFieldToGrid &storage = node_storage(*node);
46 const eNodeSocketDatatype data_type = storage.data_type;
47
48 b.add_input(data_type, "Topology"_ustr).structure_type(StructureType::Grid);
49
50 const Span<GeometryNodeFieldToGridItem> items(storage.items, storage.items_num);
51 for (const int i : items.index_range()) {
52 const GeometryNodeFieldToGridItem &item = items[i];
53 const eNodeSocketDatatype data_type = item.data_type;
54 const UString name(item.name);
55 const UString input_identifier(ItemsAccessor::input_socket_identifier_for_item(item));
56 const UString output_identifier(ItemsAccessor::output_socket_identifier_for_item(item));
57
58 b.add_input(data_type, name, input_identifier)
59 .structure_type(StructureType::Field)
60 .socket_name_ptr(&tree->id, *FieldToGridItemsAccessor::item_srna, &item, "name");
61 b.add_output(data_type, name, output_identifier)
62 .structure_type(StructureType::Grid)
63 .align_with_previous()
64 .description("Output grid with evaluated field values");
65 }
66
67 b.add_input<decl::Extend>(""_ustr, "__extend__"_ustr)
68 .structure_type(StructureType::Field)
69 .custom_draw(socket_items::ui::draw_extend_socket_fn<FieldToGridItemsAccessor>());
70 b.add_output<decl::Extend>(""_ustr, "__extend__"_ustr)
71 .structure_type(StructureType::Grid)
72 .align_with_previous();
73}
74
75static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
76{
77 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
78}
79
80static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *ptr)
81{
82 bNodeTree &tree = *reinterpret_cast<bNodeTree *>(ptr->owner_id);
83 bNode &node = *static_cast<bNode *>(ptr->data);
84 if (ui::Layout *panel = layout.panel(C, "field_to_grid_items", false, IFACE_("Fields"))) {
85 socket_items::ui::draw_items_list_with_operators<ItemsAccessor>(C, panel, tree, node);
86 socket_items::ui::draw_active_item_props<ItemsAccessor>(tree, node, [&](PointerRNA *item_ptr) {
87 panel->use_property_split_set(true);
88 panel->use_property_decorate_set(false);
89 panel->prop(item_ptr, "data_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
90 });
91 }
92}
93
94static std::optional<eNodeSocketDatatype> node_type_for_socket_type(const bNodeSocket &socket)
95{
96 switch (socket.type) {
97 case SOCK_FLOAT:
98 return SOCK_FLOAT;
99 case SOCK_BOOLEAN:
100 return SOCK_BOOLEAN;
101 case SOCK_INT:
102 return SOCK_INT;
103 case SOCK_VECTOR:
104 case SOCK_RGBA:
105 return SOCK_VECTOR;
106 default:
107 return std::nullopt;
108 }
109}
110
112{
113 const std::optional<eNodeSocketDatatype> data_type = node_type_for_socket_type(
114 params.other_socket());
115 if (!data_type) {
116 return;
117 }
118 if (params.in_out() == SOCK_IN) {
119 params.add_item(IFACE_("Topology"), [data_type](LinkSearchOpParams &params) {
120 bNode &node = params.add_node("GeometryNodeFieldToGrid"_ustr);
121 node_storage(node).data_type = *data_type;
122 params.update_and_connect_available_socket(node, "Topology"_ustr);
123 });
124 params.add_item(IFACE_("Field"), [data_type](LinkSearchOpParams &params) {
125 bNode &node = params.add_node("GeometryNodeFieldToGrid"_ustr);
127 params.node_tree, node, *data_type, params.socket.name);
130 });
131 }
132 else {
133 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
134 bNode &node = params.add_node("GeometryNodeFieldToGrid"_ustr);
136 params.node_tree, node, *data_type, params.socket.name);
138 });
139 }
140}
141
143{
144#ifdef WITH_OPENVDB
145 const GeometryNodeFieldToGrid &storage = node_storage(params.node());
146 const Span<GeometryNodeFieldToGridItem> items(storage.items, storage.items_num);
147 bke::GVolumeGrid topology_grid = params.extract_input<bke::GVolumeGrid>("Topology"_ustr);
148 if (!topology_grid) {
149 params.error_message_add(NodeWarningType::Error, "The topology grid input is required");
151 return;
152 }
153
154 bke::VolumeTreeAccessToken tree_token;
155 const openvdb::GridBase &topology_base = topology_grid->grid(tree_token);
156 const openvdb::math::Transform &transform = topology_base.transform();
157
158 Vector<int> required_items;
159 for (const int i : items.index_range()) {
160 if (params.output_is_required(
162 {
163 required_items.append(i);
164 }
165 }
166
167 Vector<fn::GField> fields;
168 fields.reserve(required_items.size());
169 for (const int i : required_items.index_range()) {
170 const int item_i = required_items[i];
171 const std::string identifier = ItemsAccessor::input_socket_identifier_for_item(items[item_i]);
172 fields.append(params.extract_input<fn::GField>(UString(identifier)));
173 }
174
175 openvdb::MaskTree mask_tree;
176 grid::to_typed_grid(topology_base,
177 [&](const auto &grid) { mask_tree.topologyUnion(grid.tree()); });
178
179 Vector<bke::GVolumeGrid> output_grids(fields.size());
180 evaluate_fields_to_grid(mask_tree, transform, fields, output_grids);
181
182 for (const int i : required_items.index_range()) {
183 const int item_i = required_items[i];
184 const std::string identifier = ItemsAccessor::output_socket_identifier_for_item(items[item_i]);
185 params.set_output(UString(identifier), std::move(output_grids[i]));
186 }
187
188#else
190#endif
191}
192
193static void node_init(bNodeTree * /*tree*/, bNode *node)
194{
195 GeometryNodeFieldToGrid *data = MEM_new<GeometryNodeFieldToGrid>(__func__);
196 data->data_type = SOCK_FLOAT;
197 node->storage = data;
198}
199
201{
203 MEM_delete(static_cast<GeometryNodeFieldToGrid *>(node->storage));
204}
205
206static void node_copy_storage(bNodeTree * /*dst_tree*/, bNode *dst_node, const bNode *src_node)
207{
208 const GeometryNodeFieldToGrid &src_storage = node_storage(*src_node);
209 auto *dst_storage = MEM_new<GeometryNodeFieldToGrid>(__func__, dna::shallow_copy(src_storage));
210 dst_node->storage = dst_storage;
211
212 socket_items::copy_array<ItemsAccessor>(*src_node, *dst_node);
213}
214
219
221{
223 params.ntree, params.node, params.node, params.link);
224}
225
226static void node_blend_write(const bNodeTree & /*tree*/, const bNode &node, BlendWriter &writer)
227{
229}
230
231static void node_blend_read(bNodeTree & /*tree*/, bNode &node, BlendDataReader &reader)
232{
234}
235
237 const bNode &node,
238 const bNodeSocket &output_socket)
239{
240 return node.input_by_identifier(output_socket.identifier_ustr());
241}
242
243static void node_register()
244{
245 static bke::bNodeType ntype;
246
247 geo_node_type_base(&ntype, "GeometryNodeFieldToGrid"_ustr);
248 ntype.ui_name = "Field to Grid";
249 ntype.ui_description =
250 "Create new grids by evaluating new values on an existing volume grid topology";
252 ntype.declare = node_declare;
253 ntype.initfunc = node_init;
254 bke::node_type_storage(ntype, "GeometryNodeFieldToGrid", node_free_storage, node_copy_storage);
266}
268
269} // namespace nodes::node_geo_field_to_grid_cc
270
271namespace nodes {
272
273StructRNA **FieldToGridItemsAccessor::item_srna = &RNA_GeometryNodeFieldToGridItem;
274
276{
277 writer->write_string(item.name);
278}
279
284
285} // namespace nodes
286} // namespace blender
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
UString()=default
int64_t size() const
void append(const T &value)
IndexRange index_range() const
void reserve(const int64_t min_capacity)
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)
bool output_is_required(const UString identifier) const
void set_output(const UString identifier, T &&value)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
void update_and_connect_available_socket_by_identifier(bNode &new_node, UString socket_identifier)
#define storage(slot, qualifiers)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
static std::optional< eNodeSocketDatatype > node_type_for_socket_type(const bNodeSocket &socket)
static void node_declare(NodeDeclarationBuilder &b)
static void node_gather_link_search_ops(GatherLinkSearchOpParams &params)
static bool node_insert_link(bke::NodeInsertLinkParams &params)
static void node_copy_storage(bNodeTree *, bNode *dst_node, const bNode *src_node)
static void node_blend_read(bNodeTree &, bNode &node, BlendDataReader &reader)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_geo_exec(GeoNodeExecParams params)
static void node_blend_write(const bNodeTree &, const bNode &node, BlendWriter &writer)
static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *ptr)
static void node_init(bNodeTree *, bNode *node)
static const bNodeSocket * node_internally_linked_input(const bNodeTree &, const bNode &node, const bNodeSocket &output_socket)
void blend_write(BlendWriter *writer, const bNode &node)
void blend_read_data(BlendDataReader *reader, bNode &node)
void copy_array(const bNode &src_node, bNode &dst_node)
Accessor::ItemT * add_item_with_socket_type_and_name(bNodeTree &ntree, bNode &node, const eNodeSocketDatatype socket_type, const char *name, std::optional< int > dimensions=std::nullopt)
bool try_add_item_via_any_extend_socket(bNodeTree &ntree, bNode &extend_node, bNode &storage_node, bNodeLink &link, const std::optional< StringRef > socket_identifier=std::nullopt, typename Accessor::ItemT **r_new_item=nullptr)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams &params)
BMesh Iterators.
BMesh const char void * data
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
void write_string(const char *data)
eNodeSocketDatatype type
Defines a node type.
Definition BKE_node.hh:265
NodeInternallyLinkedInputFunction internally_linked_input
Definition BKE_node.hh:415
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
NodeBlendWriteFunction blend_write_storage_content
Definition BKE_node.hh:421
void(*) register_operators()
Definition BKE_node.hh:448
std::string ui_description
Definition BKE_node.hh:271
NodeBlendDataReadFunction blend_data_read_storage_content
Definition BKE_node.hh:422
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) draw_buttons_ex(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:292
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
bool ignore_inferred_input_socket_visibility
Definition BKE_node.hh:453
bool(*) insert_link(NodeInsertLinkParams &params)
Definition BKE_node.hh:364
static std::string input_socket_identifier_for_item(const GeometryNodeFieldToGridItem &item)
static void blend_write_item(BlendWriter *writer, const ItemT &item)
static void blend_read_data_item(BlendDataReader *reader, ItemT &item)
static std::string output_socket_identifier_for_item(const GeometryNodeFieldToGridItem &item)
PanelLayout panel(const bContext *C, StringRef idname, bool default_closed)
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)