[#21807] Import Key Configuration problems
[blender.git] / release / scripts / ui / space_userpref.py
index 9717169386aa769e439da4f12f98575940b648dc..4ee1f119f53626336e0b0305a0c69f4d0c085787 100644 (file)
@@ -280,6 +280,10 @@ class USERPREF_PT_interface(bpy.types.Panel):
         col.prop(view, "open_toplevel_delay", text="Top Level")
         col.prop(view, "open_sublevel_delay", text="Sub Level")
 
+        col.separator()
+
+        col.prop(view, "show_splash")
+
 
 class USERPREF_PT_edit(bpy.types.Panel):
     bl_space_type = 'USER_PREFERENCES'
@@ -1398,100 +1402,22 @@ class USERPREF_PT_addons(bpy.types.Panel):
 
         # del sys.path[0]
         return modules
-    
-    def _attributes(self, mod):
-        # collect, check and process all attributes of the add-on
-        module_name = mod.__name__
-        if not hasattr(mod, 'expanded'):
-            mod.expanded = False
-        
-        script = hasattr(mod, '__script__')
-        author = hasattr(mod, '__author__')
-        version = hasattr(mod, '__version__')
-        blender = hasattr(mod, '__blender__')
-        category = hasattr(mod, '__category__')
-        url = hasattr(mod, '__url__')
-        email = hasattr(mod, '__email__')
-        bpydoc = hasattr(mod, '__bpydoc__')
-
-        if script:
-            script = str(mod.__script__)
-        else:
-            script = module_name
-        if version:
-            version = str(mod.__version__)
-        if author:
-            if type(mod.__author__).__name__ == 'list':
-                if len(mod.__author__) == 0:
-                    author = False
-                else:
-                    author = ""
-                    for i in mod.__author__:
-                        author += str(i) + ", "
-                    author = author[:-2]
-            else:
-                author = str(mod.__author__)
-        if blender:
-            blender = str(mod.__blender__)
-        if category:
-            if type(mod.__category__).__name__ != 'list':
-                category = str(mod.__category__)
-            else:
-                category = str(mod.__category__[0])
-        links = []
-        if url:
-            if type(mod.__url__).__name__ != 'list':
-                mod.__url__ = [str(mod.__url__)]
-            for i in mod.__url__:
-                link = str(i).rsplit(',', 1)
-                if len(link)>1:
-                    link_desc = link[0].strip()
-                    link = link[1].strip()
-                else:
-                    link_desc = False
-                    link = link[0].strip()
-                if link.lower() == 'blender':
-                    link = 'http://www.blender.org/forum/viewforum.php?f=9'
-                if link.lower() == 'blenderartists':
-                    link = 'http://blenderartists.org/forum/forumdisplay.php?f=11'
-                links.append([link, link_desc])
-        emails = []
-        if email:
-            if type(mod.__email__).__name__ != 'list':
-                mod.__email__ = [str(mod.__email__)]
-            for i in mod.__email__:
-                mail = str(i).rsplit(',', 1)
-                if len(mail)>1:
-                    mail_desc = mail[0].strip()
-                    mail = mail[1].strip()
-                else:
-                    mail_desc = False
-                    mail = mail[0].strip()
-                if mail.lower() == 'python':
-                    mail = 'bf-python:blender*org'
-                mail = 'mailto:'+mail.replace(':','@').replace('*','.')+"?subject="+script
-                emails.append([mail, mail_desc])
-        if bpydoc:
-            bpydoc = str(mod.__bpydoc__).splitlines()
-        return module_name, script, author, version, blender, category, url, email, bpydoc, links, emails
 
     def draw(self, context):
         layout = self.layout
 
         userpref = context.user_preferences
         used_ext = {ext.module for ext in userpref.addons}
-        
+
         # collect the categories that can be filtered on
-        cats = []
-        for mod in self._addon_list():
-            try:
-                if mod.__category__[0] not in cats:
-                    cats.append(mod.__category__[0])
-            except:
-                pass
-        cats.sort()
-        cats = ['All', 'Disabled', 'Enabled']+cats
-        bpy.types.Scene.EnumProperty(items=[(cats[i],cats[i],str(i)) for i in range(len(cats))],
+        addons = [(mod, addon_info_get(mod)) for mod in self._addon_list()]
+
+        cats = {info["category"] for mod, info in addons}
+        cats.discard("")
+
+        cats = ['All', 'Disabled', 'Enabled'] + sorted(cats)
+
+        bpy.types.Scene.EnumProperty(items=[(cats[i], cats[i], str(i)) for i in range(len(cats))],
             name="Category", attr="addon_filter", description="Filter add-ons by category")
         bpy.types.Scene.StringProperty(name="Search", attr="addon_search",
             description="Search within the selected filter")
@@ -1502,32 +1428,35 @@ class USERPREF_PT_addons(bpy.types.Panel):
         layout.separator()
 
         filter = context.scene.addon_filter
-        search = context.scene.addon_search
-        for mod in self._addon_list():
-            module_name, script, author, version, blender, category, url, email, bpydoc, links, emails = \
-                self._attributes(mod)
-            
+        search = context.scene.addon_search.lower()
+
+        for mod, info in addons:
+            module_name = mod.__name__
+
             # check if add-on should be visible with current filters
-            if filter!='All' and filter!=category and not (module_name in used_ext and filter=='Enabled')\
-                and not (module_name not in used_ext and filter=='Disabled'):
+            if filter != "All" and \
+                    filter != info["category"] and \
+                    not (module_name not in used_ext and filter == "Disabled"):
+
                 continue
-            if search and script.lower().find(search.lower())<0:
-                if author:
-                    if author.lower().find(search.lower())<0:
+
+            if search and search not in info["name"].lower():
+                if info["author"]:
+                    if search not in info["author"].lower():
                         continue
                 else:
                     continue
-                    
+
             # Addon UI Code
             box = layout.column().box()
-            column = box.column(align=True)
+            column = box.column()
             row = column.row()
-            
+
             # Arrow #
             # If there are Infos or UI is expanded
-            if mod.expanded:
+            if info["expanded"]:
                 row.operator("wm.addon_expand", icon="TRIA_DOWN").module = module_name
-            elif author or version or url or email:
+            elif info["author"] or info["version"] or info["url"] or info["location"]:
                 row.operator("wm.addon_expand", icon="TRIA_RIGHT").module = module_name
             else:
                 # Else, block UI
@@ -1535,45 +1464,72 @@ class USERPREF_PT_addons(bpy.types.Panel):
                 arrow.enabled = False
                 arrow.operator("wm.addon_expand", icon="TRIA_RIGHT").module = module_name
 
-            row.label(text=script)
+            row.label(text=info["name"])
             row.operator("wm.addon_disable" if module_name in used_ext else "wm.addon_enable").module = module_name
-            
+
             # Expanded UI (only if additional infos are available)
-            if mod.expanded:
-                if author:
+            if info["expanded"]:
+                if info["author"]:
                     split = column.row().split(percentage=0.15)
                     split.label(text='Author:')
-                    split.label(text=author)
-                if version:
+                    split.label(text=info["author"])
+                if info["version"]:
                     split = column.row().split(percentage=0.15)
                     split.label(text='Version:')
-                    split.label(text=version)
-                if url:
+                    split.label(text=info["version"])
+                if info["location"]:
                     split = column.row().split(percentage=0.15)
-                    split.label(text="Links:")
-                    for i in range(len(links)):
-                        if links[i][1]:
-                            split.operator("wm.addon_links", text=links[i][1]).link = links[i][0]
-                        else:
-                            split.operator("wm.addon_links", text="Link "+str(i+1)).link = links[i][0]
-                if email:
+                    split.label(text='Location:')
+                    split.label(text=info["location"])
+                if info["url"]:
                     split = column.row().split(percentage=0.15)
-                    split.label(text="Email:")
-                    for i in range(len(emails)):
-                        if emails[i][1]:
-                            split.operator("wm.addon_links", text=emails[i][1]).link = emails[i][0]
-                        else:
-                            split.operator("wm.addon_links", text="Email "+str(i+1)).link = emails[i][0]
-                if bpydoc:
-                    column = box.column(align=True)
-                    column.label(text='Description: '+bpydoc[0])
-                    for line in bpydoc[1:]:
-                        column.label(text=line)
-
+                    split.label(text="Internet:")
+                    split.operator("wm.addon_links", text="Link to the Wiki").link = info["url"]
+                    split.separator()
+                    split.separator()
+
+        # Append missing scripts
+        # First collect scripts that are used but have no script file.
+        module_names = {mod.__name__ for mod, info in addons}
+        missing_modules = {ext for ext in used_ext if ext not in module_names}
+
+        if missing_modules and filter in ("All", "Enabled"):
+            layout.column().separator()
+            layout.column().label(text="Missing script files")
+
+            module_names = {mod.__name__ for mod, info in addons}
+            for ext in sorted(missing_modules):
+                # Addon UI Code
+                box = layout.column().box()
+                column = box.column()
+                row = column.row()
+
+                row.label(text=ext, icon="ERROR")
+                row.operator("wm.addon_disable").module = ext
 
 from bpy.props import *
 
 
+def addon_info_get(mod, info_basis={"name": "", "author": "", "version": "", "blender": "", "location": "", "url": "", "category": "", "expanded": False}):
+    addon_info = getattr(mod, "bl_addon_info", {})
+
+    # avoid re-initializing
+    if "_init" in addon_info:
+        return addon_info
+
+    if not addon_info:
+        mod.bl_addon_info = addon_info
+
+    for key, value in info_basis.items():
+        addon_info.setdefault(key, value)
+
+    if not addon_info["name"]:
+        addon_info["name"] = mod.__name__
+
+    addon_info["_init"] = None
+    return addon_info
+
+
 class WM_OT_addon_enable(bpy.types.Operator):
     "Enable an addon"
     bl_idname = "wm.addon_enable"
@@ -1582,33 +1538,24 @@ class WM_OT_addon_enable(bpy.types.Operator):
     module = StringProperty(name="Module", description="Module name of the addon to enable")
 
     def execute(self, context):
-        import traceback
-        ext = context.user_preferences.addons.new()
         module_name = self.properties.module
-        ext.module = module_name
 
         try:
             mod = __import__(module_name)
             mod.register()
         except:
+            import traceback
             traceback.print_exc()
-        
+            return {'CANCELLED'}
+
+        ext = context.user_preferences.addons.new()
+        ext.module = module_name
+
         # check if add-on is written for current blender version, or raise a warning
-        version = hasattr(mod, '__blender__')
-        if version:
-            version = str(mod.__blender__).split('.',2)
-            for i in range(len(version)):
-                try:
-                    version[i] = int(version[i])
-                except:
-                    break
-                if version[i]>bpy.app.version[i]:
-                    self.report('WARNING','This script was written for a newer version of Blender \
-and might not function (correctly).\nThe script is enabled though.')
-                elif version[i]==bpy.app.version[i]:
-                    continue
-                else:
-                    break
+        info = addon_info_get(mod)
+
+        if info.get("blender", (0, 0, 0)) > bpy.app.version:
+            self.report("WARNING','This script was written for a newer version of Blender and might not function (correctly).\nThe script is enabled though.")
 
         return {'FINISHED'}
 
@@ -1713,24 +1660,23 @@ class WM_OT_addon_expand(bpy.types.Operator):
     module = StringProperty(name="Module", description="Module name of the addon to expand")
 
     def execute(self, context):
-        import traceback
         module_name = self.properties.module
 
+        # unlikely to fail, module should have alredy been imported
         try:
             mod = __import__(module_name)
         except:
+            import traceback
             traceback.print_exc()
+            return {'CANCELLED'}
 
-        if mod.expanded:
-            mod.expanded = False
-        else:
-            mod.expanded = True
-
+        info = addon_info_get(mod)
+        info["expanded"] = not info["expanded"]
         return {'FINISHED'}
 
 
 class WM_OT_addon_links(bpy.types.Operator):
-    "Open in webbrowser"
+    "Open the Blender Wiki in the Webbrowser"
     bl_idname = "wm.addon_links"
     bl_label = ""
 
@@ -1907,7 +1853,7 @@ class WM_OT_keyconfig_import(bpy.types.Operator):
         else:
             shutil.move(self.properties.path, path)
 
-        __import__(config_name)
+        exec("import " + config_name)
 
         wm = bpy.context.manager
         wm.active_keyconfig = wm.keyconfigs[config_name]