typo in own recent commit.
[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 compliant>
20
21 import bpy
22 from bpy.types import Menu, Operator
23 from bpy.props import StringProperty, BoolProperty
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 = StringProperty(
36             name="Name",
37             description="Name of the preset, used to make the path name",
38             maxlen=64,
39             options={'SKIP_SAVE'},
40             )
41     remove_active = BoolProperty(
42             default=False,
43             options={'HIDDEN', 'SKIP_SAVE'},
44             )
45
46     @staticmethod
47     def as_filename(name):  # could reuse for other presets
48         for char in " !@#$%^&*(){}:\";'[]<>,.\\/?":
49             name = name.replace(char, '_')
50         return name.lower().strip()
51
52     def execute(self, context):
53         import os
54
55         if hasattr(self, "pre_cb"):
56             self.pre_cb(context)
57
58         preset_menu_class = getattr(bpy.types, self.preset_menu)
59
60         is_xml = getattr(preset_menu_class, "preset_type", None) == 'XML'
61
62         if is_xml:
63             ext = ".xml"
64         else:
65             ext = ".py"
66
67         if not self.remove_active:
68             name = self.name.strip()
69             if not name:
70                 return {'FINISHED'}
71
72             filename = self.as_filename(name)
73
74             target_path = os.path.join("presets", self.preset_subdir)
75             target_path = bpy.utils.user_resource('SCRIPTS',
76                                                   target_path,
77                                                   create=True)
78
79             if not target_path:
80                 self.report({'WARNING'}, "Failed to create presets path")
81                 return {'CANCELLED'}
82
83             filepath = os.path.join(target_path, filename) + ext
84
85             if hasattr(self, "add"):
86                 self.add(context, filepath)
87             else:
88                 print("Writing Preset: %r" % filepath)
89
90                 if is_xml:
91                     import rna_xml
92                     rna_xml.xml_file_write(context,
93                                            filepath,
94                                            preset_menu_class.preset_xml_map)
95                 else:
96
97                     def rna_recursive_attr_expand(value, rna_path_step, level):
98                         if isinstance(value, bpy.types.PropertyGroup):
99                             for sub_value_attr in value.bl_rna.properties.keys():
100                                 if sub_value_attr == "rna_type":
101                                     continue
102                                 sub_value = getattr(value, sub_value_attr)
103                                 rna_recursive_attr_expand(sub_value, "%s.%s" % (rna_path_step, sub_value_attr), level)
104                         elif type(value).__name__ == "bpy_prop_collection_idprop":  # could use nicer method
105                             file_preset.write("%s.clear()\n" % rna_path_step)
106                             for sub_value in value:
107                                 file_preset.write("item_sub_%d = %s.add()\n" % (level, rna_path_step))
108                                 rna_recursive_attr_expand(sub_value, "item_sub_%d" % level, level + 1)
109                         else:
110                             # convert thin wrapped sequences
111                             # to simple lists to repr()
112                             try:
113                                 value = value[:]
114                             except:
115                                 pass
116
117                             file_preset.write("%s = %r\n" % (rna_path_step, value))
118
119                     file_preset = open(filepath, 'w')
120                     file_preset.write("import bpy\n")
121
122                     if hasattr(self, "preset_defines"):
123                         for rna_path in self.preset_defines:
124                             exec(rna_path)
125                             file_preset.write("%s\n" % rna_path)
126                         file_preset.write("\n")
127
128                     for rna_path in self.preset_values:
129                         value = eval(rna_path)
130                         rna_recursive_attr_expand(value, rna_path, 1)
131
132                     file_preset.close()
133
134             preset_menu_class.bl_label = bpy.path.display_name(filename)
135
136         else:
137             preset_active = preset_menu_class.bl_label
138
139             # fairly sloppy but convenient.
140             filepath = bpy.utils.preset_find(preset_active,
141                                              self.preset_subdir,
142                                              ext=ext)
143
144             if not filepath:
145                 filepath = bpy.utils.preset_find(preset_active,
146                                                  self.preset_subdir,
147                                                  display_name=True,
148                                                  ext=ext)
149
150             if not filepath:
151                 return {'CANCELLED'}
152
153             if hasattr(self, "remove"):
154                 self.remove(context, filepath)
155             else:
156                 try:
157                     os.remove(filepath)
158                 except:
159                     import traceback
160                     traceback.print_exc()
161
162             # XXX, stupid!
163             preset_menu_class.bl_label = "Presets"
164
165         if hasattr(self, "post_cb"):
166             self.post_cb(context)
167
168         return {'FINISHED'}
169
170     def check(self, context):
171         self.name = self.as_filename(self.name.strip())
172
173     def invoke(self, context, event):
174         if not self.remove_active:
175             wm = context.window_manager
176             return wm.invoke_props_dialog(self)
177         else:
178             return self.execute(context)
179
180
181 class ExecutePreset(Operator):
182     """Execute a preset"""
183     bl_idname = "script.execute_preset"
184     bl_label = "Execute a Python Preset"
185
186     filepath = StringProperty(
187             subtype='FILE_PATH',
188             options={'SKIP_SAVE'},
189             )
190     menu_idname = StringProperty(
191             name="Menu ID Name",
192             description="ID name of the menu this was called from",
193             options={'SKIP_SAVE'},
194             )
195
196     def execute(self, context):
197         from os.path import basename, splitext
198         filepath = self.filepath
199
200         # change the menu title to the most recently chosen option
201         preset_class = getattr(bpy.types, self.menu_idname)
202         preset_class.bl_label = bpy.path.display_name(basename(filepath))
203
204         ext = splitext(filepath)[1].lower()
205
206         # execute the preset using script.python_file_run
207         if ext == ".py":
208             bpy.ops.script.python_file_run(filepath=filepath)
209         elif ext == ".xml":
210             import rna_xml
211             rna_xml.xml_file_run(context,
212                                  filepath,
213                                  preset_class.preset_xml_map)
214         else:
215             self.report({'ERROR'}, "unknown filetype: %r" % ext)
216             return {'CANCELLED'}
217
218         return {'FINISHED'}
219
220
221 class AddPresetRender(AddPresetBase, Operator):
222     """Add a Render Preset"""
223     bl_idname = "render.preset_add"
224     bl_label = "Add Render Preset"
225     preset_menu = "RENDER_MT_presets"
226
227     preset_defines = [
228         "scene = bpy.context.scene"
229     ]
230
231     preset_values = [
232         "scene.render.field_order",
233         "scene.render.fps",
234         "scene.render.fps_base",
235         "scene.render.pixel_aspect_x",
236         "scene.render.pixel_aspect_y",
237         "scene.render.resolution_percentage",
238         "scene.render.resolution_x",
239         "scene.render.resolution_y",
240         "scene.render.use_fields",
241         "scene.render.use_fields_still",
242     ]
243
244     preset_subdir = "render"
245
246
247 class AddPresetCamera(AddPresetBase, Operator):
248     """Add a Camera Preset"""
249     bl_idname = "camera.preset_add"
250     bl_label = "Add Camera Preset"
251     preset_menu = "CAMERA_MT_presets"
252
253     preset_defines = [
254         "cam = bpy.context.object.data"
255     ]
256
257     preset_values = [
258         "cam.sensor_width",
259         "cam.sensor_height",
260         "cam.sensor_fit"
261     ]
262
263     preset_subdir = "camera"
264
265
266 class AddPresetSSS(AddPresetBase, Operator):
267     """Add a Subsurface Scattering Preset"""
268     bl_idname = "material.sss_preset_add"
269     bl_label = "Add SSS Preset"
270     preset_menu = "MATERIAL_MT_sss_presets"
271
272     preset_defines = [
273         ("material = "
274          "bpy.context.material.active_node_material "
275          "if bpy.context.material.active_node_material "
276          "else bpy.context.material")
277     ]
278
279     preset_values = [
280         "material.subsurface_scattering.back",
281         "material.subsurface_scattering.color",
282         "material.subsurface_scattering.color_factor",
283         "material.subsurface_scattering.error_threshold",
284         "material.subsurface_scattering.front",
285         "material.subsurface_scattering.ior",
286         "material.subsurface_scattering.radius",
287         "material.subsurface_scattering.scale",
288         "material.subsurface_scattering.texture_factor",
289     ]
290
291     preset_subdir = "sss"
292
293
294 class AddPresetCloth(AddPresetBase, Operator):
295     """Add a Cloth Preset"""
296     bl_idname = "cloth.preset_add"
297     bl_label = "Add Cloth Preset"
298     preset_menu = "CLOTH_MT_presets"
299
300     preset_defines = [
301         "cloth = bpy.context.cloth"
302     ]
303
304     preset_values = [
305         "cloth.settings.air_damping",
306         "cloth.settings.bending_stiffness",
307         "cloth.settings.mass",
308         "cloth.settings.quality",
309         "cloth.settings.spring_damping",
310         "cloth.settings.structural_stiffness",
311     ]
312
313     preset_subdir = "cloth"
314
315
316 class AddPresetFluid(AddPresetBase, Operator):
317     """Add a Fluid Preset"""
318     bl_idname = "fluid.preset_add"
319     bl_label = "Add Fluid Preset"
320     preset_menu = "FLUID_MT_presets"
321
322     preset_defines = [
323     "fluid = bpy.context.fluid"
324     ]
325
326     preset_values = [
327     "fluid.settings.viscosity_base",
328     "fluid.settings.viscosity_exponent",
329     ]
330
331     preset_subdir = "fluid"
332
333
334 class AddPresetSunSky(AddPresetBase, Operator):
335     """Add a Sky & Atmosphere Preset"""
336     bl_idname = "lamp.sunsky_preset_add"
337     bl_label = "Add Sunsky Preset"
338     preset_menu = "LAMP_MT_sunsky_presets"
339
340     preset_defines = [
341         "sky = bpy.context.object.data.sky"
342     ]
343
344     preset_values = [
345         "sky.atmosphere_extinction",
346         "sky.atmosphere_inscattering",
347         "sky.atmosphere_turbidity",
348         "sky.backscattered_light",
349         "sky.horizon_brightness",
350         "sky.spread",
351         "sky.sun_brightness",
352         "sky.sun_intensity",
353         "sky.sun_size",
354         "sky.sky_blend",
355         "sky.sky_blend_type",
356         "sky.sky_color_space",
357         "sky.sky_exposure",
358     ]
359
360     preset_subdir = "sunsky"
361
362
363 class AddPresetInteraction(AddPresetBase, Operator):
364     """Add an Application Interaction Preset"""
365     bl_idname = "wm.interaction_preset_add"
366     bl_label = "Add Interaction Preset"
367     preset_menu = "USERPREF_MT_interaction_presets"
368
369     preset_defines = [
370         "user_preferences = bpy.context.user_preferences"
371     ]
372
373     preset_values = [
374         "user_preferences.edit.use_drag_immediately",
375         "user_preferences.edit.use_insertkey_xyz_to_rgb",
376         "user_preferences.inputs.invert_mouse_zoom",
377         "user_preferences.inputs.select_mouse",
378         "user_preferences.inputs.use_emulate_numpad",
379         "user_preferences.inputs.use_mouse_continuous",
380         "user_preferences.inputs.use_mouse_emulate_3_button",
381         "user_preferences.inputs.view_rotate_method",
382         "user_preferences.inputs.view_zoom_axis",
383         "user_preferences.inputs.view_zoom_method",
384     ]
385
386     preset_subdir = "interaction"
387
388
389 class AddPresetTrackingCamera(AddPresetBase, Operator):
390     """Add a Tracking Camera Intrinsics  Preset"""
391     bl_idname = "clip.camera_preset_add"
392     bl_label = "Add Camera Preset"
393     preset_menu = "CLIP_MT_camera_presets"
394
395     preset_defines = [
396         "camera = bpy.context.edit_movieclip.tracking.camera"
397     ]
398
399     preset_values = [
400         "camera.sensor_width",
401         "camera.units",
402         "camera.focal_length",
403         "camera.pixel_aspect",
404         "camera.k1",
405         "camera.k2",
406         "camera.k3"
407     ]
408
409     preset_subdir = "tracking_camera"
410
411
412 class AddPresetTrackingTrackColor(AddPresetBase, Operator):
413     """Add a Clip Track Color Preset"""
414     bl_idname = "clip.track_color_preset_add"
415     bl_label = "Add Track Color Preset"
416     preset_menu = "CLIP_MT_track_color_presets"
417
418     preset_defines = [
419         "track = bpy.context.edit_movieclip.tracking.tracks.active"
420     ]
421
422     preset_values = [
423         "track.color",
424         "track.use_custom_color"
425     ]
426
427     preset_subdir = "tracking_track_color"
428
429
430 class AddPresetTrackingSettings(AddPresetBase, Operator):
431     """Add a motion tracking settings preset"""
432     bl_idname = "clip.tracking_settings_preset_add"
433     bl_label = "Add Tracking Settings Preset"
434     preset_menu = "CLIP_MT_tracking_settings_presets"
435
436     preset_defines = [
437         "settings = bpy.context.edit_movieclip.tracking.settings"
438     ]
439
440     preset_values = [
441         "settings.default_correlation_min",
442         "settings.default_pattern_size",
443         "settings.default_search_size",
444         "settings.default_frames_limit",
445         "settings.default_pattern_match",
446         "settings.default_margin",
447         "settings.default_motion_model",
448         "settings.use_default_brute",
449         "settings.use_default_normalization",
450         "settings.use_default_mask",
451         "settings.use_default_red_channel",
452         "settings.use_default_green_channel",
453         "settings.use_default_blue_channel"
454     ]
455
456     preset_subdir = "tracking_settings"
457
458
459 class AddPresetNodeColor(AddPresetBase, Operator):
460     """Add a Node Color Preset"""
461     bl_idname = "node.node_color_preset_add"
462     bl_label = "Add Node Color Preset"
463     preset_menu = "NODE_MT_node_color_presets"
464
465     preset_defines = [
466         "node = bpy.context.active_node"
467     ]
468
469     preset_values = [
470         "node.color",
471         "node.use_custom_color"
472     ]
473
474     preset_subdir = "node_color"
475
476
477 class AddPresetInterfaceTheme(AddPresetBase, Operator):
478     """Add a theme preset"""
479     bl_idname = "wm.interface_theme_preset_add"
480     bl_label = "Add Tracking Settings Preset"
481     preset_menu = "USERPREF_MT_interface_theme_presets"
482     preset_subdir = "interface_theme"
483
484
485 class AddPresetKeyconfig(AddPresetBase, Operator):
486     """Add a Key-config Preset"""
487     bl_idname = "wm.keyconfig_preset_add"
488     bl_label = "Add Keyconfig Preset"
489     preset_menu = "USERPREF_MT_keyconfigs"
490     preset_subdir = "keyconfig"
491
492     def add(self, context, filepath):
493         bpy.ops.wm.keyconfig_export(filepath=filepath)
494         bpy.utils.keyconfig_set(filepath)
495
496     def pre_cb(self, context):
497         keyconfigs = bpy.context.window_manager.keyconfigs
498         if self.remove_active:
499             preset_menu_class = getattr(bpy.types, self.preset_menu)
500             preset_menu_class.bl_label = keyconfigs.active.name
501
502     def post_cb(self, context):
503         keyconfigs = bpy.context.window_manager.keyconfigs
504         if self.remove_active:
505             keyconfigs.remove(keyconfigs.active)
506
507
508 class AddPresetOperator(AddPresetBase, Operator):
509     """Add an Operator Preset"""
510     bl_idname = "wm.operator_preset_add"
511     bl_label = "Operator Preset"
512     preset_menu = "WM_MT_operator_presets"
513
514     operator = StringProperty(
515             name="Operator",
516             maxlen=64,
517             options={'HIDDEN', 'SKIP_SAVE'},
518             )
519
520     preset_defines = [
521         "op = bpy.context.active_operator",
522     ]
523
524     @property
525     def preset_subdir(self):
526         return AddPresetOperator.operator_path(self.operator)
527
528     @property
529     def preset_values(self):
530         properties_blacklist = Operator.bl_rna.properties.keys()
531
532         prefix, suffix = self.operator.split("_OT_", 1)
533         op = getattr(getattr(bpy.ops, prefix.lower()), suffix)
534         operator_rna = op.get_rna().bl_rna
535         del op
536
537         ret = []
538         for prop_id, prop in operator_rna.properties.items():
539             if not (prop.is_hidden or prop.is_skip_save):
540                 if prop_id not in properties_blacklist:
541                     ret.append("op.%s" % prop_id)
542
543         return ret
544
545     @staticmethod
546     def operator_path(operator):
547         import os
548         prefix, suffix = operator.split("_OT_", 1)
549         return os.path.join("operator", "%s.%s" % (prefix.lower(), suffix))
550
551
552 class WM_MT_operator_presets(Menu):
553     bl_label = "Operator Presets"
554
555     def draw(self, context):
556         self.operator = context.active_operator.bl_idname
557
558         # dummy 'default' menu item
559         layout = self.layout
560         layout.operator("wm.operator_defaults")
561         layout.separator()
562
563         Menu.draw_preset(self, context)
564
565     @property
566     def preset_subdir(self):
567         return AddPresetOperator.operator_path(self.operator)
568
569     preset_operator = "script.execute_preset"