Handy Tips for GHPython Editor/Component (Rhino 6)

Perhaps it will be helpful to have an “unofficial” list of things/tips, that might help answer some of the “re-occurring” questions that come up on the forum, (in regards to ghpython).
This isn’t intended to be any sort of comprehensive guide, but more of a running list of things that folks might find useful to know when working, (or starting to learn to work), with a ghpython component.
Most of these have come from the generous minds of this forum.
Feel free to post other handy tips/suggestions! (or corrections!)

Must Read links:
1 - https://developer.rhino3d.com/guides/rhinopython/
2 - https://developer.rhino3d.com/guides/rhinopython/ghpython-editor/
3 - https://developer.rhino3d.com/guides/rhinopython/ghpython-question-answer/

Be sure to familarize yourself with this secition "What is “ghdoc” and how does it relate to the rhinoscriptsyntax? , in the link above. You can save yourself a bit of “GUID” headache by sticking exclusively with Rhinocommon. With that said…opinions on Rhinocommon vs rhinoscriptsyntax may vary.

4 - Compile GHPython Component Tutuorial

Limitations:
You can’t use numpy/scipy. This is an ironpython limitation, not specifically a ghpython limitation

Adding additional component interface options:
It’s probably best to NOT go down the path of trying to add certain types of component interface options, (buttons for example). read more here:
Button and Context Menu discussion

You absolutely can add additional context menu items.

Code Editor tips:
highlight line/s and tab will indent multiple lines
highlight line/s and shift+tab will unindent highlighted lines

API Documentation, hints, and things to know:
Autocomplete may not always return all the information you may be looking for. Get used to using the RhinoCommon API documentation
https://developer.rhino3d.com/api/RhinoCommon/html/R_Project_RhinoCommon.htm#!

the objects that come up in the autocomplete, (intellisense), of an imported module, will have icons that indicate what they are.
pubproperty = property
pubmethod = method OR constuctor

Informative introspection methods to use:
print(), type(), help(), dir(), inspect()
example:

import rhinoscriptsyntax as rs
import inspect
source = inspect.getsource(rs.BoundingBox)
print(source)

You can also look directly at what rhinocommon methods are being used in rhinoscriptsyntax: (path to folder is something like this)
C:\Users\YOURUSERNAME\AppData\Roaming\McNeel\Rhinoceros\6.0\Plug-ins\IronPython Scripting (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript

Edge Case scenarios (these are not terribly common occurrences).
Scenario 1:
There are few/rare occasions where a rhinocommon method won’t accept a python list. You need to use a structured DotNet Array: (typed array)
http://www.ironpython.info/index.php?title=Typed_Arrays_in_IronPython
(I don’t think this is terribly common…but here is an example).
Example: (component inputs set to G, U, V, PS, F, PE, tol)
RhinoCommon Method - CreatePatch

import System

    def PatchSrf(self, G, U, V, PS, F, PE, tol):
        startingSurface = None
        if tol == None:
            tol = Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance
        if PE:# need to create dot net typed array
            fixEdges = System.Array[bool]([False,False,False,False])
        else:
            fixEdges = System.Array[bool]([True, True, True, True]) 
        Patches = Rhino.Geometry.Brep.CreatePatch(G, startingSurface, U, V, self.Trim, self.Tangency, PS, F, 100, fixEdges, tol)
        return Patches

Scenario 2:
RhinoCommon method overloads.
use StrongBox in ghpython.
Discussion 1
Discussion 2
Discussion 3

Random tips:
If you are in SDK mode, and are returning multiple variables from your runscript, make sure to turn off the output “out” parameter.

9 Likes

Python comes with batteries included
Python has a large standard library of functionality built in for things like web connections, handling csv files, parsing xml and json, etc.
Want to do some maths? Type import math. Use fractions ? import fractions . Want to fly? import antigravity
It is always worth going back to learn more about the built in data types and standard library modules : just make sure you are reading the right documentation :
Ironpython 2.7, not modern Python
GrassHopper and Rhino implement Ironpython, a .Net implementation of Python 2.7. From around 2016 most online tutorials have been for Python 3.5 or greater. There is still plenty of online material for Python 2.7 and any open source packages written in pure Python, compatible with 2.7 and with no compiled C dependencies should work in GrassHopper and Rhino.

2 Likes