Blender V5.3
node_geo_collection_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
5#include "BLI_listbase.hh"
6#include "BLI_string.hh"
7
9
10#include "NOD_rna_define.hh"
11
13#include "UI_resources.hh"
14
15#include "BKE_collection.hh"
16#include "BKE_instances.hh"
17
19
20#include "node_geometry_util.hh"
21
22#include <algorithm>
23
25
27
29{
30 b.add_input<decl::Collection>("Collection"_ustr).optional_label();
31 b.add_input<decl::Bool>("Separate Children"_ustr)
33 "Output each child of the collection as a separate instance, sorted alphabetically");
34 b.add_input<decl::Bool>("Reset Children"_ustr)
36 "Reset the transforms of every child instance in the output. Only used when Separate "
37 "Children is enabled");
38 b.add_output<decl::Geometry>("Instances"_ustr)
40 "Instance of the collection or instances of all the children in the collection");
41}
42
43static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
44{
45 layout.prop(ptr, "transform_space", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
46}
47
48static void node_node_init(bNodeTree * /*tree*/, bNode *node)
49{
50 NodeGeometryCollectionInfo *data = MEM_new<NodeGeometryCollectionInfo>(__func__);
51 data->transform_space = GEO_NODE_TRANSFORM_SPACE_ORIGINAL;
52 node->storage = data;
53}
54
60
62{
63 Collection *collection = params.extract_input<Collection *>("Collection"_ustr);
64
65 if (collection == nullptr) {
67 return;
68 }
69 const Object *self_object = params.self_object();
70 /* Compare by `orig_id` because objects may be copied into separate depsgraphs. */
72 collection, const_cast<Object *>(self_object));
73 if (is_recursive) {
74 params.error_message_add(NodeWarningType::Error, TIP_("Collection contains current object"));
76 return;
77 }
78 if (!DEG_collection_geometry_is_evaluated(*collection)) {
80 TIP_("Cannot access collections geometry because it's not evaluated "
81 "yet. This can happen when there is a dependency cycle"));
83 return;
84 }
85
86 const NodeGeometryCollectionInfo &storage = node_storage(params.node());
87 const bool use_relative_transform = (storage.transform_space ==
89
90 std::unique_ptr<bke::Instances> instances = std::make_unique<bke::Instances>();
91
92 const bool separate_children = params.extract_input<bool>("Separate Children"_ustr);
93 if (separate_children) {
94 const bool reset_children = params.extract_input<bool>("Reset Children"_ustr);
95 Vector<Collection *> children_collections;
96 for (CollectionChild &collection_child : collection->children) {
97 children_collections.append(collection_child.collection);
98 }
99 Vector<Object *> children_objects;
100 for (CollectionObject &collection_object : collection->gobject) {
101 children_objects.append(collection_object.ob);
102 }
103
105 entries.reserve(children_collections.size() + children_objects.size());
106
107 for (Collection *child_collection : children_collections) {
109 if (!reset_children) {
110 transform.location() += float3(child_collection->instance_offset);
111 if (use_relative_transform) {
112 transform = self_object->world_to_object() * transform;
113 }
114 else {
115 transform.location() -= float3(collection->instance_offset);
116 }
117 }
118 const int handle = instances->add_reference(*child_collection);
119 entries.append({handle, &(child_collection->id.name[2]), transform});
120 }
121 for (Object *child_object : children_objects) {
122 const int handle = instances->add_reference(*child_object);
124 if (!reset_children) {
125 if (use_relative_transform) {
126 transform = self_object->world_to_object();
127 }
128 else {
129 transform.location() -= float3(collection->instance_offset);
130 }
131 transform *= child_object->object_to_world();
132 }
133 entries.append({handle, &(child_object->id.name[2]), transform});
134 }
135
136 std::ranges::sort(entries, [](const InstanceListEntry &a, const InstanceListEntry &b) {
137 return BLI_strcasecmp_natural(a.name, b.name) < 0;
138 });
139
140 instances->resize(entries.size());
141 MutableSpan<int> handles = instances->reference_handles_for_write();
142 MutableSpan<float4x4> transforms = instances->transforms_for_write();
143 for (const int i : entries.index_range()) {
144 handles[i] = entries[i].handle;
145 transforms[i] = entries[i].transform;
146 }
147 }
148 else {
150 if (use_relative_transform) {
151 transform.location() = collection->instance_offset;
152 transform = self_object->world_to_object() * transform;
153 }
154
155 instances->resize(1);
156 instances->reference_handles_for_write().first() = instances->add_reference(*collection);
157 instances->transforms_for_write().first() = transform;
158 }
159 GeometrySet geometry = GeometrySet::from_instances(std::move(instances));
160 geometry.set_name(collection->id.name + 2);
161
162 params.set_output("Instances"_ustr, std::move(geometry));
163}
164
165static void node_rna(StructRNA *srna)
166{
167 static const EnumPropertyItem rna_node_geometry_collection_info_transform_space_items[] = {
169 "ORIGINAL",
170 0,
171 "Original",
172 "Output the geometry relative to the collection offset"},
174 "RELATIVE",
175 0,
176 "Relative",
177 "Bring the input collection geometry into the modified object, maintaining the relative "
178 "position between the objects in the scene"},
179 {0, nullptr, 0, nullptr, nullptr},
180 };
181
183 srna,
184 "transform_space",
185 "Transform Space",
186 "The transformation of the instances output. Does not affect the internal geometry",
187 rna_node_geometry_collection_info_transform_space_items,
188 NOD_storage_enum_accessors(transform_space),
191}
192
193static void node_register()
194{
195 static bke::bNodeType ntype;
196
197 geo_node_type_base(&ntype, "GeometryNodeCollectionInfo"_ustr, GEO_NODE_COLLECTION_INFO);
198 ntype.ui_name = "Collection Info";
199 ntype.ui_description = "Retrieve geometry instances from a collection";
200 ntype.enum_name_legacy = "COLLECTION_INFO";
201 ntype.nclass = NODE_CLASS_INPUT;
202 ntype.declare = node_declare;
203 ntype.initfunc = node_node_init;
205 ntype, "NodeGeometryCollectionInfo", node_free_standard_storage, node_copy_standard_storage);
209
210 node_rna(ntype.rna_ext.srna);
211}
213
214} // namespace blender::nodes::node_geo_collection_info_cc
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_COLLECTION_INFO
#define TIP_(msgid)
Object groups, one object can be in many groups at once.
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
int64_t size() const
void append(const T &value)
IndexRange index_range() const
void reserve(const int64_t min_capacity)
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
#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
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
static void node_node_init(bNodeTree *, bNode *node)
static void node_geo_exec(GeoNodeExecParams 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)
int char char int int int BLI_strcasecmp_natural(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
bool BKE_collection_has_object_recursive_instanced_orig_id(Collection *collection_eval, Object *object_eval)
MatBase< float, 4, 4 > float4x4
BMesh const char void * data
void rna_Node_update_relations(Main *bmain, Scene *scne, PointerRNA *ptr)
@ GEO_NODE_TRANSFORM_SPACE_ORIGINAL
@ GEO_NODE_TRANSFORM_SPACE_RELATIVE
bool DEG_collection_geometry_is_evaluated(const Collection &collection)
void RNA_def_property_update_runtime(PropertyRNA *prop, RNAPropertyUpdateFunc func)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
VecBase< float, 3 > float3
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
ListBaseT< CollectionChild > children
ListBaseT< CollectionObject > gobject
char name[258]
Definition DNA_ID.h:433
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
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
static GeometrySet from_instances(std::unique_ptr< Instances > instances)
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)