Understanding scriptcontext

(Willem Derks) #1


I’m trying to get my head around adding object to a document via python and rhino common and more in general "what scriptcontext is"
In examples I find for instance

What I cannot find is a list of the methods available from or accessible from scriptcontext. I have a guess it is the list found at Rhino.DocObjects.Tables.ObjectTable

However it is just a hunch for now. Is there any reading available to explain the paradigm of scriptcontext in relation to Python-Rhino Common?

Many Thanks

Where can I learn ScriptContext's functions?
Get layer of an object with rs.ObjectLayer(x) inside Python compnent
Creating a new viewport with python/rhinocommon

I’m not the finest expert on the matter, but my amateur interpretation is that scriptcontext is the “bridge” between Python/RhinoCommon and the active document. You use it to access objects that are already in the document as well as add new ones. You can also access and modify any other document related items - tolerances, views, etc. Most of what’s in there can be found in the Rhino.DocObjects Namespace.

To see what subdirectories are under something like scriptcontext.doc, I use a script to write out a text file:

import rhinoscriptsyntax as rs
import scriptcontext as sc

def ExportClassDirectory(direc):
    filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*||"
    filename = rs.SaveFileName("Save directory as", filter)
    if not filename: return
    for item in direc:

I guess I should add that I usually use the autocomplete to get where I want to go, my most often used are:

sc.doc.Objects(Add, Find, etc)
sc.doc.Model… (file tolerances)


(Willem Derks) #3

Thanks again Mitch!

Not yet tested your script but this sounds like a great help in finding my way.



Thanks, Mitch! This is great.



On this PC the scriptcontext.py file is in:

C:\Users\Pc\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib


Thanks emilio.

From your information, I found belowing:

The Active Rhino document (Rhino.RhinoDoc in RhinoCommon) while a script is executing. This variable is set by Rhino before the exection of every script.
doc = None

So “scriptcontext.doc” will direct to the current active rhino document’s instance of the RhinoDoc class, which is here: http://developer.rhino3d.com/api/RhinoCommonWin/html/T_Rhino_RhinoDoc.htm

“scriptcontext.doc.Objects” will direct to the property of RhinoDoc class, Object

Since Object’s type is ObjectTable, you can find all the methods available under ObjectTable:

So I think Willem your guess is correct…

(Steve Baer) #7

That is correct when the python script is running in Rhino. If the script is running in Grasshopper, scriptcontext.doc will refer to a temporary document for storing objects created during the execution of the script in a grasshopper component.