developer:python


Python

http://python.rhino3d.comhttp://python.rhino3d.com
community site for using python in Rhino.
Summary: Where we are headed with python scripting in Rhino. For support, examples, tutorials, and other resources, please visit the Rhino.Python community site.

A new scripting language for Rhino

Both the Rhino 5 Beta and the Mac WIP of Rhino contain support for the Python scripting language.

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

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 version

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.

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

NOTE: 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:

#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

Samples that use rhinoscriptsyntax

A Quadratic Solver in Script 2007/11/28 20:53  
Adding Curvature Circles 2010/01/12 09:49  
Adding Points at Curve Endpoints 2010/01/12 09:46  
Arc Point Distribution 2010/01/12 09:46  
Arraying Points on a Surface 2010/01/12 09:46  
Assign Material To Object 2012/04/19 13:35 John Morse
Auto Label Objects with Script 2010/01/12 09:49  
Display the Flamingo nXt legacy plant browser 2012/04/19 13:18 John Morse
Get Flamingo nXt material assignment 2012/04/19 11:47 John Morse
Get Mapping Information From Object 2012/04/19 11:30 John Morse
Get Material List 2012/04/19 12:53 John Morse
Get Plant Points 2012/04/19 12:57 John Morse
Import Flamingo nXt Material 2012/04/19 13:05 John Morse
Modify an existing Flamingo nXt plant 2012/04/19 13:21 John Morse
Query plant object 2012/04/19 10:29 John Morse
Query plant wire-frame 2012/04/19 11:19 John Morse
Set Flamingo nXt Object Mapping Properties 2012/04/19 13:29 John Morse
Tag object as plant 2012/04/19 13:39 John Morse
Un-tag an object as a Flamingo nXt Plant 2012/04/19 13:41 John Morse

Samples that use RhinoCommon

Rhino Application Platform (RAP) using RhinoCommon 2012/05/08 09:07 Giulio
Sample: Add Background Bitmap 2010/07/30 14:22 Steve Baer
Sample: Add Brep Box 2010/07/30 14:26 Steve Baer
Sample: Add Child Layer 2010/12/20 11:32 Steve Baer
Sample: Add Circle 2010/07/30 14:26 Steve Baer
Sample: Add Clipping Plane 2010/07/30 14:27 Steve Baer
Sample: Add Command Line Options 2011/01/12 18:51 Steve Baer
Sample: Add Cone 2011/01/15 09:58 Steve Baer
Sample: Add Cylinder to Rhino 2011/01/03 09:24 Steve Baer
Sample: Add Gumball 2011/07/14 14:20 Steve Baer
Sample: Add Layer 2010/07/30 14:27 Steve Baer
Sample: Add Line 2010/07/30 14:28 Steve Baer
Sample: Add Linear Dimension 2010/07/30 14:28 Steve Baer
Sample: Add Linear Dimension2 2010/07/30 14:28 Steve Baer
Sample: Add Mesh 2010/07/30 14:29 Steve Baer
Sample: Add NURBS Circle 2010/07/30 14:29 Steve Baer
Sample: Add NURBS Curve 2010/07/30 14:29 Steve Baer
Sample: Add Named View 2010/07/30 14:29 Steve Baer
Sample: Add Objects to Group 2010/07/30 14:30 Steve Baer
Sample: Add Sphere 2010/08/02 11:09 Steve Baer
Sample: Add Text 2010/08/02 11:31 Steve Baer
Sample: Add Texture 2010/09/01 09:24 Steve Baer
Sample: Add Torus 2010/09/01 09:25 Steve Baer
Sample: Add Truncated Cone 2010/09/01 09:26 Steve Baer
Sample: Add a basic Material 2011/03/17 18:39 Steve Baer
Sample: Add a block definition 2011/05/21 14:12 Steve Baer
Sample: Add a layout with a detail view 2011/11/06 09:54 Steve Baer
Sample: Adding Arrowheads to Curves 2011/05/28 21:57 Steve Baer
Sample: Adjusting Surface Isocurve Density 2011/05/28 22:47 Steve Baer
Sample: Array By Distance Command 2011/11/30 14:28 Steve Baer
Sample: Boolean Difference 2011/03/10 16:13 Steve Baer
Sample: Brep Face Area 2011/08/12 17:00 Dale Fugier
Sample: Calculate Curve Intersections 2011/06/04 14:33 Steve Baer
Sample: Closest Point Calculations with an RTree 2012/01/27 08:49 Steve Baer
Sample: Creating a hatch from a curve 2011/11/07 11:32 Steve Baer
Sample: Curve Bounding Box (world and plane oriented) 2011/06/04 09:26 Steve Baer
Sample: Determine Active Viewport 2010/12/16 09:44 Steve Baer
Sample: Determine Object's Layer 2010/12/29 09:29 Steve Baer
Sample: Display conduit to draw overlay text 2011/07/26 15:35 Steve Baer
Sample: Divide Curve By Length 2010/07/30 14:30 Steve Baer
Sample: Duplicate borders of a surface 2011/03/20 11:44 Steve Baer
Sample: Edit Text 2011/11/07 09:57 Steve Baer
Sample: Explode a Hatch 2012/01/31 16:16 Steve Baer
Sample: Find Circles and their centers 2011/03/10 16:14 Steve Baer
Sample: Find Objects by Name 2010/09/01 09:27 Steve Baer
Sample: Find point on curve at distance 2011/01/15 10:00 Steve Baer
Sample: Insert knot in curve 2011/03/10 16:15 Steve Baer
Sample: Intersecting line curves 2011/03/22 10:44 Steve Baer
Sample: IsBrepBox Test 2012/05/16 09:16 Steve Baer
Sample: List Block Definition Geometry 2011/06/01 14:06 Steve Baer
Sample: Modifying Advanced Display Settings 2011/01/18 15:52 Steve Baer
Sample: Obtain insertion point of block 2011/01/15 10:01 Steve Baer
Sample: Orient On Surface 2010/07/30 14:31 Steve Baer
Sample: Select objects on layer 2010/12/16 09:53 Steve Baer
Sample: Set object display mode 2011/03/19 11:08 Steve Baer
Sample: Sweeping Surfaces using Sweep1 2011/05/28 09:26 Steve Baer
Sample: Transform a Brep 2011/11/06 09:55 Steve Baer
Sample: Unroll developable surface and associated mesh 2011/03/10 16:16 Steve Baer
Sample: Unrolling a developable surface 2011/02/05 13:12 Steve Baer
Sample: Use a constrained GetPoint to copy a curve 2011/05/19 09:58 Steve Baer
Sample: Visual Analysis Modes 2011/11/05 22:32 Steve Baer
Sample: Zoom to a Selected Object 2011/06/16 15:56 Steve Baer
developer/python.txt · Last modified: 2011/10/07 10:21 by steve Driven by DokuWiki Recent changes RSS feed

 © 1997-2012 

McNeel North America Europe Latin AmericaAsia