How to properly use the template_any_id?

Hi there,

I hope it’s the right place to ask. I would like to use the template_any_id layout with custom properties in an add-on. I defined an id pointer property and an id_type enum, like this:

class OtherAnimationTargets(PropertyGroup):
    ''' Other Animation Targets, any ID and data path.'''
    name: StringProperty(
        name='Name',
    )
    id: PointerProperty(
        name='ID',
        type=ID,
    )
    id_type: EnumProperty(
        name='ID Type',
        items=ID_types,
    )

Right now the ID types are hard set as a list of tuples (which feels wrong, but I haven’t found a different method):

ID_types = [
    ('ACTION', 'Action', 'Action', 'ACTION', 0),
    ('ARMATURE', 'Armature', 'Armature', 'ARMATURE_DATA', 1),
    ('BRUSH', 'Brush', 'Brush', 'BRUSH_DATA', 2),
    ...
]

Unfortunately, I am not able to use the template right now, because the id field is greyed out. I can assign an ID through python, but the uilayout won’t allow to edit it (see the image below). Here is how I call it in the ui:

col_other.template_any_ID(active_any_target, property="id", type_property='id_type', text="ID block")

Currently, I think that I am simply misusing the template, so I posted here instead of filing a report.
Thanks for taking the time to read!

Just wondering, if you try exposing the id property in the UI directly, without using template, is it editable there?

Looking at the code behind the template, it does not look like it handles editability in any special way for the ID selector.

Hi, thanks for getting back! Yes, I found a workaround that works for me using prop_search:

row.prop(active_any_target, "id_type", text="ID Block", icon_only=True)
row.prop_search(active_any_target, "id", bpy.data, active_any_target.get_bpy_identifier(), text="")

I store the property identifiers in a dict like this:

type_identifiers = {
    'ACTION': 'actions',
    'ARMATURE': 'armatures',
    'BRUSH': 'brushes',
    ...
}

The only downside I found so far is that the picker doesn’t work for objects, materials, keys, etc. when using the general ID type with prop_search…
This seems to work with the template_any_ID. At least it does in the keying set layout.

Edit: This is how the two layouts look in comparison:

Could the property type be allocated dynamically, so that the picker works? I’m not comfortable with C unfortunately, so I wasn’t able to tell the difference to the keying set properties.