Enhance robustness of the basic flatten command (#20)

Older connector versions and still some edge cases in the current set refer to elements with the @ decorator  (Groups in Revit for example)
This commit is contained in:
Jonathon Broughton
2024-05-16 18:45:32 +02:00
committed by GitHub
parent 4b9d6b051d
commit c0fecb160b
+17 -3
View File
@@ -6,8 +6,22 @@ from specklepy.objects import Base
def flatten_base(base: Base) -> Iterable[Base]:
"""Take a base and flatten it to an iterable of bases."""
if hasattr(base, "elements") and base.elements is not None:
for element in base["elements"]:
"""Flatten a base object into an iterable of bases.
This function recursively traverses the `elements` or `@elements` attribute of the
base object, yielding each nested base object.
Args:
base (Base): The base object to flatten.
Yields:
Base: Each nested base object in the hierarchy.
"""
# Attempt to get the elements attribute, fallback to @elements if necessary
elements = getattr(base, "elements", getattr(base, "@elements", None))
if elements is not None:
for element in elements:
yield from flatten_base(element)
yield base