Running scripts from PyCharm on Mac

I’ve found a way to run Python scripts from PyCharm in Rhino for Mac.

CAVEAT: This is a hack. It may not work in the future. McNeel has provided us a way to run Python scripts in Rhino from Atom. It’s currently implemented with a REST API, but McNeel has not publicly documented the API or made any promises that it won’t change or that it will even continue to exist.

That said, the following is currently working for me:

Create an External Tool in PyCharm.
In PyCharm, open Preferences, and go to Tools > External Tools. Click the Add (+) button. The Create Tool dialog opens.

In the Name field, enter something like “Run In Rhino”. In the Program field, enter the following:

curl

In the Parameters field, enter the following

-d {\"FileName\":\"$FilePath$\"} http://localhost:8080/runpythonscriptfile

Leave all other fields at defaults. Click OK.

I’m running PyCharm 4.5.3 and Rhino for Mac version 5.0.2 (5A865) on Yosemite 10.10.5 (Build 14F27).

Run a Python script.
In Rhino, make sure the Atom Editor Listener is running. In PyCharm, open a .py file that contains a script you’ve written to run in Rhino. Make sure it’s the active file (i.e. it’s visible in the editor and its tab is highlighted). Right-click anywhere in the editor, and in the context menu, under External Tools, click the name you entered in the Create Tool dialog. A console should open. If you’re lucky, the stuff in the console will include this:

{"msg":"Successfully ran script"}

Bonus points: Add a button.
In the PyCharm toolbar, right-click and select Customize Menus and Toolbars… In the dialog that opens, expand Main Toolbar. Click the last item in the sub-list under Main Toolbar to highlight it. Click Add Separator. It will add a separator after the last item. Click the separator to highlight it. Click Add After… In the dialog that opens, expand External Tools and select the name you entered in the Create Tool dialog. Click OK. It will complain about not having an icon; just accept the default icon. (If you can figure out how to put the Rhino logo on the icon, post it here!) Now you should have a button at the end of your toolbar that runs your script in Rhino.

3 Likes

Glad to see this! I did something similar not too long ago to run scripts from Emacs, the only difference being that it automatically saves the current buffer to /tmp so that it doesn’t matter whether you want to keep the file around or not. I imagine we’ll soon see a RunEditorListener command to replace the current Atom listener.

This is nice @jjpr! We can definitely consider freezing the API after letting it mature a bit if some of you are interested in doing this kind of work. In the meantime if something stops working of if you have any questions please don’t hesitate to ask. The API will evolve and it would be great if some of the other great editors / IDEs could take advantage of it.

Alain

the same trick works for the vim editor by adding the line

:command RS exec '!curl -d {\"FileName\":\"' . shellescape('%:p') . '\"} http://localhost:8080/runpythonscriptfile'

to your ~/.vimrc file. Now you can run the script by just typing “:RS” in vi…

sd

1 Like