This is a repository for storing various useful python scripts for the embedded IronPython engines in McNeel's Rhino 5 and Grasshopper. Please feel free to fork this repo and add your own scripts or edit the exsiting ones. For any given scripts, please do your best to document any dependencies it has and put examples of use in the docstrings. If you want to edit or add to any of these scripts, please do.
Some of these scripts may be generally useful for IronPython, and for interfacing with related tools. I'm hoping to start a very general open repository of scripts that can become a shared collective resource.
For help and more information:
- Rhino 5 Forums - For help and suggestions about Rhino 5
- Grasshopper Scripting Forum - For help with scripting in Grasshopper
- RhinoPython Scripting Forum - For help with scripting in Rhino.
- RhinoCommon SDK - The API reference.
- McNeel GitHub repos - Official McNeel code repositories.
- Help with GitHub - For learning how to use Git and GitHub.
- Pro Git - For learning more about Git.
- StackOverflow - For general programming questions.
-
A module for translating GeoJSON objects into Rhino objects, using RhinoCommon.
-
A function for running python scripts that do not play well with IronPython. This script sends arguments to a script that is run on the normal CPython interpreter, and returns stdout, stderr, and exit code results. Obviously depends on having a CPython (aka "traditional" Python) installed on the system.
-
A module for automatically setting up camera views and exporting Make2Ds from Rhino
-
A module with some very general functions for managing data. Not Rhino or IronPython specific.
You can easily download the entire collection of scripts in one folder, and quickly get started. Here's how:
-
Choose a folder on your computer where you would like to keep downloaded scripts.
-
Look for the link right here on this webpage named 'Downloads'. Click on it, and then select the zip file.
-
After you download the zip file, make sure that the unzipped folder is in the folder where you want to keep downloaded scripts. Rename the unzipped folder to
rhinopythonscripts
. -
The unzipped folder contains all the scripts in this repository. If you look in the folder, you'll notice a file called
__init__.py
, along with all the other scripts. In Python lingo, this folder is a package of modules. -
Start writing your own script. On Windows you can do this using the
EditPythonScript
Rhino command, which will open a script editor. On Mac or Windows, you can write a script with any basic text editor, and there are many to choose from. Some popular text editors for people who use Python are TextMate(Mac), E Text Editor (Windows), Notepad++ (Windows), or you can set up a full IDE using Steve Baer's helpful instructions on setting up PyDev. If you enjoy scripting in Rhino and using a command line, you might like Vim, which is a scriptable text editor that, like Rhino, includes a command line. -
In your script, write the following, and then save it (make sure it has the
.py
extension).# 'sys' is a built-in python module containing system utilities import sys # sys.path is the list of folders that python can 'see' for i in sys.path: # for each folder path in the list: print i # print the folder path
-
Run the script using the
RunPythonScript
command in Rhino, or by simply pressing the green 'play' button in the Rhino python script editor. You should see a list of file paths to different folders. -
Make sure that the folder containing your
rhinopythonscripts
folder is in the list of folders. If not, then add the following to your script (using the appropriate file path):# here we are appending a folder path to the list # of folders that python can 'see' sys.path.append("the/path/to/the/folder/that/contains/rhinopythonscripts")
-
Add the following to the end of your script, and then run it again:
import rhinopythonscripts
-
If you don't see any errors when you run it, that means it's working. You can now import any of the functions from the package and use them in your own scripts. For example, the following code would move objects from one layer in your current Rhino document to another. You can add this to the end of your script and it should work (assuming both layers exist and you have objects on the first layer).
# first we import the switchLayers function from the Make2D module from rhinopythonscripts.Make2D import switchLayers # now we can run it. It will moves objects from the first layer # to the second layer. switchLayers("Layer 5000", "Layer 2999")
-
A couple quick ways to explore the package using python:
# .__doc__ will print out the docstring of a function or module switchLayers.__doc__ # dir() will print out a list available commands for any module, # function or object in python dir(rhinopythonscripts.Make2D)
Enjoy! See above for more places to get help.
The following code will reveal what folders python can see. If folders are missing from the resulting list, then you cannot use the modules they contian.
import sys
for directory in sys.path:
print directory
To add a folder to sys.path
, assuming you already have the import sys
line:
sys.path.append("path/to/my/folder/that/contains/python/scripts")
Then you can import scripts from you folder.
The main folder, rhinopythonscripts
, has been provided with an __init__.py
file, so the whole thing can be treated as a package. Assuming your new script has access to the folder that the rhinopythonscripts
folder is inside of (see Checking Your Path above), you can import various modules in the following way:
from rhinopythonscripts import RunCPythonScript
out = RunCPythonScript.run("someCPythonScript", argumentList)
or you can do stuff like this to import individual functions or give things shorter names:
import rhinopythonscripts.InfraPy as ip
from rhinopythonscripts.RunCPythonScript import run as runC
out = runC("someCPythonScript", argumentList)
- First, install git for your system and set it up.
-
Fork the repository on GitHub.
-
Get to the folder where you want things to be stored
- On Windows, open the Git Bash program, and use
cd foldername
andcd ..
to get to the folder you want. - On Mac, open Terminal and use
cd foldername
andcd ..
to get to the folder you want.
- On Windows, open the Git Bash program, and use
-
Using Terminal (Mac) or Git Bash (Windows), use git to clone the repository onto your computer by typing the following command:
git clone [email protected]:yourusername/rhinopythonscripts.git
Whereyourusername
is replaced by your user name, obviously. -
Now you have created a folder, inside of whatever folder you were in, called
rhinopythonscripts
. Everything is inside of that folder and you can use it. :)
coming soon ...
coming soon ...
coming soon ...
coming soon ...