[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [43260] trunk/blender/release/scripts: initial support for XML presets, these have the advantage...

Campbell Barton ideasman42 at gmail.com
Tue Jan 10 16:08:15 CET 2012


Revision: 43260
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=43260
Author:   campbellbarton
Date:     2012-01-10 15:08:12 +0000 (Tue, 10 Jan 2012)
Log Message:
-----------
initial support for XML presets, these have the advantage...

- missing attributes are ignored and don't error out like they would on a script when the API changes.
- don't run code (secure to run from untrusted sources).

use xml presets for themes.

Modified Paths:
--------------
    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/rna_xml.py
===================================================================
--- trunk/blender/release/scripts/modules/rna_xml.py	2012-01-10 14:54:51 UTC (rev 43259)
+++ trunk/blender/release/scripts/modules/rna_xml.py	2012-01-10 15:08:12 UTC (rev 43260)
@@ -298,3 +298,40 @@
                             pass
 
     rna2xml_node(root_xml, root_rna)
+
+
+
+# -----------------------------------------------------------------------------
+# Utility function used by presets.
+# The idea is you can run a preset like a script with a few args.
+#
+# This roughly matches the operator 'bpy.ops.script.python_file_run'
+
+def xml_file_run(context, filepath, rna_map):
+
+    import rna_xml
+    import xml.dom.minidom
+
+    xml_nodes = xml.dom.minidom.parse(filepath)
+    bpy_xml = xml_nodes.getElementsByTagName("bpy")[0]
+
+    for rna_path, xml_tag in rna_map:
+
+        # first get xml
+        # 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 = Ellipsis
+
+        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)

Modified: trunk/blender/release/scripts/startup/bl_operators/presets.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_operators/presets.py	2012-01-10 14:54:51 UTC (rev 43259)
+++ trunk/blender/release/scripts/startup/bl_operators/presets.py	2012-01-10 15:08:12 UTC (rev 43260)
@@ -158,15 +158,27 @@
             )
 
     def execute(self, context):
-        from os.path import basename
+        from os.path import basename, splitext
         filepath = self.filepath
 
         # change the menu title to the most recently chosen option
         preset_class = getattr(bpy.types, self.menu_idname)
         preset_class.bl_label = bpy.path.display_name(basename(filepath))
 
+        ext = splitext(filepath)[1].lower()
+
         # execute the preset using script.python_file_run
-        bpy.ops.script.python_file_run(filepath=filepath)
+        if ext == ".py":
+            bpy.ops.script.python_file_run(filepath=filepath)
+        elif ext == ".xml":
+            import rna_xml
+            rna_xml.xml_file_run(context,
+                                 filepath,
+                                 preset_class.preset_xml_map)
+        else:
+            self.report({'ERROR'}, "unknown filetype: %r" % ext)
+            return {'CANCELLED '}
+
         return {'FINISHED'}
 
 

Modified: trunk/blender/release/scripts/startup/bl_operators/wm.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_operators/wm.py	2012-01-10 14:54:51 UTC (rev 43259)
+++ trunk/blender/release/scripts/startup/bl_operators/wm.py	2012-01-10 15:08:12 UTC (rev 43260)
@@ -1823,6 +1823,7 @@
         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 14:54:51 UTC (rev 43259)
+++ trunk/blender/release/scripts/startup/bl_ui/space_userpref.py	2012-01-10 15:08:12 UTC (rev 43260)
@@ -99,7 +99,9 @@
             layout.operator("wm.theme_import")
             layout.operator("wm.theme_export")
 
+            layout.menu("USERPREF_MT_interface_theme_presets", text=bpy.types.USERPREF_MT_interface_theme_presets.bl_label)
 
+
 class USERPREF_PT_tabs(Panel):
     bl_label = ""
     bl_space_type = 'USER_PREFERENCES'
@@ -491,6 +493,15 @@
             row.prop(system, "use_translate_tooltips", text="Tooltips")
 
 
+class USERPREF_MT_interface_theme_presets(Menu):
+    bl_label = "Presets"
+    preset_subdir = "interface_theme"
+    preset_operator = "script.execute_preset"
+    preset_type = 'XML'
+    preset_xml_map = (("user_preferences.themes[0]", "Theme"), )
+    draw = Menu.draw_preset
+
+
 class USERPREF_PT_theme(Panel):
     bl_space_type = 'USER_PREFERENCES'
     bl_label = "Themes"



More information about the Bf-blender-cvs mailing list