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?
Hi @clement, thanks for your reply.
That doesn’t seem to work, no . 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.
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
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.
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”:
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: