Basic question: What is 'Rhino' & 'scriptcontext' when we typing 'import rhino' & 'import scriptcontext'?

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?

I only find introduction about importing Rhinoscriptsyntax in this page:
https://developer.rhino3d.com/guides/rhinopython/python-rhinoscriptsyntax-introduction/

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?

Many thanks

Hey @fmyl,

There is lots of good “getting started” info here:

– Dale

Thanks Dale,working on it.

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.

Do you know any specific page to do so?

Thanks again.

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:

import scriptcontext as sc

print(sc.__file__)

The same goes for rhinoscriptsyntax.

3 Likes

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…

You’re welcome! As @bahman.farshbaf already pointed out, you import what you need. It’s as simple as that!

Thank you Bahman, simple and clear. gonna digging more on these three files.

Totally understand, i m going to go through more scripts to figure out when-which thing.