Fixing issues with i18n stuff:
[blender.git] / release / scripts / startup / bl_operators / 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-80 compliant>
20
21 import bpy
22 from bpy.types import Menu, Operator
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     bl_options = {'REGISTER'}  # only because invoke_props_popup requires.
33
34     name = bpy.props.StringProperty(
35             name="Name",
36             description="Name of the preset, used to make the path name",
37             maxlen=64,
38             )
39     remove_active = bpy.props.BoolProperty(
40             default=False,
41             options={'HIDDEN'},
42             )
43
44     @staticmethod
45     def as_filename(name):  # could reuse for other presets
46         for char in " !@#$%^&*(){}:\";'[]<>,.\\/?":
47             name = name.replace(char, '_')
48         return name.lower().strip()
49
50     def execute(self, context):
51         import os
52
53         if hasattr(self, "pre_cb"):
54             self.pre_cb(context)
55
56         preset_menu_class = getattr(bpy.types, self.preset_menu)
57
58         if not self.remove_active:
59             name = self.name.strip()
60             if not name:
61                 return {'FINISHED'}
62
63             filename = self.as_filename(name)
64
65             target_path = os.path.join("presets", self.preset_subdir)
66             target_path = bpy.utils.user_resource('SCRIPTS',
67                                                   target_path,
68                                                   create=True)
69
70             if not target_path:
71                 self.report({'WARNING'}, "Failed to create presets path")
72                 return {'CANCELLED'}
73
74             filepath = os.path.join(target_path, filename) + ".py"
75
76             if hasattr(self, "add"):
77                 self.add(context, filepath)
78             else:
79                 file_preset = open(filepath, 'w')
80                 file_preset.write("import bpy\n")
81
82                 if hasattr(self, "preset_defines"):
83                     for rna_path in self.preset_defines:
84                         exec(rna_path)
85                         file_preset.write("%s\n" % rna_path)
86                     file_preset.write("\n")
87
88                 for rna_path in self.preset_values:
89                     value = eval(rna_path)
90                     # convert thin wrapped sequences to simple lists to repr()
91                     try:
92                         value = value[:]
93                     except:
94                         pass
95
96                     file_preset.write("%s = %r\n" % (rna_path, value))
97
98                 file_preset.close()
99
100             preset_menu_class.bl_label = bpy.path.display_name(filename)
101
102         else:
103             preset_active = preset_menu_class.bl_label
104
105             # fairly sloppy but convenient.
106             filepath = bpy.utils.preset_find(preset_active, self.preset_subdir)
107
108             if not filepath:
109                 filepath = bpy.utils.preset_find(preset_active,
110                                                  self.preset_subdir,
111                                                  display_name=True)
112
113             if not filepath:
114                 return {'CANCELLED'}
115
116             if hasattr(self, "remove"):
117                 self.remove(context, filepath)
118             else:
119                 try:
120                     os.remove(filepath)
121                 except:
122                     import traceback
123                     traceback.print_exc()
124
125             # XXX, stupid!
126             preset_menu_class.bl_label = "Presets"
127
128         if hasattr(self, "post_cb"):
129             self.post_cb(context)
130
131         return {'FINISHED'}
132
133     def check(self, context):
134         self.name = self.as_filename(self.name.strip())
135
136     def invoke(self, context, event):
137         if not self.remove_active:
138             wm = context.window_manager
139             return wm.invoke_props_dialog(self)
140         else:
141             return self.execute(context)
142
143
144 class ExecutePreset(Operator):
145     ''' Executes a preset '''
146     bl_idname = "script.execute_preset"
147     bl_label = "Execute a Python Preset"
148
149     filepath = bpy.props.StringProperty(
150             name="Path",
151             description="Path of the Python file to execute",
152             maxlen=512,
153             )
154     menu_idname = bpy.props.StringProperty(
155             name="Menu ID Name",
156             description="ID name of the menu this was called from",
157             )
158
159     def execute(self, context):
160         from os.path import basename
161         filepath = self.filepath
162
163         # change the menu title to the most recently chosen option
164         preset_class = getattr(bpy.types, self.menu_idname)
165         preset_class.bl_label = bpy.path.display_name(basename(filepath))
166
167         # execute the preset using script.python_file_run
168         bpy.ops.script.python_file_run(filepath=filepath)
169         return {'FINISHED'}
170
171
172 class AddPresetRender(AddPresetBase, Operator):
173     '''Add a Render Preset'''
174     bl_idname = "render.preset_add"
175     bl_label = "Add Render Preset"
176     preset_menu = "RENDER_MT_presets"
177
178     preset_defines = [
179         "scene = bpy.context.scene"
180     ]
181
182     preset_values = [
183         "scene.render.field_order",
184         "scene.render.fps",
185         "scene.render.fps_base",
186         "scene.render.pixel_aspect_x",
187         "scene.render.pixel_aspect_y",
188         "scene.render.resolution_percentage",
189         "scene.render.resolution_x",
190         "scene.render.resolution_y",
191         "scene.render.use_fields",
192         "scene.render.use_fields_still",
193     ]
194
195     preset_subdir = "render"
196
197
198 class AddPresetSSS(AddPresetBase, Operator):
199     '''Add a Subsurface Scattering Preset'''
200     bl_idname = "material.sss_preset_add"
201     bl_label = "Add SSS Preset"
202     preset_menu = "MATERIAL_MT_sss_presets"
203
204     preset_defines = [
205         ("material = "
206          "bpy.context.material.active_node_material "
207          "if bpy.context.material.active_node_material "
208          "else bpy.context.material")
209     ]
210
211     preset_values = [
212         "material.subsurface_scattering.back",
213         "material.subsurface_scattering.color",
214         "material.subsurface_scattering.color_factor",
215         "material.subsurface_scattering.error_threshold",
216         "material.subsurface_scattering.front",
217         "material.subsurface_scattering.ior",
218         "material.subsurface_scattering.radius",
219         "material.subsurface_scattering.scale",
220         "material.subsurface_scattering.texture_factor",
221     ]
222
223     preset_subdir = "sss"
224
225
226 class AddPresetCloth(AddPresetBase, Operator):
227     '''Add a Cloth Preset'''
228     bl_idname = "cloth.preset_add"
229     bl_label = "Add Cloth Preset"
230     preset_menu = "CLOTH_MT_presets"
231
232     preset_defines = [
233         "cloth = bpy.context.cloth"
234     ]
235
236     preset_values = [
237         "cloth.settings.air_damping",
238         "cloth.settings.bending_stiffness",
239         "cloth.settings.mass",
240         "cloth.settings.quality",
241         "cloth.settings.spring_damping",
242         "cloth.settings.structural_stiffness",
243     ]
244
245     preset_subdir = "cloth"
246
247
248 class AddPresetSunSky(AddPresetBase, Operator):
249     '''Add a Sky & Atmosphere Preset'''
250     bl_idname = "lamp.sunsky_preset_add"
251     bl_label = "Add Sunsky Preset"
252     preset_menu = "LAMP_MT_sunsky_presets"
253
254     preset_defines = [
255         "sky = bpy.context.object.data.sky"
256     ]
257
258     preset_values = [
259         "sky.atmosphere_extinction",
260         "sky.atmosphere_inscattering",
261         "sky.atmosphere_turbidity",
262         "sky.backscattered_light",
263         "sky.horizon_brightness",
264         "sky.spread",
265         "sky.sun_brightness",
266         "sky.sun_intensity",
267         "sky.sun_size",
268         "sky.sky_blend",
269         "sky.sky_blend_type",
270         "sky.sky_color_space",
271         "sky.sky_exposure",
272     ]
273
274     preset_subdir = "sunsky"
275
276
277 class AddPresetInteraction(AddPresetBase, Operator):
278     '''Add an Application Interaction Preset'''
279     bl_idname = "wm.interaction_preset_add"
280     bl_label = "Add Interaction Preset"
281     preset_menu = "USERPREF_MT_interaction_presets"
282
283     preset_defines = [
284         "user_preferences = bpy.context.user_preferences"
285     ]
286
287     preset_values = [
288         "user_preferences.edit.use_drag_immediately",
289         "user_preferences.edit.use_insertkey_xyz_to_rgb",
290         "user_preferences.inputs.invert_mouse_zoom",
291         "user_preferences.inputs.select_mouse",
292         "user_preferences.inputs.use_emulate_numpad",
293         "user_preferences.inputs.use_mouse_continuous",
294         "user_preferences.inputs.use_mouse_emulate_3_button",
295         "user_preferences.inputs.view_rotate_method",
296         "user_preferences.inputs.view_zoom_axis",
297         "user_preferences.inputs.view_zoom_method",
298     ]
299
300     preset_subdir = "interaction"
301
302
303 class AddPresetKeyconfig(AddPresetBase, Operator):
304     '''Add a Keyconfig Preset'''
305     bl_idname = "wm.keyconfig_preset_add"
306     bl_label = "Add Keyconfig Preset"
307     preset_menu = "USERPREF_MT_keyconfigs"
308     preset_subdir = "keyconfig"
309
310     def add(self, context, filepath):
311         bpy.ops.wm.keyconfig_export(filepath=filepath)
312         bpy.utils.keyconfig_set(filepath)
313
314     def pre_cb(self, context):
315         keyconfigs = bpy.context.window_manager.keyconfigs
316         if self.remove_active:
317             preset_menu_class = getattr(bpy.types, self.preset_menu)
318             preset_menu_class.bl_label = keyconfigs.active.name
319
320     def post_cb(self, context):
321         keyconfigs = bpy.context.window_manager.keyconfigs
322         if self.remove_active:
323             keyconfigs.remove(keyconfigs.active)
324
325
326 class AddPresetOperator(AddPresetBase, Operator):
327     '''Add an Application Interaction Preset'''
328     bl_idname = "wm.operator_preset_add"
329     bl_label = "Operator Preset"
330     preset_menu = "WM_MT_operator_presets"
331
332     operator = bpy.props.StringProperty(
333             name="Operator",
334             maxlen=64,
335             options={'HIDDEN'},
336             )
337
338     # XXX, not ideal
339     preset_defines = [
340         "op = bpy.context.space_data.operator",
341     ]
342
343     @property
344     def preset_subdir(self):
345         return AddPresetOperator.operator_path(self.operator)
346
347     @property
348     def preset_values(self):
349         properties_blacklist = Operator.bl_rna.properties.keys()
350
351         prefix, suffix = self.operator.split("_OT_", 1)
352         op = getattr(getattr(bpy.ops, prefix.lower()), suffix)
353         operator_rna = op.get_rna().bl_rna
354         del op
355
356         ret = []
357         for prop_id, prop in operator_rna.properties.items():
358             if not (prop.is_hidden or prop.is_skip_save):
359                 if prop_id not in properties_blacklist:
360                     ret.append("op.%s" % prop_id)
361
362         return ret
363
364     @staticmethod
365     def operator_path(operator):
366         import os
367         prefix, suffix = operator.split("_OT_", 1)
368         return os.path.join("operator", "%s.%s" % (prefix.lower(), suffix))
369
370
371 class WM_MT_operator_presets(Menu):
372     bl_label = "Operator Presets"
373
374     def draw(self, context):
375         self.operator = context.space_data.operator.bl_idname
376         Menu.draw_preset(self, context)
377
378     @property
379     def preset_subdir(self):
380         return AddPresetOperator.operator_path(self.operator)
381
382     preset_operator = "script.execute_preset"