5
$\begingroup$

I am trying to present the user with a gallery view to select a item from the thumbnails. Ideally, they click a button, are presented with the gallery view, and with a single click choose the item. I am able to open a dialog and display the thumbnails with template_icon (top), but I can't seem to get an event to handle by when clicking on them. I can show an operator and scale it to the size I want, but the icon is tiny and I can't seem to make it bigger (bottom):

enter image description here

I got something working with template_icon_view but it had a few drawbacks:

  1. I need to display one of 9 variants of item selected in the Sidebar - so the subset displayed in the gallery is not sufficient for the Sidebar, and I don't want to have to display both the one from the template_icon_view and the variant as a template_icon.
  2. Minimal ability to add captions
  3. Poor scrolling with a large number of items

Is it possible to either scale the operator icon or handle a click on a template_icon ?

EDIT Aug 7, 2020

Since posting this, I have settled on the following:

def draw_gallery(self, context, tp):
    global THICKET_SCALE
    layout = self.layout
    # TODO:
    #  - add a filter box (not sure how this will work yet)
    panel_w = context.region.width
    # cell_w = int(0.75 * scale * bpy.app.render_icon_size)
    cell_w = 175
    num_cols = max(1, panel_w / cell_w)
    o = layout.operator("thicket.change_mode", text="Cancel")
    o.next_mode = self.next_mode('CANCEL')

    # Search box to filter on name and common name (label)
    r = layout.row()
    r.prop(context.window_manager, "thicket_search", icon='VIEWZOOM', text="")
    r.operator("thicket.clear_search", text="", icon='CANCEL')

    grid = layout.grid_flow(columns=num_cols, even_columns=True, even_rows=False)
    for plant in db:
        search = context.window_manager.thicket_search.lower()
        name = plant.name
        label = plant.label
        if search not in name.lower() and search not in label.lower():
            continue
        cell = grid.column().box()
        cell.template_icon(icon_value=get_preview(name).icon_id, scale=THICKET_SCALE)
        cell.label(text="%s" % label)
        cell.label(text="(%s)" % name)
        o = cell.operator("thicket.select_plant")
        o.filepath = plant.filepath
        o.next_mode = self.next_mode('CONFIRM')
    o = layout.operator("thicket.change_mode", text="Cancel")
    o.next_mode = self.next_mode('CANCEL')

This uses a template_icon and a separate operator to choose the plant. I am not using template_icon_view for the reasons listed above. This code is now part of the open source Thicket Add-on: https://github.com/Thicket-Blender/thicket

Ideally, I'd like to be able to either scale the image icon on an operator, or add an on_click event to the template_icon (which does not appear to be the Blender way of doing this).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.