Find children by property

Hi :slight_smile:
How can I find children by property? (with python)
For example object have some children and only one have that property.

The same way you find any other object by Python - search through a list


childrenWithProperty = findObjectsWithProperty("property", parentObject.childrenRecursive)

I guess you next question is how to implement findObjectsWithProperty, here is one possible implementation:


def findObjectsWithProperty(property, objects)
    return [object for object in objects
            if property in object]

Be aware it returns a list that:

  • can be empty
  • can contain one or more objects

For speed test.


....
def findObjectsWithProperty(property,objects):
    
    print([object for object in objects if property in objects])
           
findObjectsWithProperty('jerzy',own.children) 

yee empty :frowning:

Did you spell the property correctly? Is the child included in own.children? Also, own.children only returns the direct children of the parent object, while own.childrenRecursive returns all of the children, grandchildren, etc. of the parent object.

yeahh I got it :smiley:

for x in range(len(own.children)):  
    for y in range(len(own.children[x].getPropertyNames())):
        if own.children[x].getPropertyNames()[y] == 'jerzy':
            print(own.children[x])

Did you spell the property correctly? Is the child included in own.children? Also, own.children only returns the direct children of the parent object, while own.childrenRecursive returns all of the children, grandchildren, etc. of the parent object.

Yes, I checked it 10x times. i don’t know why doesn’t return anything.

You have made a typo, the list comprehension is looking for a property in the object list, not the object (you have written ‘property in objects’)
You shouldn’t use ranges and get property names to do this, it is rather unpythonic

You are rigth, i was typong to fadt. … Removed the s

I don’t think it was your code monster

Yes, it was. The objects came so easy, and I didn’t tested the code. No wonder there was no result.

Ah, I see. At least it works now!