[python] Nested blocks as nested lists?

@ivelin.peychev, just FYI, rs.Sleep is calling Rhino.RhinoApp.Wait().

_
c.

Oh, I thought you meant the Time.Sleep()

Hi @ivelin.peychev,

i did. rs.Sleep does both, the timeout and keeping the message pump alive using Wait(). To ignore the timeout i usually set it to zero like this: rs.Sleep(0).

_
c.

1 Like

Hi all,

I am trying to write a recursive function in GhPython for grabbing all the sublayers of any given layer.

Here is the code that I have. It is extremely slow, but seems to work. Any ideas of how to do this smarter?

def recursivelyFetchSublayers(layers):
allSublayers =
if len(layers) > 0:
for layer in layers:
if layer.GetChildren():
sublayers = list(layer.GetChildren())
if len(sublayers) > 0:
for sublayer in sublayers:
allSublayers.append(sublayer)
if sublayer.GetChildren():
subSublayers = recursivelyFetchSublayers(sublayers)
if subSublayers:
for sublayer in subSublayers:
allSublayers.append(sublayer)
if len(allSublayers) > 0:
return allSublayers
else:
return

I realized that it was just the printout that was really slow, the code runs fine. Here is a working gh.py component.

201016_RecursiveTestDoc.3dm (332.7 KB)
201026_RecursivelyFetchChildLayers_v0.0_AMJ.gh (5.7 KB)

Hi Alex, you shouldn’t need recursion for this. Here’s simple approach: checking if the parent layer name is in the full path of the layers in the Rhino document, and then grabbing the “leaf” level layer if it is:

201026_RecursivelyFetchChildLayers_v0.0_AHD_00.gh (7.5 KB)

1 Like

Ah, interesting!

My goal for this is to take any given layer, and force all of its sublayers (with unknown depth) to be either 1) Turned on
2) Checked against a list of names and turned on if it matches the name, and turned off if it does not.

The problem I am having is that I need to control the sequence of these ‘turn on’ actions because it is impossible to turn on a layer when the parent layer is off.

Can you imagine a way that I could use the approach you’re proposing in order to create a hierarchical structure of layers where I can start at the highest level of hierarchy and then work my way downwards?