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

    Get all objects in doc [SOLVED]

    Scheduled Pinned Locked Moved PYTHON Development
    9 Posts 0 Posters 919 Views
    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 Offline
      Helper
      last edited by

      On 22/10/2014 at 11:53, xxxxxxxx wrote:

      Hi,

      Is there a way to get all objects with a filter without having them selected?

      GetActiveObjectsFilter(...)

      That command requires them to be selected.. I currently have a script that calls the command to select all objects, grab the objects I want, the deselect all. But it gets messy when I want to have undos.

      Is there a better way?

      Thanks, 
      Joe

      Heres the code I currently have..

      def getAllNulls() :
          doc = c4d.documents.GetActiveDocument()
          c4d.CallCommand(100004766) #select all
          allNulls = doc.GetActiveObjectsFilter(True, type=5140, instanceof=True)
          c4d.CallCommand(100004767) #deselect all
          return allNulls
      
      1 Reply Last reply Reply Quote 0
      • H Offline
        Helper
        last edited by

        On 22/10/2014 at 12:07, xxxxxxxx wrote:

        If you want your undo function wrapped around your method, it should work fine, I guess.
        Like this:

        def otherMethod
          do some stuff
          doc.StartUndo()
          doc.AddUndo()
          exampleList = getAllNulls()
          doc.EndUndo()

        I have just started with python, so I could be wrong very much, hope it helps.

        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

          On 22/10/2014 at 12:37, xxxxxxxx wrote:

          The problem is that the select-all and deselect-all adds undo events which are system made, so I cant get around them..

          I need to avoid the CallCommands..

          1 Reply Last reply Reply Quote 0
          • H Offline
            Helper
            last edited by

            On 22/10/2014 at 13:00, xxxxxxxx wrote:

            Yea, you should definitely be avoiding CallCommand(), especially for such a trivial task.
            You can use a recursive function to collect a list of all objects in the scene, or even write a generator
            function which will be far more memory efficient.

            There are plenty topics covering recursive and even non-recursive walking of the object hierarchy
            here on the forum. Give the search or googles site: filter a try, and link here what helped you
            finding the solution.

            -Niklas

            1 Reply Last reply Reply Quote 0
            • H Offline
              Helper
              last edited by

              On 22/10/2014 at 13:06, xxxxxxxx wrote:

              Right. I tried getting the top object and walking down the hierarchy. But seems like issues when there are children, then sub children, (and even more children).

              If anyone has had luck with looking through a document, please share.

              There should really be something in the API for this.

              1 Reply Last reply Reply Quote 0
              • H Offline
                Helper
                last edited by

                On 22/10/2014 at 13:31, xxxxxxxx wrote:

                It's not in the API because only few built-in commands work on all objects in the scene of a specific type.
                And another reason could be that its just a few lines, which however often need small adjustments.

                This is a very generic approach:

                import c4d
                  
                def get_all_objects(op, filter, output) :
                    while op:
                        if filter(op) :
                            output.append(op)
                        get_all_objects(op.GetDown(), filter, output)
                        op = op.GetNext()
                    return output
                  
                def main() :
                    nulls = get_all_objects(doc.GetFirstObject(), lambda x: x.CheckType(c4d.Onull), [])
                    print len(nulls)
                    
                main()
                
                1 Reply Last reply Reply Quote 0
                • H Offline
                  Helper
                  last edited by

                  On 22/10/2014 at 13:56, xxxxxxxx wrote:

                  Hmmm.. that's a bit more advanced then I thought. I'm not too familiar with lambda and its functions.

                  That function doesn't work when theres children.. I'll have to mess around with it.

                  1 Reply Last reply Reply Quote 0
                  • H Offline
                    Helper
                    last edited by

                    On 22/10/2014 at 14:00, xxxxxxxx wrote:

                    Ah I think I got it.

                    after the while loop, you had the variables switched.

                    get_all_objects(op.GetDown(), filter, output)

                    1 Reply Last reply Reply Quote 0
                    • H Offline
                      Helper
                      last edited by

                      On 22/10/2014 at 16:33, xxxxxxxx wrote:

                      Uups, that was a mistake of mine. 🙂 fixed

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