Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Iterate recursively & replace [SOLVED]

    SDK Help
    0
    3
    274
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      Helper
      last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • H
        Helper
        last edited by

        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. 🙂

        1 Reply Last reply Reply Quote 0
        • H
          Helper
          last edited by

          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

          1 Reply Last reply Reply Quote 0
          • First post
            Last post