Sample Code Documentation

The resources for the class include several examples of Grasshopper sketches and a library of Python code. The example code can be found on the lab Windows server at \\dfabserver1\dfab_robot_temp\02_USERS\S18_HMV\Resources\MOCAP\MOCAP BASE Files. The resources tree includes several top-level folders: python with a collection of Python modules, and Grasshopper with several sub-folders with Grasshopper sketches. For help with server access, please see the instructors.

The primary interactive example for Grasshopper and GHPython code is the “House of Cards” demonstration. The sample sketches implement an interactive design system for constructing 3D structures from a set of 52 playing cards assembled using laser-cut slots. The editor uses motion capture gestures for natural 1:1 modeling, and the post-processor automatically generates a laser-cutting plan for a deck of cards. This example can be found within the resources as ...\MOCAP BASE Files\Grasshopper\HouseOfCards\.

GHPython, RhinoPython, and CPython

There are several flavors of Python used in this library:

  1. Python scripts embedded within GHPython blocks in Grasshopper sketches.
  2. Loadable modules using RhinoPython.
  3. Plain Python modules or scripts which work in either RhinoPython (IronPython) or CPython.

The GHPython scripts and loadable modules have several different APIs available: Grasshopper for Grasshopper-specific functions; rhinoscriptsyntax for high-level operations on either Grasshopper or RhinoDoc objects; Rhino for low-level access to Rhino itself, including Rhino.RhinoDoc.

There are also a few example of modules or scripts which work only in CPython, usually because they use third-party libraries such as scipy unavailable within Rhino.

Please note that several of the GHPython scripts load Python modules from the module library using relative paths. For this to work, the directory structure must be maintained. E.g. in MocapDemo, the AddGeometry GHPython object imports the ghutil.rhinodoc module using a relative path equivalent to ..\..\python\ghutil\

GHPython blocks keep their Python source within the binary Grasshopper files. Our convention is to export this code into an identically named .py file for ease of inspection and version tracking. Please remember that Grasshopper only uses the code within the .gh file, this is simply a maintenance convention, and editing these .py files will have no effect unless reloaded into the corresponding GHPython blocks.

Wherever possible, the modules within the python tree work in either the mainstream CPython interpreter or the IronPython system used within Rhino. This is useful for developing low-level functionality independent of Rhino.