Last active
February 18, 2024 05:14
-
-
Save natecraddock/7bf6b74768b377f260b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ##### BEGIN GPL LICENSE BLOCK ##### | |
# | |
# This program is free software; you can redistribute it and/or | |
# modify it under the terms of the GNU General Public License | |
# as published by the Free Software Foundation; either version 2 | |
# of the License, or (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software Foundation, | |
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
# | |
# ##### END GPL LICENSE BLOCK ##### | |
# <pep8-80 compliant> | |
bl_info = { | |
"name": "Preview Addon", | |
"author": "Nathan Craddock", | |
"version": (1, 0, 0), | |
"blender": (2, 76, 0), | |
"location": "Object panel", | |
"description": "Just a test", | |
"warning": "", | |
"category": "Object" | |
} | |
if "bpy" in locals(): | |
import importlib | |
importlib.reload(Previews) | |
else: | |
from . import Previews | |
import bpy | |
def register(): | |
bpy.utils.register_module(__name__) | |
Previews.register() | |
def unregister(): | |
bpy.utils.unregister_module(__name__) | |
Previews.unregister() | |
if __name__ == "__main__": | |
register() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
from bpy.types import Panel, EnumProperty, WindowManager | |
import bpy.utils.previews | |
import os | |
# UI | |
class PreviewsExamplePanel(bpy.types.Panel): | |
bl_label = "Previews Example Panel" | |
bl_idname = "OBJECT_PT_previews" | |
bl_space_type = 'PROPERTIES' | |
bl_region_type = 'WINDOW' | |
bl_context = "object" | |
def draw(self, context): | |
layout = self.layout | |
wm = context.window_manager | |
row = layout.row() | |
# This tells Blender to draw the my_previews window manager object | |
# (Which is our preview) | |
row.template_icon_view(context.scene, "my_thumbnails") | |
# Just a way to access which one is selected | |
row = layout.row() | |
row.label(text="You selected: " + bpy.context.scene.my_thumbnails) | |
preview_collections = {} | |
def generate_previews(): | |
# We are accessing all of the information that we generated in the register function below | |
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 | |
def register(): | |
from bpy.types import Scene | |
from bpy.props import StringProperty, EnumProperty | |
# Create a new preview collection (only upon register) | |
pcoll = bpy.utils.previews.new() | |
# This line needs to be uncommented if you install as an addon | |
pcoll.images_location = os.path.join(os.path.dirname(__file__), "images") | |
# This line is for running as a script. Make sure images are in a folder called images in the same | |
# location as the Blender file. Comment out if you install as an addon | |
#pcoll.images_location = bpy.path.abspath('//images') | |
# Enable access to our preview collection outside of this function | |
preview_collections["thumbnail_previews"] = pcoll | |
# This is an EnumProperty to hold all of the images | |
# You really can save it anywhere in bpy.types.* Just make sure the location makes sense | |
bpy.types.Scene.my_thumbnails = EnumProperty( | |
items=generate_previews(), | |
) | |
def unregister(): | |
from bpy.types import WindowManager | |
for pcoll in preview_collections.values(): | |
bpy.utils.previews.remove(pcoll) | |
preview_collections.clear() | |
del bpy.types.Scene.my_thumbnails | |
if __name__ == "__main__": | |
register() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@BETAPANDERETA this script was written many years ago, and things have changed in Blender 2.8x and above. I may update it eventually, but I don't have time to help debug right now :(
But Blender ships with example python templates, and there is one very similar to this one. In the Text Editor header choose Templates > Python > ui_previews_dynamic_enum.py. That should get you close :)