Parsing the object tree recursivly?



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 21/01/2006 at 05:35, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   R9.5 
    Platform:   Windows  ;   Mac OSX  ; 
    Language(s) :     C++  ;

    ---------
    For an exporter I would like to parse the object tree.

    What's the most elegant way to do this recursively, especially to ensure that visibility, material etc. are read correctly in the scope.

    Does GetNext() find child objects as well or does it work just in the same level of the hierarchy?

    Thanks

    Kabe



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 21/01/2006 at 06:50, xxxxxxxx wrote:

    You need to use GetDown to get the child, GetNext does only work on the same level. Search the forum for Hierarchy or so. There is coffee code by Mikael there somewhere, that is easily adaptable to C++.

    HTH



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 21/01/2006 at 08:34, xxxxxxxx wrote:

    My standard C++ recursive hierarchy traversal (example) :

    // Remove Child Selections with Selected Parents - recursive  
    //*---------------------------------------------------------------------------*  
    void Selections::RemoveChildSelections(BaseObject* obj)  
    //*---------------------------------------------------------------------------*  
    {  
         for (obj; obj; obj = obj->GetNext())  
         {  
              // Children first  
              if (obj->GetDown())     RemoveChildSelections(obj->GetDown());  
              // Operate on current object  
         }  
    }  
    

    Note that whether you do children first or last can have an impact depending on what you are doing. For instance, when making objects editable or doing bone rotations, better to do them first. Watch your stack usage!



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 22/01/2006 at 03:40, xxxxxxxx wrote:

    Thanks, that was very helpful.

    I would suggest that the clarification about GetNext() should find it's way into the SDK docs :D

    Kabe



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 22/01/2006 at 05:48, xxxxxxxx wrote:

    A couple of other important areas of note, especially if you find any object loop causing you grief:

    * Some operations are best done backwards, so you'll want to start at the bottom/back of the list:

    for (obj; obj; obj = obj->GetPred()) {...}
    

    and

    if (obj->GetDownLast()) Func(obj->GetDownLast());
    

    * Operations where you might be shuffling, removing, or adding objects will cause pointer errors, so store them in such circumstances:

    for (obj; obj; obj = nextObj) { nextObj = obj->GetNext(); ... }
    

    I've become use to looking for these, but they are a b!tch to find if you don't know about them.

    HTH!


Log in to reply