How do I add an "image_user" attribute to a custom material property group?

I want to use

layout.template_image()

in my material panels UI, but I want to do it outside of the context of texture panels and such.

layout.template_image(<i>data</i>, <i>property</i>, <i>image_user</i>)

takes an “image_user” argument, and textures have this inherently. Can I add a custom “image_user” attribute to my material property group? I can’t figure out how to do it.
I tried creating the attribute as a property in my property group, it’s a PointerProperty whose “type” is a custom class inheriting from bpy.types.ImageUser. But then I get register errors when it tries to register the property group.

What am I supposed to do here?

You can’t create the necessary pointer property, nor an image user with python. But you can use instances of them, e.g.


        tex = bpy.data.textures['Tex']
        col = layout.column()
        col.template_image(tex, "image", tex.image_user)

But what if I don’t want to use bpy.data.textures at all?

You can only use what already exists (so if you find other suitable pointer properties and image users, that should be fine), or change blender and compile your customized version.