[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [43261] trunk/blender/release/scripts: enable xml preset save/load, replace install/export themes with these.

Campbell Barton ideasman42 at gmail.com
Tue Jan 10 17:20:06 CET 2012


Revision: 43261
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=43261
Author:   campbellbarton
Date:     2012-01-10 16:20:01 +0000 (Tue, 10 Jan 2012)
Log Message:
-----------
enable xml preset save/load, replace install/export themes with these.

Modified Paths:
--------------
    trunk/blender/release/scripts/modules/bpy/utils.py
    trunk/blender/release/scripts/modules/rna_xml.py
    trunk/blender/release/scripts/startup/bl_operators/presets.py
    trunk/blender/release/scripts/startup/bl_operators/wm.py
    trunk/blender/release/scripts/startup/bl_ui/space_userpref.py

Modified: trunk/blender/release/scripts/modules/bpy/utils.py
===================================================================
--- trunk/blender/release/scripts/modules/bpy/utils.py	2012-01-10 15:08:12 UTC (rev 43260)
+++ trunk/blender/release/scripts/modules/bpy/utils.py	2012-01-10 16:20:01 UTC (rev 43261)
@@ -408,7 +408,7 @@
     return smpte_from_seconds((frame * fps_base) / fps, fps)
 
 
-def preset_find(name, preset_path, display_name=False):
+def preset_find(name, preset_path, display_name=False, ext=".py"):
     if not name:
         return None
 
@@ -417,11 +417,11 @@
         if display_name:
             filename = ""
             for fn in _os.listdir(directory):
-                if fn.endswith(".py") and name == _bpy.path.display_name(fn):
+                if fn.endswith(ext) and name == _bpy.path.display_name(fn):
                     filename = fn
                     break
         else:
-            filename = name + ".py"
+            filename = name + ext
 
         if filename:
             filepath = _os.path.join(directory, filename)

Modified: trunk/blender/release/scripts/modules/rna_xml.py
===================================================================
--- trunk/blender/release/scripts/modules/rna_xml.py	2012-01-10 15:08:12 UTC (rev 43260)
+++ trunk/blender/release/scripts/modules/rna_xml.py	2012-01-10 16:20:01 UTC (rev 43261)
@@ -307,9 +307,21 @@
 #
 # This roughly matches the operator 'bpy.ops.script.python_file_run'
 
+def _get_context_val(context, path):
+    path_full = "context." + path
+    try:
+        value = eval(path_full)
+    except:
+        import traceback
+        traceback.print_exc()
+        print("Error: %r could not be found" % path_full)
+
+        value = Ellipsis
+
+    return value
+
 def xml_file_run(context, filepath, rna_map):
 
-    import rna_xml
     import xml.dom.minidom
 
     xml_nodes = xml.dom.minidom.parse(filepath)
@@ -321,17 +333,24 @@
         # TODO, error check
         xml_node = bpy_xml.getElementsByTagName(xml_tag)[0]
 
-        # now get 
-        rna_path_full = "context." + rna_path
-        try:
-            value = eval(rna_path_full)
-        except:
-            import traceback
-            traceback.print_exc()
-            print("Error: %r could not be found" % rna_path_full)
+        value = _get_context_val(context, rna_path)
 
-            value = Ellipsis
+        if value is not Ellipsis and value is not None:
+            print("  loading XML: %r" % rna_path)
+            xml2rna(xml_node, root_rna=value)
 
-        if value is not Ellipsis and value is not None:
-            print("Loading XML: %r" % rna_path_full)
-            rna_xml.xml2rna(xml_node, root_rna=value)
+
+def xml_file_write(context, filepath, rna_map):
+
+    file = open(filepath, 'w', encoding='utf-8')
+    fw = file.write
+
+    fw("<bpy>\n")
+
+    for rna_path, xml_tag in rna_map:
+        # xml_tag is ignored, we get this from the rna
+        value = _get_context_val(context, rna_path)
+        rna2xml(fw, root_rna=value, method='ATTR')
+
+    fw("</bpy>\n")
+    file.close()

Modified: trunk/blender/release/scripts/startup/bl_operators/presets.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_operators/presets.py	2012-01-10 15:08:12 UTC (rev 43260)
+++ trunk/blender/release/scripts/startup/bl_operators/presets.py	2012-01-10 16:20:01 UTC (rev 43261)
@@ -55,6 +55,13 @@
 
         preset_menu_class = getattr(bpy.types, self.preset_menu)
 
+        is_xml = getattr(preset_menu_class, "preset_type", None) == 'XML'
+
+        if is_xml:
+            ext = ".xml"
+        else:
+            ext = ".py"
+
         if not self.remove_active:
             name = self.name.strip()
             if not name:
@@ -71,45 +78,56 @@
                 self.report({'WARNING'}, "Failed to create presets path")
                 return {'CANCELLED'}
 
-            filepath = os.path.join(target_path, filename) + ".py"
+            filepath = os.path.join(target_path, filename) + ext
 
             if hasattr(self, "add"):
                 self.add(context, filepath)
             else:
                 print("Writing Preset: %r" % filepath)
-                file_preset = open(filepath, 'w')
-                file_preset.write("import bpy\n")
 
-                if hasattr(self, "preset_defines"):
-                    for rna_path in self.preset_defines:
-                        exec(rna_path)
-                        file_preset.write("%s\n" % rna_path)
-                    file_preset.write("\n")
+                if is_xml:
+                    import rna_xml
+                    rna_xml.xml_file_write(context,
+                                           filepath,
+                                           preset_menu_class.preset_xml_map)
+                else:
+                    file_preset = open(filepath, 'w')
+                    file_preset.write("import bpy\n")
 
-                for rna_path in self.preset_values:
-                    value = eval(rna_path)
-                    # convert thin wrapped sequences to simple lists to repr()
-                    try:
-                        value = value[:]
-                    except:
-                        pass
+                    if hasattr(self, "preset_defines"):
+                        for rna_path in self.preset_defines:
+                            exec(rna_path)
+                            file_preset.write("%s\n" % rna_path)
+                        file_preset.write("\n")
 
-                    file_preset.write("%s = %r\n" % (rna_path, value))
+                    for rna_path in self.preset_values:
+                        value = eval(rna_path)
+                        # convert thin wrapped sequences
+                        # to simple lists to repr()
+                        try:
+                            value = value[:]
+                        except:
+                            pass
 
-                file_preset.close()
+                        file_preset.write("%s = %r\n" % (rna_path, value))
 
+                    file_preset.close()
+
             preset_menu_class.bl_label = bpy.path.display_name(filename)
 
         else:
             preset_active = preset_menu_class.bl_label
 
             # fairly sloppy but convenient.
-            filepath = bpy.utils.preset_find(preset_active, self.preset_subdir)
+            filepath = bpy.utils.preset_find(preset_active,
+                                             self.preset_subdir,
+                                             ext=ext)
 
             if not filepath:
                 filepath = bpy.utils.preset_find(preset_active,
                                                  self.preset_subdir,
-                                                 display_name=True)
+                                                 display_name=True,
+                                                 ext=ext)
 
             if not filepath:
                 return {'CANCELLED'}
