6
$\begingroup$

I want to use the template_icon_view to display a set of images from bpy.data.images. I know there is bpy.utils.preview which has a load function to load in external images. Is it possible to feed a template_icon_view with internal images?

What would be the proper approach to create an enum property with the images?

$\endgroup$
2
  • $\begingroup$ Actually, I asked that two weeks ago, here's the answer: blender.stackexchange.com/questions/47504/… $\endgroup$ Commented Mar 9, 2016 at 1:10
  • $\begingroup$ Hi Ray, yes.. I have seen this question. My problem is that this thumbnails are generate of external images. I want to use internal images that are not stored somewhere on the harddrive. The bpy.tils.preview module needs external images to generate that previews. $\endgroup$
    – ndee
    Commented Mar 9, 2016 at 6:23

1 Answer 1

9
$\begingroup$

Data-blocks store their own previews, so you can access bpy.types.ImagePreview.icon_id

The icon_id can be passed to UI elements that take an icon value.

eg:

import bpy
for image in bpy.data.images:
    print(image.name, image.preview.icon_id)

This will print the id of already loaded icons.

One area I'd need to look into is how to generate icons from images. The interface does this automatically, but you may want to generate the images on demand (as far as I can see we don't have a direct API call to do this).


Heres a fully working example of using image previews in Blender's interface.

# Run this script and check the object properties.
import bpy


class ImagePreviewPanel(bpy.types.Panel):
    bl_label = "Image Icons"
    bl_idname = "OBJECT_PT_image_icons"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

    def draw(self, context):
        layout = self.layout
        for image in bpy.data.images:
            layout.label(image.name, icon_value=image.preview.icon_id)
        if not bpy.data.images:
            layout.label("No images!")

def register():
    bpy.utils.register_class(ImagePreviewPanel)


def unregister():
    bpy.utils.unregister_class(ImagePreviewPanel)


if __name__ == "__main__":
    register()
$\endgroup$
4
  • $\begingroup$ It seems I have exactly that problem. I get the icon id and the template_icon_view generates the items based on the created enum. But the icons are empty. $\endgroup$
    – ndee
    Commented Mar 9, 2016 at 6:59
  • $\begingroup$ The icons should work in enums too, if you have issues here could be different question. $\endgroup$
    – ideasman42
    Commented Mar 9, 2016 at 7:20
  • $\begingroup$ I have a question concerning the part how to generate icons from images. Is there a way to manually trigger an icon to be generated? $\endgroup$
    – ndee
    Commented Jul 11, 2016 at 20:56
  • $\begingroup$ Unfortunately this method that I find quite obvious and right, does not always generate a preview, it probably depends on the format and size of the image. The blender itself does not seem to generate previews of such images, it seems to happen on some png images in 2k. I think it's a bug or if it's not it creates big problems. $\endgroup$
    – Noob Cat
    Commented Feb 25, 2021 at 13:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .