Insert Carriage Return for python text string on Windows

I have a script that prints a text string. When I run the script on Rhino for Mac all is correct for layout when I view the text in the properties panel.

At first I thought it was a carriage return issue between Mac & Windows.

When I open the file in Rhino for Windows, with the text from the script run on a Mac, it look like this in text properties.

So the above was from the script run in Rhino for Mac.

If I run the same script in Windows, I get the same thing in the text properties panel.

This makes it hard to edit any of the text in Windows, quickly.

SO the question is, can I add a carriage return to the script to make the Windows text look like the Mac text in the properties panel?

Text portion of code …

point = rs.GetPoint("Point for text insertion")
if point:
    tObjs=[]
    #create your text string
textString='''Calculated {mType} Weight:
{grams} grams in Rhino
{finished} grams finished

Price for silver is ${cost1} US,
based on 51¢ per gram.

{text}
Designer: {designer}
Rhino: {fileBy}
Factory: {build_for}
© JewelPop Inc. {now}
'''
context = {
        "mType":mType,
        "grams":grams,
		"finished":finished,
		"cost1":cost1,
		"text":text,
		"designer":designer,
		"fileBy":fileBy,
		"build_for":build_for,
		"now":now,
}

tObjs.append(rs.AddText(textString.format(**context),point,tHt))

Thanks
«Randy

@lowell, isn’t this just an issue with the text panel?

Hi Randy,

I usually set things up with a single long string and add \n for each carriage return. This works fine in text blocks. However, it looks like the Object Properties panel in Windows Rhino does not recognize \n as a carriage return. As a workaround, I found that using \r\n does seem to work on Windows though - as well as in MacRhino - example below…

–Mitch

import rhinoscriptsyntax as rs
import datetime

mType="Unobtanium"
grams="10"
finished="9.9"
cost1="50,000,000"
designer="Nobody"
fileBy="Someone"
build_for="Client"

my_str="Calculated {} Weight\r\n".format(mType)
my_str+="{} grams in Rhino\r\n".format(grams)
my_str+="{} grams finished\r\n".format(finished)
my_str+="Price for {} is ${} US\r\n\r\n".format(mType,cost1)
my_str+="Designer: {}\r\n".format(designer)
my_str+="Rhino: {}\r\n".format(fileBy)
my_str+="Factory: {}\r\n".format(build_for)
my_str+="Copyright JewelPop Inc. {}".format(datetime.date.today())

tObjs=[]
point=rs.coerce3dpoint([0,0,0])
tObjs.append(rs.AddText(my_str,point,5))
1 Like

Thanks Mitch,

I will give that a shot in a minute.

«Randy

Thanks Mitch, working here

@Helvetosaur, is there a way to round the output in the TextStrings?

I have round in the variable, but it is not coming out that way.

Here is script - screenshot

Here is output

Found it … format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]

example line - textString+="{:.4} grams finished\r\n".format(finished)

Thanks, «Randy

You can round and specify the number of digits within the format statement - I like this feature very much. To do so, instead of just having a blank {}, you can put in formatting codes.

For example to limit the output to a specified number of places past the decimal (and round) it might look like this:

x=2.03567
print "{:.0f}".format(x)
print "{:.1f}".format(x)
print "{:.2f}".format(x)
print "{:.3f}".format(x)
print "{:.4f}".format(x)

>>> 2 2.0 2.04 2.036 2.0357
Have a look at the Python online docs on string formatting with the format statement - there are a lot of possibilities…

–Mitch

1 Like

Thanks again Mitch,

I did find it see my edit above. Just going back so I can get the dollar symbol in the format, way cool.

«Randy