Retrieving Link from Hyperfile



  • Hello,
    I'm having an issue saving a link within a BaseContainer to a Hyperfile, then loading it.

    This is what happens in my Console in these three lines:

    1. This is the confirmation that the Hyperfile was written to disk from the save_hyperfile function
    2. This is the print of the object from the BaseContainer at line 49 (in the save_hyperfile function)
    3. This is from the load_hyperfile function after loading the Hyperfile from disk. The Atom is now None.
      hyperfile.png

    Here is the code I'm using:

    import c4d, os
    from c4d import documents, bitmaps, gui
    from c4d.gui import GeDialog
    
    key = 12345
    OBJ_BC_ID = 90210
    OBJ_ID = 1000
    
    def load_hyperfile():
        global doc
        path = c4d.storage.LoadDialog()
    
        hf = c4d.storage.HyperFile()
        if not hf.Open(ident=key, filename=path, mode=c4d.FILEOPEN_READ,
                       error_dialog=c4d.FILEDIALOG_NONE):
            # Meaningful exception for when read access fails goes here.
            print "Could not read file."
            return False
        bc = hf.ReadContainer()
    
        for cid, value in bc[OBJ_BC_ID]:
            print cid, value
    
        obj = bc[OBJ_BC_ID].GetLink(OBJ_ID,doc)
        print obj
    
        hf.Close()
    
    
    def save_hyperfile(obj):
        path = c4d.storage.SaveDialog(title="Save your Pose Library",force_suffix="file")
    
        bc = c4d.BaseContainer()
        objBc = c4d.BaseContainer()
        objBc.SetLink(OBJ_BC_ID,obj)
        bc.SetContainer(OBJ_BC_ID, objBc)
    
        hf = c4d.storage.HyperFile()
        if not hf.Open(ident=key, filename=path, mode=c4d.FILEOPEN_WRITE,
                       error_dialog=c4d.FILEDIALOG_ANY):
            # Meaningful exception for when write access fails goes here.
            print "Could not write file."
            return False
        else:
            print "Wrote file at %s"%path
    
        hf.WriteContainer(bc)
    
        for cid, value in bc[OBJ_BC_ID]:
            print cid, value
    
        hf.Close()
    
    class Dlg(gui.GeDialog):
        def __init__(self):
            global doc,obj
    
        def CreateLayout(self):
            self.SetTitle('Hyperfile')
            self.GroupBegin(1001, c4d.BFH_SCALEFIT, 1, 2)
            self.AddButton(1002, c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT, initw=0, inith=20, name="Save Hyperfile")
            self.AddButton(1003, c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT, initw=0, inith=20, name="Load Hyperfile")
            return True
    
        def Command(self, id, msg):
            if id == 1002:
                save_hyperfile(obj)
            elif id == 1003:
                load_hyperfile()
            return True
    
    def main():
        global doc,obj
        doc = documents.GetActiveDocument()
        obj = doc.GetActiveObject()
        if obj is None:
            gui.MessageDialog("Please select an object to save.")
            return
        dlg = Dlg()
        dlg.Open(c4d.DLG_TYPE_MODAL_RESIZEABLE, xpos=-2, ypos=-2, defaultw=580, defaulth=380)
    
    if __name__=='__main__':
        main()
    

    Can anyone please help me to understand how to get this link back from the Hyperfile? Thank you!



  • Hi blastframe, thanks for reaching out us.

    With regard to the issue mentioned, beside the lack of the BaseDocument instance to be passed to the BaseContainer::GetLink() method I'm confused about the scope of your script. What's the final goal? Saving an object to an hyperfile? Or actually saving a link to refer an object to another in the hyperfile?
    Last but not least, it's a bit weird that the dialog should not pop-up if there's no active object.

    Lookng forward your comments, give best
    Riccardo



  • Thank you @r_gigante for the reply!

    Sorry if the intent is unclear: this self-contained script was something I came up with last night to demonstrate an issue I'm having in my plugin which is too many scripts to post to the forums. Since the user can't select an object after the script is running, I added that catch to make sure an object was selected to show what was going wrong.

    My Goal
    I am trying to save a link to a scene object and its matrix to a hyperfile so the user can load the hyperfile in another session and restore the link to the object so that I can apply the saved matrix. I don't need the object itself.

    Update
    I updated the original script's GetLink() method to pass a reference to the active document, but that did not solve the issue.


Log in to reply