Creating sublayers with python

Hi guys. I have a simple python script that creates a few layers with different colors which I think I based on a script I found from Helvetosaur here on the forum (I think). Now I’m wondering how I can create one Main layer and have the 3 layers be created as a sublayer. I’ve been trying this trick with Mainlayer::Sublayer but somehow I couldn’t get it to work. Anybody that could help me out here?

import scriptcontext as sc
import Rhino
import rhinoscriptsyntax as rs

layer = Rhino.DocObjects.Layer()

def AddLayers():
    layernames = ["Layer1", "Layer2", "Layer3"]
    Colors = [(255,0,0), (0,255,0), (0,255,255)]
    Count = 0
    for x in layernames:
        layer.Name = x
        sc.doc.Layers.Add(layer)
        rs.LayerColor(x, Colors[Count])
        Count += 1
AddLayers()

@siemen, just use rs.AddLayer which allows to define a parent layer:

import rhinoscriptsyntax as rs

def DoSomething():
    
    main_layer = rs.AddLayer("Main")
    names = ["Layer1", "Layer2", "Layer3"]
    colors = [(255,0,0), (0,255,0), (0,255,255)]
    
    for i in xrange(len(names)):
        rs.AddLayer(names[i], colors[i], True, False, main_layer)
    
DoSomething()

_
c.

1 Like

AddLayer() has some optional arguments including ‘parent’ - which you can use to specify a parent layer if you want a layer to be its sublayer. The parent layer has to exist first.

Thus, create your parent later and then add your sublayers:

import rhinoscriptsyntax as rs

def TestAddSubLayers():
    parent_layer="MainLayer"
    if not rs.IsLayer(parent_layer): rs.AddLayer(parent_layer)
    sublayernames = ["Layer1", "Layer2", "Layer3"]
    Colors = [(255,0,0), (0,255,0), (0,255,255)]
    for i in range(len(sublayernames)):
        # Color is also directly specifiable
        rs.AddLayer(sublayernames[i],color=Colors[i],parent=parent_layer)
TestAddSubLayers()

–Mitch

1 Like

Thanks @clement & @Helvetosaur! I’ll try this out.

Just out of curiosity: On that for loop, why do you both use something like “range(len(names))” instead of just “names”? I would think the way I did it is faster (as in, less typing) so I assume there’s a reason why you’re doing it like that?

@siemen,

because we then have an index to access both lists, the one for the layer names and the one for the colors.

_
c.

Aha! So it replaces my “Count” logic?

@siemen, yes, you could also simplify it using this:

    for name, color in zip(names, colors):
        rs.AddLayer(name, color, True, False, main_layer)

_
c.

1 Like

Great, thanks a lot!

Hi ,
I just tested your sub layer script and Layer1,2 and 3 are created as layers.
It correctly created MainLayer and then created the others as new layers not sub layers. This is on ver 7 on mac…is this still a problem ?
Giusseppe

Hi Giuseppe,

It works on Windows… Sadly I don’t have a Mac to test on. @pascal - can you test?

image

Of course a came in to the office today without my mac, but can test tomorrow or this evening.

-Pascal

layers
I hope you can solve it…thanks…Giusseppe

Hi, Did anyone find the solution for this on a mac please ?

Hi @giusseppe,

the parent option stopped working a long time ago, i remember to report it but it never got fixed. The workaround is to give rs.AddLayer the fullpath instead eg:

rs.AddLayer("ParentLayerName::SubLayerName")

_
c.

Thanks Clement, I see how that works on its own but cannot work out how to add it to the script with the i values…

Hi @giusseppe, try below:

import rhinoscriptsyntax as rs

def DoSomething():
    
    main_layer = rs.AddLayer("Main")
    names = ["Layer1", "Layer2", "Layer3"]
    colors = [(255,0,0), (0,255,0), (0,255,255)]
    
    for i in xrange(len(names)):
        full_path = "{}::{}".format(main_layer, names[i])
        rs.AddLayer(full_path, colors[i], True, False)
    
DoSomething()

_
c.

well, that certainly works, thank you…where can i read about the use of curly brackets like that ?

It’s called string formatting:

Keep in mind that Rhino 7 uses IronPython 2.x, which means that you can’t use the super new stuff from Python 3.6 and beyond, like f-strings.

Thanks. Very interesting reading