Alias to Python Function

G’day all,

I have developed a python script with a number of similar functions for slightly different scenarios. Generally I create command aliases as such:

Procedure 2 -

  • Create a folder somewhere for all your Python scripts.
  • Open the Python script editor with the _EditPythonScript command
  • In Tools>Options>Files tab, set a path to the folder you just created, then OK. You can close the editor.
    (you only need to do the above once)

Now in Options>Aliases, you can create your aliases without the full path, just the name will work:

! _-RunPythonScript "TestScript"

(from here…Running Python scripts from aliases or toolbar buttons)

But I have not been able to find a way to reference a specific function with this method.

Is there a way to do this with aliases, or do I need to split up this script into separate py files for each function?

cheers,
Nick

Yes this is possible, for example:

import Rhino
def getFunctionOption():

    get_o = Rhino.Input.Custom.GetOption()
    get_o.SetCommandPrompt("choose option")
    option = 0 #default option
    functions = ['function1','function2','function3']
    functions_index = get_o.AddOptionList("function", functions, option)

    #accept Enter as an option
    get_o.AcceptNothing(True)

    while True:
        # perform the get operation. 
        get_rc = get_o.Get()
        if get_o.CommandResult()!= Rhino.Commands.Result.Success:
            return
            
        if get_rc==Rhino.Input.GetResult.Nothing:
            pass

        elif get_rc==Rhino.Input.GetResult.Option:
            
            if get_o.OptionIndex() == functions_index:
                option = get_o.Option().CurrentListOptionIndex
                
            continue
        
        break
    return option
print getFunctionOption()

btw: say you want to run the script with function 3 you can bypass the option question in your alias this way:

-_Runpythonscript (“D:\scripts\getoptionexample.py”) function=function3 enter

3 Likes

Hello, I do this like this, for instance with the annotate_visible function in Num_Points.py :

! -_RunPythonScript "import Num_Points as NP;NP.annotate_visible()"

2 Likes

So, I really like @Dancergraham suggestion, and it nearly works, but…

This is what I’m trying to implement…

This is the command alias…
!_-runpythonscript “import AllySections as AS;AS.FlatBar()”

And this is the error message…
image

This script works as expected, either through the alias or in the script editor, if I call the function specifically in the AllySection.py file (if name==“main”:…etc) , so the AllySection.ini is able to be located otherwise.

Does AllySection.ini also need to be imported?

Hmm strange… I wonder whether it is running a cached version of the script (to save execution time) from before when you fixed that bug.

Can you try this:

!_-runpythonscript “import AllySections;reload(AllySections);AllySections.FlatBar()”

Note that I am not using as here…

2 Likes

or it could be a problem with the location where you have saved the ini file - you could try putting it in the same folder as your current rhino file

https://developer.rhino3d.com/api/RhinoScriptSyntax/#collapse-FindFile

1 Like

There is something weird going on because if I run any of the other scripts, they still work, but the command line shows weird stuff happening…

Run your latest alias…

Command: naFlatBar
Command: _-runpythonscript
Python Script ( ResetEngine ): “import AllySections;reload(AllySections);AllySections.FlatBar()”

The reload suggestion resulted in the same error message as above where it couldn’t find the ini.

Then I run a previously functional alias…

Command: na12PL
Command: _-runpythonscript
Python Script <import AllySections;reload(AllySections);AllySections.FlatBar() “make12PL.py”
Please select surfaces to make 12PL:

So it works, but the command line shows feedback of the previous runpythonscript alias…weird, but I don’t think that is the problem.

I have a feeling this is a problem with GetSettings. It can’t be a problem with the Alias code because all the other aliases can find their .py files in the same location as the .ini file.

Saving the ini file to the 3dm file directory solves the problem…but creates another one.

I will just implement an options function similar to @Gijs solution, but cruftier.

Thank you for all the help.

1 Like

OK - you should be able to save the ini file in any of these locations if you prefer

  1. The current document’s folder.
  2. Folder’s specified in Options dialog, File tab.
  3. Rhino’s System folders
1 Like

Thank you @Dancergraham, this is it. We got there eventually and this was an education for me.

This is the command alias…
!_-runpythonscript “import AllySections as AS;AS.FlatBar()”

Options -> Files -> Search Paths -> add the same search path as was added in the Python Editor to enable the python script aliases from a common directory.

image

There is no need to save the ini file anywhere else, just keep it in the common directory with the custom python files. Black highlighted search paths are the same.