Hello @derekheisler,
Welcome to the Plugin Café forum and the Cinema 4D development community, it is great to have you with us!
Getting StartedBefore creating your next postings, we would recommend making yourself accustomed with our Forum and Support Guidelines, as they line out details about the Maxon SDK Group support procedures. Of special importance are:
Support Procedures: Scope of Support: Lines out the things we will do and what we will not do. Support Procedures: Confidential Data: Most questions should be accompanied by code but code cannot always be shared publicly. This section explains how to share code confidentially with Maxon. Forum Structure and Features: Lines out how the forum works. Structure of a Question: Lines out how to ask a good technical question. It is not mandatory to follow this exactly, but you follow the idea of keeping things short and mentioning your primary question in a clear manner. About your First QuestionPlease excuse the slight delay and thank you for the community answer provided by @HerrMay.
The core misconception in your code lies in the line objs = doc.GetObjects(), as it does not get a list of all objects in doc, but only the root level objects. Users are expected to do scene graph traversal on their own at the moment, there is no method which would do it for you. There are also other problems with your code, as for example trying to instantiate a BaseThread or using CallCommand (not a real problem but not recommended in most cases).
There have been also multiple similar threads in the past:
How do you collapse complex dependencies in order?: This is about implementing a connect and delete. How to traverse a GeListNode Tree?: This is about hierarchical traversal, i.e., getting all descendants and siblings for a node. There is also something that I would call branch traversal, it is much more complex and we have talked about it for example here. CAD Normal Tag flipped: Entirely different topic, but could give you an idea on how to realize a simple GUI for this.Finde below a simple example.
Cheers,
Ferdinand
Result:
collapse.gif
Code:
"""Demonstrates how to collapse all SDS objects in a scene. One could also make this more fancy with a GUI and undo steps, I did not do this here. """ import c4d doc: c4d.documents.BaseDocument # The active document. def IterNodes(node: c4d.GeListNode) -> c4d.GeListNode: """Yields all descendants and next-siblings of `node` in a semi-iterative fashion. """ if node is None: return while node: yield node # The recursion moving down. for descendant in IterNodes(node.GetDown()): yield descendant # The iteration in one level. node = node.GetNext() def Collapse(objects: list[c4d.BaseObject]) -> None: """Collapses all items in #objects as individual root nodes into singular objects. This function mimics the behaviour of the builtin (but unexposed) "Connect & Delete" command by first running the "CSTO" and then "JOIN" command. With setups complex enough, this can still fail due to the non-existent dependency graph of the classic API (when one does CSTO things in the wrong order). In 99.9% of the cases this will not be the case, but one should get the inputs with #GETACTIVEOBJECTFLAGS_SELECTIONORDER as I did below to give the user more control. (or alternatively do not batch operate). """ if len(objects) < 1: raise RuntimeError() doc: c4d.documents.BaseDocument = objects[0].GetDocument() doc.StartUndo() # CSTO all local hierarchies in #objects and replace these root nodes with their collapsed # counter parts. result = c4d.utils.SendModelingCommand(c4d.MCOMMAND_CURRENTSTATETOOBJECT, objects, c4d.MODELINGCOMMANDMODE_ALL, c4d.BaseContainer(), doc, c4d.MODELINGCOMMANDFLAGS_NONE) if not result or len(result) != len(objects): raise RuntimeError() for old, new in zip(objects, result): parent, pred = old.GetUp(), old.GetPred() doc.AddUndo(c4d.UNDOTYPE_DELETEOBJ, old) old.Remove() doc.InsertObject(new, parent, pred) doc.AddUndo(c4d.UNDOTYPE_NEWOBJ, new) # Join the CSTO results root by root object, and then replace the CSTO results with the final # collapsed result. JOIN is a bit weird when it comes to transforms, so we must store the # transform of the to be joined object, then zero it out, and finally apply it to the joined # result again. for obj in result: mg: c4d.Matrix = obj.GetMg() obj.SetMg(c4d.Matrix()) joined = c4d.utils.SendModelingCommand(c4d.MCOMMAND_JOIN, [obj], c4d.MODELINGCOMMANDMODE_ALL, c4d.BaseContainer(), doc, c4d.MODELINGCOMMANDFLAGS_NONE) if not joined: raise RuntimeError() parent, pred = obj.GetUp(), obj.GetPred() doc.AddUndo(c4d.UNDOTYPE_DELETEOBJ, obj) obj.Remove() new: c4d.BaseObject = joined[0] new.SetMg(mg) doc.InsertObject(new, parent, pred) doc.AddUndo(c4d.UNDOTYPE_NEWOBJ, new) doc.EndUndo() c4d.EventAdd() def main() -> None: """Runs the example. """ # Find all SDS objects in the scene. objectList: list[c4d.BaseObject] = [ n for n in IterNodes(doc.GetFirstObject()) if n.CheckType(c4d.Osds)] # Collapse all SDS objects we have found. for obj in objectList: # We have to check if #obj is still a valid reference because we could have already # collapsed the object away in a prior iteration; sds-objects can live inside sds-objects. if not obj.IsAlive(): continue # Collapse the object. Collapse([obj]) c4d.EventAdd() if __name__ == "__main__": main()