Python: deleting layers and sublayers

When deleting (sub)layers an error occurs: [layer name] does not exist in LayerTable.

My code:

import rhinoscriptsyntax as rs
layers = rs.LayerNames()
for layer in layers:
    if rs.IsLayerEmpty(layer) and layer != 'aLayerName':
        rs.DeleteLayer(layer)

Is there a way to update the LayerTable each time a layer is deleted?

(Solution on Python please.)

Kind regards, Dick

Hello,

You need to add a check on the updated layer table like this:

import rhinoscriptsyntax as rs

layers = rs.LayerNames()
for layer in layers:
    if layer in rs.LayerNames():
        if rs.IsLayerEmpty(layer) and layer != 'aLayerName':
            rs.DeleteLayer(layer)

-Graham

1 Like

Thanks!

Just came across this issue too. Thanks Graham.