Listing Game Properties

I’m in a position where I need to copy a game object’s properties, but I don’t know how to get a list of what it has. Since the objects being looked at aren’t uniform, I have to be able to read their dictionaries individually. Running an object through dir() isn’t showing me any property that I can use, and the object itself isn’t iterable.

How can I just get a list of what properties a game object is holding?

http://www.blender.org/documentation/blender_python_api_2_70_5/bge.types.KX_GameObject.html

getPropertyNames()
Gets a list of all property names.

[TABLE=“class: docutils field-list”]
[TR=“class: field-odd field”]
[TH=“class: field-name”]Returns:[/TH]
All property names for this object.
[/TR]
[TR=“class: field-even field”]
[TH=“class: field-name”]Return type:[/TH]
list
[/TR]
[/TABLE]

Example:


# Iterate through property names list of game object
# Pedantically, propertyName is a string and not the property itself
for propertyName in gameObject.getPropertyNames():

    # Retrieve property value directly from object, as API states they are accessible this way
    propertyValue = gameObject[propertyName]

    # Retrieve property value's type's name
    propertyType = type(propertyValue).__name__

    # Print out the property name, value, type
    print('{}: {}, {}'.format(propertyName, propertyValue, propertyType))

How the **** did I miss that. I am a moron.

It happens with me a lot as well, you just have so many menu’s and icons to deal with you don’t notice what’s right in front of you, Maya is even worse because when you click one menu several more pop up and so on :stuck_out_tongue:

Also it’s worthwhile to mention that objects also act as a list of the properties in it. So instead of object.getPropertyNames()[prop], you could just do object[prop].

@MichelleSea
I believe what you’re referring to is a CListValue like KX_Scene.objects etc. KX_GameObject.getPropertyNames() returns a standard Python list which is only accessible with an index (number).

A bonus example (to simulate something like iterating over a dictionary’s keys):


propertyNames = gameObject.getPropertyNames()
propertyValues = [gameObject[x] for x in propertyNames]
propertyTypes = [type(x).__name__ for x in propertyValues]

for name,value,ptype in zip(propertyNames, propertyValues, propertyTypes):

    print('{}: {}, {}'.format(name, value, ptype))