Blender V5.3
node_geo_collection_children.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 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 "BKE_collection.hh"
11#include "BKE_lib_id.hh"
12
14
16
17#include "node_geometry_util.hh"
18
19#include <algorithm>
20
22
24{
25 b.add_input<decl::Collection>("Collection"_ustr).optional_label();
26 b.add_input<decl::Bool>("Recursive"_ustr)
27 .description("Recursively retrieve collections and objects");
28 b.add_output<decl::Collection>("Collections"_ustr).structure_type(StructureType::List);
29 b.add_output<decl::Object>("Objects"_ustr).structure_type(StructureType::List);
30}
31
33 Vector<Collection *> &collections,
34 Set<Collection *> &visited)
35{
36 for (CollectionChild &child : collection->children) {
37 Collection *cc = child.collection;
38 if (visited.add(cc)) {
39 collections.append(cc);
40 collection_children_recursive(cc, collections, visited);
41 }
42 }
43}
44
46{
47 Collection *collection = params.extract_input<Collection *>("Collection"_ustr);
48 const bool recursive = params.extract_input<bool>("Recursive"_ustr);
49
50 if (collection == nullptr) {
52 return;
53 }
54
55 Vector<Collection *> child_collections;
56 if (recursive) {
57 Set<Collection *> visited;
58 collection_children_recursive(collection, child_collections, visited);
59 }
60 else {
61 for (CollectionChild &child : collection->children) {
62 child_collections.append(child.collection);
63 }
64 }
65
66 std::ranges::sort(child_collections, [](const Collection *a, const Collection *b) {
68 });
69
70 Vector<Object *> child_objects;
71 Vector<Collection *> obj_collections;
72 obj_collections.append(collection);
73 if (recursive) {
74 obj_collections.extend(child_collections);
75 }
76
77 params.set_output("Collections"_ustr, GList::from_container(std::move(child_collections)));
78
79 if (!params.output_is_required("Objects"_ustr)) {
81 return;
82 }
83
84 Set<const Object *> obj_visited;
85 for (Collection *col : obj_collections) {
86 for (CollectionObject &cob : col->gobject) {
87 if (obj_visited.add(cob.ob)) {
88 child_objects.append(cob.ob);
89 }
90 }
91 }
92
93 std::ranges::sort(child_objects, [](const Object *a, const Object *b) {
95 });
96
97 params.set_output("Objects"_ustr, GList::from_container(std::move(child_objects)));
98}
99
100static void node_register()
101{
102 static blender::bke::bNodeType ntype;
103
104 geo_node_type_base(&ntype, "GeometryNodeCollectionChildren"_ustr);
105 ntype.ui_name = "Collection Children";
106 ntype.ui_description =
107 "Retrieve a collection's object and collection children, in a name-based order";
108 ntype.nclass = NODE_CLASS_INPUT;
109 ntype.declare = node_declare;
112}
114
115} // namespace blender::nodes::node_geo_collection_children_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
Object groups, one object can be in many groups at once.
#define NOD_REGISTER_NODE(REGISTER_FUNC)
bool add(const Key &key)
Definition BLI_set.hh:248
void append(const T &value)
void extend(Span< T > array)
static GListPtr from_container(ContainerT &&container)
T extract_input(const UString identifier)
bool output_is_required(const UString identifier) const
void set_output(const UString identifier, T &&value)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static void collection_children_recursive(Collection *collection, Vector< Collection * > &collections, Set< Collection * > &visited)
int char char int int int BLI_strcasecmp_natural(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
const char * BKE_id_name(const ID &id)
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
ListBaseT< CollectionChild > children
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
NodeDeclareFunction declare
Definition BKE_node.hh:393