Skip to content

Scripts for learning and for use in Rhino3D and Grasshopper3D

License

Notifications You must be signed in to change notification settings

DarrelRonald/RhinoPythonScripts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RhinoPython and ghPyton Scripts

Scripts for use in Rhino 3D and Grasshopper. These are public scripts that can perhaps be of use in day-to-day projects and learning.

What has been especially frustrating for me when learning scripting within the context of Rhino3D and Grasshopper3D is the many different ways to do the same things. For example, you can create the same script functionality in Python by using scriptcontext versus RhinoCommon versus rhinoscriptcontext. This flexibility is both good and bad, because it simplifies some things, but creates confusion around what is the best method to write scripts. In addition, each of the methods has pros/cons that need workarounds. For example, the rhinoscriptsyntax is simpler for writing scripts, but the API is incomplete.

Further frustrating in the learning process is about understanding and using the .NET framework, IronPython and how to work with .NET Assemblies. The Microsoft documentation is very difficult to understand, there are many different versions of .NET and McNeel doesn't offer any guides or overviews (that I know of) to work with these libraries.

For many of the ghPython scripts we only need proedural programming, but the class-oriented and object-oriented programming of the .NET environment and the McNeel Rhino Developer Examples make learning more confusing for non-professional coders.

Learning Scripts/Components

  • Add-Delete-Circle.py (includes: .py): this is a modification of the add-circle.py code of McNeel to see the 2 different methods of writing the circle() class and also to compare 2 ways to delete objects within the Rhino3D file using either scriptcontext versus RhinoCommon versus rhinoscriptcontext. One very good article I found after all this exploration was by Danil Nagy-however, the one mistake he makes is writing Rhino.Geometry instead of the correct Rhino library. Rhino.Geometry only gives you access to that specific Geometry Namespace.
  • Dir-Help.py (includes: .py): this is an example of how to use the builtin python functions dir() and help() when learning about new Python or RhinoPython modules while you're writing scripts. It can be used in your IDE as an alternative to looking up the API documentation online, which in some cases doesn't exist.
  • GetIronPythonModules.py (includdes: .py, .gh): This allows you to see all importable modules as well as a selected sys.path directory because sometimes the Rhino Python Editor Intellisense doesn't recognize all the IronPython modules. This allows you to see what Rhino will recognize. Thanks to Anders Deleuran for helping me with this on this McNeel Discourse thread.

Utility Scripts/Components

  • PythonTemplate_0.1.0 (includes: .py): This python script has the most important python template elements.
  • ListOfRhinoCommands (includes: .py, .gh): this sorts a list of all Rhino.Commands that can be accessed through RhinoCommon. It is a tool for seeing quickly what commands are available. You can also copy the output panel contents and paste into an excel sheet.
  • ImportSysExplorer_0.1.0 (includes: .gh): This ghPython module imports the sys module from IronPython and exports to panels both sys.builtin_module_names and the sys.path.
  • ScriptContextChanger (includes different: .py, .gh): these are more educational code blocks. However, if you're writing python scripts using RhinoCommon, you will sometimes need to change the scriptcontext.doc

Tips & Tricks

Below are some things that I've noticed along the way, read up on the McNeel Discourse Forum and know that others will probably find these same challenges, bugs or frustrations. Feel free to fork and propose changes.

  1. Rhino Dev Docs General Guides: Rhino Dev Docs has a somewhat hidden but very helpful set of General Guides. Some valuable articles are: Rhino Technology Overview and Developer Preprequisites among others
  2. Rhino Python Intellisense: In a Python script, sometimes the builtin Rhino Python Editor (both Rhino and Grasshopper) won't recognize the full set of IronPython modules. Other people also had this problem. You can import the modules you want, and intellisense should start working after it has been imported. I had this problem on Window 10, Rhino v6 SR 24. In the above Learning section you will see the script GetIronPythonModules.py you can see the full list of importable modules from Rhino Python.
  3. Rhino Python IDE with PyCharm: Steve Baer has an article explaining how to use PyCharm with intellisense stubs for RhinoCommon and Grasshopper.
  4. Rhino Python IDE with VSCode: Python Stubs Github
  5. Python Imports: Here is a Definitive Guide to Python Imports by Chris Yeh.
  6. Using Grasshoppers builtin Loop functionality in ghPython: Grasshopper does automatic iteration in the ghPython component. This means that if you create your own loops, you might encounter bugs. Alternatively, as Giulio Piacentino writes, you can write your python script as though you're operating on 1 item; Grasshopper will handle all the loop iterations behind the scenes. Guiulio also references this thread for more explanation.
  7. Compiling ghPython Scripts: Here is a thread by Giulio Piacentino about how to Create a Grasshopper Component using the Python GHPY compiler. There are two guides: (A) Single Component, and (B) Multiple components exports.
  8. GHPython Data Tree Helpers: McNeel has provided two Data Tree helpers for GHPython that are very important once you figure out how to use them. The 2 functions can be added to a python script with import ghpythonlib.treehelpers. One command will construct a Data Tree from a List or Nested List. The second command will deconstruct a Data Tree to a List or Nested List. You can see the functions on Github here.
  9. Breps versus Meshes: I recently refactored my python code to test using meshes instead of breps to speed up the functions in the code. I was doing boolean operations, testing for inclusions and creating contours. While using breps, the script was running for 5.1 seconds. Once I refactored to using Meshes, the scripts ran at 384 milliseconds, about 8% of the time.
  10. Object Oriented Design: Python OOP Tutorials is a set of 6 free videos by Corey Schafer on Youtube is a fantastic source for understanding how to write OOP code with Python. It is concise, quick-paced and goes into deeper topics of how Python handles classes and inhertance among other topics.

About

Scripts for learning and for use in Rhino3D and Grasshopper3D

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages