Can't access the imported object (python)

Below is the code snippet giving me problems. The object loads, but I can’t access the guid in that last statement (I get an index out of range error as if there were no objects in the document, but the object is clearly there and selected). And it prints “loaded”, so the rs.Command is returning True.

Any ideas? (Or alternatives to using rs.Command? I tried using File3dm but could never figure out how to get the geometry into my document)

(ABE: Just in case it matters, this code is inside a class definition for a custom ETO dialog that is displayed semimodally. If certain values are changed in the form, I need to delete the current object and import another one in its place)

if self.myobject:
    deleted = rs.DeleteObject(self.myobject)
    if deleted:
        self.myobject= None

loaded = rs.Command("_-Import " + self.mypath + " _Enter")
if loaded:
    print("loaded")
self.myobject= rs.AllObjects()[0]

Update. It works if I do not show the dialog semi-modally. (I really like semi-modal, tho :frowning: )