Delete layer problem

Hi all,
i tried to wrote a script that delete children layer and move all object to the coresponding parent layer.
This script should than running recursiv till no more child layers are present.

The problem is, after the object in the child layer is moved to the parent layer and the child layer is deleted Rhino dont recognize the objects on that layer.
But if i select the object the property panel shows the right layer.

import Rhino
import scriptcontext as sc

rh_active_doc = Rhino.RhinoDoc.ActiveDoc
# GET THE LOWEST LAYER IN THE HIRARCHY
D = {}
for i in rh_active_doc.Layers:
    D[len(str(i).split('::'))] = i

if max(D) > 1:
    print D[max(D)]
    # FIND OBJECTS ON THIS LOWEST LAYER
    lay_objs = rh_active_doc.Objects.FindByLayer(D[max(D)])
    print lay_objs
    if len(lay_objs) > 0:
        # MOVE OBJECTS ON THE LOWEST LAYER TO THE PARENT LAYER
        lay_parent = D[max(D)].ParentLayerId
        lay_parent_index = rh_active_doc.Layers.FindId(lay_parent).Index
        for obj in lay_objs:
            obj.Attributes.LayerIndex = lay_parent_index
            obj.CommitChanges()
    parent_lay = rh_active_doc.Layers.FindId(D[max(D)].ParentLayerId)
    print parent_lay
    parent_lay_objs = rh_active_doc.Objects.FindByLayer(parent_lay)
    print parent_lay_objs

FIRST PRINT RESULT BEFORE CIRCLE LAYER IS DELETED

Layer 01::Layer 01::Rect::Circle
Array[RhinoObject]((<Rhino.DocObjects.CurveObject object at 0x00000000000000ED [CurveObject: (unnamed) (0)]>))
Layer 01::Layer 01::Rect
Array[RhinoObject]((<Rhino.DocObjects.CurveObject object at 0x00000000000000EE [CurveObject: (unnamed) (0)]>, <Rhino.DocObjects.CurveObject object at 0x00000000000000EF [CurveObject: (unnamed) (0)]>))


SECOND PRINT RESULT

Layer 01::Layer 01::Rect
Array[RhinoObject](())
Layer 01::Layer 01::Rect
Array[RhinoObject]((<Rhino.DocObjects.CurveObject object at 0x00000000000000F5 [CurveObject: (unnamed) (0)]>, <Rhino.DocObjects.CurveObject object at 0x00000000000000F6 [CurveObject: (unnamed) (0)]>))


In the second print result it says empty array in the Rect layer, the object attribute tell a different story.
Why is the Layer 01::Layer 01::Rect empty ?

Help is welcome

I wrote one of these awhile back - don’t know if this helps…

FlattenLayers.py (1.2 KB)

moved to Scripting category

Thanks,

unfortunatly i only want to understand why the layer is still a parentlayer even if the childlayer is deleted
and why the objectsarray on the layer is empty even if the object attribute tells me it is on the layer.

I only want to practice.