[Bf-blender-cvs] [7c568e7d367] master: Python API: add "children_recursive" property to Object & Collection

Campbell Barton noreply at git.blender.org
Fri Jan 14 02:22:56 CET 2022


Commit: 7c568e7d36710aba782a628dfff3b8bcea88be3b
Author: Campbell Barton
Date:   Fri Jan 14 12:19:37 2022 +1100
Branches: master
https://developer.blender.org/rB7c568e7d36710aba782a628dfff3b8bcea88be3b

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.

===================================================================

M	release/scripts/modules/bpy_types.py

===================================================================

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):
         """



More information about the Bf-blender-cvs mailing list