Custom icon for a gizmo button? (Python)


I have a working code for putting my custom made .png icons on a test button:

    def draw(self, context):
        layout = self.layout
        pcoll = preview_collections["main"]

        my_icon = pcoll["some_icon_name"]
        layout.operator("render.render", icon_value=my_icon .icon_id)

I also have another addon that has a “gizmo” button like this:

    def setup(self, context):
        mpr ="GIZMO_GT_button_2d")
        mpr.icon = 'ORIENTATION_VIEW' # How do I put my custom icon here?
        mpr.draw_options = {'BACKDROP', 'OUTLINE'}
        props = mpr.target_set_operator("cameras.callop")
        mpr.alpha = 1.0
        mpr.color = 0.1, 0.1, 0.1
        mpr.color_highlight = 0.8, 0.8, 0.8
        mpr.alpha_highlight = 0.2
        mpr.scale_basis = (80 * 0.35) / 2

But I’m unable to merge the two. I don’t see how I can transfer the first code from a ‘draw’ function into a ‘setup’ function. Any idea how can I achieve that?


Have you tried

mpr.icon = my_icon .icon_id


I did. But as soon as I change the mpr_icon to something else than the normal integrated icons, Blender crashes when I run the script.

Also I’m not sure if I should have the def draw and def setup in the same class. I don’t know if the problem comes from here…

The icon property accepts only a string, which is eventually converted to an id. In the current implementation of GIZMO_GT_button_2d you can’t pass custom icons or icons generated by materials and textures.

But I made some change in the source code, which allows you to call the icon directly through the icon id, you just pass the id of your icon to the icon_value property.

I hope the developers will accept this Pull Request, in order to create more advanced panels in the future

Is there any progress on this? I am interested in adding custom icons to the 2d gizmos in my addon. Is there any way to do it with current build of Blender?

Just want to renew this thread because I am really interested in knowing if there is any way at all to add custom icons to 2D Gizmos with Python. It seems like an obvious functionality to have since custom icons is something that is available in other places to the Blender developers. Does anyone know if it is possible yet?