diff options
author | Campbell Barton <campbell@blender.org> | 2022-01-14 04:19:37 +0300 |
---|---|---|
committer | Campbell Barton <campbell@blender.org> | 2022-01-14 04:21:56 +0300 |
commit | 7c568e7d36710aba782a628dfff3b8bcea88be3b (patch) | |
tree | 1d04742a34c652e22e39c4d0078fdcc64c09681c /release/scripts/modules | |
parent | cea588b9ef8f9bdb2729fb233d6f1ed0886700e1 (diff) |
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.
Diffstat (limited to 'release/scripts/modules')
-rw-r--r-- | release/scripts/modules/bpy_types.py | 34 |
1 files changed, 34 insertions, 0 deletions
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 @@ -101,6 +101,19 @@ 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""" import bpy @@ -121,6 +134,27 @@ class Object(bpy_types.ID): 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): """ The collections this object is in. |