Layers in Python Scripting

This is a script that someone kindly provided an outline for me and I have have cleaned up—that still needs work. The objective of the script is to import a CSV file containing layer names. It is supposed to read the file. If it finds a string, it is to create a new layer below the current layer. If finds a value, it is to add a point in the last created layer.

The problem is that the script creates new layers in a mixture of locations. It seems to create some layers below the current layer and some layers at the root.

Why would that be?

I think I have a lack of understanding on how layers work the python interface. The CurrentLayer() function returns a string. How can I distinguish between "Abc"s with a layer structure like this:

Layer 1
Layer 2


import Rhino
import csv
import rhinoscriptsyntax as rs
import random

def importtolayers():

    file = rs.OpenFileName(filter = "CSV files|*.csv|Text files|*.txt||", extension = ".csv")
    with open(file, "rU") as f:
        reader = csv.reader (f, dialect=csv.excel)
        parentlayer = rs.CurrentLayer ()
        layer = parentlayer
        print "Root Layer: ", parentlayer
        for line in reader:
            value = line[0].strip () ;
                if value <> "":
                    float (value)
                    x = rs.AddPoint(rs.Str2Pt(line))
                    if x: rs.ObjectLayer(x, layer)
            except ValueError:
                print "New Layer ", value ;
                color = rs.CreateColor (random.randint (0, 255), random.randint (0, 255), random.randint (0, 255))
                layer = rs.AddLayer(value, color=color, parent=parentlayer)

Hi @miano,

Can you post a sample .csv file?


– Dale

Longl #4 (2.6 KB)

Here is one.


Those are "Layer 1::Abc" and "Layer 2::Abc" respectively


Are there functions for getting the absolute path for the layers so that I can specify the full path unambiguously?

But am increasingly thinking that this is not the problem.

I restarted Rhino and create a file:

I run my script and I get this:

The first 17 layers were created at the root while the rest were created below the current layer (Layer 01).

I delete that file and create a new one without restarting Rhino.

And I run the script again.

This time all the layers were created below Layer 01.

The internal state of Rhino appears to have an effect.