example use of addon preferences.
authorCampbell Barton <ideasman42@gmail.com>
Sat, 29 Dec 2012 11:07:31 +0000 (11:07 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sat, 29 Dec 2012 11:07:31 +0000 (11:07 +0000)
doc/python_api/examples/bpy.types.AddonPreferences.1.py [new file with mode: 0644]

diff --git a/doc/python_api/examples/bpy.types.AddonPreferences.1.py b/doc/python_api/examples/bpy.types.AddonPreferences.1.py
new file mode 100644 (file)
index 0000000..08de6f4
--- /dev/null
@@ -0,0 +1,70 @@
+bl_info = {
+    "name": "Example Addon Preferences",
+    "author": "Your Name Here",
+    "version": (1, 0),
+    "blender": (2, 65, 0),
+    "location": "SpaceBar Search -> Addon Preferences Example",
+    "description": "Example Addon",
+    "warning": "",
+    "wiki_url": "",
+    "tracker_url": "",
+    "category": "Object"}
+
+
+import bpy
+from bpy.types import Operator, AddonPreferences
+from bpy.props import StringProperty, IntProperty, BoolProperty
+
+
+class ExampleAddonPreferences(AddonPreferences):
+    bl_idname = __name__
+
+    filepath = StringProperty(
+            name="Example File Path",
+            subtype='FILE_PATH',
+            )
+    number = IntProperty(
+            name="Example Number",
+            default=4,
+            )
+    boolean = BoolProperty(
+            name="Example Boolean",
+            default=False,
+            )
+
+    def draw(self, context):
+        layout = self.layout
+        layout.label(text="This is a preferences view for our addon")
+        layout.prop(self, "filepath")
+        layout.prop(self, "number")
+        layout.prop(self, "boolean")
+
+
+class OBJECT_OT_addon_prefs_example(Operator):
+    """Display example preferences"""
+    bl_idname = "object.addon_prefs_example"
+    bl_label = "Addon Preferences Example"
+    bl_options = {'REGISTER', 'UNDO'}
+
+    def execute(self, context):
+        user_preferences = context.user_preferences
+        addon_prefs = user_preferences.addons[__name__].preferences
+
+        info = ("Path: %s, Number: %d, Boolean %r" %
+                (addon_prefs.filepath, addon_prefs.number, addon_prefs.boolean))
+
+        self.report({'INFO'}, info)
+        print(info)
+
+        return {'FINISHED'}
+
+
+# Registration
+def register():
+    bpy.utils.register_class(OBJECT_OT_addon_prefs_example)
+    bpy.utils.register_class(ExampleAddonPreferences)
+
+
+def unregister():
+    bpy.utils.unregister_class(OBJECT_OT_addon_prefs_example)
+    bpy.utils.unregister_class(ExampleAddonPreferences)