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
    • Login

    GetChildren into an InExcludeData

    Cinema 4D SDK
    2024 python
    2
    3
    507
    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.
    • H
      hoffwerr
      last edited by

      Hi there!

      I'm trying to add all the children (no grandchildren) underneath an object to an InExcludeData on the parent.

      This s what I could come up with but it doesn't seem to work for some reason that I can't figure out.

      import c4d
      
      def main():
          obj = op.GetObject()
          print (obj)
          children = obj.GetChildren()
          print (children)
          
          GroupList = c4d.InExcludeData()
          GroupList.InsertObject(children, 1)
          obj[c4d.ID_USERDATA,1] = GroupList
          c4d.EventAdd()
      

      This is the error I'm getting.

      Traceback (most recent call last):
        File "Python", line 10, in main
      TypeError: argument 1 must be c4d.BaseList2D, not list
      

      TEST_ADD_HEIARCHY.c4d
      Any idea how to fix that ?

      i_mazlovI 1 Reply Last reply Reply Quote 0
      • i_mazlovI
        i_mazlov @hoffwerr
        last edited by i_mazlov

        Hi @hoffwerr,

        First of all, please make sure you actually visited our Support Procedures page.

        Your issue here is a very basic python type mismatch. I know that dealing with some 3rd party APIs and leaky documentation can be frustrating and at some point even obvious things can be perceived as impossible, but

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

        Regarding your question. The issue here is that you're trying to insert the list of objects list[c4d.BaseObject] using function InsertObject() that only expects a single object. You need to iterate your list of objects, e.g. using for loop:

        for child in children:
            GroupList.InsertObject(child, 1)
        

        A couple more things about your code.

        1. Calling c4d.EventAdd() is useless from within the Python tag, as mentioned in the documentation, it does nothing in this case
        2. Populating your InExcludeList every time the python tag is executed is not ideal (it's slow!). Hence, you'd need some smarter way to distinguish when you need to repopulate the list and when it is not necessary. There's at least one thread about it, where Maxime mentioned this issue: Execute python tag only when hierarchy changes. Please check the code snippet below that implements a very draft way of doing so, simply by comparing the number of children. Ideally, you need to keep track of the entire children list and do actual comparison there, but this can be redundant depending on your goals.

        There's one another thing. Please do not give up old threads and start new ones instead, this only makes more clutter on the forum and doesn't help with processing your questions. Hence I've resurrected your previous thread. For your future threads please consolidate your consequent answers in a single posting and do not duplicate threads.

        Cheers,
        Ilia

        Python tag script (to be inserted on a Group effector):

        import c4d
        
        doc: c4d.documents.BaseDocument  # The document containing this field object.
        op: c4d.BaseTag # The Python tag containing this code.
        
        childrenCount: int = 0  # keep number of children globally between main() calls
        
        def main() -> None:
            global childrenCount
            obj: c4d.BaseObject = op.GetObject()
        
            children: list[c4d.BaseObject] = obj.GetChildren()
            if childrenCount == len(children):  # early exit if number of children hasn't changed
                return
        
            childrenCount = len(children)
            print('Updating InExclude list')
        
            inexcl = c4d.InExcludeData()
            for child in children:  # Process children one-by-one
                inexcl.InsertObject(child, 1)
        
            obj[c4d.MGGROUPEFFECTOR_EFFECTORLIST] = inexcl
        

        MAXON SDK Specialist
        developers.maxon.net

        1 Reply Last reply Reply Quote 1
        • H
          hoffwerr
          last edited by

          Thank you so much @i_mazlov . I'm still new to this and your reply is extremely helpful. Thanks again!
          Apologies for the double post. I wanted to completely remove the old post since I kinda spammed too many replies on it while I was figuring out what was wrong.
          Appreciate the help. Have a good day!

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