After reading several other posts, it's still not clear to me why my code does work as expected. While I am using the Ansys SpaceClaim API, that shouldn't have any bearing on the recursion logic which is the basis of my question. In essence, I would like to recurse through all the components (iterable) in my structure, find the component I'm looking for by name, and return it when found.
NAME = "SomeComponentName"
def recurse(complist):
for comp in complist:
if comp.GetName() == NAME:
print(comp.GetName())
return comp
else:
recurse(comp.Components)
component = recurse(GetRootPart().Components)
print(component)
The output is currently something like:
SomeComponentName
None
Which clearly indicates to me that the component was found, yet None is returned. The only thing I can think is that the function is continuing to recurse even after the component is found but why would it continue recursion if the "else" path is not taken?
EDIT: Contrary to the answer in the post linked as the duplicate, changing recurse(comp.Components)
to return return recurse(comp.Components)
has no effect