How to Swap location in Object Manager
-
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.
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!
-
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
orInsertAfter
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,
FerdinandCode
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);
-
@ferdinand Thank you very much for your reply