Set Layer to a specific Layer

I would like to add a custom python command, I’m not quite familiar with the rhinosyntax though, and I am not too good in python either :smiley:
So what I want to create is an alias which does this:
First, it checks if I have selected One Object. If yes, it should run the command SetLayerToObject. If not, it should Set the Layer named “0_Default”
If the Layer “0_Default” is not created yet, it should create this Layer and set it active.

Any help? Thank you!

What did you try so far?

Could you attach the code you’ve created?

try this:

import rhinoscriptsyntax as rs
import scriptcontext as sc

selectedObjs = rs.SelectedObjects(True, True)

if selectedObjs:
    layer = sc.doc.Layers.FindName(rs.ObjectLayer(selectedObjs[0]))
    sc.doc.Layers.SetCurrentLayerIndex(layer.Index,True)
else:
    layer = sc.doc.Layers.FindName("0_Default")
    if layer:
        sc.doc.Layers.SetCurrentLayerIndex(layer.Index,True)
    else:
        rs.AddLayer("0_Default")

since you didn’t specify, I assumed multiple object selected is possible and therefor you might aswell have objects on different layers selected, so I took the layer of the first object.

1 Like

Thanks rgr! This is exactly what I wanted!