[Bf-blender-cvs] [748769e] ui-preview-buttons: cleanup dynamic enum template script and adding a new one for a simple custom icon

Ines Almeida noreply at git.blender.org
Fri May 8 19:31:49 CEST 2015


Commit: 748769e194e15787f96cba79613fc38cac13d898
Author: Ines Almeida
Date:   Fri May 8 18:31:56 2015 +0100
Branches: ui-preview-buttons
https://developer.blender.org/rB748769e194e15787f96cba79613fc38cac13d898

cleanup dynamic enum template script and adding a new one for a simple custom icon

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

D	release/scripts/templates_py/ui_previews.py
A	release/scripts/templates_py/ui_previews_custom_icon.py
A	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
new file mode 100644
index 0000000..0f66c8c
--- /dev/null
+++ b/release/scripts/templates_py/ui_previews_custom_icon.py
@@ -0,0 +1,80 @@
+# This sample script demonstrates how to place a custom icon on a button or
+# menu entry.
+#
+# IMPORTANT NOTE: if you run this sample, there will be no icon in the button
+# You need to replace the image path with a real existing one.
+# For distributable addons, it is recommended to place the icons inside the
+# addon folder and access it with bpy.utils.user_resource for portability
+#
+#
+# Other use cases for UI-previews:
+# - 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"
+
+
+import os
+import bpy
+
+
+class PreviewsExamplePanel(bpy.types.Panel):
+    """Creates a Panel in the Object properties window"""
+    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()
+        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
+
+
+# We can store multiple preview collections here,
+# however in this example we only store "main"
+preview_collections = {}
+
+
+def register():
+
+    # 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()
+    pcoll.my_icons_dir = bpy.utils.user_resource('SCRIPTS', "addons") \
+                        + "/my_addon_folder/icons/"
+    # load a preview thumbnail of a file and store in the previews collection
+    pcoll.load(
+        # identifier
+        "my_icon",
+        # path to image
+        pcoll.my_icons_dir + "icon-image.png",
+        # file type to generate preview from. others are: MOVIE, FONT, BLEND
+        'IMAGE')
+
+    preview_collections["main"] = pcoll
+
+    bpy.utils.register_class(PreviewsExamplePanel)
+
+
+def unregister():
+
+    for pcoll in preview_collections.values():
+        bpy.utils.previews.remove(pcoll)
+    preview_collections.clear()
+
+    bpy.utils.unregister_class(PreviewsExamplePanel)
+
+
+if __name__ == "__main__":
+    register()
+
diff --git a/release/scripts/templates_py/ui_previews.py b/release/scripts/templates_py/ui_previews_dynamic_enum.py
similarity index 90%
rename from release/scripts/templates_py/ui_previews.py
rename to release/scripts/templates_py/ui_previews_dynamic_enum.py
index db684f3..0bf1a5f 100644
--- a/release/scripts/templates_py/ui_previews.py
+++ b/release/scripts/templates_py/ui_previews_dynamic_enum.py
@@ -7,15 +7,14 @@
 # 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
+# - 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"
 #
-# Example:
-#    my_addon_icons = bpy.utils.previews.new("MyAddonIcons")
-#    myicon = my_addon_icons.load("myicon", "/path/to/icon-image.png", 'IMAGE')
-#    layout.operator("render.render", icon_value=int(myicon.icon_id))
-#
-# You can generate thumbnails of your own made icons to associate with an action
+# For distributable addons, it is recommended to place the icons inside the
+# addon folder and access it with bpy.utils.user_resource for portability:
+#     bpy.utils.user_resource('SCRIPTS', "addons") + "/my_addon/imgs/"
+
 
 import os
 import bpy




More information about the Bf-blender-cvs mailing list