I have a bunch of objects and need to create some common properties to all of them like this

for ob in bpy.data.objects:
    if ob.name.find("someName") >= 0:
        theObject = ob.game
        ...create new property in theObject, and set value
        ...create new property in theObject, and set value
        ...create new property in theObject, and set value
        ...create new property in theObject, and set value

The result should show the properties added to every object "someName" (someName.001, someName.002, etc) in the Logic Editor like this

enter image description here


There's the object.game.properties collection, but it doesn't seem to have a new method. You can use an operator to create the property, though, and then modify the value:

for ob in bpy.context.scene.objects:
    if ob.name.find("someName") >= 0:
        # Set active object first so operator works on the right one
        bpy.context.scene.objects.active = ob
        bpy.ops.object.game_property_new(type='STRING', name='foo')
        prop = ob.game.properties['foo']
        prop.value = 'bar'

I'm pretty sure the items in the list are kept in the order they are created, so you can also access the most recently-created property with ob.game.properties[-1].

| improve this answer | |

Add the properties to the active object using:

bpy.ops.object.game_property_new(type='FLOAT', name="")

Create a new property available to the game engine

Then copy to the rest of your selection:

bpy.ops.object.game_property_copy(operation='COPY', property='')

Copy/merge/replace a game property from active object to all selected objects


import bpy

bpy.ops.object.select_pattern(pattern="Cube*", extend=False)
if bpy.context.selected_objects:
    bpy.ops.object.game_property_new(type="STRING", name="foo")
    bpy.context.object.game.properties[-1].value = "bar"
    # ...
| improve this answer | |

Heres another method:

def addPropertyToObj(npType, npName, npVal, objPName, objPVal):
for obj in bpy.data.objects:
    for prop in  obj.game.properties:
        if prop.name== objPName and prop.value== objPVal:
            print("added new gameProperty to object: "+obj.name)
            bpy.context.scene.objects.active = obj
            bpy.ops.object.game_property_new(type=npType, name=npName)
            obj.game.properties[npName].value= npVal

will add the gameProperty: "npName" with type "npType" and initial value of "npVal" to ALL objects in scene with an existing gameProperty called "objPName" with value "objPVal" example: addPropertyToObj('BOOL', "usingFarModel", False, "structName", "MapleWood")

special thanks to z0r

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.