Solved Finding name/handle to last object merged into scene

After calling MergeDocument()

merge_status = c4d.documents.MergeDocument(doc, path,
                                           c4d.SCENEFILTER_MERGESCENE | c4d.SCENEFILTER_OBJECTS)

I check merge_status to be true and wish to find some handle/name to the object associated with the call to MergeDocument(), how do I do that ?

I tried the following on an empty scene

        doc = c4d.documents.GetActiveDocument()
        ao = doc.GetActiveObject()

that works if I am calling MergeDocument for a single path but if I am calling MergeDocument (in a loop) for a collection of paths, GetActiveObject() points to the same object, not the next one corresponding to the next path I am merging into the scene

Cheers

Hi,

there's no direct way of doing it. You must store the different object. Either you can store the original object's list and select/inverse the selection to retrieve all the objects that were not in the original document. Or you can store the object in a list each time you import a document.

import c4d
import os
from c4d import gui


def GetNextObject(op):
    if op==None:
        return None
  
    if op.GetDown():
        return op.GetDown()
  
    while not op.GetNext() and op.GetUp():
        op = op.GetUp()
  
    return op.GetNext()
 

def GetOriginalObjectList():
    originalObjects = list()
    child = doc.GetFirstObject()
    if child is None:
        return originalObjects
    
    while child:
        originalObjects.append(child)
        child = GetNextObject(child)
    
    return originalObjects




def main():
    # Store the original object in a list
    originalObjects = GetOriginalObjectList()
    mergedObject = list()
    
    path="D:\\MAXON\\temp\\merging_doc"
    for root, dirs, files in os.walk(path):
        for name in files:
            filePath = os.path.join(path,name)
            merge_status = c4d.documents.MergeDocument(doc, filePath, c4d.SCENEFILTER_MERGESCENE | c4d.SCENEFILTER_OBJECTS)
            if merge_status:
                mergedObject.append(doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_CHILDREN))

    c4d.CallCommand(100004767) # Deselect All object
    # select all object that were on the orignal document
    for obj in originalObjects:
        obj.SetBit(c4d.BIT_ACTIVE)
    
    c4d.CallCommand(100004794) # Invert Selection    

    #Print all the object list we gather during the import.
    for objList in  mergedObject:
        print (objList)
          
    c4d.EventAdd()

# Execute main()
if __name__=='__main__':
    main()

Cheers,
Manuel

MAXON SDK Specialist

MAXON Registered Developer

Hi,

there's no direct way of doing it. You must store the different object. Either you can store the original object's list and select/inverse the selection to retrieve all the objects that were not in the original document. Or you can store the object in a list each time you import a document.

import c4d
import os
from c4d import gui


def GetNextObject(op):
    if op==None:
        return None
  
    if op.GetDown():
        return op.GetDown()
  
    while not op.GetNext() and op.GetUp():
        op = op.GetUp()
  
    return op.GetNext()
 

def GetOriginalObjectList():
    originalObjects = list()
    child = doc.GetFirstObject()
    if child is None:
        return originalObjects
    
    while child:
        originalObjects.append(child)
        child = GetNextObject(child)
    
    return originalObjects




def main():
    # Store the original object in a list
    originalObjects = GetOriginalObjectList()
    mergedObject = list()
    
    path="D:\\MAXON\\temp\\merging_doc"
    for root, dirs, files in os.walk(path):
        for name in files:
            filePath = os.path.join(path,name)
            merge_status = c4d.documents.MergeDocument(doc, filePath, c4d.SCENEFILTER_MERGESCENE | c4d.SCENEFILTER_OBJECTS)
            if merge_status:
                mergedObject.append(doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_CHILDREN))

    c4d.CallCommand(100004767) # Deselect All object
    # select all object that were on the orignal document
    for obj in originalObjects:
        obj.SetBit(c4d.BIT_ACTIVE)
    
    c4d.CallCommand(100004794) # Invert Selection    

    #Print all the object list we gather during the import.
    for objList in  mergedObject:
        print (objList)
          
    c4d.EventAdd()

# Execute main()
if __name__=='__main__':
    main()

Cheers,
Manuel

MAXON SDK Specialist

MAXON Registered Developer

Hello @nicholas_yue,

without any further questions or postings, we will consider this topic as solved by Wednesday and flag it accordingly.

Cheers,
Ferdinand

MAXON SDK Specialist
developers.maxon.net