Search object by partial name [SOLVED]

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!

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

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
Recursive hierarchy iteration

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

Is this thread solved?

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

yes ^_^