Python "PlaneCurveIntersection" - index out of range" Error

Hello all,

I’m writing a script where I need to find the intersection of a series of created section curves with the centreline (World XZ plane)… I’ve got the folllowing code, but get an error for “index out of range: 1”.


def curve_CL(curves):
    points = []
    plane = rs.WorldZXPlane()
    for crv in curves:
        int = rs.PlaneCurveIntersection(plane,crv)
        if not int:
            print "Error with plane-curve intersection"
        points.append(int[1])
    return points

I can see why this is - because the resulting point that I’m after is hidden inside a tuple, which itself is inside a list (see below from debugging)… but I can’t understand why this is… the help file info for the PlaneCurveIntersection gives the return as just a list, where the point should be at index [1].

I’m sure this is a really rookie error, but any help would be much appreciated - it’s driving me mad trying to find out where the problem lies.

In your case, looks like your check function is simply missing an “else” and an indent.

def curve_CL(curves):
    points = []
    plane = rs.WorldZXPlane()
    for crv in curves:
        int = rs.PlaneCurveIntersection(plane,crv)
        if int:
            points.append(int[1])
        else:            
            print "Error with plane-curve intersection"
    return points

–Mitch

Thanks, Mitch.

Yes, that was a bit of code that I added to try and see what was happening, but your revision doesn’t change the outcome of the code… I still get the “index out of range” error.

The really annoying bit is that I can see where the error is coming from, but not how to avoid it.

Oh, sorry, spaced that one - the intersection function could return several intersection events, thus it’s a tuple of lists. So you need to iterate through the various possibly returned intersection events:

def curve_CL(curves):
    points = []
    plane = rs.WorldZXPlane()
    for crv in curves:
        int = rs.PlaneCurveIntersection(plane,crv)
        if int:
            for i in range(len(int)):
                points.append(int[i][1])
        else:            
            print "Error with plane-curve intersection"
    return points

–Mitch

Brilliant! That’s sorted it. Thanks for the help!

Now that the problem is sorted you should also call your intersection variable something else than int, since your current code hides the builtin numerical type int (:

1 Like