ghdoc.AddObject outside of cluster / reference to "parent" of cluster

Is it possible to add objects to the document from within a cluster?
This code adds the ValueList object to the cluster document, but I would like to add the ValueList to the “main” document.
Is this possible?

This code adds a ValueList with the elements in the preset list.

preset = ["test1", "test"]
vl = gh.Kernel.Special.GH_ValueList()
vl.CreateAttributes()
vl.ListItems.Clear()
for preset in presets:
    preset_str = '"{}"'.format(preset)
    vl.ListItems.Add(gh.Kernel.Special.GH_ValueListItem(preset_str, preset_str))

x, y = ghenv.Component.Attributes.Bounds.X, ghenv.Component.Attributes.Bounds.Y
vl.Attributes.Pivot = PointF(x, y)
vl.Attributes.ExpireLayout()
vl.Attributes.PerformLayout()
ghdoc = ghenv.Component.OnPingDocument()
ghdoc.AddObject(vl, False)

Thanks for your help and suggestions!

The same “problem” occurs when doing

io = gh.Kernel.GH_DocumentIO(ghenv.Component.OnPingDocument())
io.Save()

It tries to save the cluster, not the global document.

@martinborst1
I stumbled on your post, whilst trying to get the parent document as well.
Trying to solve it, here is a small recursive function that retrieves the Top parent document.

def getTopDoc(d):
    owner = None
    topDoc = d
    try:
        owner = d.Owner
    except:
        pass
        
    if owner != None:
        topDoc = getTopDoc(d.Owner.OnPingDocument())
        
    #type GH_Document
    return topDoc

doc = getTopDoc(ghenv.Component.OnPingDocument())
print doc
1 Like