By analyzing the two provided scripts, as well as the free version of Pro Lighting: Skies I was able to understand how to create a previews menu.
Here is my script that I commented in if you want to study it.
If you run it in Blender's text editor, it will create a panel at the bottom of the Object properties area.
The key parts of what you want to do are:
Create a property collection
This happens in the register
function so only one is created. Because you want the the images folder to be predefined, we hard-code the location inside of the script. You also set a globally defined preview_collections
variable to hold the collection. Then you create an EnumProperty
to hold the images. This has a function to generate the images from the folder.
pcoll = bpy.utils.previews.new()
pcoll.images_location = bpy.path.abspath('//images')
preview_collections["thumbnail_previews"] = pcoll
bpy.types.Scene.my_thumbnails = EnumProperty(
items=generate_previews(),
)
Generate Previews
In the generate_previews
function, we create a pcoll object that references the one we created in the register
function.
def generate_previews():
pcoll = preview_collections["thumbnail_previews"]
image_location = pcoll.images_location
VALID_EXTENSIONS = ('.png', '.jpg', '.jpeg')
enum_items = []
# Generate the thumbnails
for i, image in enumerate(os.listdir(image_location)):
if image.endswith(VALID_EXTENSIONS):
filepath = os.path.join(image_location, image)
thumb = pcoll.load(filepath, filepath, 'IMAGE')
enum_items.append((image, image, "", thumb.icon_id, i))
return enum_items
Access the values
We can then access this preview collection in any Blender UI Panel with
row=layout.row()
row.template_icon_view(context.scene, "my_thumbnails")
Or however else you named it. Then you can use bpy.context.Scene.my_thumbnails
to access the currently selected image name. From there you can create an append button to append from a file based on the image name.
Using in an Addon
To use this method in an addon, you need to change a line in the Previews.py file. Instead of getting the location of the images folder based on the current .blend file, you need to get it based on the location of the addon. So change
pcoll.images_location = bpy.path.abspath('//images')
to
pcoll.images_location = os.path.join(os.path.dirname(__file__), "images")
In the __init__.py
file, the register
and unregister
functions need to call the Previews.py
's register and unregister functions.
def register():
bpy.utils.register_module(__name__)
Previews.register()
def unregister():
bpy.utils.unregister_module(__name__)
Previews.unregister()
And finally, because you use bpy.utils.register_module(__name__)
you need to remove the register_class call in the Previews.py
file to register the UI class, or it will be called twice.
# Delete this, as well as the one in the unregister function from Previews.py
bpy.utils.register_class(PreviewsExamplePanel)
I have updated the Gist on GitHub with the changes.