Understanding scriptcontext vs ghdoc


#1

Hi,

I am familiar with GH, but new to Python. I found a Python script that I’d like to ‘port’ to a GH context. It uses scriptcontext a lot (to reference geometry in the open document).

I understand that I can reference geometry and parameters easily from the GH definition. Is it the right approach to remove all instance of scriptcontext and find more GH native solutions?

Thanks for you insights.


#2

ghdoc is Grasshopper’s document, to which you will be adding your Grasshopper geometry. Scriptcontext is a module with some useful functions, and you probably won’t need it, as you could reference Rhino geometry even without it (by using grasshopper parameters like “Point”, “Curve”, “Surface”…).
Each time you perform some transformation on your Grasshopper model, a separate object get’s added to the ghdoc, unlike Rhino where you could perform a transformation directly onto the object in Rhino Document (that’s one of the usages of scriptcontext too).


(Giulio Piacentino) #3

Just one quick addition to the informative text by Djordje:

scriptcontext.doc is the variable that is used all the time by rhinoscriptsyntax to access the active document. You can imagine that, before running a script in Rhino, the EditPythonScript editor assigns the open model to that variable. Also, before GhPython runs, it assigns the active, hidden Grasshopper document to that variable, so that you will use a document that is better suited for Grasshopper (it’s not having to keep track of undo’s, so it’s more efficient for some operations. It also does not have some interface aids and does not per se allow picking, like Grasshopper. Also, geometry is not visualized by default in this document).

I hope this helps,

Giulio

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com


Stuck in Grasshopper-Python Recursion
#4

Thank you both. I’ll give this a try.