@@ -397,6 +415,14 @@
     preset_subdir = "tracking_settings"
 
 
+class AddPresetInterfaceTheme(AddPresetBase, Operator):
+    '''Add a theme preset'''
+    bl_idname = "wm.interface_theme_preset_add"
+    bl_label = "Add Tracking Settings Preset"
+    preset_menu = "USERPREF_MT_interface_theme_presets"
+    preset_subdir = "interface_theme"
+
+
 class AddPresetKeyconfig(AddPresetBase, Operator):
     '''Add a Keyconfig Preset'''
     bl_idname = "wm.keyconfig_preset_add"

Modified: trunk/blender/release/scripts/startup/bl_operators/wm.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_operators/wm.py	2012-01-10 15:08:12 UTC (rev 43260)
+++ trunk/blender/release/scripts/startup/bl_operators/wm.py	2012-01-10 16:20:01 UTC (rev 43261)
@@ -1769,61 +1769,3 @@
         info["show_expanded"] = not info["show_expanded"]
         return {'FINISHED'}
 
-
-# -----------------------------------------------------------------------------
-# Theme IO
-from bpy_extras.io_utils import (ImportHelper,
-                                 ExportHelper,
-                                 )
-
-
-class WM_OT_theme_import(Operator, ImportHelper):
-    bl_idname = "wm.theme_import"
-    bl_label = "Import Theme"
-    bl_options = {'REGISTER', 'UNDO'}
-
-    filename_ext = ".xml"
-    filter_glob = StringProperty(default="*.xml", options={'HIDDEN'})
-
-    def execute(self, context):
-        import rna_xml
-        import xml.dom.minidom
-
-        filepath = self.filepath
-
-        xml_nodes = xml.dom.minidom.parse(filepath)
-        theme_xml = xml_nodes.getElementsByTagName("Theme")[0]
-
-        # XXX, why always 0?, allow many?
-        theme = context.user_preferences.themes[0]
-
-        rna_xml.xml2rna(theme_xml,
-                        root_rna=theme,
-                        )
-
-        return {'FINISHED'}
-
-
-class WM_OT_theme_export(Operator, ExportHelper):
-    bl_idname = "wm.theme_export"
-    bl_label = "Export Theme"
-
-    filename_ext = ".xml"
-    filter_glob = StringProperty(default="*.xml", options={'HIDDEN'})
-
-    def execute(self, context):
-        import rna_xml
-
-        filepath = self.filepath
-        file = open(filepath, 'w', encoding='utf-8')
-
-        # XXX, why always 0?, allow many?
-        theme = context.user_preferences.themes[0]
-
-        rna_xml.rna2xml(file.write,
-                        root_rna=theme,
-                        method='ATTR',
-                        root_node="bpy"
-                        )
-
-        return {'FINISHED'}

Modified: trunk/blender/release/scripts/startup/bl_ui/space_userpref.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_ui/space_userpref.py	2012-01-10 15:08:12 UTC (rev 43260)
+++ trunk/blender/release/scripts/startup/bl_ui/space_userpref.py	2012-01-10 16:20:01 UTC (rev 43261)
@@ -96,10 +96,12 @@
             layout.menu("USERPREF_MT_addons_dev_guides")
         elif userpref.active_section == 'THEMES':
             layout.operator("ui.reset_default_theme")
-            layout.operator("wm.theme_import")
-            layout.operator("wm.theme_export")
 
+            layout.label(text="Presets:")
             layout.menu("USERPREF_MT_interface_theme_presets", text=bpy.types.USERPREF_MT_interface_theme_presets.bl_label)
+            row = layout.row(align=True)

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-blender-cvs mailing list