Blender V5.3
geometry_nodes_srna.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
8
9#include <fmt/format.h>
10
11#include "BLI_listbase.hh"
12#include "BLI_string.hh"
13
14#include "DEG_depsgraph.hh"
15
17#include "NOD_socket.hh"
18
19#include "DNA_modifier_types.h"
20#include "DNA_node_types.h"
21
22#include "BKE_idprop.hh"
23#include "BKE_node_runtime.hh"
24
25#include "RNA_access.hh"
26#include "RNA_define.hh"
27#include "RNA_prototypes.hh"
28
29#include "PRF_profile.hh"
30
31namespace blender::nodes {
32
34 int(GeometryNodesInputType::Fallback), "FALLBACK", 0, "Fallback", "Fallback"};
36 int(GeometryNodesInputType::Value), "VALUE", 0, "Value", "Pass a single value"};
38 int(GeometryNodesInputType::Attribute), "ATTRIBUTE", 0, "Attribute", "Pass an attribute"};
40 int(GeometryNodesInputType::Layer), "LAYER", 0, "Layer", "Pass a layer selection"};
41
46
51
57
64
66{
67 for (const AncestorPointerRNA &ancestor : ptr->ancestors) {
68 if (RNA_struct_is_a(ancestor.type, RNA_Modifier)) {
69 return static_cast<const ModifierData *>(ancestor.data);
70 }
71 }
72 const Object *object = id_cast<const Object *>(ptr->owner_id);
73 for (const ModifierData &md : object->modifiers) {
74 bool found = false;
76 if (id_prop == ptr->data) {
77 found = true;
78 }
79 });
80 if (found) {
81 return &md;
82 }
83 }
84 return nullptr;
85}
86
87static std::optional<std::string> rna_NodesModifierPropertyInput_path(const PointerRNA *ptr)
88{
89 StructRNA *srna = ptr->type;
90 const char *identifier = RNA_struct_identifier(srna);
92 std::string name_esc = BLI_str_escape(md->name);
93 return fmt::format("modifiers[\"{}\"].properties.inputs.{}", name_esc, identifier);
94}
95
97 const bNodeTreeInterfaceSocket &socket,
98 GeneratedTreeSrnaData &r_generated)
99{
100 const bke::bNodeSocketType *stype = socket.socket_typeinfo();
101 if (!stype) {
102 return nullptr;
103 }
104 // TODO: Does this actually need to copy the string?
105 const StringRefNull srna_identifier = r_generated.scope.allocator().copy_string(
106 socket.identifier);
107
109 r_generated.generated_rna, srna_identifier.c_str(), RNA_PropertyGroup);
112 stype->make_geometry_nodes_input_srna(tree, *srna, socket, r_generated);
113 }
114
115 return srna;
116}
117
119{
121 r_generated.generated_rna, "GeometryNodesInterfaceInputs", RNA_PropertyGroup);
122
123 for (const bNodeTreeInterfaceSocket *socket : tree.interface_inputs()) {
124 StructRNA *socket_srna = get_input_socket_struct_rna(tree, *socket, r_generated);
125 if (!socket_srna) {
126 continue;
127 }
128 const StringRefNull identifier = r_generated.scope.allocator().copy_string(socket->identifier);
130 srna, identifier.c_str(), socket_srna, socket->name().c_str(), "");
132 }
133
134 return srna;
135}
136
137static std::optional<std::string> rna_NodesModifierPropertyOutput_path(const PointerRNA *ptr)
138{
139 StructRNA *srna = ptr->type;
140 const char *identifier = RNA_struct_identifier(srna);
142 std::string name_esc = BLI_str_escape(md->name);
143 return fmt::format("modifiers[\"{}\"].properties.outputs.{}", name_esc, identifier);
144}
145
146void geometry_nodes_property_update(Main * /*bmain*/, Scene * /*scene*/, PointerRNA *ptr)
147{
149}
150
152{
154 r_generated.generated_rna, "GeometryNodesInterfaceOutputs", RNA_PropertyGroup);
155
156 LinearAllocator<> &allocator = r_generated.scope.allocator();
157
158 for (const bNodeTreeInterfaceSocket *output : tree.interface_outputs()) {
159 const bke::bNodeSocketType *socket_type = bke::node_socket_type_find(output->socket_type);
160 if (!nodes::socket_type_supports_attributes(socket_type->type)) {
161 continue;
162 }
163
164 const StringRefNull identifier = allocator.copy_string(output->identifier);
165 const StringRefNull name = allocator.copy_string(output->name());
166 const StringRefNull description = allocator.copy_string(output->description());
167 const StringRefNull default_value = allocator.copy_string(output->default_attribute_name);
168
169 StructRNA *output_srna = RNA_def_struct_ptr(
170 r_generated.generated_rna, identifier.c_str(), RNA_PropertyGroup);
172 PropertyRNA *prop = RNA_def_string(output_srna,
173 "attribute_name",
174 default_value.is_empty() ? nullptr : default_value.c_str(),
175 0,
176 name.c_str(),
177 description.c_str());
181 prop = RNA_def_pointer_runtime(srna, identifier.c_str(), output_srna, name.c_str(), "");
183 }
184
185 return srna;
186}
187
189{
191 r_generated.generated_rna, "GeometryNodesInterfacePanels", RNA_PropertyGroup);
192
193 LinearAllocator<> &allocator = r_generated.scope.allocator();
194
195 tree.ensure_interface_cache();
196 for (const bNodeTreeInterfaceItem *item : tree.interface_items()) {
197 if (item->item_type != NodeTreeInterfaceItemType::Panel) {
198 continue;
199 }
200 const auto &panel = *reinterpret_cast<const bNodeTreeInterfacePanel *>(item);
201 const StringRefNull identifier = allocator.copy_string(
202 fmt::format("open_{}", panel.identifier));
203 PropertyRNA *prop = RNA_def_boolean(srna,
204 identifier.c_str(),
206 "Is Open",
207 "");
209 }
210
211 return srna;
212}
213
214std::shared_ptr<GeneratedTreeSrnaData> create_geometry_nodes_rna_for_modifier(
215 const bNodeTree &tree)
216{
218 auto generated = std::make_unique<GeneratedTreeSrnaData>();
219 tree.ensure_interface_cache();
220
222 generated->generated_rna, "GeometryNodesModifierInterface", RNA_NodesModifierProperties);
223 generated->properties_struct = srna;
224
225 StructRNA *inputs_srna = create_inputs_srna(tree, *generated);
226 StructRNA *outputs_srna = create_outputs_srna(tree, *generated);
227 StructRNA *panels_srna = create_panels_srna(tree, *generated);
228
229 PropertyRNA *prop;
231 srna, "inputs", inputs_srna, "Inputs", "Settings for input sockets");
234 srna, "outputs", outputs_srna, "Outputs", "Settings for output sockets");
236 prop = RNA_def_pointer_runtime(srna, "panels", panels_srna, "Panels", "Settings for panels");
238
239 return generated;
240}
241
242} // namespace blender::nodes
#define PRF_scope(category)
#define output
StringRefNull copy_string(StringRef str)
LinearAllocator & allocator()
constexpr const char * c_str() const
constexpr bool is_empty() const
bNodeSocketType * node_socket_type_find(StringRef idname)
Definition node.cc:2966
static std::optional< std::string > rna_NodesModifierPropertyInput_path(const PointerRNA *ptr)
static StructRNA * create_inputs_srna(const bNodeTree &tree, const CompositorNodesSrnaContext &context, GeneratedTreeSrnaData &r_generated)
const EnumPropertyItem geometry_nodes_input_type_items_value_or_attribute_or_layer[]
void geometry_nodes_property_update(Main *, Scene *, PointerRNA *ptr)
static std::optional< std::string > rna_NodesModifierPropertyOutput_path(const PointerRNA *ptr)
static StructRNA * create_panels_srna(const bNodeTree &tree, GeneratedTreeSrnaData &r_generated)
static StructRNA * get_input_socket_struct_rna(const bNodeTree &tree, const bNodeTreeInterfaceSocket &socket, const CompositorNodesSrnaContext &context, GeneratedTreeSrnaData &r_generated)
bool socket_type_supports_attributes(const eNodeSocketDatatype socket_type)
const EnumPropertyItem geometry_nodes_input_type_items_value[]
static constexpr EnumPropertyItem input_type_item_fallback
static constexpr EnumPropertyItem input_type_item_layer
static constexpr EnumPropertyItem input_type_item_value
static constexpr EnumPropertyItem input_type_item_attribute
static const ModifierData * find_modifier_data_from_system_property(const PointerRNA *ptr)
static StructRNA * create_outputs_srna(const bNodeTree &tree, GeneratedTreeSrnaData &r_generated)
std::shared_ptr< GeneratedTreeSrnaData > create_geometry_nodes_rna_for_modifier(const bNodeTree &tree)
const EnumPropertyItem geometry_nodes_input_type_items_fallback[]
const EnumPropertyItem geometry_nodes_input_type_items_value_or_attribute[]
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
void IDP_foreach_property(IDProperty *id_property_root, int type_filter, FunctionRef< void(IDProperty *id_property)> callback)
PropertyRNA * RNA_def_pointer_runtime(StructOrFunctionRNA *cont, const char *identifier, StructRNA *type, const char *ui_name, const char *ui_description)
StructRNA RNA_PropertyGroup
@ PROPOVERRIDE_OVERRIDABLE_LIBRARY
Definition RNA_types.hh:588
void DEG_id_tag_update(ID *id, unsigned int flags)
void RNA_def_property_override_flag(PropertyRNA *prop, PropertyOverrideFlag flag)
char char size_t BLI_str_escape(char *__restrict dst, StringRef src, size_t dst_maxncpy) ATTR_NONNULL(1)
const char * RNA_struct_identifier(const StructRNA *type)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
void RNA_def_struct_path_func_runtime(StructRNA *srna, StructPathFunc path_fn)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_FORCE_GEOMETRY_EVAL
Definition RNA_types.hh:546
@ PROP_NO_DEG_UPDATE
Definition RNA_types.hh:509
void RNA_def_property_update_runtime(PropertyRNA *prop, RNAPropertyUpdateFunc func)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
StructRNA * RNA_def_struct_ptr(BlenderRNA *brna, const char *identifier, StructRNA *srnafrom)
struct IDProperty * system_properties
StructRNA * type
Definition RNA_types.hh:63
Vector< AncestorPointerRNA, ANCESTOR_POINTERRNA_DEFAULT_SIZE > ancestors
Definition RNA_types.hh:90
Defines a socket type.
Definition BKE_node.hh:163
SocketMakeNodesInputSrnaFunction make_geometry_nodes_input_srna
Definition BKE_node.hh:218
eNodeSocketDatatype type
Definition BKE_node.hh:198