Custom icon previews from internal images

I am trying to create a custom preview collection using utils.previews API something like this:

icons_dict = bpy.utils.previews.new()
icons_dict.load("custom_icon", os.path.join(icons_dir, "icon.png"), 'IMAGE')

I am wondering if it is possible to to load a preview from an internal image instead of file?
I can use the icons_dict.new() API to create an empty preview but I have no idea how to load it with pixels from another image. Any ideas?

Thanks!

2 Likes

Ok, answering my own question:

You can load pixels into an ImagePreview like this

offscreen buffer

off_px = [v / 255 for v in buffer]

custom_icons = preview_collections[“main”]
iprev = custom_icons.new(‘Test_Preview’)
iprev.image_size = (WIDTH,HEIGHT)
iprev.image_pixels_float = off_px

and you can show it anywere like this

global preview_collections
custom_icons = preview_collections[“main”]
layout.template_icon(custom_icons[‘Test_Preview’].icon_id, scale=5.0)

2 Likes

Good job @rpopovici and thanks for sharing the solution here too. :+1:

Hello rpopovici!!! :wave: :slight_smile:

I am new to Blender and Blender Scripting of course, so can I please have a simple script example so I can understand how all this thing works?

Thank you for your time!!!

Sorry I don’t have an example script, but you can find more info here:
https://docs.blender.org/api/current/gpu.html
https://docs.blender.org/api/current/bpy.utils.previews.html
https://docs.blender.org/api/current/bpy.types.UILayout.html

Basicaly I am trying to load an offscreen buffer as a preview image into an UILayout icon. The icon prop can be inside a panel, node or any other ui compment which supports custom UILayout props

2 Likes