Add / remove and update game properties on multi objects in one go (now an addon)

i have written a scripts that makes it easy to add / remove and update game properties on multi objects in one go, you only need to select 1 of each object that needs modified.

import bge
import bpy

import hashlib
import random
import time

def genkey():
    data = str( time.time() ) + str( random.randrange(1,10000) )
    key = hashlib.md5(data.encode('utf-8'))
    return key.hexdigest()

cont = bge.logic.getCurrentController()
scene = cont.owner.scene

## defaults ########################################################

property_name = "ID"      # name of game property
property_value = None     # value of game property if None ( se setval function)
property_type = 'STRING'  #('BOOL', 'INT', 'FLOAT', 'STRING', 'TIMER')

update_value = True   
clear = False             # WARNING if 'True' Clears  game property from selected objects
select_only = False       # IF True turns off Property functionality

#####################################################################

## setval function (modify if needed) #########

def setval():
    if property_value:
        return property_value
    return genkey()[:8]

###############################################

oblist = []

for ob in bpy.context.selected_objects:
    name = ob.name.split(".")
    oblist.append(name[0])

if oblist:    
    for o in bpy.data.objects:
        n = o.name.split(".")
        o.select = False
        if n[0] in oblist:
            o.select = True
            
            if not clear and select_only == False:
                if o.name in scene.objects:
                    obj = scene.objects[o.name]
                    bpy.context.scene.objects.active = o
                    if property_name not in obj:
                        
                        bpy.ops.object.game_property_new(type=property_type,name=property_name)
                        bpy.data.objects[o.name].game.properties[property_name].value=setval()
                        
                        
                    if bpy.data.objects[o.name].game.properties and update_value:
                        bpy.data.objects[o.name].game.properties[property_name].value=setval()
                                            

                    
#----WARNING Clears ALL game properties from selected objects------------------
if clear and select_only == False:
    if oblist:    
        for o in bpy.data.objects:
            n = o.name.split(".")
            o.select = False
            if n[0] in oblist:
                o.select = True
                
                if o.name in scene.objects:
                    obj = scene.objects[o.name]
                    bpy.context.scene.objects.active = o
                    if property_name in obj:
                        for i,k in enumerate(bpy.context.object.game.properties.keys()):
                            if k == property_name:
                                bpy.ops.object.game_property_remove(index=i)


Latest Version:
Example blend: ( press “Run Script” button to activate addon.)
GameObject_tool_addon_v2.blend

The script by itself:
object_tools_addon.py (15.8 KB)

1 Like

And change the values in under the #defaults#####

This works quite nice, better then the ‘object->game->copy properties’ function. Gonna save me a lot of time :slight_smile:

Thanks!

//edit, hmm it does not update Boolean properties? i added it with True, now if i set value to False it stays True.

//edit 2, we can’t switch from bool to an other property either? added INT 10, now set to BOOL the only thing it does is set the INT value to 1.

hmm a bug, need to check that.

workaround:
remove property and put in the desired type

That is not really a work around. as you stated it clears ALL properties, so if i want to change only 1 property, i need to add them all 1 by one again.

only clears the one with “property_name”

1 Like

Ok my bad did not test it with multiple actually on it. Workaround works indeed!

if you set “select_only” to True , you can use the script to select a bunch of objects for example if you want to move them to another layer.

Yes that all works well, would be nice if you could fix the BOOLEAN state. Other then that i’m gonna use this a lot haha, thanks!

btw, you know that you can simply check this with:

if select_only: #True
if not select_only: #False

it is all about personal preferences :sunglasses:

True.

Also i’m wondering if it could be made purely with/in bpy. So we can just hit the ‘run script’ button instead of running the game engine for every change we want to make.

good idea going to look into it.

new version ,no need to run bge to use simply press the “run script” button.

bug fix: change bool works now.

update: change type is supported.


# version 2

import bpy

import hashlib
import random
import time

def genkey():
    data = str( time.time() ) + str( random.randrange(1,10000) )
    key = hashlib.md5(data.encode('utf-8'))
    return key.hexdigest()


## defaults ########################################################

property_name = "ID2"      # name of game property
property_value = 1   # value of game property if None ( se setval function)
property_type = 'FLOAT'  #('BOOL', 'INT', 'FLOAT', 'STRING', 'TIMER')

update_value = True   
clear = False             # WARNING if 'True' Clears game property from selected objects
select_only = False       # IF True turns off Property functionality

#####################################################################

## setval function (modify if needed) #########

def setval():
    if property_value != None:
        if property_type == "STRING":
            return str(property_value)
        
        return property_value
    return genkey()[:8]

###############################################

oblist = []

for ob in bpy.context.selected_objects:
    name = ob.name.split(".")
    oblist.append(name[0])

if oblist:    
    for o in bpy.data.objects:
        n = o.name.split(".")
        o.select = False
        if n[0] in oblist:
            o.select = True
            
            if not clear and select_only == False:
                if o.name in bpy.data.objects:
                                    
                    bpy.context.scene.objects.active = o
                    if property_name not in bpy.data.objects[o.name].game.properties.keys():
                        
                        bpy.ops.object.game_property_new(type=property_type,name=property_name)
                        bpy.data.objects[o.name].game.properties[property_name].value=setval()
                        
                        
                    if bpy.data.objects[o.name].game.properties and update_value:
                        if property_type == bpy.data.objects[o.name].game.properties[property_name].type:
                            bpy.data.objects[o.name].game.properties[property_name].value=setval()
                        else:
                            if property_name in bpy.data.objects[o.name].game.properties.keys():
                                for i,k in enumerate(bpy.context.object.game.properties.keys()):
                                    if k == property_name:
                                        bpy.ops.object.game_property_remove(index=i)
                            bpy.ops.object.game_property_new(type=property_type,name=property_name)
                            bpy.data.objects[o.name].game.properties[property_name].value=setval()
                            

                                            

                    
#----WARNING Clears game property from selected objects------------------
if clear and select_only == False:
    if oblist:    
        for o in bpy.data.objects:
            n = o.name.split(".")
            o.select = False
            if n[0] in oblist:
                o.select = True
                
                if o.name in  bpy.data.objects:  
                    bpy.context.scene.objects.active = o
                    
                    if property_name in bpy.data.objects[o.name].game.properties.keys():
                        for i,k in enumerate(bpy.context.object.game.properties.keys()):
                            if k == property_name:
                                bpy.ops.object.game_property_remove(index=i)
1 Like

Works great, thanks again!

a made this script into an addon

GameObject_tool_addon.blend

gameobject_tools.py (10.3 KB)

I have added a tool to apply random rotations to selected objects.

opdated script:
gameobject_tools_addon.py (13.9 KB)

2019-03-26_15-50_addon

new vesion.
GameObject_tool_addon_v2.blend

Final Version:
i made a few updates.
added auto select buttons
and a section for batch rename (i often forget to name my objects properly before using them :sleepy:)

get the files from post 1

2019-03-30_17-39-addon