Running Python scripts from aliases or toolbar buttons

First, here is some basic info from the Rhino developer docs.

Following are some more detailed instructions. They are platform-specific.

For Windows Rhino users:

To run a python script (*.py) from a toolbar button:
Right-click in a blank space in any toolbar and choose “New Button”.
In the button editor that opens, in the "Command area, type the following:

! _-RunPythonScript (

<paste the entire script here>

)

Then OK to exit the button editor. When you click the button, the script should run. Make sure to save your workspace file.

To run a Python script using an alias:

  • First, save the script somewhere known.

Procedure 1 -

  • In Options>Aliases, create a new alias with the following:

! _-RunPythonScript "<full path to your script including extension>"

(Path example: "C:\Users\<username>\Documents\Myscripts\TestScript.py")

Your script should then run when you type the alias and Enter.

-or-

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"

Note that the above two procedures for running scripts with aliases can also be done from toolbar buttons, just paste what you would for the alias into a new toolbar button.


For Mac Rhino users:

To create new toolbar buttons is different than in Windows Rhino, it’s a rather more involved procedure that can be found here. So I will only cover aliases below.

The alias procedure #1 outlined above for Windows Rhino should also work on Mac Rhino:

  • In Preferences>Aliases, create a new alias with the following:

! _-RunPythonScript "<full path to your script including extension>"

(Path example: "C:\Users\<username>\Documents\Myscripts\TestScript.py")

  • Your script should then run when you type the alias and Enter.

On the Mac, there is also “default” scripts folder, which is a good location for storing all your scripts. Unfortunately, that folder is hidden away in your user library - about the same way it is in Windows. To get to it, you need to press the “Alt” key while pulling down the “Go” menu - then you will see “Library” entry.

For Rhino 5, navigate to: /Library/Application/Support/McNeel/Rhinoceros/Scripts and drop your script files in there.

For Mac Rhino 6.0 and later, the version number has been added to the folder path like in Windows. Each version therefore has its own folder. The full path will be

~/Library/Application Support/McNeel/Rhinoceros/6.0/Scripts (or 7.0, etc.)

Then, to run your script via alias, you can simply add your alias and use:

! _-RunPythonScript Scriptname

where Scriptname is simply the script file’s name. Neither the full path nor the extension is necessary, as Mac Rhino knows where to look)

As in Windows, typing the alias and Enter will then run the script.

Note that for both platforms, it is also possible to run scripts from keyboard shortcuts with a similar setup.


HTH, --Mitch

23 Likes

helpful, cheers :slight_smile:

Hey!

I have followed this procedure to make 3 scripts to sort objects in specific layers. The scripts and aliases worked fine the last time I used them but now Rhino returns “Python Script <Makelayer_Epais.py> ( ResetEngine )Makelayer_Epais.py” when I run my aliases and does (apparently) nothing.
I have checked and when I run the scripts manually they do work.

The only thing I can think of which might have an impact on this is that there has been a Rhino update in between uses.

Side note : I also use your python script to change object colors (MakeColor) and these ones have the same issue.

Any idea what’s wrong?

Many thanks!

Cécile

Hi Cécile - what macro does your alias run?
-wim

Hi!

This is the macro that my alias runs : !_-RunPythonScript “Makelayer_Fin.py”

I also have a version without the “” around the file name.

Thanks

Cécile

Hi - have you tried to include the entire file path?
! _-RunPythonScript "C:\[full path to your script]\Makelayer_Fin.py"
-wim

1 Like

Hi

I tried by including the entire file path (which I think I wrote correctly). But it still doesn’t work.
Here are the lines I get in the history (Make3 is my alias) :

Command: Make3
Command: _-RunPythonScript
Python Script </Users/cecilezaugg/Library/Application\Support/McNeel/Rhinoceros/Scripts/Makelayer_Cache.py> ( ResetEngine )"/Users/cecilezaugg/Library/Application/Support/McNeel/Rhinoceros/Scripts/Makelayer_Cache.py"

  • Cécile

edit : I forgot to say that I’m working on a Mac.

Hi -

That complicates things : )
I hardly use Mac but this doesn’t look good to me:
image

What if you copy the path to the script in Finder like this:
image
(that is, make sure that the entire path is shown at the status bar in Finder and then right-click and copy the full path). Then paste that path in the Alias editor in Rhino.
-wim

Hi all, I am having a very strange problem with this
! _-RunPythonScript “C:/Users/augusto/Documents/projects/tests/topology.py”

it is loading the external script but when I modify it in the py file, it does not get updated inside rhino.
Should I need something like reload(module)?

Hello,

Yes I import then reload the module or execute it from the editor in this situation. It seems to cache the last executed version

Graham

Very helpful. Thank you.