Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware 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
    • Register
    • Login

    How to Swap location in Object Manager

    Cinema 4D SDK
    c++ s26 windows
    2
    3
    392
    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.
    • P
      pchg
      last edited by

      Hello colleagues, I used C++ to write the C4D S26 plugin on Windows 10. Swap the positions of 2 and 10 in the Object Manager. I cannot find the relevant API.
      777.png
      888.png

      BaseDocument* doc = GetActiveDocument();
      Int32 count = selection->GetCount();
      AutoAlloc<AtomArray> selection;
      doc->GetActiveObjects(selection, GETACTIVEOBJECTFLAGS::CHILDREN);
      BaseObject* obj_2 = static_cast<BaseObject*>(selection->GetIndex(count - obj_10));
      BaseObject* obj_10 = static_cast<BaseObject*>(selection->GetIndex(count - obj_2));
      

      What should we do next?Thanks in advance!

      ferdinandF 1 Reply Last reply Reply Quote 0
      • ferdinandF
        ferdinand @pchg
        last edited by ferdinand

        Hey @pchg,

        Thank you for reaching out to us. There is no dedicated "Swap" function for node hierarchies. All the relevant tools can be found on cinema::GeListNode. The general drill would be here to either get the node before or after the nodes you want to swap, remove the nodes, and then use GeListNode::InsertBefore or InsertAfter to insert the nodes. Please note that other than in the Python API, there is no fail safe which prevents you from inserting nodes multiple times into a scene graph (which is not allowed and will lead to crashes).

        Cheeers,
        Ferdinand

        Code

        I wrote this 'blind' in a text editor, i.e., this is uncompiled code to demonstrate the principle:

        BaseObject* const a = static_cast<BaseObject*>(selection->GetIndex(count - obj_10));
        BaseObject* const b = static_cast<BaseObject*>(selection->GetIndex(count - obj_2));
        if (!a || !b)
          return maxon::UnexpectedError(MAXON_SOURCE_LOCATION, "a or b is nullptr"_s);
        
        // Get the predecessor of the objects a and b which can be a null pointer when either a or b is the 
        // first object in the hierarchy. We could then use either the next object as the insertion point or
        // use GeListNode::InsertUnder or BaseDocument::InsertObject to insert the object at the top of its
        // hierarchy local hierarchy. I went here for the latter as for the next object you also have to deal
        // with the case that there is neither a predecessor nor a next object.
        BaseObject* const predA = a->GetPred();
        BaseObject* const predB = b->GetPred();
        BaseObject* const parentA = a->GetUp();
        BaseObject* const parentB = b->GetUp();
        
        // Remove both objects from the hierarchy.
        a->Remove();
        b->Remove();
        
        // Insert the objects back into the hierarchy, either after the predecessor, under the parent, 
        // or at the top of the document hierarchy.
        if (predA) 
          b->InsertAfter(predA);
        else if (parentA)
          b->InsertUnder(parentA); // Will insert at the top of the hierarchy.
        else
          doc->InsertObject(b, nullptr, nullptr); // Will insert at the top of the hierarchy.
        
        if (predB)
          a->InsertAfter(predB);
        else if (parentB)
          a->InsertUnder(parentB);
        else
          doc->InsertObject(a, nullptr, nullptr);
        

        MAXON SDK Specialist
        developers.maxon.net

        P 1 Reply Last reply Reply Quote 0
        • P
          pchg @ferdinand
          last edited by

          @ferdinand Thank you very much for your reply

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