[python] how to properly reconstruct layer hierarchy after re-importing from DWG?

When one exports a Rhino document into DWG the whole layer tree is flattened - the parent/sublayer hierarchy is lost. The information can be preserved by using the proper export option so that the layers are named like this:

The problem:
When importing the DWG back into Rhino6, the parent/child hierarchy is not reconstructed, developers please fix that!

Failed attempt at a workaround:
I replace every $ with :: by running a script:

import rhinoscriptsyntax as rs
from scriptcontext import doc

def rename():
    currentLayersAll = rs.LayerNames()
    for currentLayerName in currentLayersAll:
        newLayerName = currentLayerName.replace("$","::")
        rs.RenameLayer(currentLayerName, newLayerName)


All the layers are renamed, but they do not get nested! The double colon becomes part of the layer name.
However when I print() the name of actual nested layer with rs.LayerNames() this is the output:

I am confused, how can I reconstruct the layer hierarchy when Rhino does not bother tot?


I think you can use rs.ParentLayer() for this. You may need to sort your layer list first with something like currentLayersAll.sort(key = lambda x: x.count('$')

Hope this helps