Oy!
Recursion would be SO much easier to read.
Start with something like this:
#------------
# Recursively scan a container for children and the children's children, calling
# a given routine for each child encountered.
#
def forEachChild(container, func, lvl=None):
if not lvl:
lvl = 1
func(container, lvl)
for i in range(container.getComponentCount()):
component = container.getComponent(i)
forEachChild(component, func, lvl+1)
Pass it a function that takes a component and the depth as its parameters.
{ Note that this version enumerates the nested objects of regular components, too. }