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