Get object by property

Hi -

I am using a scene in my game that I need to duplicate. However, when I duplicate it Blender automatically renames all the objects in the scene (e.g. adds .001). I have Python scripts that rely on the object name so when they get renamed in the new scene, the scripts stop working!

What can I do?

I seem to remember seeing once that you can ‘access’ an object in Python by searching for a property but can’t remember how to do this - any ideas? This way I can give each object I’m interested in a unique property and access the object by searching for it. I must admit this feels like a hack so if you have any other suggestions, I’d be more than happy to hear them (especially if it stops me having to re-write my code)! :wink:


import bge

def get_object(property, scene):
    matches = [o for o in scene.objects if property in o]
    if matches:
        return matches[0]
    return None

# Example usage

current_scene = bge.logic.getCurrentScene()
object = get_object("property_name", current_scene)