How get LayerTable names in Grasshopper Python

Hi all,

i tried to get.Selected Layer names into grasshopper with python.

but i have no idea how the rh.DocObjects.Tables.LayerTable works.

A small example would be very helpful.

Thanks in advanced

2 Likes

found the solution.

first a LayerTables have to be created from the active document.

rh.RhinoDoc.ActiveDoc.Layers

you can “one line it” like this: (L is output of ghpython component)

import Rhino

L = [l.Name for l in Rhino.RhinoDoc.ActiveDoc.Layers if l.IsDeleted == False]

Thanks, but i want only the selected ones
like this

import rhinoscriptsyntax as rs
import Rhino as rh
import scriptcontext as sc

LayTa =  rh.RhinoDoc.ActiveDoc.Layers
LayIn = rh.DocObjects.Tables.LayerTable.GetSelected(LayTa)
a = [rh.DocObjects.Tables.LayerTable.FindIndex(LayTa,i) for i in LayIn[1]]

I have also a lot of problems to get the geometry from these layers…i think its because its from the DocObjects
and not ported to grasshopper objects

I see. what do you mean by “selected ones”. Do you mean, reference an object from the rhino document into a grasshopper parameter, then get the object attributes, (layer index, color, etc…). If so, you could do something like this.

import Rhino
import scriptcontext as sc

sc.doc = Rhino.RhinoDoc.ActiveDoc

refgeo = sc.doc.Objects.Find(x)
layerIndex = refgeo.Attributes.LayerIndex
a = Rhino.RhinoDoc.ActiveDoc.Layers.FindIndex(layerIndex)

sc.doc = ghdoc

you will need to set your type hint on the input to guid.

I mean the selected Layers in the Document

Big Thanks for your help
Now time for bed

Thanks again for your help,

I your example you get attributes from geometry but i try to get geometry from selected Layers. I tried rs.ObjectsByLayer with LayerObjects from rh.DocObjects.Tables.LayerTable.FindIndex but its not supported.
But from Rhino.RhinoDoc.ActiveDoc.Objects.FindByLayer is supported

By the way how do you make this nice screenshots and nice and clear presentations ?

For the screenshots, from grasshopper, you can go to File->Export Quick Image.

One last note: A slightly different approach, (instead of selecting the layers from the layers tab in the rhino document), might be to select your layernames from within GH, (although, at this point, it’s literally recreating functionality that already exists in the geometry pipeline component so maybe this is not terribly helpful…)

Use one component to get the layernames (one liner I posted earlier), pick your layers from that list, then feed them into new component? Also, perhaps this is a matter of opinion, but if you are already working in rhinocommon, stay in rhinocommon! You can get the geometry without using rs.coercegeometry…One less namespace to import. Either way, it looks like you have a working solution!

import Rhino

geo = []

for l in Rhino.RhinoDoc.ActiveDoc.Layers:
    if l.Name == x:
        RhinoObj = (Rhino.RhinoDoc.ActiveDoc.Objects.FindByLayer(x))
        for obj in RhinoObj:
            geo.append(obj.Geometry)

a = geo

Thanks for all your help