getting a list of objects that have a certain property?

hello blender nation!
i was wondering if somone would know how i could get a list of the object names that have a certain property.

im working on an AI and what i would like to do is get a list of all the objects that have a certain property, then get the value of the property that those objects have. for example, if the property im looking for is “number” i would like to be able to find all of the objects with the property “number”. lets say “cube1” “cube2” and “cube3” have this property. i would then like to get the value that each of those objects have. for instance:
cube1number = 1
cube2number = 3
cube3number = 601
i hope i explained it correctly. and thank you for reading :slight_smile:

Here’s one way:


scene = GameLogic.getCurrentScene()
objects = scene.objects

for obj in objects:#check object has a certain property 
if obj.has_key('<i>someProperty</i>')[INDENT]#You could append obj or it's property to a list here,
#for examples sake I've just printed each object with that property and the property value.

print obj.name, obj['<i>someProperty</i>']
[/INDENT]

Hope this helps!

or just this:

 
propName = "whateverThePropertyNameIs"
 
objectsWithProp = [obj for obj in GameLogic.getCurrentScene().objects if propName in obj]