Select layer's object

I’m trying to select layer’s object through grasshopper python and that’s what I get:

Runtime error (NotSupportedException): This type of object is not supported in Grasshopper, so this Python script cannot create it. You might want to use ‘scriptcontext.doc = Rhino.RhinoDoc.ActiveDoc’ to use the Rhino doc, instead? If you do, remember to restore it: ‘scriptcontext.doc = ghdoc’.
Traceback:
line 339, in LayerNames, "C:\Users\Hamia\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\layer.py"
line 5, in script

any suggestion?
Thanks

Hi Hamia,

What happens is that ghpython component is trying to access the “Layers” property of the grasshopper document. Grasshopper document does not have this property. So you need to switch to Rhino document in order to retrieve the Rhino layer names.
Check the attached file.
layerNames.gh (8.4 KB)

many thanks djordje.

@djordje djordj is there any command to select sub-layer’s objects?

You can use the rs.ObjectsByLayer function with :: to separate the layer levels.
For example: rs.ObjectsByLayer("Layer 04::subLayer04::subSubLayer04") will return the ids of “subSubLayer04” sub-sub layer.
Check the attached file.
selectObjectsByLayer.gh (5.6 KB)

Perfect. Thank you so much @djordje . . .

@djordje,

So that script works as long as The target layer doesn’t have any sub-layer. Lets say that my target layer will contain 4 sublayers and

1- I want to select all the objects under those sub-layers
2-Export them as a separate rhino file
3- Delete geometries and layers from main file
4-attach the exported file to the main file

I’m almost there except:

1 - I can’t really select all geometries under sub-layers
2 - when I export and delete the geometries something weird happens and I cant do anything in grasshopper any more!(I kinda solve this issue now!) but I think its a bug in grasshopper!

Hamia,

If your target layer contains 4 sublayers, and you want to select all objects under those 4 targer sublayers, then that logically means that those objects do not belong to that target layer, but to those 4 sublayers. The target layer is only their parent layer.
So you have to provide the paths of those 4 sublayers, not the target layer.
Check the attached .gh and .3dm files.

I do not know if it is a bug, but grasshopper can sometimes confuse when switching the documents, in case you ended up on switching to Rhino document, and did not switch back to grasshopper one.
Because of this, the goal that you are trying to achieve can be accomplished much better in RhinoPython editor.

selectObjectsByLayer2.gh (6.6 KB)
selectObjectsByLayer2.3dm (297.7 KB)

@djordje many thanks for that, it works perfectly.