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

    Search object by partial name [SOLVED]

    Scheduled Pinned Locked Moved PYTHON Development
    5 Posts 0 Posters 405 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 11/07/2015 at 13:47, xxxxxxxx wrote:

      HI 🙂

      with this code, I find an select, only object by name Cube.

      import c4d

      def main() :
      > name = "Cube"
      >
      > obj = doc.SearchObject(name)
      >
      > doc.SetActiveObject(obj)
      >
      > c4d.EventAdd()
      >
      >
      >
      >
      >
      >
      if __name__ == "__main__":
          main()

      if I wanted to find all objects by partial Cube in name?, example: AlphaCube or CubeBetha or AlphaCubeBetha?

      Thanks!

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

        On 12/07/2015 at 04:02, xxxxxxxx wrote:

        Hello,

        you might use a hierarchy traversal(or lampda with a key) and check if the desired name is in the object´s name. Therefore a string could be treated pretty much like an array...

          
        import c4d  
          
          
        def GetNextObject(op) :  
          if op==None:  
              return None  
            
          if op.GetDown() :  
              return op.GetDown()  
            
          while not op.GetNext() and op.GetUp() :  
              op = op.GetUp()  
            
          return op.GetNext()  
          
        def HierarchyLoop(op,name) :  
          collection = []  
          while op:  
              if name in op.GetName() :  
                  collection.append(op)  
              op = GetNextObject(op)  
          return collection  
          
          
        def main() :  
          op = doc.GetFirstObject()  
          name = "Cube"  
          allObjectsWithName = HierarchyLoop(op,name)  
          print allObjectsWithName  
          
          
        if __name__=='__main__':  
          main()  
        

        Best wishes
        Martin

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

          On 13/07/2015 at 04:39, xxxxxxxx wrote:

          Hi,

          I assume, Martin's code already solved your problem.

          Nothing much to add, but for completeness sake:

          There's actually a function, that can search for an object with he closest matching name: SearchObjectInc() Unfortunately it returns only one object, so it's useful in very specific cases, only.

          And on hierarchy traversal we also have two articles on our blog:
          Non-recursive hierarchy iteration[URL-REMOVED]
          Recursive hierarchy iteration[URL-REMOVED]


          [URL-REMOVED] @maxon: This section contained a non-resolving link which has been removed.

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

            On 26/08/2015 at 09:35, xxxxxxxx wrote:

            Is this thread solved?

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

              On 26/08/2015 at 10:48, xxxxxxxx wrote:

              yes ^_^

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