Python - A new scripting language for Rhino

Both the Rhino 5 WIP and the Mac WIP of Rhino contain support for the Python scripting language. Python is a modern programming language developed for remarkable power with very clear syntax. Come join all the others looking at this new tool.

Let's clear up some questions about terms.
  • Python is a high level programming language (scripting) that interprets text files and performs operations Python_(programming_language).
  • IronPython is a specific implementation of the python language that runs on top of .NET. CPython is another common python implementation that people use that is written in C while Jython is written in Java. I chose IronPython because of it's incredible ability to use all of the classes available in the .NET framework including all classes and functions made available through RhinoCommon

Windows and Mac

Since Rhino python scripting is available on both Windows and Mac, the exact same python scripts will run on both “breeds” of Rhino!

What about RhinoScript?

Rhino already has a very successful scripting language called RhinoScript. There are no plans to stop supporting RhinoScript and we plan to continue to add functions to RhinoScript based on user requests. Python can be considered an alternative scripting language that you may want to use for writing scripts.

RhinoScript style functions

One of the key features of RhinoScript that make it easy to write powerful scripts is a large library of Rhino specific functions that can be called from scripts. Our python implementation includes a set of very similar functions that can be imported and used in any python script for Rhino. This set of functions is known as the rhinoscript package. Documentation on this package can be found at http://www.rhino3d.com/5/ironpython/index.html

Let's compare scripts for letting a user pick two points and adding a line to Rhino.

RhinoScript Version

VB Code:
    Dim arrStart, arrEnd
    arrStart = Rhino.GetPoint("Start of line")
    If IsArray(arrStart) Then
      arrEnd = Rhino.GetPoint("End of line")
      If IsArray(arrEnd) Then
        Rhino.AddLine arrStart, arrEnd
      End If
    End If



Python Code:
    import rhinoscriptsyntax as rs
     
    start = rs.GetPoint("Start of line")
    if start:
        end = rs.GetPoint("End of line")
        if end: rs.AddLine(start,end)

Different… but similar enough that you should be able to figure out what is happening in python if you've written RhinoScript.

Using RhinoCommon

Along with the RhinoScript style functions you will be able to use all of the classes in the .NET Framework, including the classes available in RhinoCommon. As a matter of fact, if you look at the source for the RhinoScript style functions, they are just python scripts that use RhinoCommon. This allows you to do some pretty amazing things inside of a python script. Many of the features that once could only be done in a .NET plug-in can now be done in a python script.

For example, you can implement some custom drawing while a user is picking a point with the following script. This script draws a Red and Blue line connected to the point under the mouse cursor while the user is picking a point.

Python Code:
    import Rhino
    import System.Drawing
     
    def GetPointDynamicDrawFunc( sender, args ):
        pt1 = Rhino.Geometry.Point3d(0,0,0)
        pt2 = Rhino.Geometry.Point3d(10,10,0)
        args.Display.DrawLine(pt1, args.CurrentPoint, System.Drawing.Color.Red, 2)
        args.Display.DrawLine(pt2, args.CurrentPoint, System.Drawing.Color.Blue, 2)
     
    # Create an instance of a GetPoint class and add a delegate for the DynamicDraw event
    gp = Rhino.Input.Custom.GetPoint()
    gp.DynamicDraw += GetPointDynamicDrawFunc
    gp.Get()

Scripts, Plug-Ins, and Commands

The command and plug-in concept for python is still being implemented. This feature should become available in a near future Rhino WIP release (both windows and Mac).

Okay here's where things get really fun! Since a python script can access all of the same code as a .NET plug-in, we might as well allow for the concept of a python plug-in. That is exactly where we are headed with our implementation of Python in Rhino. Python scripts in Rhino can be run as standard scripts from files or as text on a toolbar button, but they can also become actual Rhino commands. Python commands are script files that are named [CommandName]_cmd.py and at least contain a function named RunCommand which takes one parameter.

For example, if I wanted to create a command name “StevesPythonTest”, I would create a python script file named StevesPythonTest_cmd.py and add the following text:


Python Code:
    #This is a basic Rhino command implemented in Python
    import rhinoscriptsyntax as rs
     
    def RunCommand( is_interactive ):
        print "Hello from my command"
        # add a point at (1,2,3) to Rhino
        rs.AddPoint( (1,2,3) )

This python file would need to be saved in the proper location on your computer (I'll go into details about this in the future). Rhino will automatically recognize this script as a command and you will be able to type “StevesPythonTest” on the command line to run the script; pretty cool!

One or more of these python script commands can be grouped toghether in a directory. This group is considered a plug-in and can be shared with other people just like other Rhino plug-ins.

Resources

IronPython is relatively new technology, but Python has been around for a while and there are excellent web tutorials and books on learning Python. Here are a few quick links that you might find useful

A good place to start is at the Rhino.Python community site on our Tutorials page...
You also can find resources on Python on our resources page...

http://python.rhino3d.com/content/128