Rhino for Mac users can also write and execute Python scripts


(Alain Cormier) #1

Rhino for Mac users who are familiar with the Windows version may suffer from scripting envy not being aware that there are ways to write and execute Python scripts in Rhino for Mac. Atom is an open source text editor that’s becoming very popular for good reason. Not only is it very powerful out of the box but it’s also very extensible and that’s why we created rhino-python: an Atom package that assists in writting and executing Python scripts in Rhino.

You might even find the rhino-python package usefull if all you need is a central location from which to launch your scripts. Here’s a one minute preview of a possible workflow.

If you’re interested please read the documentation and give it a try. This is an early release and we’re just getting started so please let us know what you like, what you don’t like, and what you’d like to see in the future.

Alain


Rhino 5 Mac Python Best Practices
GH Python component not interpreting code
How to generate a plane of a specified size, position and orientation?
The rhino-python package for the Atom text editor and Rhino for Mac
Script editor for rhino mac
EditPythonScript Problem - Version 5.2.3
The rhino-python package for the Atom text editor and Rhino for Mac
Drilling holes
MacroEditor command doesnt work on Rhino 5 for Mac
Installing Python for Mac Rhino 5
Installing Python for Mac Rhino 5
Running scripts from PyCharm on Mac
External editor possibility
External editor possibility
Motor Control Curves-Export Help Needed
NumPy (or other external lib) on OSX?
#2

There is also @jeff_hammond’s way.


t has been working great for me these last few weeks. or however long. Time flies when you are having fun.

Keep up the good work!
«Randy


#3

May I ask what kinds of things you’re writing scripts for?


(Alain Cormier) #4

Hi. There are an infinite number of reasons. From the [starter page][1]:

You may need Python if you want to:

  • Automate a repetitive task in Rhino much faster than you could do
  • manually Perform tasks in Rhino or Grasshopper that you don’t
  • normally have access to in the standard set of Rhino commands or
  • Grasshopper components Generate geometry using algorithms Many many
  • other things. It is a programming language after all.

Since you make guitars (awesome guitars btw!) you could draw fret lines on a fingerboard with the following script for example (over simplified):

import math
import rhinoscriptsyntax as rs

scale_length = 25.5
number_of_frets = 24

#calculate
f = lambda n: scale_length - (scale_length / math.pow(2, (n/12)))
fret_x = [f(n) for n in range(0,number_of_frets)]

#draw
rs.AddLine((0,0,0),(0,3,0)) #nut
for x in fret_x:
  rs.AddLine((x,0,0),(x,3,0)) #frets

Alain
[1]: http://wiki.mcneel.com/developer/python


#5

Much of my scriptwriting is making custom tools for Rhino that go beyond what is natively available. A few examples are:

  • Specific object type/characteristic selection (by length, area, volume, etc.)
  • Import/Export tools for various purposes, geo data processing, batch import or export of dozens or hundreds of files, export stl’s with specifc settings, etc.
  • Multiple object processing - offset, copy to points, etc.
  • Boundary trimming, multiple splitting with hundreds to thousands of curves
  • Bounding box based alignment tools
  • etc…

–Mitch


#6

Interesting. I’m not sure if anything i do would benefit from using a script, but it’s good to know the capability is there. I know some basic scripting stuff, but could probably pick it up fairly quickly.


#7

Just been using the Terminal / Atom / Rhino workflow. It works well. Thanks.


#8

Any chance of getting Windows support for this? I’d rather be using a familiar editor like atom than Rhino’s own Python editor.


Construction Lines request
(Alain Cormier) #9

Yes! That’s one of the reasons we chose the Atom editor. I have the code completion part working in my environment now and it’ll make it in an future release of Rhino.


#10

Wow! It’s a special day when Mac Rhino has something Windows users want! :stuck_out_tongue_closed_eyes:

It is a great tool and workflow. I love it. Thanks for implementing it!
cheers Peter


#11

Just doing a restart and wondering, we can now keep our scripts wherever we want and not in the specific location to run from the RunPythonScript command …

/Users/NAME/Library/Application\ Support/McNeel/Rhinoceros/scripts

Right? If we start Atom in Terminal and have changed to our Scripts location folder before starting Atom.


(Alain Cormier) #13

I wrote my last post too fast. BRB with a readable post


(Alain Cormier) #14

I’m not sure this is any better than the post I just deleted but here it goes …

