How to enter spacebar into a command in python

I’m trying to automate the Text object command using python and am running into 2 issues which I think are related to the same problem. I don’t know how to enter a spacebar with the commandline options. In the example below I’m trying to enter “Hello font” as the text for the textobject and I want to set the font to “American typewriter” (a font with a spacebar in it). This is what I have which obviously doesn’t work. Is this not possible perhaps?

import rhinoscriptsyntax as rs

txt = “Hello font”

commStr="-_TextObject "
commStr+="_Font “+“American_Typewriter”+” "
commStr+=""+txt+""

rs.Command(commStr, True)

This is what I’m in the above example am trying to get for positioning

Hi @siemen, i do not have the right font, does below work any better ?

import rhinoscriptsyntax as rs

txt = "Hello font"

commStr = "-_TextObject "
commStr += "_Font " + "American_Typewriter "
commStr += chr(34) + txt + chr(34)

rs.Command(commStr, True)

_
c.

Hi @clement, thanks for your reply.
That doesn’t seem to work, no :confused:. To be honest the font can be any font that is made up of multiple words. I just took american typewriter because I thought everyone had it but it can be Times new roman as well.

How about this:

import rhinoscriptsyntax as rs

txt = 'Hello font'

commStr = '-_TextObject _Font "American Typewriter" "{}"'

rs.Command(commStr.format(txt), True)
1 Like

Yes! Genius. Again.

Do I get it correctly if I say that “{}” is where txt will be positioned because of the “format”-thing, according to your solution?

Yes. The .format() statement is one of my favorite things in Python for putting together command or message strings. The {} are replaced with the arguments inside the () in order (you can have as many as you want).

The nice thing is it also takes care of converting the data into the proper string form:

import Rhino
pt=Rhino.Geometry.Point3d(1,2,3)
print "I have {} point at location {}".format(1,pt)
>>> I have 1 point at location 1,2,3

It also supports conditionals:

import Rhino
pt=Rhino.Geometry.Point3d(1,2,3)
n=1
print "I have {} point{} at location {}".format(n,"" if n==1 else "s",pt)
>>> I have 1 point at location 1,2,3
import Rhino
pt=Rhino.Geometry.Point3d(1,2,3)
n=2
print "I have {} point{} at location {}".format(n,"" if n==1 else "s",pt)
>>> I have 2 points at location 1,2,3

–Mitch

1 Like

Ah. I now understand, it was slightly confusing since you where wrtiting “American_Typewriter” without a space using an underscore instead, in your opening post. You can set any text into “coded quotes” when passing it to the commandline to prevent Rhino from interpreting the space as Enter. I did this with your “Hello font” text in the txt variable which was the only one having a space in it.

_
c.

1 Like

Aha, I see, interesting! And with coded quotes you mean the “chr(34)” thing you did?

Yep, chr(34) is ASCII code for the double-quote mark.

My trick in Python is that you can use both single quotes and double quotes, so you can enclose a set of double quotes inside a single-quoted string (without using chr(34). Probably only works in Python, but it’s a neat trick.

Yes.And i think that the format string which Mitch suggested does not control this, nor can it work. If you look at his first example he is adding extra quotes inside quoted string by mixing single and double quotes:

While this makes it slightly less readable, it only makes the quotes around the txt string, but requires another set of quotes around the string “American Typewriter”.

What i did to make Rhino interpret the quotes as coded quotes is just surrounding the string with the ASCII code 34 for quotes. This works with the string of your txt variable and can be done for the variable font_name in below example. It works for variables as well as concatenated strings containing spaces like “American Typewriter” or “Times New Roman”:

import rhinoscriptsyntax as rs

txt = "Hello font"
font_name = "Times New Roman"

commStr = "-_TextObject "
commStr += "_Font " 
commStr += chr(34) + font_name + chr(34) + " "
commStr += chr(34) + txt + chr(34)

rs.Command(commStr, True)

I personally prefer readability and compatibility, but everyone can decide what fits best.

Yes, it fails in VB script while using chr(34) works the same in both languages.
_
c.

1 Like

Dunno, this worked fine here:

import rhinoscriptsyntax as rs
txt = 'Hello font'
commStr = '-_TextObject _Font "Times New Roman" "{}"'
rs.Command(commStr.format(txt), True)

Asked me for an insert point and then inserted Hello font at that point…

This also works here:

import rhinoscriptsyntax as rs
txt = 'Hello font'
font='Times New Roman'
commStr = '-_TextObject _Font "{}" "{}"'
rs.Command(commStr.format(font,txt), True)

Yes, it works because you are adding quotes inside a quoted string. It is technically the same as when using chr(34). Without it, you’re preventing an error because you’re mixing two types of quotes. This would also work without using format.

And to make the confusion complete, instead of mixing two types of quotes, you could also use tripple quotes of the same type. It has been suggested numerous times before, but i will repeat it, if someone does not want to mess with quotes, writing a simple function can help:

def Quote(text):
    return chr(34) + text + chr(34)

_
c.

1 Like

Thanks for the thorough explanation @clement