# Turning On and Off layers with Python Grasshopper Laggy

**URL:** <https://discourse.mcneel.com/t/turning-on-and-off-layers-with-python-grasshopper-laggy/98035>\
**Category:** Grasshopper\
**Tags:** python\
**Created:** [March 12, 2020, 10:34am UTC](https://discourse.mcneel.com/t/turning-on-and-off-layers-with-python-grasshopper-laggy/98035 "2020-03-12T10:34:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattgaydon](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mattgaydon/32/770786_2.png) [@mattgaydon](https://discourse.mcneel.com/u/mattgaydon)\
**Post date:** [March 12, 2020, 10:34am UTC](https://discourse.mcneel.com/t/turning-on-and-off-layers-with-python-grasshopper-laggy/98035/1 "2020-03-12T10:34:09Z")

</div>

I am trying to control Layer Visibility from Human UI though Python in grasshopper, I have been able to create a python code that works but is very laggy (2 Seconds+) in a larger file with lots of layers. So tried to rewrite it so it did not search all layers, but just the ones that where children of the parent layer but this option is still laggy and breaks if the Layer to turn on is none.

**Working but Laggy**

```
import Rhino.RhinoDoc as rhDoc
import rhinoscriptsyntax as rs
import scriptcontext as sc

# Turn select layers on and off with Switch
sc.doc = rhDoc.ActiveDoc

for l in rhDoc.ActiveDoc.Layers:
    if LayerOn is not None and l.Name == LayerOn:
        pLayer = rs.ParentLayer(LayerOn)
        rs.LayerVisible(pLayer,True)
        rs.LayerVisible(LayerOn,True)
    elif l.Name == LayerOff:
        rs.LayerVisible(LayerOff,False)

sc.doc = ghdoc

```

**Broken and Also Laggy**  
As wants a string for pLayer = rs.ParentLayer(LayerOn) even if LayerOn is none.

```
# Turn Selected layers on and off with Grasshoper
import Rhino.RhinoDoc as rhDoc
import rhinoscriptsyntax as rs
import scriptcontext as sc

sc.doc = rhDoc.ActiveDoc

if LayerOn is not None:
    pLayer = rs.ParentLayer(LayerOn)
    rs.LayerVisible(pLayer,True)
    rs.LayerVisible(LayerOn,True)
else:
    rs.LayerVisible(LayerOff,False)

sc.doc = ghdoc

```

Demo Files  
[Layer Control GH.3dm](https://discourse.mcneel.com/uploads/short-url/qBXcID4K2kQfWnchoru4w77ML1M.3dm) (271.9 KB)  
[Control Layer Visabilty with GH.gh](https://discourse.mcneel.com/uploads/short-url/weIC0FzZuzdadCIE5Ku1wnyc6St.gh) (9.4 KB)

---

<div class="post-metadata">

**Author:** ![Mahdiyar](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mahdiyar/32/472862_2.png) [@Mahdiyar](https://discourse.mcneel.com/u/Mahdiyar)\
**Post date:** [March 12, 2020, 11:56am UTC](https://discourse.mcneel.com/t/turning-on-and-off-layers-with-python-grasshopper-laggy/98035/2 "2020-03-12T11:56:36Z")

</div>

I’m not sure I understood you correctly, Do you have subLayers with similar names but different parents in your file? If so, Do you know which one you want to change?

```
import Rhino
currentLayer = Rhino.RhinoDoc.ActiveDoc.Layers.CurrentLayer
parentLayer = Rhino.RhinoDoc.ActiveDoc.Layers.FindName(ParentLayerName)
for subLayer in parentLayer.GetChildren():
    if subLayer.Name in LayerNames:
        parentLayer.IsVisible = True
        subLayer.IsVisible = True
    elif subLayer != currentLaye:
        subLayer.IsVisible = False
        subLayer.SetPersistentVisibility(False)

```

[LayerVisibility.gh](https://discourse.mcneel.com/uploads/short-url/wnUNKIQT59GEDpw872ZryiDRVGv.gh) (13.7 KB)

---

<div class="post-metadata">

**Author:** ![ivelin.peychev](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ivelin.peychev/32/676038_2.png) [@ivelin.peychev](https://discourse.mcneel.com/u/ivelin.peychev)\
**Post date:** [March 12, 2020, 12:22pm UTC](https://discourse.mcneel.com/t/turning-on-and-off-layers-with-python-grasshopper-laggy/98035/3 "2020-03-12T12:22:30Z")

</div>

A hacky idea 😉 (untested)

What if you run rs.Command() that invokes RunPythonScript command?

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/3/4/348cefde606cf2c10e293409cea432062885b707.png)

changing the layers from Rhino instead of GH?

---

<div class="post-metadata">

**Author:** ![mattgaydon](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mattgaydon/32/770786_2.png) [@mattgaydon](https://discourse.mcneel.com/u/mattgaydon)\
**Post date:** [March 12, 2020, 1:45pm UTC](https://discourse.mcneel.com/t/turning-on-and-off-layers-with-python-grasshopper-laggy/98035/4 "2020-03-12T13:45:58Z")

</div>

Interesting revised workflow. Works well just need to add a extra boolean so i could bring the name in but only turn the layer on if a switch was on.

```
    if subLayer.Name in LayerNames and Preview != False:

```

[LayerVisibility PreviewSwitch.gh](https://discourse.mcneel.com/uploads/short-url/zluD8fSHUQ14scfT0z2LWHFHFkR.gh) (17.6 KB)

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [March 12, 2020, 2:06pm UTC](https://discourse.mcneel.com/t/turning-on-and-off-layers-with-python-grasshopper-laggy/98035/5 "2020-03-12T14:06:48Z")

</div>

If you’re doing this for a lot of layers, it might help to call `rs.EnableRedraw(False)` before fiddling with the Rhino document/layers, and `rs.EnableRedraw(True)` afterwards.

---

<div class="post-metadata">

**Author:** ![Balazs](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/balazs/32/493129_2.png) [@Balazs](https://discourse.mcneel.com/u/Balazs)\
**Post date:** [June 8, 2023, 7:22am UTC](https://discourse.mcneel.com/t/turning-on-and-off-layers-with-python-grasshopper-laggy/98035/6 "2023-06-08T07:22:15Z")

</div>

Does not work for me.

 ![Screenshot 2023-06-08 09.19.25](https://global.discourse-cdn.com/mcneel/uploads/default/original/4X/a/e/8/ae89e080c9db66e0b3336cd6aa4990bec9f62f71.png)

---

<div class="post-metadata">

**Author:** ![Jacques\_Gaudin](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/jacques_gaudin/32/473733_2.png) [@Jacques\_Gaudin](https://discourse.mcneel.com/u/Jacques_Gaudin)\
**Post date:** [June 9, 2023, 8:48am UTC](https://discourse.mcneel.com/t/turning-on-and-off-layers-with-python-grasshopper-laggy/98035/7 "2023-06-09T08:48:00Z")

</div>

There is an “r” missing at the end of `currentLayer`.
