Grasshopper

algorithmic modeling for Rhino

I'm moving a bit out of Grasshopper into pure Python scripting of Rhino, ignoring Rhinoscript in favor of Rhinocommon, but becoming more aware of scriptcontext.

I note that in the non-Grasshopper EditPythonScript editor within Rhino, there is a help outline on the left that has scriptcontext, that is devoid of much of any content, BUT that when I type scriptcontext.doc. into the editor, after I type the last period, a whole menu of command options appears that I have no reference for!

Well, where is it? I have either links to online documentation or CHM files for Rhinocommon, Grasshopper internals, Rhino, IronPython etc....

...but where is the manual for scriptcontext?

Views: 4899

Replies to This Discussion

On my hard drive I did find scriptcontext.py:

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

But it's quite short, yet I must assume this is really what is being imported with the standard import line I see in all the examples:

# scriptcontext module
import RhinoPython.Host as __host

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


'''Identifies how the script is currently executing
1 = running as standard python script
2 = running inside grasshopper component
3... potential other locations where script could be running
'''
id = 1


'''A dictionary of values that can be reused between execution of scripts
'''
sticky = dict()

def escape_test( throw_exception=True, reset=False ):
"Tests to see if the user has pressed the escape key"
rc = __host.EscapePressed(reset)
if rc and throw_exception:
raise Exception('escape key pressed')
return rc


def errorhandler():
'''
The default error handler called by functions in the rhinoscript package.
If you want to have your own predefined function called instead of errorhandler,
replace the scriptcontext.errorhandler value
'''
return None

There are only a handful of hits on http://discourse.mcneel.com for "scriptcontext," including a nearly identical question:

"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?"

http://discourse.mcneel.com/t/understanding-scriptcontext/7175

He gets no coherent answer whatsoever.

In the only other relevant answer ( http://discourse.mcneel.com/t/understanding-scriptcontext-vs-ghdoc/... ) Giulio Piacentino of McNeel says scriptcontext.doc is a *variable*, but that makes no sense if it's being used in object oriented dot format to issue commands like AddBrep.

Here is a solid clue, Giulio Piacentino also says here on this forum ( http://www.grasshopper3d.com/xn/detail/2985220:Comment:973147 ) that:

"...GrasshopperDocument does the same as RhinoDoc with ObjectTable...."

And here I find a Rhinocommon entry that replicates the suggestion menu from the non-Grasshopper EditPythonScript editor:

http://4.rhino3d.com/5/rhinocommon/html/AllMembers_T_Rhino_DocObjec...

Google search leads to another enticing clue, some sort of outline of the real scriptcontext.doc. command structure, close to what I need:

http://nullege.com/codes/search/scriptcontext.doc.Objects.AddPointC...

Another clue is that when I manually search in Rhinocommon ( http://4.rhino3d.com/5/rhinocommon/html/M_Rhino_DocObjects_Tables_O... ) for a given command to add an item to Rhino such as AddBrep, the Python example file included itself uses scriptcontext instead of a long format Rhinocommon command, and I also notice that scriptcontext is only for Python, whereas both VB and C# just use doc.Objects. instead of scriptcontext.doc.Objects. The word "scriptcontext" doesn't even appear in the Rhino Python book in PDF format from 2011 though, I sadly note.

import Rhino
import scriptcontext
import System.Guid

def AddBrepBox():
    pt0 = Rhino.Geometry.Point3d(0, 0, 0)
    pt1 = Rhino.Geometry.Point3d(10, 10, 10)
    box = Rhino.Geometry.BoundingBox(pt0, pt1)
    brep = box.ToBrep()
    rc = Rhino.Commands.Result.Failure
    if( scriptcontext.doc.Objects.AddBrep(brep) != System.Guid.Empty ):
        rc = Rhino.Commands.Result.Success
        scriptcontext.doc.Views.Redraw()
    return rc

if( __name__ == "__main__" ):
    AddBrepBox()

If I use:

from scriptcontext import doc

...then I can use the same VB/C# commands that simply use "doc.Objects" and the pop-up command suggestion menu still works too.

And I can search the Rhinocommon reference for the commands to tack on in the Object Table Class in the Rhino.DocObjects.Tables namespace:

http://4.rhino3d.com/5/rhinocommon/html/Methods_T_Rhino_DocObjects_...

I wish there was a way to avoid the rather bizarre use of scriptcontext, outside of Grasshopper, in a normal Rhino Python script via the EditPythonScript command's editor. This is a very strange rule to have to figure out that isn't in any manual I can locate so far. I understood the issue only as a Grasshopper user before, where it made some sense to switch between Grasshopper space and Rhino space, but I guess there's also a Python space that can hold geometry too, just like Grasshopper did.

To make a universal Python script that works in both Grasshopper's Python node and in the normal EditPythonScript editor outside of Grasshopper, I can include the usual Grasshopper toggles over to the Rhino document and back, and it works fine in both cases as long as I prevent an error in the non-Grasshopper case with a try/except container, here adding a cylinder to Rhino:

import Rhino
from scriptcontext import doc

plane = Rhino.Geometry.Plane(Rhino.Geometry.Point3d(0,0,0), Rhino.Geometry.Vector3d(0,0,10))
cylinder = Rhino.Geometry.Cylinder(Rhino.Geometry.Circle(plane, 5), 10) # RADIUS & LENGTH

try:
  doc = Rhino.RhinoDoc.ActiveDoc
  doc.Objects.AddBrep(cylinder.ToBrep(True, True))
  doc = ghdoc
except: pass

Thanks for sharing

Hey Nik,

Thanks for your very informative posting (so far it is one of the best I have found on the forums). I am in the exact same boat as you with programming. From what I can tell from studying up on Rhino and Grasshopper Python, is that McNeel's program is based on C#. Python appears to be a red headed step child for Rhino and McNeel is doing their best to include Python, so as not to be left out of the popular crowd. The result is a royal mess of left over soup. It was what I ate growing up because my mother at the end of the week would combined all the left overs into a big pot of boiling of water (yucky!). This is why there is very little to no documentation concerning scriptcontext and RhinoCommon pertaining to Python.

To decipher what McNeel has done you need to be a computer scientist and be proficient in C# to translate it into Python. I have been studying Python now for 4 months and now realized I should have been learning C#. Oh well, they tell me that once I learn one program it easier to learn another.

But I must say, that it appears if you ask the right question McNeel does their best to answer the question. We shall see because I will be asking questions in the near future as I begin my scripting.

Cheers    : /

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service