|
| 1 | +""" |
| 2 | +Depth-first object iterator functions for Blender collections and view layers. |
| 3 | +
|
| 4 | +These functions are used to iterate over objects in a collection or view layer in a depth-first manner, including |
| 5 | +instances. This is useful for exporters that need to traverse the object hierarchy in a predictable order. |
| 6 | +""" |
| 7 | + |
| 8 | +from typing import Optional, Set, Iterable, List |
| 9 | + |
| 10 | +from bpy.types import Collection, Object, ViewLayer, LayerCollection |
| 11 | +from mathutils import Matrix |
| 12 | + |
| 13 | + |
| 14 | +class DfsObject: |
| 15 | + """ |
| 16 | + Represents an object in a depth-first search. |
| 17 | + """ |
| 18 | + def __init__(self, obj: Object, instance_objects: List[Object], matrix_world: Matrix): |
| 19 | + self.obj = obj |
| 20 | + self.instance_objects = instance_objects |
| 21 | + self.matrix_world = matrix_world |
| 22 | + |
| 23 | + @property |
| 24 | + def is_visible(self) -> bool: |
| 25 | + """ |
| 26 | + Check if the object is visible. |
| 27 | +
|
| 28 | + @return: True if the object is visible, False otherwise. |
| 29 | + """ |
| 30 | + if self.instance_objects: |
| 31 | + return self.instance_objects[-1].visible_get() |
| 32 | + return self.obj.visible_get() |
| 33 | + |
| 34 | + @property |
| 35 | + def is_selected(self) -> bool: |
| 36 | + """ |
| 37 | + Check if the object is selected. |
| 38 | + @return: True if the object is selected, False otherwise. |
| 39 | + """ |
| 40 | + if self.instance_objects: |
| 41 | + return self.instance_objects[-1].select_get() |
| 42 | + return self.obj.select_get() |
| 43 | + |
| 44 | + |
| 45 | +def _dfs_object_children(obj: Object, collection: Collection) -> Iterable[Object]: |
| 46 | + """ |
| 47 | + Construct a list of objects in hierarchy order from `collection.objects`, only keeping those that are in the |
| 48 | + collection. |
| 49 | +
|
| 50 | + @param obj: The object to start the search from. |
| 51 | + @param collection: The collection to search in. |
| 52 | + @return: An iterable of objects in hierarchy order. |
| 53 | + """ |
| 54 | + yield obj |
| 55 | + for child in obj.children: |
| 56 | + if child.name in collection.objects: |
| 57 | + yield from _dfs_object_children(child, collection) |
| 58 | + |
| 59 | + |
| 60 | +def dfs_objects_in_collection(collection: Collection) -> Iterable[Object]: |
| 61 | + """ |
| 62 | + Returns a depth-first iterator over all objects in a collection, only keeping those that are directly in the |
| 63 | + collection. |
| 64 | +
|
| 65 | + @param collection: The collection to search in. |
| 66 | + @return: An iterable of objects in hierarchy order. |
| 67 | + """ |
| 68 | + objects_hierarchy = [] |
| 69 | + for obj in collection.objects: |
| 70 | + if obj.parent is None or obj.parent not in set(collection.objects): |
| 71 | + objects_hierarchy.append(obj) |
| 72 | + for obj in objects_hierarchy: |
| 73 | + yield from _dfs_object_children(obj, collection) |
| 74 | + |
| 75 | + |
| 76 | +def dfs_collection_objects(collection: Collection) -> Iterable[DfsObject]: |
| 77 | + """ |
| 78 | + Depth-first search of objects in a collection, including recursing into instances. |
| 79 | +
|
| 80 | + @param collection: The collection to search in. |
| 81 | + @return: An iterable of tuples containing the object, the instance objects, and the world matrix. |
| 82 | + """ |
| 83 | + yield from _dfs_collection_objects_recursive(collection) |
| 84 | + |
| 85 | + |
| 86 | +def _dfs_collection_objects_recursive( |
| 87 | + collection: Collection, |
| 88 | + instance_objects: Optional[List[Object]] = None, |
| 89 | + matrix_world: Matrix = Matrix.Identity(4), |
| 90 | + visited: Optional[Set[Object]]=None |
| 91 | +) -> Iterable[DfsObject]: |
| 92 | + """ |
| 93 | + Depth-first search of objects in a collection, including recursing into instances. |
| 94 | + This is a recursive function. |
| 95 | +
|
| 96 | + @param collection: The collection to search in. |
| 97 | + @param instance_objects: The running hierarchy of instance objects. |
| 98 | + @param matrix_world: The world matrix of the current object. |
| 99 | + @param visited: A set of visited object-instance pairs. |
| 100 | + @return: An iterable of tuples containing the object, the instance objects, and the world matrix. |
| 101 | + """ |
| 102 | + |
| 103 | + # We want to also yield the top-level instance object so that callers can inspect the selection status etc. |
| 104 | + if visited is None: |
| 105 | + visited = set() |
| 106 | + |
| 107 | + if instance_objects is None: |
| 108 | + instance_objects = list() |
| 109 | + |
| 110 | + # First, yield all objects in child collections. |
| 111 | + for child in collection.children: |
| 112 | + yield from _dfs_collection_objects_recursive(child, instance_objects, matrix_world.copy(), visited) |
| 113 | + |
| 114 | + # Then, evaluate all objects in this collection. |
| 115 | + for obj in dfs_objects_in_collection(collection): |
| 116 | + visited_pair = (obj, instance_objects[-1] if instance_objects else None) |
| 117 | + if visited_pair in visited: |
| 118 | + continue |
| 119 | + # If this an instance, we need to recurse into it. |
| 120 | + if obj.instance_collection is not None: |
| 121 | + # Calculate the instance transform. |
| 122 | + instance_offset_matrix = Matrix.Translation(-obj.instance_collection.instance_offset) |
| 123 | + # Recurse into the instance collection. |
| 124 | + yield from _dfs_collection_objects_recursive(obj.instance_collection, |
| 125 | + instance_objects + [obj], |
| 126 | + matrix_world @ (obj.matrix_world @ instance_offset_matrix), |
| 127 | + visited) |
| 128 | + else: |
| 129 | + # Object is not an instance, yield it. |
| 130 | + yield DfsObject(obj, instance_objects, matrix_world @ obj.matrix_world) |
| 131 | + visited.add(visited_pair) |
| 132 | + |
| 133 | + |
| 134 | +def dfs_view_layer_objects(view_layer: ViewLayer) -> Iterable[DfsObject]: |
| 135 | + """ |
| 136 | + Depth-first iterator over all objects in a view layer, including recursing into instances. |
| 137 | +
|
| 138 | + @param view_layer: The view layer to inspect. |
| 139 | + @return: An iterable of tuples containing the object, the instance objects, and the world matrix. |
| 140 | + """ |
| 141 | + visited = set() |
| 142 | + def layer_collection_objects_recursive(layer_collection: LayerCollection): |
| 143 | + for child in layer_collection.children: |
| 144 | + yield from layer_collection_objects_recursive(child) |
| 145 | + # Iterate only the top-level objects in this collection first. |
| 146 | + yield from _dfs_collection_objects_recursive(layer_collection.collection, visited=visited) |
| 147 | + |
| 148 | + yield from layer_collection_objects_recursive(view_layer.layer_collection) |
0 commit comments