Storing an object in a PropertyGroup

Hy everyone,
as said in the title, is it possible to Store an object type (a Armature) in a PropertyGroup to be able to access it everywhere and keep it in memory?

Good Day,

not as a direct reference to the object itself. Blender doesn’t guarantee an object will ever be in the same place in memory, and recommends never caching objects and trying to recall them later. This is one of the easiest ways to crash Blender from a python script.

You can, however, store an object’s name as a StringProperty- or if you’re worried about the user renaming the object, you can tag it with a custom key and then find it again later. For example:

my_obj = bpy.data.objects[0]
my_obj["my_custom_key"] = True

and then later

my_objs = [o for o in bpy.data.objects if o.get("my_custom_key", False)]

Thank you very much,
This way of coding between [] has a name? this is the first time I’ve seen this synthase.

Good journey,

this is just dictionary notation. every object in python has a dictionary used as a mapping proxy (try it yourself- create a basic Python class and then look at MyClass.__dict__). I’m not sure how Blender is using it under the hood, but this method takes advantage of that dictionary by storing custom data to it.

All right, thank you for all the details.

List comprehension.

ah, yeah- I thought he was asking about direct assignment to a non-dict type using a string key

Have you tried to use PointerProperty?

For example

class MYADDON_PG_object(bpy.types.PropertyGroup):
    object: bpy.types.PointerProperty(type=Object)

Hy everyone

I knew about the PointerProperty but I hadn’t thought of using it that way. It guarantees that the object will not be lost?

I did this to try:

import bpy

bpy.types.WindowManager.obj_pointer_property = bpy.props.PointerProperty(type=bpy.types.Object)

arm_obj = bpy.data.objects.get("Armature")

bpy.context.window_manager.obj_pointer_property=arm_obj

print(bpy.context.window_manager.obj_pointer_property.name)

My need to always be able to find the object comes from the fact that I need the name to change in case it is renamed from the scene and also to be able to do operations on it even when it is no longer selected. Here it is to parent the objects selected in the scene to a bone of the frame via an operator who will do the rest.

Good Day,

It won’t be lost unless you delete this object.
But keep in mind that WindowManager custom property won’t be saved to the blender file. You need to use bpy.types.Scene fo that


All right, thanks for the info.