IronPython losing connection to scriptcontext.doc

I have a simple Python script that is causing an error Grasshopper. I can get the same script to work by moving the line generating the error out the loop.

This works:

import rhinoscriptsyntax as rs

all_pts = [ ]
for j in range(30):
    if j %2 == 0 :
        pts = rs.AddPoint(j , 4, 0)
    else :
        pts = rs.AddPoint(j, 10 , 0)
    all_pts.append(pts)
pl = rs.AddPolyline(all_pts)

This does not work:

import rhinoscriptsyntax as rs

all_pts = []
for j in range(30):
    if j %2 == 0 :
        pts = rs.AddPoint(j , 4, 0)
    else :
        pts = rs.AddPoint(j, 10 , 0)
    all_pts.append(pts)
    pl = rs.AddPolyline(all_pts)

The error has the following message:

Runtime error (Python Exception): Unable to add polyline to document

Traceback:
line 563, in AddPolyline,
“C:\Users\graha\AppData\Roaming\McNeel\Rhinoceros\7.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\curve.py” line 10, in script

I believe that the error case should be a valid Python script, even though it’s less efficient than the working case. In any case, I don’t believe that moving line 10 should an error.

I have attached the two versions of the Grasshopper scripts, as well as my Rhino installation information.

AddPolyline_Working.gh (2.8 KB)

AddPolyline_Error.gh (2.8 KB)

Rhino System Information.txt (2.7 KB)

Hi @graham_best,

In your second script, you are trying to add a polyline with a single point, which is invalid.

Watch your indenting as it defines scope - a Python thing.

— Dale

Yes, you’re absolutely correct. I missed that.

Thanks for the quick reply