Iterate recursively & replace [SOLVED]

On 29/05/2015 at 13:07, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   R15 
Platform:   Windows  ;   
Language(s) :     C++  ;   PYTHON  ;

---------
    Hi everybody,

I tried to write a method that is able to replace objects on the fly, respecting hierarchy.
Turned out, it's not that easy - thanks to recursion. ;)

Anybody has an idea and a solution to this problem?:

Working code example, minified. Problem is basically, that the method behaves abnormally when there's a single object on top without hierarchy:

  
void ConvertObjects(BaseObject* srcObj)   
{   
        while (srcObj)   
        {   
               if (srcObj->GetDown())   
                        ConvertObjects(srcObj->GetDown());   
  
               if (srcObj->GetType() == Olight)   
               {   
                        BaseObject* myObj = BaseObject::Alloc(Ocone);   
                        // Do other stuff here   
                        doc->InsertObject(myObj, srcObj->GetUp(), srcObj->GetPred());   
  
                        BaseObject* child = srcObj->GetDown();   
                        if (child)   
                        {   
                                child->Remove();   
                                child->InsertUnderLast(myObj);   
                        }   
                        srcObj->Remove();   
                        srcObj = doc->GetFirstObject();   
               }   
               srcObj = srcObj->GetNext();   
        }   
}   

edit: In Py, this works just fine:

  
import c4d   
from c4d import gui   
  
def ConvertObjects(srcObj) :   
    while srcObj:   
        print("run")   
        if srcObj.GetDown() :   
            ConvertObjects(srcObj.GetDown())   
          
        if srcObj.GetType() == c4d.Olight:   
            print(srcObj.GetName())   
            myObj = c4d.BaseObject(c4d.Ocone)   
            doc.InsertObject(myObj, srcObj.GetUp(), srcObj.GetPred())   
               
            child = srcObj.GetDown()   
            if child:   
               child.Remove()   
               child.InsertUnderLast(myObj)   
            srcObj.Remove()   
            srcObj = doc.GetFirstObject()   
               
        srcObj = srcObj.GetNext()   
  
    return   
def main() :   
    doc = c4d.documents.GetActiveDocument()   
    if doc:   
       ConvertObjects(doc.GetFirstObject())   
    c4d.EventAdd()   
  
if __name__=='__main__':   
    main()   
  

edit #2:
I just noticed that in Python, it raises an error ("Object is not alive"). Where's my mistake?

On 29/05/2015 at 14:41, xxxxxxxx wrote:

Nevermind, figured it out. Just forgot to update the srcObj assignemtn to reference the newly created object. :)

On 08/06/2015 at 08:18, xxxxxxxx wrote:

Hi,

Since you indicate you solved your problem, and it's been over a week since the last post, I'm tagging this topic as solved.

Joey Gaspe
SDK Support Engineer