Hi guys, Im just starting to learn rhino python. Recently ive read some python script in the forum, and notice that sometime we need to import rhino and scriptcontext. What are these files? Where could I find introduction about these 2 files?
But nothing about importing rhino and scriptcontex. Is it a must-have before scripting everytime? Or we do it when needed?
If it is a âdo-when-neededâ,then when do we need to import them and what function they have? Could âRhinoscriptsyntaxâ replace both/either of them?
But I didnt find any page explains about âImport rhinoâ & âImport scriptcontextâ and there is no explain about what commons or functions are contained in these two files as well.
On the other hand, â Import RhinoScriptSyntaxâ is clearly explained, I know why I need importing RhinoScriptSyntax, and what commons or functions it contains in this following pages:
My aim is to find explaination about âimport rhinoâ and âimport scriptcontextâ like the explaination of âimport RhinoScripSyntaxâ to help me understand them clearly.
import Rhino refers to loading the RhinoCommon API. rhinoscriptsyntax for instance is just a wrapper on top of that, meaning that it has some functions simplifying usage for beginners that call API symbols (functions, classes, etc.).
Superficially, it also applies a sort of functional programming logic, whereas the API goes full OOP (object oriented programming).
You can use the inspect module to well inspect underlying code, like this:
import rhinoscriptsyntax as rs
import inspect
print inspect.getsource(rs.AddPoint)
This will output:
def AddPoint(point, y=None, z=None):
"""Adds point object to the document.
Parameters:
point (point): a point3d or list(x,y,z) location of point to add
Returns:
guid: identifier for the object that was added to the doc
Example:
import rhinoscriptsyntax as rs
rs.AddPoint( (1,2,3) )
See Also:
IsPoint
PointCoordinates
"""
if y is not None: point = Rhino.Geometry.Point3d(point, y, z or 0.0)
point = rhutil.coerce3dpoint(point, True)
rc = scriptcontext.doc.Objects.AddPoint(point)
if rc==System.Guid.Empty: raise Exception("unable to add point to document")
scriptcontext.doc.Views.Redraw()
return rc
There is no documentation about scriptcontext, as far as I know. Itâs a very small Python script with only three functions that mostly handles interactions with the active Rhino document, and externalizes some other functionality, like for instance the persistent sticky dictionary that exists beyond the current Rhino instance (?), or the id variable.
You can check out the code yourself. Just find the relevant Python file on your system. This will give you the path:
Thank you Diff-arch. Its so clear and understandable. Id like to ask a bit further, is that mean I have no need to import rhinoscriptsyntax and scriptcontext if I import Rhino at first?
You need to import anything that is needed. There is not any priority to import which one first.
if Your code only needs Rhino syntax to run, u have to import that one onlyâŚand so onâŚ