+ Reply to Thread
Results 1 to 10 of 10

Thread: use python script as a rhino command?

  1. #1

    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?


    dr.octopus

  2. #2
    Super Moderator Mitch's Avatar
    Join Date
    May 2010
    Location
    Switzerland
    Posts
    285
    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.
    Type:

    ! _-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.
    C:\Users\Username\AppData\Roaming\McNeel\Rhinocero s\5.0\PythonScripts

    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.

    HTH, --Mitch

  3. #3
    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

    thanks


    Dr. Octopus

  4. #4
    Super Moderator Mitch's Avatar
    Join Date
    May 2010
    Location
    Switzerland
    Posts
    285
    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

  5. #5
    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?


    thanks



    Quote Originally Posted by Mitch View Post
    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

  6. #6
    Super Moderator Mitch's Avatar
    Join Date
    May 2010
    Location
    Switzerland
    Posts
    285
    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.

    --Mitch

  7. #7
    Hi Mitch,
    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) ...
    thanks
    Riccardo

  8. #8
    Super Moderator Mitch's Avatar
    Join Date
    May 2010
    Location
    Switzerland
    Posts
    285
    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

  9. #9
    Mitch thanks for the answer!
    you said "easiest way" can i also ask more about the other maybe more difficult way?
    Riccardo

  10. #10
    New Member aubergine2001's Avatar
    Join Date
    Nov 2010
    Location
    Halifax, Nova Scotia, Canada
    Posts
    19

    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.

    Randy
    Attached Images

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts