Add an Object property that refers to another object

I want to create a new Object property containing a reference to another reference object.
As an example look at the “Object” field of the Boolean Modifier.

This is how I show the property in the panel:

row = layout.row()
row.prop_search(element,"my_reference_object",bpy.data,"objects")

This is how I create the property:

bpy.types.Object.my_reference_object = StringProperty(name="Reference object")

This works, but if I change the name of the reference object the “my_reference_object” property is not updated, unlike what happens in the “Object” field of the Boolean Modifier.

This is because I am just storing the name and not a pointer to the object.

I tried this:

bpy.types.Object.my_reference_object = PointerProperty(name="Reference object",type=bpy.types.Object)

but it does not work…

Is there a way to store the object reference (as a pointer) in the property and obtain the same behaviour as in the Boolean Modifier?

Thank you in advance.
Emanuele

No there is no such Property type, PointerProperty is for PropertyGroups only, and a IDProperty is missing. Using a StringProperty is currently the only way. You could, however, add a StringProperty to every object and write a scene_update_pre handler like this:

import bpy

bpy.types.Object.name_old = bpy.props.StringProperty(maxlen=63, options={'SKIP_SAVE'})

def update_handler(scene):
    for ob in scene.objects:
        if ob.name != ob.name_old:
            print("name changed from %s to %s" % (ob.name_old, ob.name))
            # update your string references here!
            ob.name_old = ob.name
    
bpy.app.handlers.scene_update_pre.append(update_handler)

I suggest to test performance with this handler though…

Thank you for your fast reply CoDEmanX, but I share your worry on performance…
I opened an issue on that:
http://projects.blender.org/tracker/index.php?func=detail&aid=34766&group_id=9&atid=498

Emanuele