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:
- This is the confirmation that the Hyperfile was written to disk from the save_hyperfile function
- This is the print of the object from the BaseContainer at line 49 (in the save_hyperfile function)
- This is from the load_hyperfile function after loading the Hyperfile from disk. The Atom is now None.
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!