Layer.IsVisible weirdness

The other day I tried to add two layers to a Rhino document and make IsVisible true in one layer and false in the other:

int i = doc.Layers.Add("Layer 1", Color.Red);
int j = doc.Layers.Add("Layer 2", Color.Blue);
doc.Layers[i].IsVisible = true;
doc.Layers[j].IsVisible = false;

This does not work. You must do it this way:

Layer l1 = doc.Layers[i];
l1.IsVisible = true;
Layer l2 = doc.Layers[j];
l2.IsVisible = false;

I have two issues with this:

  1. Why do I need to issue CommitChanges()? Why can’t the IsVisible property propagate the desired changes?
  2. Why can’t I do it in the first order, but must I do it in the second order? It seems like every time I issue doc.Layers[i] a new Layer object is created, so that when I set IsVisible on the first and issue CommitChanges on the second the end result is not that the visibility is changed.

Admittedly, I’m still on SR0 waiting for our IT department to roll out SR4, so it may already be solved.

This hasn’t changed yet. This is part of our SDK that I never felt completely happy with what I wrote. I tried to stay consistent with other classes controlled by the document in that you had to call CommitChanges to get the changes to take.

Thanks Steve for your frank message above. I hope the part of the SDK you’re not completely happy with can be improved at some point in time. Now that I know the way to do this, I can work with it.
By the way, I upgraded to SR4 yesterday: happy to see my small contribution in the RhinoCommon SDK :smile:


Just FYI the fact/bug that you cannot set directly visibility through doc.layers(i).isVisible is still remaining in rhino 5. You need to assign a layer variable first.


This works in Rhino 6:

import Rhino
import scriptcontext
layer_index = scriptcontext.doc.Layers.Add()
if layer_index >= 0:
    scriptcontext.doc.Layers[layer_index].IsVisible = False

– Dale

1 Like

Hello @dale,

ok cool, good to know.

Merry christmas to you!

1 Like