f-Strings won't work with RunPythonScript?

Never liked 'em anyways! I’ve been having a battle trying to get a string (with spaces) into a command alias. Found this luckily (Thanks Dale!!): GetString Doesn't Accept Empty Spaces - #11 by eric.bunn

Being able to use RunPythonScript saves so much time when you’re developing and testing code. It makes it feel closer to AutoLISP. The challenge however is passing a string into a Def’s argument. But I got it working!

Thought I’d post about the f-strings because it caught me off guard. The error message was correct but I couldn’t connect the dots (scripts were running just fine). Maybe this means there’s more Python 3 stuff that RunPythonScript won’t like? If it’s just f-strings however, that’s pretty easy to steer around.

Hi Keith,

Thanks for the heads up. But it’s the weekend and my brain is sleepy and I’m not getting it. Any chance of a short code snippet to illustrate how you are using an f-string that doesn’t work with RunPythonScript?

Thanks
Jeremy

f-strings work just fine:

Note that to use f-strings means using Python 3 means through RunPythonScript you must start code with #! python3 (or save as .py3).

You should probably start using ScriptEditor (the - hyphened version): Rhino scripting | Rhino 3-D modeling

This is what was actually going on. Thanks!!

And with the -ScriptEditor, I can pass a string thereafter?

I don’t know, haven’t tried. You could try, and if it doesn’t work you can poke @eirannejad

1 Like
#! python3
import Rhino


def GetString_Test_Command(test_string):
    print(test_string)

def test_get_literal_string(prompt):

    test_string = "test string"

    test_string_2 = f"{test_string} version 2"

    print(test_string_2)

    gs = Rhino.Input.Custom.GetString()
    gs.SetCommandPrompt(prompt)
    result = gs.GetLiteralString()
    if (result == Rhino.Input.GetResult.String):
        return gs.StringResult().strip()
    return None

if __name__ == "__main__":
    user_input = test_get_literal_string("Enter String to test: ")
    if user_input: print("User Input: " + user_input)

And then you’d just put this into a command alias: “_-RunPythonScript” “<filepath.py>” “Your String”

I’ll definitely check it out later if nobody else does. Faster you figure?