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