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
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    logic problem to obtain the entire hierarchy of the object

    Cinema 4D SDK
    python windows 2024
    2
    2
    350
    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.
    • pyxelriggerP
      pyxelrigger
      last edited by

      https://developers.maxon.net/forum/topic/13045/alternative-way-to-iterate-list-other-than-getnext/3
      In my search for a solution, I came across this post that works well and returns a list of all child objects in order. However, is there a way to save lists within other lists containing all the objects so I can keep track of the hierarchy?

      maybe in this structure:
      a7f37e63-91f4-4a3f-ac41-dbd03b8ec298-image.png

               hierarchy = [obj1,  
               obj2[obj2.5, [obj2.75],
               obj3[obj4, [obj5]]
      
      i_mazlovI 1 Reply Last reply Reply Quote 0
      • i_mazlovI
        i_mazlov @pyxelrigger
        last edited by

        Hi @pyxelrigger ,

        Unfortunately your question is out of the scope of support on this forum, namely:

        We cannot provide support on learning C++, Python, or one of their popular third-party libraries.

        With that's said, I'm kind of missing the main idea behind your question, in other words what are you trying to achieve?

        Is it just another (e.g. simply more convenient) data structure for the hierarchy? If so, then you're free to create whatever data structure fits your need the best. For example, check a very fast draft code snippet below.

        Is it that you want to track changes of the hierarchy? If so, then you might need to come up with some more complex setup, e.g. involving using the c4d.EVMSG_CHANGE message. Please, refer to a great Ferdinand's answer here: Best way to detect an object has been deleted?

        Please also consider visiting our How to ask Questions section of the support procedures for your future postings.

        Cheers,
        Ilia

        A super draft example of nested lists hierarchy representation:

        import c4d
         
        doc: c4d.documents.BaseDocument
         
        class HierarchyObject:
            def __init__(self, op):
                self.op = op
                self.children = []
         
                child = self.op.GetDown() if op else doc.GetFirstObject()  # recursion entry point
                while child:
                    self.addChild(HierarchyObject(child))  # recursion
                    child = child.GetNext()
           
            def addChild(self, child):
                self.children.append(child)
           
            def toString(self, indentation: int = 0):
                indentStr: str = '\t' * indentation
                name: str = self.op.GetName() if self.op else '___root___'
                s: str = f'{indentStr}<{name}'
                if self.children:
                    s += ':'
                    for child in self.children:
                        s += f'\n{child.toString(indentation + 1)}'
                    s += f'\n{indentStr}'
                s += '>'
                return s
         
            def __str__(self):
                return self.toString()
         
         
        if __name__=='__main__':
            root: HierarchyObject = HierarchyObject(None)    
            print(root)
        

        MAXON SDK Specialist
        developers.maxon.net

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