Hello @mogh,
@mogh said in Best practice getting all objects in a certain Null:
I will update and set to solved when I do get a prototype working. please be patient.
There is no need to feel rushed. We close threads after 14 days of inactivity, but when users say they want to keep them open when we request a closing, we keep them open much longer. It is only that we will ask every 14 days.
my problem ist the
if not c4d.documents.MergeDocument(doc, mfs, loadflags=flags):
it fails while your
if c4d.documents.MergeDocument(doc, mfs, loadflags=flags):
The negation operator is certainly not the culprit, at least I could not imagine how. A Python object that is not alive anymore, means that the Python bindings and the C++ backend have gone out of sync. Or in other words, there is still a reference to an object in Python but the data it is wrapping living in the C++ backend has been deallocated. You can test an object for being still alive with c4d.C4DAtom.IsAlive()
.
However, when I looked at your script, I first ran into similar problems. The culprit must have been the document inside the MFS, because when I tested doc
it was still alive, as it was still open in the editor (c4d.documents.BaseDocument
is also a node, i.e., derived from C4DAtom
). But then the problem vanished, and I could not reproduce it anymore. I do not think that your code is or my code was the issue. I would recommend restarting Cinema 4D and see if the problem then does vanish. Otherwise we have to see if either the MFS or MergeDocument
Python wrappers produces dangling pointers in some cases.
FYI: I just ran the code below, i.e., with a not
, and it runs fine for me.
Cheers,
Ferdinand
import c4d
def create_obj(doc):
"""
"""
obj = c4d.BaseObject(c4d.Ocube)
obj.SetName("importedcube")
doc.InsertObject(obj)
def main():
"""
"""
# I removed a lot of clutter here.
temp = c4d.documents.BaseDocument()
create_obj(temp)
mfs = c4d.storage.MemoryFileStruct()
mfs.SetMemoryWriteMode()
c4d.documents.SaveDocument(temp, mfs,
c4d.SAVEDOCUMENTFLAGS_DONTADDTORECENTLIST, c4d.FORMAT_C4DEXPORT)
data = mfs.GetData()
mfs.SetMemoryReadMode(data[0], data[1])
# In this case you want to load in the objects and materials I assume.
flags = c4d.SCENEFILTER_MERGESCENE | c4d.SCENEFILTER_OBJECTS | c4d.SCENEFILTER_MATERIALS
if not c4d.documents.MergeDocument(doc, mfs, loadflags=flags):
print("Blah")
# You were missing an event add.
c4d.EventAdd()
# Execute main()
if __name__ == '__main__':
c4d.CallCommand(13957) # clear concole
main()