Select objects from layers list

Hello community!

I am working on a python script that allows me to automatically export and update big architectural rhino files to 3dsmax in the most convenient and fast way.

I have problems selecting objects from a list of layers.

let’s say I have tens of layers and sublayers in my 3dm file:

-layer00
-layer01
-layer n

My steps are:

  • Extract in a separate text file the layers names list. (DONE)

Now I would like to select just the objects from a portion of the layers that I saved in the previously created text file.

How can I select multiple layers based on a list?

Hi @Pitti,

Why do you need to save Rhino objects to a text file?

– Dale

Hi @dale, I am saving layers list on a text file.
sorry if my request was a little messed up :stuck_out_tongue:

What I need is a way to select objects from multiple layers in my python script.

i will “GetSettings” of the layer list i need to export from an ini file. like this:

[information]

export_objname = C:\Users\xxxxxx\Documents\Model\Export\2020.09.27_model_test.obj
layer_list = layer01,layer02,layer03,layer03::sublayer01…

If I understand what you are asking, you want to read in a list of layer names from an ini (text) file and select the objects on those layers (then you can run your export function)

Something like this should work - assume the name of the file is “Layer List” and it is on your desktop:

import rhinoscriptsyntax as rs

file=open(r"C:\Users\yourusername\Desktop\Layer List.txt")
layers=rs.LayerNames()

for line in file:
    #get rid of any whitespace or end of line characters
    layername=line.strip()
    if layername in layers:
        rs.ObjectsByLayer(layername,True)
file.close()

Note that as Python is case sensitive, you may also need to make provisions for non-case sensitive layer name matching - if you think that might be necessary.

Once all the objects are selected, you can export them. Or, to export layer by layer, run the export function inside the selection loop.

Oh, I forgot to describe the format for the .ini file, in my test case it has one layer name per line (not comma separated). But it’s pretty easy to adapt the scriptlet to parse other formatting.

I will test it as soon as possible, thanks! :slight_smile:

thanks @Helvetosaur!!

I solved it like this:

 import Rhino
    import scriptcontext
    import rhinoscriptsyntax as rs
    import System.Guid, System.Drawing.Color

    baseName = rs.DocumentName()

    iniPath ='C:\\Users\\xxxxxx\\Documents\\EasyPrework\\_ini_files\\' + baseName.replace(".3dm", ".ini")

    file = rs.GetSettings(iniPath,'INFORMATION','layers_list')

    layers=rs.LayerNames()
 
    for layername in file.split(","):
                  if layername in layers:
            rs.ObjectsByLayer(layername,True) 

Not bad! :wink: