-
-
Save natecraddock/7bf6b74768b377f260b7 to your computer and use it in GitHub Desktop.
# ##### 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() |
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() |
Hello Nate, I'm trying to run te preview on the scripting enviorenment of blender and don't happen anything, look the error:
Exception ignored in: <function ImagePreviewCollection.__del__ at 0x000001CCFAB451F8> Traceback (most recent call last): File "D:\Program Files\Blender Foundation\Blender 2.82\2.82\scripts\modules\bpy\utils\previews.py", line 79, in __del__ f"{self!r}: left open, remove with " ResourceWarning: <ImagePreviewCollection id=0x1ccfb008588[0], <super: <class 'ImagePreviewCollection'>, <ImagePreviewCollection object>>>: left open, remove with 'bpy.utils.previews.remove()' Exception ignored in: <function ImagePreviewCollection.__del__ at 0x000001CCFAB451F8> Traceback (most recent call last): File "D:\Program Files\Blender Foundation\Blender 2.82\2.82\scripts\modules\bpy\utils\previews.py", line 79, in __del__ f"{self!r}: left open, remove with " ResourceWarning: <ImagePreviewCollection id=0x1ccfb008708[1], <super: <class 'ImagePreviewCollection'>, <ImagePreviewCollection object>>>: left open, remove with 'bpy.utils.previews.remove()'
@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 :)
thankyou verymuch
@mati23
wm
stands for window manager. It appears though that I did not use the variable (I modified the code from the templates included with Blender). You do not need to make thewm
variable.