Import CSV to different layers

If I have a CSV file in a format like:

“SECTION NAME”
x, y, z

“ANOTHER SECTION NAME”
x, y, z

Would there be any way to import the points so that each section gets its own layer?

Should be easy to do with Data Trees and a programmatic Baker like Human or EleFront.

Hello - no error checking or anything fancy but here’s a start (python):

import Rhino
import rhinoscriptsyntax as rs


def test():
    
    
    file = rs.OpenFileName(filter = "CSV files|*.csv|Text files|*.txt||", extension = ".csv")
    if not file: return
    
    with open(file) as f:
        content = f.readlines()
    layer = rs.CurrentLayer()
    for line in content:
        
        if not line[0].isnumeric():
            layer = rs.AddLayer(line)
        else:
            x = rs.AddPoint(rs.Str2Pt(line))
            if x: rs.ObjectLayer(x, layer)
    
test()

-Pascal

Thanks, I’ll try it.

That brings me to another question. Where did all the scripts I had in Rhino 5 go?

If I do EditPythonScript

  1. When I close, there is no option to discard the file. However, Cancel discards when one would expect to just return to the editor without change.

  2. How do I debug? The DEBUG>STEP command is grayed out.

  3. I get this error when I run the script “Message: ModelComponent.set_Id failed. The component is likely locked”

Probably because the script didn’t close the .csv file, so it’s locked… The script needs to include a

f.close() at the end to close the file after reading.

You need to set a breakpoint in order to debug. Once that’s set the script will automatically stop at the bereakpoint(s).

To me it looks like the python editor on mac is still in its infancy. I couldn’t get it to work either, because I could not find anything that enables me to add a breakpoint. Also there is no autocomplete like in Windows (named intelliClunk by Steve because it is said to be buggy, but I haven’t ran into too many issues and it helps a lot)

Ah, Mac… sorry, didn’t read the subhead. Yep, the Python editor is in a very primitive state on Mac currently.

I don’t see a way to add a breakpoint either.

The script above is a good starting point but I need to fill in the gaps.

The with block automatically closes the file on exit, even if there is an exception inside the block. It is good practice to use this rather than explicitly closing the file.

3 Likes

This is my first python programming but methinks the error is that the layer is locked.

This line appears to cause the problem]

        layer = rs.AddLayer(line)

better post your whole script, because an error is often caused by code that is in front of the line that throws the error

AddLayer() will fail if the layer already exists…

if not IsLayer(layername): rs.AddLayer(layername)

1 Like

Here’s what I have that kinda sorta works:

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 
    for line in reader:
        value = line[0].strip () ;
        try:
            float (value)
            x = rs.AddPoint(rs.Str2Pt(line))
            if x: rs.ObjectLayer(x, layer)
            
        except ValueError:
            color = rs.CreateColor (random.randint (0, 255), random.randint (0, 255), random.randint (0, 255))
            layer = rs.AddLayer(value, color=color, parent=parentlayer)

importtolayers()

I am trying to create sublayers of the current layer. For reasons that were not apparent to me, the first 16 layers get created at the root and all the remaining layers were created as sublayers of the current layer.

I restarted rhino and things worked as I expected.

hm. ‘with open(file)’ is supposed to take care of the cleanup as far as I know… at least so I thought.

-Pascal

Yup. Technically known as a context manager

I know nothing about Python and fumbled through using my knowledge of other languages. Seeing this and the bizarre behavior I saw during debugging (e.g., the first sixteen layers going to the top and the rest going into the desired parent layer) I wonder if some kind of Content Manager should be used in a Rhino script to ensure Rhino cleans everything up of something goes FUBAR.

Undo generally undoes any single rhinoscript result.

and that’s another (severe) issue regarding the python editor on the Mac. It currently doesn’t work with undo!

Really? So if you launch a script via the script editor, Undo doesn’t undo the result? Ouch.

Well, current conditions look like they make it very difficult to develop scripts on Mac Rhino - personally I wouldn’t even bother trying…

That script I posted above does not undo.