[python] set layer visibility issue

say you have this structure:

image

and you want all layers with the name “Default” to be turned off

Script 1 (using rs.LayerVisibility()):

import rhinoscriptsyntax as rs
import scriptcontext as sc
import time


def lights_out(layer_list=None):
    if layer_list is None:
        layers_to_hide = ["Default"]
        layer_list = layers_to_hide
    else:
        pass
    for l in sc.doc.Layers:
        if l.Name in layer_list:
            #print l.Index, l.Name
            #l.IsVisible = False
            rs.LayerVisible(l.Name,False)

if __name__=="__main__":
    ts = time.time()
    
    lights_out()
    
    te = time.time()
    print "Elapsed time is {:.2f}".format(te-ts)

Result:
image

Just the first layer which matches the criteria is turned off :thinking:

Script 2 (using RhinoCommon through scriptcontext):

import rhinoscriptsyntax as rs
import scriptcontext as sc
import time


def lights_out(layer_list=None):
    if layer_list is None:
        layers_to_hide = ["Default"]
        layer_list = layers_to_hide
    else:
        pass
    for l in sc.doc.Layers:
        if l.Name in layer_list:
            #print l.Index, l.Name
            l.IsVisible = False
            #rs.LayerVisible(l.Name,False)

if __name__=="__main__":
    ts = time.time()
    
    lights_out()
    
    te = time.time()
    print "Elapsed time is {:.2f}".format(te-ts)

Result:
image

How do you even do that semi-turning off sublayers??? :woozy_face::upside_down_face::zipper_mouth_face::face_with_raised_eyebrow:

Is there a way to turn layers on/off using their index in the layer_table or the id?

https://mcneel.myjetbrains.com/youtrack/issue/RH-52120

This is an old bug IIRC, maybe from V5…

1 Like

I rely a lot on “selectability” when picking objects in my scripts and this is a blocking issue :frowning:

Select all objects in the layer and hide them, show them after script is finished as a work-around.

Yes this is what I do now due to this bug, but this is slower.

You can also set a “collection” filter in GetObject(s) that limits the selection to a certain pre-defined set of objects (using the objects argument). I also like to temporarily lock objects I don’t want getting selected before a GetObject(s), then unlock them afterward. Just some workarounds.

I do this, but before I have set usertext attributes my “predefined set” of criteria is limited to just the type of the object and the name. Or layername.endswith() or .startswith(). Still not enough.

I found the quickest approach to be:

  • hide / show layers relevant to the current script
  • execute the script on all selectable objects.

in RhinoCommon the following 2 lines need to be called.

layer.IsVisible = False
layer.SetPersistentVisibility(False)

Thanks for the reply @Alain,

Is Visible = False I understand,

but Set Persistance to false? That I don’t get. I want it persistant why do I have to set it to False?

What is SetPersistant doing?

Here’s an explanation:

1 Like