No error before compile. Get error after compile GHpython into component. Says that can't recognize ghdoc

I have 2 ghpython, I hope they can work together after compile. Tried to get the object’s name in RhinoDoc.
No error before I compile it.
But after compile into component it get error: Solution exception: global name “ghdoc” is not defined.
It seems that can’t recognize ghdoc after compile.

Here’s the code and original file.

GHpython 1:

class MyComponent(component):
    def RunScript(self, ):
        def getIDName(ID):
            sc.doc = Rhino.RhinoDoc.ActiveDoc
            try:
                obj = sc.doc.Objects.Find(ID)
                name = obj.Attributes.Name
                sc.doc = ghdoc
                return name
            except:
                sc.doc = ghdoc
                return None
        sc.sticky["getIDName"] = getIDName
        return 



GHpython 2:
 class MyComponent(component):
     def RunScript(self, x, y):
         a = sc.sticky["getIDName"](x)
         return a

ObjectName.3dm (55.4 KB)
ObjectName.gh (8.3 KB)

I have change “ghdoc” into GhPython.DocReplacement.GrasshopperDocument() and solved this problem.

2 Likes

Thanks for this, I reported it as RH-43345.

You could just count on the fact that ghdoc is the doc that is set as scriptcontext.doc before the script runs.

        def getIDName(ID):
            predoc = sc.doc
            try:
                sc.doc = Rhino.RhinoDoc.ActiveDoc
                obj = sc.doc.Objects.Find(ID)
                name = obj.Attributes.Name
                sc.doc = predoc
                return name
            except:
                return None
            finally:
                sc.doc = predoc

Switching to a new doc is not a great idea. It could mean that other data is lost. It’s not so important, really though, because the component will switch back to its own document once it will run again.