Your scripts can reside anywhere but if they are not on the search path they can’t be imported by other scripts.

One current limitation is that the python search path is not configurable. That means you can’t import modules that are not under: /Users/NAME/Library/Application\ Support/McNeel/Rhinoceros/Scripts or not in the directory or a subdirectory of the file you’re currently running. Subdirectories need to have a __init__.py file so they are recognised as modules.

Let’s say your working directory is ~/py. ex:

cd ~/py
mkdir mylib
cd mylib
touch __init__.py # create empty file
echo "def doit(): print 'doit'" >> myscript.py #simple script

you can now import your module from a script that’s in ~/py. Ex:

import mylib.myscript as s
s.doit()

If your library is in a directory that is not under a search path you can create a link to it from /Users/NAME/Library/Application\ Support/McNeel/Rhinoceros/Scripts. If in the previous example the mylib directory is somewhere that’s not on the path you’d do something like this:

mkdir /somepath/mylib
cd ~/Library/Application\ Support/McNeel/Rhinoceros/Scripts
ln -s /somepath/mylib mylib # link

The example script:

import mylib.myscript as s
s.doit()

would still work.

Being able to configure the search path is important and we’ll add it sooner than later but in the meantime I hope this workaround is useful.

Alain


The rhino-python package for the Atom text editor and Rhino for Mac
#15

Thanks, I will keep them in regular / default folder for now.

Great weekend,
«Randy


#16

Cool. I installed the Atom thing, and have noticed the bit of having to CD in terminal before launching atom to get my scripts to show up. Will move them to application support for now as others have mentioned.

I’m noticing the autocomplete is VERY picky and if there’s so much as a missed Uppercase vs lowercase character the code won’t run. (for example the example wouldn’t run when typed, but only after I copied it wholesale from the web page).

The whole reason I dived into it was in wanting to get Thomas Anagnostou’s bad assed gear plug in to run under the mac rhino.

While a coder, I’m not at all fluent in Python, and barely cognizant of VB (which Thomas’s script was done in). I started attempting to translate the VB version over to Python with all the obvious stuff (changing comment format, losing the VB DIMs, nuking the end ifs and changing elseif’s to elif’s etc) but still have quite a ways to go and I’m pretty sure my edits to the lengthy select case stuff in the VB code have more than probably jacked up the indentation in python thus breaking havoc on the logic in the process.

If anybody who’s python savvy wants to have a go at porting the gear script, I’d be thrilled to have a running version.

The download link to the original script is here:

http://www.rayflectar.com/Rhino/gearGen.zip

cheers.


#17

Hi Alain,

Wonder if it would be useful to turn some of this info into a wiki page with a link from the main Mac Rhino wiki page…?

–Mitch


(Alain Cormier) #18

Definitely. Thanks. I’m working on the documentation right now. Here’s the page for the rhino-python package in case you haven’t seen it already.


#19

I just updated to Autocomplete plus 2.80 - is that too far along? @jeff_hammond said 2.7.1…

I like the auto-update system from inside Atom…

In answer to this question from this thread

Mitch,
I recall that you were asking for some central way to
manage your scripts so if you’re testing this I’d like to know if you
can get what you need by doing what’s described here or here.

I will try to set up my Mac script links now - haven’t had time before, but at least we have a long weekend now… I’ll surely have some questions, being almost entirely Mac-illiterate.

–Mitch


#20

heh… they must of updated that within the past 4 hours as i went to the latest 2.7.1 this morning.
i haven’t tried it yet but i’m writing python today… i’ll update soon and report back if i find anything is broken.


EDIT
after a very quick go at 2.8, everything still appears to be properly working with rhino… the one thing i noticed is it’s not honoring my ‘Maximum Visible Suggestions’ preference… mine is set to 10 but it’s showing the long list instead…

i may just need to turn if off and on again ? :slight_smile:


#21

OK, now I wanted to try opening Atom from inside Rhino… On Windows, you can actually open another windows app by using the “Run” command inside Rhino… Seems like no such animal in MacRhino, but I saw you can actually launch apps with a python script… :smile:

! _RunPythonScript OpenAtom.py

The script:

import os
os.system("/Applications/Atom.app/Contents/MacOS/Atom")

Unfortunately, although that does open Atom, it leaves Rhino with a spinning beachball until I quit Atom. Maybe there’s some other way…

–Mitch