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