Understanding scriptcontext

Hi,

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
scriptcontext.doc.Objects.AddSurface(srf)

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
-Willem

3 Likes

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
    namext=filename.split("\\")
    name=namext[-1].split(".")
    
    file=open(filename,"w")
    file.write(name[0]+"\n\n")
    for item in direc:
        file.write(item+"\n")
    file.close()
    
direc=dir(sc.doc)
ExportClassDirectory(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.Views.Redraw()
sc.doc.Model… (file tolerances)

–Mitch

11 Likes

Thanks again Mitch!

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

-Willem

Thanks, Mitch! This is great.

JFYI

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
http://developer.rhino3d.com/api/RhinoCommonWin/html/P_Rhino_RhinoDoc_Objects.htm

Since Object’s type is ObjectTable, you can find all the methods available under ObjectTable:
http://developer.rhino3d.com/api/RhinoCommonWin/html/T_Rhino_DocObjects_Tables_ObjectTable.htm

So I think Willem your guess is correct…

2 Likes

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.

2 Likes

Thanks a lot Mitch, many, many people like me, are 100% lost… when trying RhinoCommon with Rhino only, with GH is is fine, I think.

Hey Andres,
It’s perhaps more familiar in GH perhaps because everything stays “virtual” until you bake it - like the rest of GH - but you still get a preview. When working directly in Rhino via Python, it’s more abstract maybe, and you do have to get the virtual geometry into the Rhino document at the end. Towards that end the scriptcontext.Add… methods are basically the same as Grasshopper Bake.

I’m actually more comfortable working scripts in Rhino than GH, to me it’s more “direct” and most of my stuff ends up as aliases that run like Rhino commands.

3 Likes

Hi Steve,

As you see on another thread, I finally resolved the issue for finding the Rhino and System Modules. However, I still can’t get scriptcontext and rhinoscriptsyntax. I made a small script
import sys
print(sys.path)

[‘C:\Users\user\AppData\Local\Temp’, ‘C:\Program Files\Rhino 7\Plug-ins\IronPython\Lib’, ‘C:\Users\user\AppData\Roaming\McNeel\Rhinoceros\7.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib’, ‘C:\Users\user\AppData\Roaming\McNeel\Rhinoceros\7.0\scripts’]

and it all looks ok. the 2 modules are in the IronPython\Lib
I’m using Rhino 7.23
Any suggestions?
Tx

In many cases things like rhinoscriptsyntax and scriptcontext will not work when executing in a Rhino.Inside CPython environment as there is no user interface and there is no active document to work with.

These libraries are merely there as high level functions on routines in RhinoCommon to make things easier for users.

What are you trying to do? Maybe we can figure out a solution.