Selecting objects on layers with Rhino python


#1

I’m trying to write a simple script to set the color and line weight of lines based on a list that maps layers to the relevant properties.

This seems to be running into problems with nested layers - rs.IsLayer can find a layer like “parent::child”, but scriptcontext.doc.Objects.FindByLayer won’t - is there another way that I should call the layer / a different way to do this?

My code is linked here:
http://pastebin.com/CG8MDJV2

Thanks,
Chris


#2

@chirs

See if this might be helpful: http://www.grasshopper3d.com/forum/topics/visual-studio-namespace-errors-doc-objects-findbylayer-c


(Willem Derks) #3

Hi Chris,

.FindByLayer does not only accept strings but also layer-objects. See if the below example works for you.
It basically gets a list of all possible layer-objects and finds the layer-object with the name to retrieve the objects from.

HTH
-Willem

import rhinoscriptsyntax as rs
import scriptcontext

def main():

    #list of layer names
    layer_names = rs.LayerNames()
    
    # get a list of all layer objects in the document that are not deleted
    layer_objects = list(layer for layer in scriptcontext.doc.Layers if not layer.IsDeleted)
    
    
    for name in layer_names:
        # find layer object with FullPath == name and break out to pass layer_obj in FindByLayer
        layer_obj = None
        for layer_obj in layer_objects:
            if layer_obj.FullPath == name:
                break
        if layer_obj:
            # FindByLayer accepts both names ad layer objects so pass the layer object
            rhobjs = scriptcontext.doc.Objects.FindByLayer(layer_obj)
            print rhobjs
        else:
            #catch possible error here
            pass


main()

#4

Note rs.ObjectsByLayer() should work with full layer names if they are input. Just beware to always supply the full name if you have like-named sublayers…

–Mitch