Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBastien Montagne <montagne29@wanadoo.fr>2015-05-11 17:29:12 +0300
committerBastien Montagne <montagne29@wanadoo.fr>2015-05-11 17:37:15 +0300
commitd30f664c0438e8378c79d5beb114b1338d0e1d94 (patch)
tree77e6684c118464c899f6e031fe999891eb38fb52 /release/scripts/templates_py/ui_previews_custom_icon.py
parente38f9144212dc0e7e7f3e6be9b3315435d1669eb (diff)
Expose PreviewImage & custom icons to py API.
This commit mainly: * Exposes PreviewImage struct in RNA, including ways for user to set images data. * Adds a new kind of PreviewImage, using a file path and IMB_thumb to get image. * Adds a new kind of custom icon using PreviewImage, unrelated to ID previews system. * Adds a python API (utils.previews) to allow python scripts to access those custom previews/icons. Note that loading image from files' thumbnails is done when needed (deferred loading), not when defining the custom preview/icon. WARNING: for release addons who would want to use this, please keep it to a strict minimum, really needed level. We do not want our UI to explode under hundreds of different flashy icons! For more info, see also the release notes of Blender 2.75 (http://wiki.blender.org/index.php/Dev:Ref/Release_Notes/2.75/Addons) and the example/templates featured with Blender. Patch by Campbell (ideasman42), InĂªs (brita) and Bastien (mont29). Differential Revision: https://developer.blender.org/D1255
Diffstat (limited to 'release/scripts/templates_py/ui_previews_custom_icon.py')
-rw-r--r--release/scripts/templates_py/ui_previews_custom_icon.py82
1 files changed, 82 insertions, 0 deletions
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 00000000000..defa2d266e6
--- /dev/null
+++ b/release/scripts/templates_py/ui_previews_custom_icon.py
@@ -0,0 +1,82 @@
+# 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 relative to the py script file 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.py"
+
+
+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
+ pcoll = preview_collections["main"]
+
+ row = layout.row()
+ 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()
+
+ # 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"),
+ # 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()