[Bf-blender-cvs] [d5e97ba] ui-preview-buttons: tweaks to Python scripts

Campbell Barton noreply at git.blender.org
Sun May 10 23:44:22 CEST 2015


Commit: d5e97ba3d39c80d1fd2d0f49b42fc7c6499d5830
Author: Campbell Barton
Date:   Mon May 11 07:41:23 2015 +1000
Branches: ui-preview-buttons
https://developer.blender.org/rBd5e97ba3d39c80d1fd2d0f49b42fc7c6499d5830

tweaks to Python scripts

===================================================================

M	release/scripts/templates_py/ui_previews_custom_icon.py
M	release/scripts/templates_py/ui_previews_dynamic_enum.py

===================================================================

diff --git a/release/scripts/templates_py/ui_previews_custom_icon.py b/release/scripts/templates_py/ui_previews_custom_icon.py
index 236e90c..defa2d2 100644
--- a/release/scripts/templates_py/ui_previews_custom_icon.py
+++ b/release/scripts/templates_py/ui_previews_custom_icon.py
@@ -11,7 +11,7 @@
 # - provide a fixed list of previews to select from
 # - provide a dynamic list of preview (eg. calculated from reading a directory)
 #
-# For the above use cases, see the template 'ui_previews_dynamic_enum"
+# For the above use cases, see the template 'ui_previews_dynamic_enum.py"
 
 
 import os
@@ -28,15 +28,15 @@ class PreviewsExamplePanel(bpy.types.Panel):
 
     def draw(self, context):
         layout = self.layout
-        wm = context.window_manager
+        pcoll = preview_collections["main"]
 
         row = layout.row()
-        pcoll = preview_collections["main"]
         my_icon = pcoll.get("my_icon")
         row.operator("render.render", icon_value=my_icon.icon_id)
 
-        # my_icon.icon_id can be used in any UI function that accepts icon_value
-        # try also setting text="" to get an icon only operator button
+        # my_icon.icon_id can be used in any UI function that accepts
+        # icon_value # try also setting text=""
+        # to get an icon only operator button
 
 
 # We can store multiple preview collections here,
@@ -46,21 +46,21 @@ preview_collections = {}
 
 def register():
 
-    # path to the folder where the icon is
-    # the path is calculated relative to this py file inside the addon folder
-    my_icons_dir = os.path.join(os.path.dirname(__file__), "icons")
-
     # Note that preview collections returned by bpy.utils.previews
     # are regular py objects - you can use them to store custom data.
     import bpy.utils.previews
     pcoll = bpy.utils.previews.new()
 
+    # path to the folder where the icon is
+    # the path is calculated relative to this py file inside the addon folder
+    my_icons_dir = os.path.join(os.path.dirname(__file__), "icons")
+
     # load a preview thumbnail of a file and store in the previews collection
     pcoll.load(
         # identifier
         "my_icon",
         # path to image
-        os.path.join( my_icons_dir, "icon-image.png"),
+        os.path.join(my_icons_dir, "icon-image.png"),
         # file type to generate preview from. others are: MOVIE, FONT, BLEND
         'IMAGE')
 
@@ -80,4 +80,3 @@ def unregister():
 
 if __name__ == "__main__":
     register()
-
diff --git a/release/scripts/templates_py/ui_previews_dynamic_enum.py b/release/scripts/templates_py/ui_previews_dynamic_enum.py
index 2e45e4b..1603df0 100644
--- a/release/scripts/templates_py/ui_previews_dynamic_enum.py
+++ b/release/scripts/templates_py/ui_previews_dynamic_enum.py
@@ -8,12 +8,15 @@
 #
 # Other use cases:
 # - make a fixed list of enum_items instead of calculating them in a function
-# - generate isolated thumbnails to use as custom icons in buttons and menu items
-# For custom icons, see the template 'ui_previews_custom_icon"
+# - generate isolated thumbnails to use as custom icons in buttons
+#   and menu items
+#
+# For custom icons, see the template "ui_previews_custom_icon.py".
 #
 # For distributable addons, it is recommended to place the icons inside the
-# addon folder and access it relative to the py script file for portability:
-#     os.path.join(os.path.dirname(__file__), "imgs")
+# addon directory and access it relative to the py script file for portability:
+#
+#    os.path.join(os.path.dirname(__file__), "images")
 
 
 import os
@@ -27,29 +30,28 @@ def enum_previews_from_directory_items(self, context):
     enum_items = []
     directory = wm.my_previews_dir
 
-    # gets the already existing preview collection (defined in register func).
+    # Get the preview collection (defined in register func).
     pcoll = preview_collections["main"]
 
     if directory == pcoll.my_previews_dir:
         return pcoll.my_previews
 
-    print("Scanning folder: %s" % directory)
+    print("Scanning directory: %s" % directory)
 
     if directory and os.path.exists(directory):
-        # scan the directory for png files
-        dir_contents = os.listdir(directory)
+        # Scan the directory for png files
         image_paths = []
-        for c in dir_contents:
-            if c.lower().endswith(".png"):
-                image_paths.append(c)
+        for fn in  os.listdir(directory):
+            if fn.lower().endswith(".png"):
+                image_paths.append(fn)
 
-        for idx, img_name in enumerate(image_paths):
+        for i, name in enumerate(image_paths):
             # generates a thumbnail preview for a file.
             # Also works with previews for 'MOVIE', 'BLEND' and 'FONT'
-            filepath = os.path.join(directory, img_name)
+            filepath = os.path.join(directory, name)
             thumb = pcoll.load(filepath, filepath, 'IMAGE')
             # enum item: (identifier, name, description, icon, number)
-            enum_items.append((img_name, img_name, img_name, thumb.icon_id, idx))
+            enum_items.append((name, name, name, thumb.icon_id, i))
 
     pcoll.my_previews = enum_items
     pcoll.my_previews_dir = directory
@@ -93,7 +95,7 @@ def register():
     WindowManager.my_previews_dir = StringProperty(
             name="Folder Path",
             subtype='DIR_PATH',
-            default="/d"
+            default=""
             )
 
     WindowManager.my_previews = EnumProperty(
@@ -101,7 +103,7 @@ def register():
             )
 
     # Note that preview collections returned by bpy.utils.previews
-    # are regular py objects - you can use them to store custom data.
+    # are regular Python 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.
@@ -132,4 +134,3 @@ def unregister():
 
 if __name__ == "__main__":
     register()
-




More information about the Bf-blender-cvs mailing list