use python script as a rhino command?
Hi, i would like to know how can i convert a rhinopython script into a rhino command, i wouuld like to execute rhino.ptyhon script from the command line directly and not execute RunPythonScript command and then load the .py file.
Is it possible to call python functions from the command line?
Yes. Two methods work here:
1) Embed your script in a toolbar button : this is easy and relatively portable
Create a new button and shift+click on it to open the editor.
! _-RunPythonScript (
>>>paste your entire script in here<<<
Then just run the script from the toolbar button as you would any other command
2) Create a keyboard alias for the script:
a) Store your script in some specific folder, i.e.
b) In Rhino Options>Aliases, create a new alias like "DoThis" and in the right hand box enter the following:
! _-RunPythonScript "FilePath\ScriptFileName" (the quotes are important if you have spaces)
(In this case the whole thing would be "C:\Users\Username\.......\PythonScripts\DoThi s")
c) Now, when you type the alias, the script will run. Beware, the first time you do this in a Rhino session, the Python interpreter has to initialize, it takes a couple of seconds.
Mitch when you say create a new button , which button?
Heres i have hundreds of button with predefined functions to add to the tool bar, which one should i choose?
im on macosx rhino
Ohh... Mac... I don't think you can add toolbar buttons yet as you can in Windows. I'm not sure how the alias thing works either on the mac, given the file path thing is really a Windows procedure... Probably better ask in the RhinoMac forum. --Mitch
Ive just tried in a windows machine and its almost the same, when i want to edit the toolbar i get a set of predefined buttons... which one should i choose in order to add the "RunPythonScript " command?
Originally Posted by Mitch
In Windows, it's fairly easy. The main thing to check is that you have only one instance of Rhino running when you do this.
The toolbar you want to add the button to needs to be undocked, so if it's docked, just drag it out into the window temporarily (you can redock it later - also make sure "Lock docked toolbars" is unchecked in the Tools menu). With the toolbar undocked, *right* click on the toolbar tab (the part with the name) and choose "add toolbar button". A new button will be added and the editor will automatically open.
In the lower left big box - which represents the code that will be executed when you click the toolbar button with the left mouse button - paste your script as per above with the ! _-RunPythonScript ( at the top and a close parentheses at the bottom below the script. The lower right hand box represents the code for the right mouse button, so if you want to have a RMB function, you can do something in there.
While you're in the editor, you can also type in a tooltip, add an image to the button and/or give the button a text title (you may need to uncheck "get appearance style from tab").
When done, hit OK to close and accept changes, you may need to resize the toolbar to see the new button you added. You can then redock the toolbar if you had undocked it. For the Rhino 5 Beta, as we get a new one each week and it overwrites the default workspace, you want to save your workspace under a different name, as otherwise all your changes will be lost when the next Beta is installed.
Go to Tools>Toolbar Layout and from the file menu, choose "SaveAs" and give it a new name - this will make sure your changes are saved to your new "personal" workspace file.
thanks for the explanation about how to create a button with a phyton scritpt, its super cool !
I have another question: there's any way I can use for pack everything and let people download my button without the need for them of setting up everything... in other words like a plug-in (even if I'm aware they are not the same) ...
You can with certain limitations. The easiest way is to create a new toolbar in Rhino (5) in your normal workspace file and put a copy of your scripted button(s) in it. Just create the new toolbar and Ctrl+left mouse button to drag the original button(s) to the new toolbar (that will copy it/them there). Then, from Tools>Toolbars... with the workspace highlighted in the upper window, in the File menu, select Export groups (a group in this case will be your new toolbar). Give the file you are going to export a name and OK, then find the name of your new toolbar in the list in the "Select groups to export" window that pops up, check it and OK.
If all went well, your toolbar with the scripts will be exported as an .rui file. This opened from inside any installation of V5 from Tools>Toolbars..., or you can also drag and drop it into a running Rhino window. (If you drag and drop, make sure you put the .rui file somewhere safe and that it stays where it was dropped from). You will then have two (or more) workspaces open in Rhino the original plus the new one that consists of this one toolbar. If you want, you can then copy the button(s) from the new toolbar to your normal workspace, you can then close the new toolbar file if you want.
It seems a bit complex, but it's actually easier to do than to describe... --Mitch
Mitch thanks for the answer!
you said "easiest way" can i also ask more about the other maybe more difficult way?
use python as command
The easiest way on OSX Rhino, is to add _cmd (underscore+cmd) to end of your python script file name. Then place it in dev folder, under "insert your folder name here", under PythonPlugins etc.. See attached image. Create folders if not there. Restart Rhino and you should get script by typing the file name into the command line prompt.