Questions Other New Rhino.Python Users Might Have Like Me


(Steve Baer) #1

NOTE: This is a repost from the old python forum originally posted by
@piac. It was very useful for people getting started, so placing the
content here on discourse

Hi all,

like most of the people on this forum, I’m new to Rhino.Python and I’m posting some of the questions (with some answers I found so far) that were puzzling me when transitioning from RhinoScript to Rhino.Python.

0. What’s the RhinoScript package and where is it?

It’s a series of Python functions that mimic the procedural programming style found in RhinoScript, the scripting language that is available in Rhino v4 and previous&future Windows versions of Rhino and is based on vbScript. I could find the Python source code of the RhinoScript package in
C:\Documents and Settings#my-name#\Application Data\McNeel\Rhinoceros\5.0\IronPython\lib\rhinoscr ipt (on winXP)
/Library/Application Support/McNeel/Rhinoceros/MacPlugIns/IronPython/settings/rhinoscript/lib/ (on Mac)
C:\Users#my-name#\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript (on Vista/7)

1. How do I reference a Rhino point with the RhinoScript package?

The point entity in the Rhino document is linked with a Guid (a globally unique identifier of the point inside the document) in the RhinoScript package. Similarly as its precursor, generally RhinoScript uses Guids (in this case, System.Guid and not text any longer as in vbScript) all the time to reference all geometric entities in the document.

import rhinoscriptsyntax as rs
 
ptId = rs.GetObject("Please select point", rs.filter.point)
if ptId:
    coords = rs.PointCoordinates(ptId)
    print(coords)

2. Ok, then what does coords in the example above contain now?

coords above is a RhinoCommon object, a Point3d. It’s a Rhino.Geometry.Point3d type to be precise. The same happens to almost every type: for a line RhinoCommon uses the Rhino.Geometry.Line type, and RhinoScript simply normally uses Guids to a line in the document.
To gain access to the RhinoCommon object linked by Guid, you can use the rs.coerceXXX(ID) functions.

3. How can I print the x coordinate of a point that is not in the document using RhinoCommon?

from Rhino.Input import GetResult
from Rhino.Input.Custom import GetPoint
 
getter = GetPoint() #calls the constructor of GetPoint
try:
    getter.SetCommandPrompt("Please select a point")
    if getter.Get() == GetResult.Point: #we wait for the result here
        pt = getter.Point()
        print("x coordinate: %.5f" % (pt[0], ) ) #float formatting 5 digits
 
finally:
    getter.Dispose() #this will be called in all cases

4. The “command wizard” doesn’t work?

The command wizard is a great feature. The idea behind it is to write scripts that are automatically converted in runnable command aliases. Too bad, at first it seems not to be working! It is actually working, but it needs a couple of actions to make it functional after the script is completed:
Save the file (name is *_cmd.py)
Restart Rhino
Run the _EditPythonScript command
The alias is now in the Rhino command bar
Rhino.Python keeps track of the name of the file, not the name written after “commandname” in the file itself
You can find all IronPython command writing “IronPython.” in the toolbar

5. In which folder do I need to put commands?

The folder where Rhino looks for *_cmd.py files is:
Code:
C:\Documents and Settings#my-name-here#\Application Data\McNeel\Rhinoceros\5.0\Plug-ins\PythonPlugins\ (on winXP)
\Library\Application Support\McNeel\Rhinoceros\MacPlugIns\PythonPlugIns#my-name-here#\dev (on Mac)
See this great discussion: http://python.rhino3d.com/threads/27-Add-a-command about the Mac setup.

6. What book can I read to learn the Python syntax and style?

I personally think the books on Python that are going to be most relevant for Rhino.Python are the ones that are also taking into account the Iron prefix. I’ve only read IronPython in Action so far and I liked it, even if sometimes I found it a bit too ardent at the expense of objectiveness. Do you have other suggestions?

If you have any additional trick, book suggestions, advice for new users, comments etc., I’d be glad if you reply down here or start a new conversation!
Have a good start,

  • Giulio