Merge IsolateObjects into a second document



  • I have a list of objects in one document (OptimizedDoc) that I want to copy over into another (doc) with materials and tags. My current code is:

    tempDoc = c4d.documents.IsolateObjects(OptimizedDoc, Transfer_Objects)
    c4d.documents.MergeDocument(doc, tempDoc, c4d.SCENEFILTER_NONE)
    

    I get - TypeError: unable to convert c4d.documents.BaseDocument to @net.maxon.interface.url-C
    The documentation says the second spot on Merge Document should be the file to merge into doc, but I think the way I'm presenting it is incorrect. Am I supposed to be pointing to a file location in memory? Thanks!



  • hi,

    you have to save the document to memory and use the result to merge the document.
    When you merge, you have to set the flags to what you want to merge, otherwise it will not merge what you want.

    On this example i'm merging a document with another selected document.

    import c4d
    from c4d import gui
    # Welcome to the world of Python
    
    
    # Main function
    def main():
        
        obj = doc.GetActiveObject()
        if obj is None:
            return
        # Isolate the active object
        isolateDoc = c4d.documents.IsolateObjects(doc, [obj])
        
        # Creates the Memory File Strcuture
        mfs = c4d.storage.MemoryFileStruct()
        
        # Sets the mfs to write mode
        mfs.SetMemoryWriteMode()
        # save the document to mfs
        c4d.documents.SaveDocument(isolateDoc, mfs, c4d.SAVEDOCUMENTFLAGS_DONTADDTORECENTLIST, c4d.FORMAT_C4DEXPORT)
        # retrieve the data    
        data = mfs.GetData()
        # Set the MFS to read mode
        mfs.SetMemoryReadMode(data[0], data[1] )    
        
        
        flags =  c4d.SCENEFILTER_OBJECTS | c4d.SCENEFILTER_MATERIALS
        
        # Load Document 
        selectedFile = c4d.storage.LoadDialog()
        if selectedFile is None:
            return
        newDoc = c4d.documents.LoadDocument(selectedFile, flags)
        
        
        #Merge the isolated document with the current one.
        
        c4d.documents.MergeDocument(newDoc, mfs, flags)
        
        c4d.documents.InsertBaseDocument(newDoc)
        c4d.documents.SetActiveDocument(newDoc)
        
    
    # Execute main()
    if __name__=='__main__':
        main()
    

    Cheers,
    Manuel