Get color of refrenced objects in grasshopper

import rhinoscriptsyntax as rs

obj = rs.GetObject("Select:", 0, True, True)

the above python code has worked for me inside of rhino’s scripteditor but not in ghpython component

heres the error:

Traceback (most recent call last):
  File "rhinocode:///grasshopper/1/ee4a242e-117c-46ef-8b25-8edba4948778/0a1b1270-fceb-4f53-806e-815e35aae507", line 35, in <module>
  File "C:\Users\ACER\.rhinocode\py39-rh8\site-rhinopython\rhinoscript\", line 788, in ObjectColor
    return rhino_object.Attributes.DrawColor(scriptcontext.doc)
TypeError: RhinoCodePlatform.Rhino3D.GH1.Legacy.ProxyDocument value cannot be converted to Rhino.RhinoDoc in method System.Drawing.Color DrawColor(Rhino.RhinoDoc)

any help is much appreciated

Hi @Lawand_Noa_man ,

You need to set the Script Context when dealing with Rhino Objects from within the Grasshopper context.

You can achieve this like so:

import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc

#Set Script Context To Rhino Doc
sc.doc = Rhino.RhinoDoc.ActiveDoc


#Set Script Context To Grasshopper Doc
sc.doc = ghdoc

Provided that your code is correct, adding it after setting the script context to the Rhino Doc will enable the GetObject functions and such to work in the correct context (The Rhino Doc) and then when you are done getting everything you set it back to ghdoc (Grasshopper) so that GH can work on the data within its own document context.

Here’s a working sample, though I’m not sure the larger context in how you are using it.

Graph Space:

import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc

# Set Script Context To Rhino Doc
sc.doc = Rhino.RhinoDoc.ActiveDoc

obj_ids = []
obj_colors = []

def GetObjs():
    objs = rs.AllObjects()
    for obj in objs:
        if obj:

    return obj_ids

def GetObjColors():
    for obj in obj_ids:
        # Use rs.ObjectColor to get the color of the object
        color = rs.ObjectColor(obj)
        if color:

    return obj_colors


guids = obj_ids
colors = obj_colors

# Set Script Context To Grasshopper Doc
sc.doc = ghdoc

Replace the line " rs.AllObjects() " with " rs.GetObjects()" to act like your initial question.
You can of course adapt to use rs.GetObject or rs.GetObjectsEx as well

Hope that helps you! (5.5 KB)

1 Like

man you’re a life saver, thanks alot. I could have never figured it out without your help. Could you by any chance share some resources where I can learn scriptcontext api?? again thanks a lot

1 Like

Happy to help @Lawand_Noa_man ,

I forget who told me about it but essentially I went through the same process as you and learned of it on these forums.

To the best of my knowledge ScriptContext is not really it’s own “section” of the API, it’s a wrapper for RhinoCommon so you set the script context to the rhino doc and then can access the full RhinoCommon methods and such and vice versa when the scriptcontext is set to ghdoc.

If I’m wrong on that, hopefully someone corrects me. I’m new to Python and Rhino scripting in general, learning as I go.

I don’t know much more information about it other than what’s in these links:

The .pdfs in this post are helpful as well though not directly covering ScriptContext:

I think it was summed up fairly well (based on my limited understanding) in the first forum post I linked, that it’s like a bridge between the Grasshopper Doc and the Rhino Doc which are two, independent documents.

All the best!

First of all, I don’t think that script context is a wrapper for Rhino.Rhinodoc but I do believe that most of their functionality is the same, like more than 85%… However, I haven’t seen the remaining %15 elsewhere. Thanks a lot for both the solution and the advice, and thanks a lot for sharing the github repository as well. I really appreciate the help.

Good to know, yea I don’t know enough about it to really be informing anyone one way or another :upside_down_face:

Thank you, you’re welcome, happy to help. Happy grasshoppering! :cricket:

1 Like