[python] getting the name or id of layer

quickest way to get the layer from the selected object is:

rs.ObjectLayer(obj), but this does not follow the philosophy of Rhinoscriptsyntax and instead of returning the id of the layer it returns the fullpath.

From this full path there is no easy way to get the name of the layer (assume it is sub-layer of sub-layer of sub-layer)

if you want to get the id of this layer rs.LayerId() this accepts the layer name as argument and doesn’t accept the fullpath

and rs.LayerName() accepts the id as argument which you cannot get as shown above.

What am I missing here?

import rhinoscriptsyntax as rs
import scriptcontext as sc

objID=rs.GetObject()
rhobj=rs.coercerhinoobject(objID)  #or rhobj=sc.doc.Objects.Find(objID)
index=rhobj.Attributes.LayerIndex
layer_id=sc.doc.Layers[index].Id
print layer_id
1 Like

Thanks @Helvetosaur
My God, never would’ve thought of that approach.

Actually just figured out that:

rs.ObjectLayer(obj)
# and 
rs.LayerName(rs.LayerId(rs.ObjectLayer(obj)))

give exactly the same result - > FullPath of the layer.
Shouldn’t LayerName give the actual (non full path) name of the layer?