From 7c568e7d36710aba782a628dfff3b8bcea88be3b Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 14 Jan 2022 12:19:37 +1100 Subject: Python API: add "children_recursive" property to Object & Collection This is a convenience property, already available for bones. Simplifies D13821 which in-lined this function. --- release/scripts/modules/bpy_types.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'release/scripts/modules') diff --git a/release/scripts/modules/bpy_types.py b/release/scripts/modules/bpy_types.py index b477f624b7b..0e457d828fe 100644 --- a/release/scripts/modules/bpy_types.py +++ b/release/scripts/modules/bpy_types.py @@ -100,6 +100,19 @@ class Texture(bpy_types.ID): class Collection(bpy_types.ID): __slots__ = () + @property + def children_recursive(self): + """A list of all children from this collection.""" + children_recursive = [] + + def recurse(parent): + for child in parent.children: + children_recursive.append(child) + recurse(child) + + recurse(self) + return children_recursive + @property def users_dupli_group(self): """The collection instance objects this collection is used in""" @@ -120,6 +133,27 @@ class Object(bpy_types.ID): return tuple(child for child in bpy.data.objects if child.parent == self) + @property + def children_recursive(self): + """A list of all children from this object. + + .. note:: Takes ``O(len(bpy.data.objects))`` time.""" + import bpy + parent_child_map = {} + for child in bpy.data.objects: + if (parent := child.parent) is not None: + parent_child_map.setdefault(parent, []).append(child) + + children_recursive = [] + + def recurse(parent): + for child in parent_child_map.get(parent, ()): + children_recursive.append(child) + recurse(child) + + recurse(self) + return children_recursive + @property def users_collection(self): """ -- cgit v1.2.3