Call is ambiguous... Curve.TryGetPolyline(...)

Dear all,

i am trying to re-write a script from ironpython for python3:

obj = rs.GetObject()

if rs.IsPolyline(obj):
    points = rs.PolylineVertices(obj)
    if points:
        for k, point in enumerate(points, start=0):
            x = str(point[0])
            y = str(point[1])
            z = str(point[2])

When i run the script i get this error:

grafik

In ironpython this script will just work fine. Looks to me that something is going wrong with the Curve.TryGetPolyline method (rhino3d.com) or the rhinoscriptsyntax, or do i miss something?
Does anyone have an idea how i can work around this?

Best Peter

i am trying to re-write a script from ironpython for python3:

If it ain’t broke…

Does anyone have an idea how i can work around this?

If there is a good reason to risk introducing new bugs into what you’ve already got, and to overcomplicate your workflow, e.g. a speed up or a cPython only library etc. is required, then I would use Grasshopper and restructure the code into two different components that both do what they’re good at, and e.g. pass a simple list of the x, y and zs between them, so that only whatever you need the co-ordinate list for happens in a Python 3 component (which could even be less efficient, if it has to deal with Rhino .Net objects), and a good old Iron Python component, that can still do what it’s great at, and is tried and tested for - working with .Net and the Rhino and Grasshopper APIs.

Even if you simply want to retire your Iron Python code, there’s even bound to be a native Grasshopper component that extracts the numerical co-ordinates from a polyline.

I don’t think he is coding in Grasshopper.

I suspected so too - I should’ve made it clearer that part of my advice was to switch to Grasshopper.

Has RhinoPython in Rhino 8 got no option to use the established well tested IronPython 2, or was IronPython actually deprecated there?

Hi @peter.zock,

The problem is that there are two versions of Curve.TryGetPolyline. And Python3 is confused as to which one to use.

@eirannejad - is this something you can help with?

@peter.zock

Try creating a StrongBox and passing that as the argument. The return value will be placed inside the box:

import clr

box = clr.StrongBox[Rhino.Geometry.Polyline]()
curve.TryGetPolyLine(box)

# this has the PolyLine value
box.Value
1 Like

You can still run IronPython 2.7 files in V8 by putting #! python 2 at the top of the script. You can force Python 3 by putting #! python 3 at the top.

I have a lot of existing Py2 scripts called by aliases/toolbar buttons that do not have #! python 2 at the top, and they run, so I have a feeling Py2 is run by default.

1 Like

@eirannejad

However, he is using a rhinoscriptsyntax method, not RhinoCommon directly, and it is failing because of this. Is this going to be a general problem? Most RhinoCommon methods are overloaded, if this happens frequently then it will invalidate a lot of rhinoscriptsyntax methods…

I expected the rhinoscriptsyntax to be updated and compatible with pypthon3. From what i have gasped, python3 is supposed to be the future in Rhino since it offers a lot of benefits and maintained external libraries.

@James_Parrott - Just to make clear, the code is just a sample so that anyone can run the script on his own. My original script is much larger then this. I am not just copying the script from 2.7 to 3, but also modify some of the script. The main reason for choosing python3 is that i want to use the pip installer and bridge the new editor with VisualStudioCode.

For now I will try to circumnavigate the problem by using RhinoCommon directly in my script. I hope the suggested Strongbox from @eirannejad will work. Still, I feel this kind of problem should be adressed.

@Helvetosaur

You can still run IronPython 2.7 files in V8 by putting #! python 2 at the top of the script. You can force Python 3 by putting #! python 3 at the top.

I have tried to run the script with #! pyton 2 at the top, but this raises the some error as well

Please post them or send them directly to @eirannejad . There may still be some unresolved bugs concerning getting code to run specifically in Py2 or Py3.

Just for the record. This function suggested by @eirannejad works for now:

from rhinoscript import utility as rhutil
import clr
import Rhino

def PolylineVertices(curve_id, segment_index=-1):
    box = clr.StrongBox[Rhino.Geometry.Polyline]()
    curve = rhutil.coercecurve(curve_id, segment_index, True)
    curve.TryGetPolyline(box)
    if box: return [pt for pt in box.Value]

That’s a workaround, but it isn’t the solution to the problem…

Yep.

https://mcneel.myjetbrains.com/youtrack/issue/RH-80838

– Dale

1 Like

I agree. IronPython behaved differently and automatically chooses the overload with least number of out parameters. I modified Python 3 method resolver to do the same.

The fix should be in Rhino 8.5 next week. The ticket below has more info documented on this issue:

RH-80838 rs.PolylineVertices needs StrongBox | Pythonnet to use overloads with least out params

1 Like

Thanks for the quick respond, appreciated!

1 Like

Anytime!

RH-80838 is fixed in Rhino 8 Service Release 5