rna data path names which are more likely to break animations.
[blender-staging.git] / release / scripts / op / presets.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 # <pep8 compliant>
20
21 import bpy
22 import os
23
24
25 class AddPresetBase():
26     '''Base preset class, only for subclassing
27     subclasses must define
28      - preset_values
29      - preset_subdir '''
30     # bl_idname = "script.preset_base_add"
31     # bl_label = "Add a Python Preset"
32
33     name = bpy.props.StringProperty(name="Name", description="Name of the preset, used to make the path name", maxlen=64, default="")
34
35     def _as_filename(self, name): # could reuse for other presets
36         for char in " !@#$%^&*(){}:\";'[]<>,./?":
37             name = name.replace('.', '_')
38         return name.lower()
39
40     def execute(self, context):
41
42         if not self.properties.name:
43             return {'FINISHED'}
44
45         filename = self._as_filename(self.properties.name) + ".py"
46
47         target_path = bpy.utils.preset_paths(self.preset_subdir)[0] # we need some way to tell the user and system preset path
48
49         filepath = os.path.join(target_path, filename)
50         if getattr(self, "save_keyconfig", False):
51             bpy.ops.wm.keyconfig_export(filepath=filepath, kc_name=self.properties.name)
52             file_preset = open(filepath, 'a')
53             file_preset.write("wm.active_keyconfig = kc\n\n")
54         else:
55             file_preset = open(filepath, 'w')
56             file_preset.write("import bpy\n")
57
58         for rna_path in self.preset_values:
59             value = eval(rna_path)
60             file_preset.write("%s = %s\n" % (rna_path, repr(value)))
61
62         file_preset.close()
63
64         return {'FINISHED'}
65
66     def invoke(self, context, event):
67         wm = context.manager
68         #crashes, TODO - fix
69         #return wm.invoke_props_popup(self, event)
70
71         wm.invoke_props_popup(self, event)
72         return {'RUNNING_MODAL'}
73
74
75 class ExecutePreset(bpy.types.Operator):
76     ''' Executes a preset '''
77     bl_idname = "script.execute_preset"
78     bl_label = "Execute a Python Preset"
79
80     filepath = bpy.props.StringProperty(name="Path", description="Path of the Python file to execute", maxlen=512, default="")
81     preset_name = bpy.props.StringProperty(name="Preset Name", description="Name of the Preset being executed", default="")
82     menu_idname = bpy.props.StringProperty(name="Menu ID Name", description="ID name of the menu this was called from", default="")
83
84     def execute(self, context):
85         # change the menu title to the most recently chosen option
86         preset_class = getattr(bpy.types, self.properties.menu_idname)
87         preset_class.bl_label = self.properties.preset_name
88
89         # execute the preset using script.python_file_run
90         bpy.ops.script.python_file_run(filepath=self.properties.filepath)
91         return {'FINISHED'}
92
93
94 class AddPresetRender(AddPresetBase, bpy.types.Operator):
95     '''Add a Render Preset'''
96     bl_idname = "render.preset_add"
97     bl_label = "Add Render Preset"
98     name = AddPresetBase.name
99
100     preset_values = [
101         "bpy.context.scene.render.field_order",
102         "bpy.context.scene.render.fps",
103         "bpy.context.scene.render.fps_base",
104         "bpy.context.scene.render.pixel_aspect_x",
105         "bpy.context.scene.render.pixel_aspect_y",
106         "bpy.context.scene.render.resolution_percentage",
107         "bpy.context.scene.render.resolution_x",
108         "bpy.context.scene.render.resolution_y",
109         "bpy.context.scene.render.use_fields",
110         "bpy.context.scene.render.use_fields_still",
111     ]
112
113     preset_subdir = "render"
114
115
116 class AddPresetSSS(AddPresetBase, bpy.types.Operator):
117     '''Add a Subsurface Scattering Preset'''
118     bl_idname = "material.sss_preset_add"
119     bl_label = "Add SSS Preset"
120     name = AddPresetBase.name
121
122     preset_values = [
123         "bpy.context.material.subsurface_scattering.back",
124         "bpy.context.material.subsurface_scattering.color[0]",
125         "bpy.context.material.subsurface_scattering.color[1]",
126         "bpy.context.material.subsurface_scattering.color[2]",
127         "bpy.context.material.subsurface_scattering.color_factor",
128         "bpy.context.material.subsurface_scattering.error_threshold",
129         "bpy.context.material.subsurface_scattering.front",
130         "bpy.context.material.subsurface_scattering.ior",
131         "bpy.context.material.subsurface_scattering.radius[0]",
132         "bpy.context.material.subsurface_scattering.radius[1]",
133         "bpy.context.material.subsurface_scattering.radius[2]",
134         "bpy.context.material.subsurface_scattering.scale",
135         "bpy.context.material.subsurface_scattering.texture_factor",
136     ]
137
138     preset_subdir = "sss"
139
140
141 class AddPresetCloth(AddPresetBase, bpy.types.Operator):
142     '''Add a Cloth Preset'''
143     bl_idname = "cloth.preset_add"
144     bl_label = "Add Cloth Preset"
145     name = AddPresetBase.name
146
147     preset_values = [
148         "bpy.context.cloth.settings.air_damping",
149         "bpy.context.cloth.settings.bending_stiffness",
150         "bpy.context.cloth.settings.mass",
151         "bpy.context.cloth.settings.quality",
152         "bpy.context.cloth.settings.spring_damping",
153         "bpy.context.cloth.settings.structural_stiffness",
154     ]
155
156     preset_subdir = "cloth"
157
158
159 class AddPresetSunSky(AddPresetBase, bpy.types.Operator):
160     '''Add a Sky & Atmosphere Preset'''
161     bl_idname = "lamp.sunsky_preset_add"
162     bl_label = "Add Sunsky Preset"
163     name = AddPresetBase.name
164
165     preset_values = [
166         "bpy.context.object.data.sky.atmosphere_extinction",
167         "bpy.context.object.data.sky.atmosphere_inscattering",
168         "bpy.context.object.data.sky.atmosphere_turbidity",
169         "bpy.context.object.data.sky.backscattered_light",
170         "bpy.context.object.data.sky.horizon_brightness",
171         "bpy.context.object.data.sky.spread",
172         "bpy.context.object.data.sky.sun_brightness",
173         "bpy.context.object.data.sky.sun_intensity",
174         "bpy.context.object.data.sky.sun_size",
175         "bpy.context.object.data.sky.use_sky_blend",
176         "bpy.context.object.data.sky.use_sky_blend_type",
177         "bpy.context.object.data.sky.use_sky_color_space",
178         "bpy.context.object.data.sky.use_sky_exposure",
179     ]
180
181     preset_subdir = "sunsky"
182
183
184 class AddPresetInteraction(AddPresetBase, bpy.types.Operator):
185     '''Add an Application Interaction Preset'''
186     bl_idname = "wm.interaction_preset_add"
187     bl_label = "Add Interaction Preset"
188     name = AddPresetBase.name
189     save_keyconfig = True
190
191     preset_values = [
192         "bpy.context.user_preferences.edit.use_drag_immediately",
193         "bpy.context.user_preferences.edit.use_insertkey_xyz_to_rgb",
194         "bpy.context.user_preferences.inputs.invert_mouse_wheel_zoom",
195         "bpy.context.user_preferences.inputs.select_mouse",
196         "bpy.context.user_preferences.inputs.use_emulate_numpad",
197         "bpy.context.user_preferences.inputs.use_mouse_continuous",
198         "bpy.context.user_preferences.inputs.use_mouse_emulate_3_button",
199         "bpy.context.user_preferences.inputs.view_rotate_method",
200         "bpy.context.user_preferences.inputs.view_zoom_axis",
201         "bpy.context.user_preferences.inputs.view_zoom_method",
202     ]
203
204     preset_subdir = "interaction"
205
206 def register():
207     pass
208
209
210 def unregister():
211     pass
212
213 if __name__ == "__main__":
214     register()