Trying to use "R=∞" as string in python but get error

Hi, I struggle with using infinity symbol in python.
I can paste/type it directly into a text object in rhino with out problems, but python seems to struggle with it, does anybody know a workaround?

I explode a curve and checks the parts and then adds text that I later on move into position.
(Right now I use two o’s as infinity)

        partradius = rs.CurveRadius(parts[i],midpoint)
        
        # Add text
        # If curve segment is straight then CurveRadius return None
        if partradius:
            text = rs.AddText("R="+str(int(round(partradius,0))), (0,0,0), 2.25, 'Verdana', 0, 2)
            
        else:
            text = rs.AddText("R=oo", (0,0,0), 2.25, 'Verdana', 0, 2)

Is this a unicode thing, like replace your ‘‘00’’ with \u221e?

Yes, I guess, but when I try that I get “8” as the result.

I think it is a limitation with ironpython 2.7 maybe. So if there is a workaround i would like to know :slight_smile:

You could also just use inf, which is a pretty normal thing to use at least in the programming world. In Python 2.7 (IronPython) you say for instance float('inf') :

Since Python 3.5 there is math.inf, which can be used with _ScriptEditor Python 3.

print(u'\u221e')

1 Like

Right, u-strings. I think I’d prefer inf because it is easier to read IMO:

1 Like

I wonder if this is a locale thing where the default locale is using ascii encoding? Running the command print("∞") in the Rhino 7 python editor throws an ascii error. If you import the locale module and set the locale to use UTF-8 maybe you can get further (I haven’t tested this).

That command runs fine in Rhino 8 with Python 3 by the way.

EDIT: Unfortunately you cannot set the locale to use UTF8 in Iron Python 2.7 - possibly because it predates the availability of that feature in Windows 10 onward. Python 3 works fine.

And using repr() to print the character in 2.7 gives you the unicode string, not the glyph. The unicode is recognised and can be interpreted correctly, just not displayed as the glyph.

Here is the difference between Rhino 7 and 8:

Thanks for the help guys, the reason I need it is that we are working on road drawings and there the correct way is to use the infinity symbol and oo works, but isn’t correct.

And as you can see if I use u221e I get ‘8’ as a result:

So I guess I have to manually replace it in Rhino 7.

FWIW, the IronPython component in R8 grasshopper (using IronPython 2.7.12) seems to handle ∞ just fine. I don’t have access to R7 to test anything there.

1 Like

Thanks for trying and nice to know, even though I am not using GH for this.

If you just need to draw the symbol in text on the canvas, rather than work with it within your program, that does work from Iron Python in R7. e.g.:

WritingInfinity.py (626 Bytes)

HTH
Jeremy

Addendum: Here’s an example using RhinoScriptSyntax instead of RhinoCommon - note that the two AddText commands have different syntax.
WritingInfinityWithRS.py (164 Bytes)

2 Likes

Dunno, this works here in Rhino V7 EditScript script editor:

print('R='+u'\u221e')

1 Like

Hm… but you are on mac. And I have a norwegian layout… I’ll investigate!

I also get 8 here in the states on Windows in Rhino7 with print(u"\u221E"). However, the rhinoscriptsyntax solution @jeremy5 gave works great for me.

Me, on Mac ? Are you kidding ? :stuck_out_tongue_winking_eye:

The above was V7 on Windows, but yes, OS running in English.

Note that I put the R= outside the u-string…

V7 with rs.AdddText()

But it also works here with the R= inside the u-string.

Yeah, we’ve established that rs.AddText() will write unicode on the canvas. But in your post above you show print() placing the Infinity glyph on the Python Editor Output panel. Nobody else has done that yet (and the R= ouside the u-string makes no difference), so I’m interested in what is different about your setup. One possibility is locale as you are in Switzerland. What does this Python code output:
import locale as l
print(l.getdefaultlocale())
?

1 Like

Sure - here you go:
('en_CH', 'cp0')

Yeah… what can I say… it looked like it after a whiskey, when I was looking at discourse on the phone in my bed… :smiley:

But you are all correct, adding \u221e works in the text object.
Thanks for pointing that out!

Just for the record: I get:
(‘en_US’, ‘cp1252’)

OK, it isn’t the obvious locale setting (en_CH isn’t recognised as a valid locale in my Windows, and isn’t supported in Python 2.7, so I can’t set my locale to that), but I was able to replicate @Helvetosaur’s result in the Output panel by changing a well-hidden windows setting. Although it isn’t necessary for writing Unicode to the Rhino canvas, here for interest is how to do it for the Output panel, in Windows 10.

  1. Go to Language settings and select Administrative language settings

  2. From the Region Administrative tab select Change system locale…

  3. From the Region settings tick the Beta:Use Unicode UTF-8 for worldwide language support box

  4. Reboot

You should now be able to see Unicode in the Output panel. But this is a system-wide setting so there is the possibility you will break something elsewhere…

Regards
Jeremy

1 Like