GHPython: PolyCurve is not iterable

Hi friends!

I am learning how to write code in GHPython.
I am trying to iterate over a data tree collecting curves, find periodic curves, close them within the tree, return the tree with all closed curves.

I am getting an error: PolyCurve is not iterable

The input is Tree Access, No Type Hint (tried curve as well).

What am I doing wrong?

Thank you!

Rhino 6, Windows 10.ghpython_err.gh (493.2 KB)

Printing out crv after the conversion to list shows that it’s just one PolyCurve in a list. I don’t know why, possibly a bug in treehelpers @piac ? :face_with_monocle:

As a workaround you could loop directly over the tree without converting it, like so:

import rhinoscriptsyntax as rs

a = []

for i, branch in enumerate(crv.Branches):

    for j, item in enumerate(branch):
        
        if rs.IsCurveClosed(item):
            bool = "closed"
            a.append(item)
        else:
            bool = "periodic"
            pts = list(rs.CurveEditPoints(item))
            pts.append(pts[0])
            a.append(rs.AddInterpCurve(pts,1,2))

Note, that you have to set typehint to ghdoc if you want to use rhinoscriptsyntax!

Hope that helps :slight_smile:

1 Like

This is normal when using treehelpers with simplified trees (no common root branch).

See explanation here:

And here:
https://gist.github.com/piac/ef91ac83cb5ee92a1294#gistcomment-3763417

Original code works with these changes (and changing input type hint to ghdoc as @efestwin pointed out):

import ghpythonlib.treehelpers as th
import rhinoscriptsyntax as rs

crv = th.tree_to_list(crv, lambda crv: crv)

a = []

for i,branch in enumerate(crv):
    
    for j,item in enumerate(branch):
        if rs.IsCurveClosed(item):
            bool = "closed"
            a.append(item)
        else:
            bool = "periodic"
            pts = list(rs.CurveEditPoints(item))
            pts.append(pts[0])
            a.append(rs.AddInterpCurve(pts,1,2))

ghpython_err_210912a.gh (493.1 KB)

-Kevin

2 Likes

Aaah yeah, this makes sense and I completely forgot it since I prefer working directly with trees. :bulb:

Thank you for the refresher! Very much appreciated :slightly_smiling_face: