Skip to content

Commit d1d6170

Browse files
committed
[core] Attribute: Add a flattenedChildren property
`flattenedChildren` returns the list of all the attributes that refer to this attribute as their parent (either direct or indirect) through the `root` property. The search for the children attributes is recursive and alllows to retrieve at once all the nested attributes, independently from their level. At the moment, only `ListAttribute` and `GroupAttribute` will return a non-empty list of flattened children attributes.
1 parent d57fc0f commit d1d6170

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

meshroom/core/attribute.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,33 @@ def updateInternals(self):
423423
# Emit if the enable status has changed
424424
self.setEnabled(self.getEnabled())
425425

426+
def getFlattenedChildren(self):
427+
""" Return a list of all the attributes that refer to this instance as their parent through
428+
the 'root' property. If no such attribute exist, return an empty list. The depth difference is not
429+
taken into account in the list, which is thus always flat. """
430+
attributes = ListModel(parent=self)
431+
if isinstance(self._value, str):
432+
# String/File attributes are iterable but cannot have children: immediately rule that case out
433+
return attributes
434+
435+
try:
436+
# If self._value is not iterable, then it is not an attribute that can have children
437+
iter(self._value)
438+
except TypeError:
439+
return attributes
440+
441+
for attribute in self._value:
442+
if not isinstance(attribute, Attribute):
443+
# Handle ChoiceParam values, which contained in a list hence iterable, but are string
444+
continue
445+
attributes.add(attribute)
446+
if isinstance(attribute, ListAttribute) or isinstance(attribute, GroupAttribute):
447+
# Handle nested ListAttributes and GroupAttributes
448+
flattened = attribute.getFlattenedChildren()
449+
for i in flattened:
450+
attributes.add(i)
451+
return attributes
452+
426453
name = Property(str, getName, constant=True)
427454
fullName = Property(str, getFullName, constant=True)
428455
fullNameToNode = Property(str, getFullNameToNode, constant=True)
@@ -465,6 +492,7 @@ def updateInternals(self):
465492
validValue = Property(bool, getValidValue, setValidValue, notify=validValueChanged)
466493
root = Property(BaseObject, root.fget, constant=True)
467494
depth = Property(int, getDepth, constant=True)
495+
flattenedChildren = Property(BaseObject, getFlattenedChildren, constant=True)
468496

469497

470498
def raiseIfLink(func):

0 commit comments

Comments
 (0)