[Bf-blender-cvs] [211ea26] ui-preview-buttons: Cleanup: minor edits
Campbell Barton
noreply at git.blender.org
Tue May 5 10:44:20 CEST 2015
Commit: 211ea268e3dad3a2faa43e52a896cd37575f758e
Author: Campbell Barton
Date: Tue May 5 18:42:49 2015 +1000
Branches: ui-preview-buttons
https://developer.blender.org/rB211ea268e3dad3a2faa43e52a896cd37575f758e
Cleanup: minor edits
===================================================================
M release/scripts/templates_py/ui_previews.py
===================================================================
diff --git a/release/scripts/templates_py/ui_previews.py b/release/scripts/templates_py/ui_previews.py
index ec56bd5..3101650 100644
--- a/release/scripts/templates_py/ui_previews.py
+++ b/release/scripts/templates_py/ui_previews.py
@@ -1,14 +1,11 @@
-import os
-import bpy
-
# This sample script demonstrates a dynamic EnumProperty with custom icons.
# The EnumProperty is populated dynamically with thumbnails of the contents of
-# a chosen directory in 'get_previews_from_folder'.
+# a chosen directory in 'enum_previews_from_directory_items'.
# Then, the same enum is displayed with different interfaces. Note that the
# generated icon previews do not have Blender IDs, which means that they can
-# not be used with UILayout templates that require IDs, such as template_list
-# and template_ID_preview.
-
+# not be used with UILayout templates that require IDs,
+# such as template_list and template_ID_preview.
+#
# Other use cases:
# - make a fixed list of enum_items instead of calculating them in a function.
# - generate a single thumbnail to pass in a UILayout function as icon_value
@@ -20,24 +17,28 @@ import bpy
#
# You can generate thumbnails of your own made icons to associate with an action
+import os
+import bpy
+
-def get_previews_from_folder(self, context):
+def enum_previews_from_directory_items(self, context):
+ """EnumProperty callback"""
wm = context.window_manager
enum_items = []
- folder_path = wm.my_previews_folderpath
+ directory = wm.my_previews_dir
# gets the already existing preview collection (defined in register func).
pcoll = bpy.utils.previews.new("PreviewsInDirectory")
- if folder_path == pcoll.my_previews_folderpath:
+ if directory == pcoll.my_previews_dir:
return pcoll.my_previews
- print("Scanning folder: %s" % folder_path)
+ print("Scanning folder: %s" % directory)
- if folder_path and os.path.exists(folder_path):
+ if directory and os.path.exists(directory):
# scan the directory for png files
- dir_contents = os.listdir(folder_path)
+ dir_contents = os.listdir(directory)
image_paths = []
for c in dir_contents:
if c.lower().endswith(".png"):
@@ -46,13 +47,13 @@ def get_previews_from_folder(self, context):
for idx, img_name in enumerate(image_paths):
# generates a thumbnail preview for a file.
# Also works with previews for 'MOVIE', 'BLEND' and 'FONT'
- path = folder_path + img_name
- thumb = pcoll.load(path, path, 'IMAGE')
+ filepath = os.path.join(directory, img_name)
+ thumb = pcoll.load(filepath, filepath, 'IMAGE')
# enum item: (identifier, name, description, icon, number)
enum_items.append((img_name, img_name, img_name, int(thumb.icon_id), idx))
- pcoll.my_previews = tuple(enum_items)
- pcoll.my_previews_folderpath = folder_path
+ pcoll.my_previews = enum_items
+ pcoll.my_previews_dir = directory
return pcoll.my_previews
@@ -69,7 +70,7 @@ class PreviewsExamplePanel(bpy.types.Panel):
wm = context.window_manager
row = layout.row()
- row.prop(wm, "my_previews_folderpath")
+ row.prop(wm, "my_previews_dir")
row = layout.row()
row.template_icon_view(wm, "my_previews")
@@ -79,28 +80,40 @@ class PreviewsExamplePanel(bpy.types.Panel):
def register():
- bpy.types.WindowManager.my_previews_folderpath = \
- bpy.props.StringProperty(name="Folder Path", subtype='DIR_PATH')
-
- bpy.types.WindowManager.my_previews = \
- bpy.props.EnumProperty(items=get_previews_from_folder)
-
- # Note that preview collections returned by bpy.utils.previews are regular py objects - you can use them
- # to store custom data.
+ from bpy.types import WindowManager
+ from bpy.props import (
+ StringProperty,
+ EnumProperty,
+ )
+
+ WindowManager.my_previews_dir = StringProperty(
+ name="Folder Path",
+ subtype='DIR_PATH',
+ )
+
+ WindowManager.my_previews = EnumProperty(
+ items=enum_previews_from_directory_items,
+ )
+
+ # Note that preview collections returned by bpy.utils.previews
+ # are regular py objects - you can use them to store custom data.
#
# This is especially useful here, since:
# - It avoids us regenerating the whole enum over and over.
- # - It can store enumitems' strings (remember you have to keep those strings somewhere in py, else they get
- # freed and blender references invalid memory!).
+ # - It can store enum_items' strings
+ # (remember you have to keep those strings somewhere in py,
+ # else they get freed and Blender references invalid memory!).
pcoll = bpy.utils.previews.new("PreviewsInDirectory")
- pcoll.my_previews_folderpath = ""
+ pcoll.my_previews_dir = ""
pcoll.my_previews = ()
bpy.utils.register_class(PreviewsExamplePanel)
def unregister():
- del bpy.types.WindowManager.my_previews
+ from bpy.types import WindowManager
+
+ del WindowManager.my_previews
bpy.utils.previews.delete("PreviewsInDirectory")
More information about the Bf-blender-cvs
mailing list