Scope of GUIDs in GHPython

Can somebody explain how GUIDs of objects in Grasshopper work? I have the following code to bake some curves into rhino and keep track of their ids:

def my_code():
    isocrvs = []
    #... populate isocrvs list here  ...

    # bake curve into rhino
    for crv in isocrvs:
        new_crv = bake_guide_crv(crv)
        #expected output: True
        #actual output: False
        print rs.IsCurve(new_crv)

def bake_guide_crv(guide_crv_id):
    sc.doc = Rhino.RhinoDoc.ActiveDoc
    # bake guide curve into rhino
    doc_object = rs.coercecurve(guide_crv_id)
    crv_id = sc.doc.Objects.AddCurve(doc_object)
    crv_obj = sc.doc.Objects.Find(crv_id)
    crv_obj.CommitChanges()

    #prints True as expected
    print rs.IsCurve(crv_id)
    rs.DeleteObject(guide_crv_id)
    #we put back the original Grasshopper document as default
    sc.doc = ghdoc
    return crv_id

 if __name__ == '__main__':
    my_code()

Since the curve now exists in rhino, the rs.IsCurve() function should return True? Why does it work within the function, but not after passing the GUID back to the parent function?

(May I ask, for next time, to upload a definition as well?
This way people who want to help will not need to re-construct what you did.)

The problem you were having was related to purposefully changing the target document to Rhino, then checking if the ID was from a curve in the Grasshopper ghdoc. Does it make sense?

See some comments,

toivosawen_sample.gh (3.4 KB)

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Thanks Giulio, I think I understand the logic of the rs.coercecurve function better now.

I’ll be sure to include a definition next time!

Cheers,
Toivo