fix [#28191] Exception when enabling a script for a newer Blender build
[blender.git] / release / scripts / startup / bl_ui / space_userpref.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 import bpy
21 import os
22 import addon_utils
23
24 from bpy.props import StringProperty, BoolProperty, EnumProperty
25
26
27 def ui_items_general(col, context):
28     """ General UI Theme Settings (User Interface)
29     """
30
31     row = col.row()
32
33     subsplit = row.split(percentage=0.95)
34
35     padding = subsplit.split(percentage=0.15)
36     colsub = padding.column()
37     colsub = padding.column()
38     colsub.row().prop(context, "outline")
39     colsub.row().prop(context, "item", slider=True)
40     colsub.row().prop(context, "inner", slider=True)
41     colsub.row().prop(context, "inner_sel", slider=True)
42
43     subsplit = row.split(percentage=0.85)
44
45     padding = subsplit.split(percentage=0.15)
46     colsub = padding.column()
47     colsub = padding.column()
48     colsub.row().prop(context, "text")
49     colsub.row().prop(context, "text_sel")
50     colsub.prop(context, "show_shaded")
51     subsub = colsub.column(align=True)
52     subsub.active = context.show_shaded
53     subsub.prop(context, "shadetop")
54     subsub.prop(context, "shadedown")
55
56     col.separator()
57
58
59 def opengl_lamp_buttons(column, lamp):
60     split = column.split(percentage=0.1)
61
62     split.prop(lamp, "use", text="", icon='OUTLINER_OB_LAMP' if lamp.use else 'LAMP_DATA')
63
64     col = split.column()
65     col.active = lamp.use
66     row = col.row()
67     row.label(text="Diffuse:")
68     row.prop(lamp, "diffuse_color", text="")
69     row = col.row()
70     row.label(text="Specular:")
71     row.prop(lamp, "specular_color", text="")
72
73     col = split.column()
74     col.active = lamp.use
75     col.prop(lamp, "direction", text="")
76
77
78 class USERPREF_HT_header(bpy.types.Header):
79     bl_space_type = 'USER_PREFERENCES'
80
81     def draw(self, context):
82         layout = self.layout
83         layout.template_header(menus=False)
84
85         userpref = context.user_preferences
86
87         layout.operator_context = 'EXEC_AREA'
88         layout.operator("wm.save_homefile", text="Save As Default")
89
90         layout.operator_context = 'INVOKE_DEFAULT'
91
92         if userpref.active_section == 'INPUT':
93             layout.operator("wm.keyconfig_export")
94             layout.operator("wm.keyconfig_import")
95         elif userpref.active_section == 'ADDONS':
96             layout.operator("wm.addon_install")
97             layout.menu("USERPREF_MT_addons_dev_guides")
98         elif userpref.active_section == 'THEMES':
99             layout.operator("ui.reset_default_theme")
100
101
102 class USERPREF_PT_tabs(bpy.types.Panel):
103     bl_label = ""
104     bl_space_type = 'USER_PREFERENCES'
105     bl_region_type = 'WINDOW'
106     bl_options = {'HIDE_HEADER'}
107
108     def draw(self, context):
109         layout = self.layout
110
111         userpref = context.user_preferences
112
113         layout.prop(userpref, "active_section", expand=True)
114
115
116 class USERPREF_MT_interaction_presets(bpy.types.Menu):
117     bl_label = "Presets"
118     preset_subdir = "interaction"
119     preset_operator = "script.execute_preset"
120     draw = bpy.types.Menu.draw_preset
121
122
123 class USERPREF_MT_appconfigs(bpy.types.Menu):
124     bl_label = "AppPresets"
125     preset_subdir = "keyconfig"
126     preset_operator = "wm.appconfig_activate"
127
128     def draw(self, context):
129         self.layout.operator("wm.appconfig_default", text="Blender (default)")
130
131         # now draw the presets
132         bpy.types.Menu.draw_preset(self, context)
133
134
135 class USERPREF_MT_splash(bpy.types.Menu):
136     bl_label = "Splash"
137
138     def draw(self, context):
139         layout = self.layout
140         split = layout.split()
141         row = split.row()
142         row.label("")
143         row = split.row()
144         row.label("Interaction:")
145         # XXX, no redraws
146         # text = bpy.path.display_name(context.window_manager.keyconfigs.active.name)
147         # if not text:
148         #     text = "Blender (default)"
149         row.menu("USERPREF_MT_appconfigs", text="Preset")
150
151
152 class USERPREF_PT_interface(bpy.types.Panel):
153     bl_space_type = 'USER_PREFERENCES'
154     bl_label = "Interface"
155     bl_region_type = 'WINDOW'
156     bl_options = {'HIDE_HEADER'}
157
158     @classmethod
159     def poll(cls, context):
160         userpref = context.user_preferences
161         return (userpref.active_section == 'INTERFACE')
162
163     def draw(self, context):
164         layout = self.layout
165
166         userpref = context.user_preferences
167         view = userpref.view
168
169         row = layout.row()
170
171         col = row.column()
172         col.label(text="Display:")
173         col.prop(view, "show_tooltips")
174         col.prop(view, "show_tooltips_python")
175         col.prop(view, "show_object_info", text="Object Info")
176         col.prop(view, "show_large_cursors")
177         col.prop(view, "show_view_name", text="View Name")
178         col.prop(view, "show_playback_fps", text="Playback FPS")
179         col.prop(view, "use_global_scene")
180         col.prop(view, "object_origin_size")
181
182         col.separator()
183         col.separator()
184         col.separator()
185
186         col.prop(view, "show_mini_axis", text="Display Mini Axis")
187         sub = col.column()
188         sub.active = view.show_mini_axis
189         sub.prop(view, "mini_axis_size", text="Size")
190         sub.prop(view, "mini_axis_brightness", text="Brightness")
191
192         col.separator()
193         row.separator()
194         row.separator()
195
196         col = row.column()
197         col.label(text="View Manipulation:")
198         col.prop(view, "use_mouse_auto_depth")
199         col.prop(view, "use_zoom_to_mouse")
200         col.prop(view, "use_rotate_around_active")
201         col.prop(view, "use_global_pivot")
202         col.prop(view, "use_camera_lock_parent")
203
204         col.separator()
205
206         col.prop(view, "use_auto_perspective")
207         col.prop(view, "smooth_view")
208         col.prop(view, "rotation_angle")
209
210         col.separator()
211         col.separator()
212
213         col.label(text="2D Viewports:")
214         col.prop(view, "view2d_grid_spacing_min", text="Minimum Grid Spacing")
215         col.prop(view, "timecode_style")
216
217         row.separator()
218         row.separator()
219
220         col = row.column()
221         #Toolbox doesn't exist yet
222         #col.label(text="Toolbox:")
223         #col.prop(view, "show_column_layout")
224         #col.label(text="Open Toolbox Delay:")
225         #col.prop(view, "open_left_mouse_delay", text="Hold LMB")
226         #col.prop(view, "open_right_mouse_delay", text="Hold RMB")
227         col.prop(view, "show_manipulator")
228         sub = col.column()
229         sub.active = view.show_manipulator
230         sub.prop(view, "manipulator_size", text="Size")
231         sub.prop(view, "manipulator_handle_size", text="Handle Size")
232         sub.prop(view, "manipulator_hotspot", text="Hotspot")
233
234         col.separator()
235         col.separator()
236         col.separator()
237
238         col.label(text="Menus:")
239         col.prop(view, "use_mouse_over_open")
240         col.label(text="Menu Open Delay:")
241         col.prop(view, "open_toplevel_delay", text="Top Level")
242         col.prop(view, "open_sublevel_delay", text="Sub Level")
243
244         col.separator()
245
246         col.prop(view, "show_splash")
247
248
249 class USERPREF_PT_edit(bpy.types.Panel):
250     bl_space_type = 'USER_PREFERENCES'
251     bl_label = "Edit"
252     bl_region_type = 'WINDOW'
253     bl_options = {'HIDE_HEADER'}
254
255     @classmethod
256     def poll(cls, context):
257         userpref = context.user_preferences
258         return (userpref.active_section == 'EDITING')
259
260     def draw(self, context):
261         layout = self.layout
262
263         userpref = context.user_preferences
264         edit = userpref.edit
265
266         row = layout.row()
267
268         col = row.column()
269         col.label(text="Link Materials To:")
270         col.prop(edit, "material_link", text="")
271
272         col.separator()
273         col.separator()
274         col.separator()
275
276         col.label(text="New Objects:")
277         col.prop(edit, "use_enter_edit_mode")
278         col.label(text="Align To:")
279         col.prop(edit, "object_align", text="")
280
281         col.separator()
282         col.separator()
283         col.separator()
284
285         col.label(text="Undo:")
286         col.prop(edit, "use_global_undo")
287         col.prop(edit, "undo_steps", text="Steps")
288         col.prop(edit, "undo_memory_limit", text="Memory Limit")
289
290         row.separator()
291         row.separator()
292
293         col = row.column()
294         col.label(text="Grease Pencil:")
295         col.prop(edit, "grease_pencil_manhattan_distance", text="Manhattan Distance")
296         col.prop(edit, "grease_pencil_euclidean_distance", text="Euclidean Distance")
297         #col.prop(edit, "use_grease_pencil_simplify_stroke", text="Simplify Stroke")
298         col.prop(edit, "grease_pencil_eraser_radius", text="Eraser Radius")
299         col.prop(edit, "use_grease_pencil_smooth_stroke", text="Smooth Stroke")
300         col.separator()
301         col.separator()
302         col.separator()
303         col.label(text="Playback:")
304         col.prop(edit, "use_negative_frames")
305
306         row.separator()
307         row.separator()
308
309         col = row.column()
310         col.label(text="Keyframing:")
311         col.prop(edit, "use_visual_keying")
312         col.prop(edit, "use_keyframe_insert_needed", text="Only Insert Needed")
313
314         col.separator()
315
316         col.prop(edit, "use_auto_keying", text="Auto Keyframing:")
317
318         sub = col.column()
319
320         # sub.active = edit.use_keyframe_insert_auto # incorrect, timeline can enable
321         sub.prop(edit, "use_keyframe_insert_available", text="Only Insert Available")
322
323         col.separator()
324
325         col.label(text="New F-Curve Defaults:")
326         col.prop(edit, "keyframe_new_interpolation_type", text="Interpolation")
327         col.prop(edit, "keyframe_new_handle_type", text="Handles")
328         col.prop(edit, "use_insertkey_xyz_to_rgb", text="XYZ to RGB")
329
330         col.separator()
331         col.separator()
332         col.separator()
333
334         col.label(text="Transform:")
335         col.prop(edit, "use_drag_immediately")
336
337         row.separator()
338         row.separator()
339
340         col = row.column()
341         col.prop(edit, "sculpt_paint_overlay_color", text="Sculpt Overlay Color")
342
343         col.separator()
344         col.separator()
345         col.separator()
346
347         col.label(text="Duplicate Data:")
348         col.prop(edit, "use_duplicate_mesh", text="Mesh")
349         col.prop(edit, "use_duplicate_surface", text="Surface")
350         col.prop(edit, "use_duplicate_curve", text="Curve")
351         col.prop(edit, "use_duplicate_text", text="Text")
352         col.prop(edit, "use_duplicate_metaball", text="Metaball")
353         col.prop(edit, "use_duplicate_armature", text="Armature")
354         col.prop(edit, "use_duplicate_lamp", text="Lamp")
355         col.prop(edit, "use_duplicate_material", text="Material")
356         col.prop(edit, "use_duplicate_texture", text="Texture")
357         #col.prop(edit, "use_duplicate_fcurve", text="F-Curve")
358         col.prop(edit, "use_duplicate_action", text="Action")
359         col.prop(edit, "use_duplicate_particle", text="Particle")
360
361
362 class USERPREF_PT_system(bpy.types.Panel):
363     bl_space_type = 'USER_PREFERENCES'
364     bl_label = "System"
365     bl_region_type = 'WINDOW'
366     bl_options = {'HIDE_HEADER'}
367
368     @classmethod
369     def poll(cls, context):
370         userpref = context.user_preferences
371         return (userpref.active_section == 'SYSTEM')
372
373     def draw(self, context):
374         layout = self.layout
375
376         userpref = context.user_preferences
377         system = userpref.system
378
379         split = layout.split()
380
381         # 1. Column
382         column = split.column()
383         colsplit = column.split(percentage=0.85)
384
385         col = colsplit.column()
386         col.label(text="General:")
387         col.prop(system, "dpi")
388         col.prop(system, "frame_server_port")
389         col.prop(system, "scrollback", text="Console Scrollback")
390         col.prop(system, "author", text="Author")
391         col.prop(system, "use_scripts_auto_execute")
392         col.prop(system, "use_tabs_as_spaces")
393
394         col.separator()
395         col.separator()
396         col.separator()
397
398         col.label(text="Sound:")
399         col.row().prop(system, "audio_device", expand=True)
400         sub = col.column()
401         sub.active = system.audio_device != 'NONE'
402         #sub.prop(system, "use_preview_images")
403         sub.prop(system, "audio_channels", text="Channels")
404         sub.prop(system, "audio_mixing_buffer", text="Mixing Buffer")
405         sub.prop(system, "audio_sample_rate", text="Sample Rate")
406         sub.prop(system, "audio_sample_format", text="Sample Format")
407
408         col.separator()
409         col.separator()
410         col.separator()
411
412         col.label(text="Screencast:")
413         col.prop(system, "screencast_fps")
414         col.prop(system, "screencast_wait_time")
415         col.separator()
416         col.separator()
417         col.separator()
418
419         #column = split.column()
420         #colsplit = column.split(percentage=0.85)
421
422         # No translation in 2.5 yet
423         #col.prop(system, "language")
424         #col.label(text="Translate:")
425         #col.prop(system, "use_translate_tooltips", text="Tooltips")
426         #col.prop(system, "use_translate_buttons", text="Labels")
427         #col.prop(system, "use_translate_toolbox", text="Toolbox")
428
429         #col.separator()
430
431         #col.prop(system, "use_textured_fonts")
432
433         # 2. Column
434         column = split.column()
435         colsplit = column.split(percentage=0.85)
436
437         col = colsplit.column()
438         col.label(text="OpenGL:")
439         col.prop(system, "gl_clip_alpha", slider=True)
440         col.prop(system, "use_mipmaps")
441         col.label(text="Anisotropic Filtering")
442         col.prop(system, "anisotropic_filter", text="")
443         col.prop(system, "use_vertex_buffer_objects")
444         #Anti-aliasing is disabled as it breaks broder/lasso select
445         #col.prop(system, "use_antialiasing")
446         col.label(text="Window Draw Method:")
447         col.prop(system, "window_draw_method", text="")
448         col.label(text="Text Draw Options:")
449         col.prop(system, "use_text_antialiasing")
450         col.label(text="Textures:")
451         col.prop(system, "gl_texture_limit", text="Limit Size")
452         col.prop(system, "texture_time_out", text="Time Out")
453         col.prop(system, "texture_collection_rate", text="Collection Rate")
454
455         col.separator()
456         col.separator()
457         col.separator()
458
459         col.label(text="Sequencer:")
460         col.prop(system, "prefetch_frames")
461         col.prop(system, "memory_cache_limit")
462
463         # 3. Column
464         column = split.column()
465
466         column.label(text="Solid OpenGL lights:")
467
468         split = column.split(percentage=0.1)
469         split.label()
470         split.label(text="Colors:")
471         split.label(text="Direction:")
472
473         lamp = system.solid_lights[0]
474         opengl_lamp_buttons(column, lamp)
475
476         lamp = system.solid_lights[1]
477         opengl_lamp_buttons(column, lamp)
478
479         lamp = system.solid_lights[2]
480         opengl_lamp_buttons(column, lamp)
481
482         column.separator()
483         column.separator()
484         column.separator()
485
486         column.label(text="Color Picker Type:")
487         column.row().prop(system, "color_picker_type", text="")
488
489         column.separator()
490         column.separator()
491         column.separator()
492
493         column.prop(system, "use_weight_color_range", text="Custom Weight Paint Range")
494         sub = column.column()
495         sub.active = system.use_weight_color_range
496         sub.template_color_ramp(system, "weight_color_range", expand=True)
497
498
499 class USERPREF_PT_theme(bpy.types.Panel):
500     bl_space_type = 'USER_PREFERENCES'
501     bl_label = "Themes"
502     bl_region_type = 'WINDOW'
503     bl_options = {'HIDE_HEADER'}
504
505     @staticmethod
506     def _theme_generic(split, themedata):
507
508         row = split.row()
509
510         subsplit = row.split(percentage=0.95)
511
512         padding1 = subsplit.split(percentage=0.15)
513         padding1.column()
514
515         subsplit = row.split(percentage=0.85)
516
517         padding2 = subsplit.split(percentage=0.15)
518         padding2.column()
519
520         colsub_pair = padding1.column(), padding2.column()
521
522         props_type = {}
523
524         for i, prop in enumerate(themedata.rna_type.properties):
525             attr = prop.identifier
526             if attr == "rna_type":
527                 continue
528
529             props_type.setdefault((prop.type, prop.subtype), []).append(prop.identifier)
530
531         for props_type, props_ls in sorted(props_type.items()):
532             for i, attr in enumerate(props_ls):
533                 colsub_pair[i % 2].row().prop(themedata, attr)
534
535     @classmethod
536     def poll(cls, context):
537         userpref = context.user_preferences
538         return (userpref.active_section == 'THEMES')
539
540     def draw(self, context):
541         layout = self.layout
542
543         theme = context.user_preferences.themes[0]
544
545         split_themes = layout.split(percentage=0.2)
546         split_themes.prop(theme, "theme_area", expand=True)
547
548         split = layout.split(percentage=0.4)
549
550         layout.separator()
551         layout.separator()
552
553         split = split_themes.split()
554
555         if theme.theme_area == 'USER_INTERFACE':
556             col = split.column()
557
558             ui = theme.user_interface.wcol_regular
559             col.label(text="Regular:")
560             ui_items_general(col, ui)
561
562             ui = theme.user_interface.wcol_tool
563             col.label(text="Tool:")
564             ui_items_general(col, ui)
565
566             ui = theme.user_interface.wcol_radio
567             col.label(text="Radio Buttons:")
568             ui_items_general(col, ui)
569
570             ui = theme.user_interface.wcol_text
571             col.label(text="Text:")
572             ui_items_general(col, ui)
573
574             ui = theme.user_interface.wcol_option
575             col.label(text="Option:")
576             ui_items_general(col, ui)
577
578             ui = theme.user_interface.wcol_toggle
579             col.label(text="Toggle:")
580             ui_items_general(col, ui)
581
582             ui = theme.user_interface.wcol_num
583             col.label(text="Number Field:")
584             ui_items_general(col, ui)
585
586             ui = theme.user_interface.wcol_numslider
587             col.label(text="Value Slider:")
588             ui_items_general(col, ui)
589
590             ui = theme.user_interface.wcol_box
591             col.label(text="Box:")
592             ui_items_general(col, ui)
593
594             ui = theme.user_interface.wcol_menu
595             col.label(text="Menu:")
596             ui_items_general(col, ui)
597
598             ui = theme.user_interface.wcol_pulldown
599             col.label(text="Pulldown:")
600             ui_items_general(col, ui)
601
602             ui = theme.user_interface.wcol_menu_back
603             col.label(text="Menu Back:")
604             ui_items_general(col, ui)
605
606             ui = theme.user_interface.wcol_menu_item
607             col.label(text="Menu Item:")
608             ui_items_general(col, ui)
609
610             ui = theme.user_interface.wcol_scroll
611             col.label(text="Scroll Bar:")
612             ui_items_general(col, ui)
613
614             ui = theme.user_interface.wcol_progress
615             col.label(text="Progress Bar:")
616             ui_items_general(col, ui)
617
618             ui = theme.user_interface.wcol_list_item
619             col.label(text="List Item:")
620             ui_items_general(col, ui)
621
622             ui = theme.user_interface.wcol_state
623             col.label(text="State:")
624
625             row = col.row()
626
627             subsplit = row.split(percentage=0.95)
628
629             padding = subsplit.split(percentage=0.15)
630             colsub = padding.column()
631             colsub = padding.column()
632             colsub.row().prop(ui, "inner_anim")
633             colsub.row().prop(ui, "inner_anim_sel")
634             colsub.row().prop(ui, "inner_driven")
635             colsub.row().prop(ui, "inner_driven_sel")
636
637             subsplit = row.split(percentage=0.85)
638
639             padding = subsplit.split(percentage=0.15)
640             colsub = padding.column()
641             colsub = padding.column()
642             colsub.row().prop(ui, "inner_key")
643             colsub.row().prop(ui, "inner_key_sel")
644             colsub.row().prop(ui, "blend")
645
646             ui = theme.user_interface
647             col.separator()
648             col.separator()
649
650             split = col.split(percentage=0.93)
651             split.prop(ui, "icon_file")
652
653             layout.separator()
654             layout.separator()
655         elif theme.theme_area == 'BONE_COLOR_SETS':
656             col = split.column()
657
658             for i, ui in enumerate(theme.bone_color_sets):
659                 col.label(text="Color Set %d:" % (i + 1))  # i starts from 0
660
661                 row = col.row()
662
663                 subsplit = row.split(percentage=0.95)
664
665                 padding = subsplit.split(percentage=0.15)
666                 colsub = padding.column()
667                 colsub = padding.column()
668                 colsub.row().prop(ui, "normal")
669                 colsub.row().prop(ui, "select")
670                 colsub.row().prop(ui, "active")
671
672                 subsplit = row.split(percentage=0.85)
673
674                 padding = subsplit.split(percentage=0.15)
675                 colsub = padding.column()
676                 colsub = padding.column()
677                 colsub.row().prop(ui, "show_colored_constraints")
678         else:
679             self._theme_generic(split, getattr(theme, theme.theme_area.lower()))
680
681
682 class USERPREF_PT_file(bpy.types.Panel):
683     bl_space_type = 'USER_PREFERENCES'
684     bl_label = "Files"
685     bl_region_type = 'WINDOW'
686     bl_options = {'HIDE_HEADER'}
687
688     @classmethod
689     def poll(cls, context):
690         userpref = context.user_preferences
691         return (userpref.active_section == 'FILES')
692
693     def draw(self, context):
694         layout = self.layout
695
696         userpref = context.user_preferences
697         paths = userpref.filepaths
698
699         split = layout.split(percentage=0.7)
700
701         col = split.column()
702         col.label(text="File Paths:")
703
704         colsplit = col.split(percentage=0.95)
705         col1 = colsplit.split(percentage=0.3)
706
707         sub = col1.column()
708         sub.label(text="Fonts:")
709         sub.label(text="Textures:")
710         sub.label(text="Texture Plugins:")
711         sub.label(text="Sequence Plugins:")
712         sub.label(text="Render Output:")
713         sub.label(text="Scripts:")
714         sub.label(text="Sounds:")
715         sub.label(text="Temp:")
716         sub.label(text="Image Editor:")
717         sub.label(text="Animation Player:")
718
719         sub = col1.column()
720         sub.prop(paths, "font_directory", text="")
721         sub.prop(paths, "texture_directory", text="")
722         sub.prop(paths, "texture_plugin_directory", text="")
723         sub.prop(paths, "sequence_plugin_directory", text="")
724         sub.prop(paths, "render_output_directory", text="")
725         sub.prop(paths, "script_directory", text="")
726         sub.prop(paths, "sound_directory", text="")
727         sub.prop(paths, "temporary_directory", text="")
728         sub.prop(paths, "image_editor", text="")
729         subsplit = sub.split(percentage=0.3)
730         subsplit.prop(paths, "animation_player_preset", text="")
731         subsplit.prop(paths, "animation_player", text="")
732
733         col = split.column()
734         col.label(text="Save & Load:")
735         col.prop(paths, "use_relative_paths")
736         col.prop(paths, "use_file_compression")
737         col.prop(paths, "use_load_ui")
738         col.prop(paths, "use_filter_files")
739         col.prop(paths, "show_hidden_files_datablocks")
740         col.prop(paths, "hide_recent_locations")
741         col.prop(paths, "show_thumbnails")
742
743         col.separator()
744         col.separator()
745
746         col.prop(paths, "save_version")
747         col.prop(paths, "recent_files")
748         col.prop(paths, "use_save_preview_images")
749         col.label(text="Auto Save:")
750         col.prop(paths, "use_auto_save_temporary_files")
751         sub = col.column()
752         sub.active = paths.use_auto_save_temporary_files
753         sub.prop(paths, "auto_save_time", text="Timer (mins)")
754
755 from bl_ui.space_userpref_keymap import InputKeyMapPanel
756
757
758 class USERPREF_MT_ndof_settings(bpy.types.Menu):
759     # accessed from the window keybindings in C (only)
760     bl_label = "3D Mouse Settings"
761
762     def draw(self, context):
763         layout = self.layout
764         input_prefs = context.user_preferences.inputs
765
766         layout.separator()
767         layout.prop(input_prefs, "ndof_sensitivity")
768
769         if context.space_data.type == 'VIEW_3D':
770             layout.separator()
771             layout.prop(input_prefs, "ndof_show_guide")
772
773             layout.separator()
774             layout.label(text="orbit options")
775             layout.prop(input_prefs, "ndof_orbit_invert_axes")
776
777             layout.separator()
778             layout.label(text="fly options")
779             layout.prop(input_prefs, "ndof_fly_helicopter", icon='NDOF_FLY')
780             layout.prop(input_prefs, "ndof_lock_horizon", icon='NDOF_DOM')
781
782
783 class USERPREF_PT_input(bpy.types.Panel, InputKeyMapPanel):
784     bl_space_type = 'USER_PREFERENCES'
785     bl_label = "Input"
786
787     @classmethod
788     def poll(cls, context):
789         userpref = context.user_preferences
790         return (userpref.active_section == 'INPUT')
791
792     def draw_input_prefs(self, inputs, layout):
793         # General settings
794         row = layout.row()
795         col = row.column()
796
797         sub = col.column()
798         sub.label(text="Presets:")
799         subrow = sub.row(align=True)
800
801         subrow.menu("USERPREF_MT_interaction_presets", text=bpy.types.USERPREF_MT_interaction_presets.bl_label)
802         subrow.operator("wm.interaction_preset_add", text="", icon='ZOOMIN')
803         subrow.operator("wm.interaction_preset_add", text="", icon='ZOOMOUT').remove_active = True
804         sub.separator()
805
806         sub.label(text="Mouse:")
807         sub1 = sub.column()
808         sub1.active = (inputs.select_mouse == 'RIGHT')
809         sub1.prop(inputs, "use_mouse_emulate_3_button")
810         sub.prop(inputs, "use_mouse_continuous")
811         sub.prop(inputs, "drag_threshold")
812
813         sub.label(text="Select With:")
814         sub.row().prop(inputs, "select_mouse", expand=True)
815
816         sub = col.column()
817         sub.label(text="Double Click:")
818         sub.prop(inputs, "mouse_double_click_time", text="Speed")
819
820         sub.separator()
821
822         sub.prop(inputs, "use_emulate_numpad")
823
824         sub.separator()
825
826         sub.label(text="Orbit Style:")
827         sub.row().prop(inputs, "view_rotate_method", expand=True)
828
829         sub.label(text="Zoom Style:")
830         sub.row().prop(inputs, "view_zoom_method", text="")
831         if inputs.view_zoom_method in {'DOLLY', 'CONTINUE'}:
832             sub.row().prop(inputs, "view_zoom_axis", expand=True)
833             sub.prop(inputs, "invert_mouse_zoom")
834
835         #sub.prop(inputs, "use_mouse_mmb_paste")
836
837         #col.separator()
838
839         sub = col.column()
840         sub.label(text="Mouse Wheel:")
841         sub.prop(inputs, "invert_zoom_wheel", text="Invert Wheel Zoom Direction")
842         #sub.prop(view, "wheel_scroll_lines", text="Scroll Lines")
843
844         col.separator()
845         sub = col.column()
846         sub.label(text="NDOF Device:")
847         sub.prop(inputs, "ndof_sensitivity", text="NDOF Sensitivity")
848
849         row.separator()
850
851     def draw(self, context):
852         layout = self.layout
853
854         #import time
855
856         #start = time.time()
857
858         userpref = context.user_preferences
859
860         inputs = userpref.inputs
861
862         split = layout.split(percentage=0.25)
863
864         # Input settings
865         self.draw_input_prefs(inputs, split)
866
867         # Keymap Settings
868         self.draw_keymaps(context, split)
869
870         #print("runtime", time.time() - start)
871
872
873 class USERPREF_MT_addons_dev_guides(bpy.types.Menu):
874     bl_label = "Development Guides"
875
876     # menu to open webpages with addons development guides
877     def draw(self, context):
878         layout = self.layout
879         layout.operator('wm.url_open', text='API Concepts', icon='URL').url = 'http://wiki.blender.org/index.php/Dev:2.5/Py/API/Intro'
880         layout.operator('wm.url_open', text='Addon Guidelines', icon='URL').url = 'http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Guidelines/Addons'
881         layout.operator('wm.url_open', text='How to share your addon', icon='URL').url = 'http://wiki.blender.org/index.php/Dev:Py/Sharing'
882
883
884 class USERPREF_PT_addons(bpy.types.Panel):
885     bl_space_type = 'USER_PREFERENCES'
886     bl_label = "Addons"
887     bl_region_type = 'WINDOW'
888     bl_options = {'HIDE_HEADER'}
889
890     _addons_fake_modules = {}
891
892     @classmethod
893     def poll(cls, context):
894         userpref = context.user_preferences
895         return (userpref.active_section == 'ADDONS')
896
897     @staticmethod
898     def module_get(mod_name):
899         return USERPREF_PT_addons._addons_fake_modules[mod_name]
900
901     @staticmethod
902     def is_user_addon(mod, user_addon_paths):
903         if not user_addon_paths:
904             user_script_path = bpy.utils.user_script_path()
905             if user_script_path is not None:
906                 user_addon_paths.append(os.path.join(user_script_path, "addons"))
907             user_addon_paths.append(os.path.join(bpy.utils.resource_path('USER'), "scripts", "addons"))
908
909         for path in user_addon_paths:
910             if bpy.path.is_subdir(mod.__file__, path):
911                 return True
912         return False
913
914     @staticmethod
915     def draw_error(layout, message):
916         lines = message.split("\n")
917         box = layout.box()
918         rowsub = box.row()
919         rowsub.label(lines[0])
920         rowsub.label(icon='ERROR')
921         for l in lines[1:]:
922             box.label(l)
923
924     def draw(self, context):
925         layout = self.layout
926
927         userpref = context.user_preferences
928         used_ext = {ext.module for ext in userpref.addons}
929
930         # collect the categories that can be filtered on
931         addons = [(mod, addon_utils.module_bl_info(mod)) for mod in addon_utils.modules(USERPREF_PT_addons._addons_fake_modules)]
932
933         split = layout.split(percentage=0.2)
934         col = split.column()
935         col.prop(context.window_manager, "addon_search", text="", icon='VIEWZOOM')
936         col.label(text="Categories")
937         col.prop(context.window_manager, "addon_filter", expand=True)
938
939         col.label(text="Supported Level")
940         col.prop(context.window_manager, "addon_support", expand=True)
941
942         col = split.column()
943
944         # set in addon_utils.modules(...)
945         if addon_utils.error_duplicates:
946             self.draw_error(col,
947                             "Multiple addons using the same name found!\n"
948                             "likely a problem with the script search path.\n"
949                             "(see console for details)",
950                             )
951
952         if addon_utils.error_encoding:
953             self.draw_error(col,
954                             "One or more addons do not have UTF-8 encoding\n"
955                             "(see console for details)",
956                             )
957
958         filter = context.window_manager.addon_filter
959         search = context.window_manager.addon_search.lower()
960         support = context.window_manager.addon_support
961
962         # initialized on demand
963         user_addon_paths = []
964
965         for mod, info in addons:
966             module_name = mod.__name__
967
968             is_enabled = module_name in used_ext
969
970             if info["support"] not in support:
971                 continue
972
973             # check if add-on should be visible with current filters
974             if (filter == "All") or \
975                     (filter == info["category"]) or \
976                     (filter == "Enabled" and is_enabled) or \
977                     (filter == "Disabled" and not is_enabled):
978
979                 if search and search not in info["name"].lower():
980                     if info["author"]:
981                         if search not in info["author"].lower():
982                             continue
983                     else:
984                         continue
985
986                 # Addon UI Code
987                 box = col.column().box()
988                 colsub = box.column()
989                 row = colsub.row()
990
991                 row.operator("wm.addon_expand", icon='TRIA_DOWN' if info["show_expanded"] else 'TRIA_RIGHT', emboss=False).module = module_name
992
993                 rowsub = row.row()
994                 rowsub.active = is_enabled
995                 rowsub.label(text='%s: %s' % (info['category'], info["name"]))
996                 if info["warning"]:
997                     rowsub.label(icon='ERROR')
998
999                 # icon showing support level.
1000                 if info["support"] == 'OFFICIAL':
1001                     rowsub.label(icon='FILE_BLEND')
1002                 elif info["support"] == 'COMMUNITY':
1003                     rowsub.label(icon='POSE_DATA')
1004                 else:
1005                     rowsub.label(icon='QUESTION')
1006
1007                 if is_enabled:
1008                     row.operator("wm.addon_disable", icon='CHECKBOX_HLT', text="", emboss=False).module = module_name
1009                 else:
1010                     row.operator("wm.addon_enable", icon='CHECKBOX_DEHLT', text="", emboss=False).module = module_name
1011
1012                 # Expanded UI (only if additional infos are available)
1013                 if info["show_expanded"]:
1014                     if info["description"]:
1015                         split = colsub.row().split(percentage=0.15)
1016                         split.label(text='Description:')
1017                         split.label(text=info["description"])
1018                     if info["location"]:
1019                         split = colsub.row().split(percentage=0.15)
1020                         split.label(text='Location:')
1021                         split.label(text=info["location"])
1022                     if info["author"]:
1023                         split = colsub.row().split(percentage=0.15)
1024                         split.label(text='Author:')
1025                         split.label(text=info["author"])
1026                     if info["version"]:
1027                         split = colsub.row().split(percentage=0.15)
1028                         split.label(text='Version:')
1029                         split.label(text='.'.join(str(x) for x in info["version"]))
1030                     if info["warning"]:
1031                         split = colsub.row().split(percentage=0.15)
1032                         split.label(text="Warning:")
1033                         split.label(text='  ' + info["warning"], icon='ERROR')
1034
1035                     user_addon = USERPREF_PT_addons.is_user_addon(mod, user_addon_paths)
1036                     tot_row = bool(info["wiki_url"]) + bool(info["tracker_url"]) + bool(user_addon)
1037
1038                     if tot_row:
1039                         split = colsub.row().split(percentage=0.15)
1040                         split.label(text="Internet:")
1041                         if info["wiki_url"]:
1042                             split.operator("wm.url_open", text="Link to the Wiki", icon='HELP').url = info["wiki_url"]
1043                         if info["tracker_url"]:
1044                             split.operator("wm.url_open", text="Report a Bug", icon='URL').url = info["tracker_url"]
1045                         if user_addon:
1046                             split.operator("wm.addon_remove", text="Remove", icon='CANCEL').module = mod.__name__
1047
1048                         for i in range(4 - tot_row):
1049                             split.separator()
1050
1051         # Append missing scripts
1052         # First collect scripts that are used but have no script file.
1053         module_names = {mod.__name__ for mod, info in addons}
1054         missing_modules = {ext for ext in used_ext if ext not in module_names}
1055
1056         if missing_modules and filter in {"All", "Enabled"}:
1057             col.column().separator()
1058             col.column().label(text="Missing script files")
1059
1060             module_names = {mod.__name__ for mod, info in addons}
1061             for module_name in sorted(missing_modules):
1062                 is_enabled = module_name in used_ext
1063                 # Addon UI Code
1064                 box = col.column().box()
1065                 colsub = box.column()
1066                 row = colsub.row()
1067
1068                 row.label(text=module_name, icon='ERROR')
1069
1070                 if is_enabled:
1071                     row.operator("wm.addon_disable", icon='CHECKBOX_HLT', text="", emboss=False).module = module_name
1072
1073
1074 class WM_OT_addon_enable(bpy.types.Operator):
1075     "Enable an addon"
1076     bl_idname = "wm.addon_enable"
1077     bl_label = "Enable Add-On"
1078
1079     module = StringProperty(
1080             name="Module",
1081             description="Module name of the addon to enable",
1082             )
1083
1084     def execute(self, context):
1085         mod = addon_utils.enable(self.module)
1086
1087         if mod:
1088             info = addon_utils.module_bl_info(mod)
1089
1090             info_ver = info.get("blender", (0, 0, 0))
1091
1092             if info_ver > bpy.app.version:
1093                 self.report({'WARNING'}, ("This script was written Blender "
1094                                           "version %d.%d.%d and might not "
1095                                           "function (correctly).\n"
1096                                           "The script is enabled though.") %
1097                                          info_ver)
1098             return {'FINISHED'}
1099         else:
1100             return {'CANCELLED'}
1101
1102
1103 class WM_OT_addon_disable(bpy.types.Operator):
1104     "Disable an addon"
1105     bl_idname = "wm.addon_disable"
1106     bl_label = "Disable Add-On"
1107
1108     module = StringProperty(name="Module", description="Module name of the addon to disable")
1109
1110     def execute(self, context):
1111         addon_utils.disable(self.module)
1112         return {'FINISHED'}
1113
1114
1115 class WM_OT_addon_install(bpy.types.Operator):
1116     "Install an addon"
1117     bl_idname = "wm.addon_install"
1118     bl_label = "Install Add-On..."
1119
1120     overwrite = BoolProperty(name="Overwrite", description="Remove existing addons with the same ID", default=True)
1121     target = EnumProperty(
1122             name="Target Path",
1123             items=(('DEFAULT', "Default", ""),
1124                    ('PREFS', "User Prefs", "")))
1125
1126     filepath = StringProperty(name="File Path", description="File path to write file to")
1127     filter_folder = BoolProperty(name="Filter folders", description="", default=True, options={'HIDDEN'})
1128     filter_python = BoolProperty(name="Filter python", description="", default=True, options={'HIDDEN'})
1129     filter_glob = StringProperty(default="*.py;*.zip", options={'HIDDEN'})
1130
1131     @staticmethod
1132     def _module_remove(path_addons, module):
1133         module = os.path.splitext(module)[0]
1134         for f in os.listdir(path_addons):
1135             f_base = os.path.splitext(f)[0]
1136             if f_base == module:
1137                 f_full = os.path.join(path_addons, f)
1138
1139                 if os.path.isdir(f_full):
1140                     os.rmdir(f_full)
1141                 else:
1142                     os.remove(f_full)
1143
1144     def execute(self, context):
1145         import traceback
1146         import zipfile
1147         import shutil
1148
1149         pyfile = self.filepath
1150
1151         if self.target == 'DEFAULT':
1152             # dont use bpy.utils.script_paths("addons") because we may not be able to write to it.
1153             path_addons = bpy.utils.user_resource('SCRIPTS', "addons", create=True)
1154         else:
1155             path_addons = bpy.context.user_preferences.filepaths.script_directory
1156             if path_addons:
1157                 path_addons = os.path.join(path_addons, "addons")
1158
1159         if not path_addons:
1160             self.report({'ERROR'}, "Failed to get addons path")
1161             return {'CANCELLED'}
1162
1163         # create dir is if missing.
1164         if not os.path.exists(path_addons):
1165             os.makedirs(path_addons)
1166
1167         # Check if we are installing from a target path,
1168         # doing so causes 2+ addons of same name or when the same from/to
1169         # location is used, removal of the file!
1170         addon_path = ""
1171         pyfile_dir = os.path.dirname(pyfile)
1172         for addon_path in addon_utils.paths():
1173             if os.path.samefile(pyfile_dir, addon_path):
1174                 self.report({'ERROR'}, "Source file is in the addon search path: %r" % addon_path)
1175                 return {'CANCELLED'}
1176         del addon_path
1177         del pyfile_dir
1178         # done checking for exceptional case
1179
1180         addons_old = {mod.__name__ for mod in addon_utils.modules(USERPREF_PT_addons._addons_fake_modules)}
1181
1182         #check to see if the file is in compressed format (.zip)
1183         if zipfile.is_zipfile(pyfile):
1184             try:
1185                 file_to_extract = zipfile.ZipFile(pyfile, 'r')
1186             except:
1187                 traceback.print_exc()
1188                 return {'CANCELLED'}
1189
1190             if self.overwrite:
1191                 for f in file_to_extract.namelist():
1192                     WM_OT_addon_install._module_remove(path_addons, f)
1193             else:
1194                 for f in file_to_extract.namelist():
1195                     path_dest = os.path.join(path_addons, os.path.basename(f))
1196                     if os.path.exists(path_dest):
1197                         self.report({'WARNING'}, "File already installed to %r\n" % path_dest)
1198                         return {'CANCELLED'}
1199
1200             try:  # extract the file to "addons"
1201                 file_to_extract.extractall(path_addons)
1202
1203                 # zip files can create this dir with metadata, don't need it
1204                 macosx_dir = os.path.join(path_addons, '__MACOSX')
1205                 if os.path.isdir(macosx_dir):
1206                     shutil.rmtree(macosx_dir)
1207
1208             except:
1209                 traceback.print_exc()
1210                 return {'CANCELLED'}
1211
1212         else:
1213             path_dest = os.path.join(path_addons, os.path.basename(pyfile))
1214
1215             if self.overwrite:
1216                 WM_OT_addon_install._module_remove(path_addons, os.path.basename(pyfile))
1217             elif os.path.exists(path_dest):
1218                 self.report({'WARNING'}, "File already installed to %r\n" % path_dest)
1219                 return {'CANCELLED'}
1220
1221             #if not compressed file just copy into the addon path
1222             try:
1223                 shutil.copyfile(pyfile, path_dest)
1224
1225             except:
1226                 traceback.print_exc()
1227                 return {'CANCELLED'}
1228
1229         addons_new = {mod.__name__ for mod in addon_utils.modules(USERPREF_PT_addons._addons_fake_modules)} - addons_old
1230         addons_new.discard("modules")
1231
1232         # disable any addons we may have enabled previously and removed.
1233         # this is unlikely but do just incase. bug [#23978]
1234         for new_addon in addons_new:
1235             addon_utils.disable(new_addon)
1236
1237         # possible the zip contains multiple addons, we could disallow this
1238         # but for now just use the first
1239         for mod in addon_utils.modules(USERPREF_PT_addons._addons_fake_modules):
1240             if mod.__name__ in addons_new:
1241                 info = addon_utils.module_bl_info(mod)
1242
1243                 # show the newly installed addon.
1244                 context.window_manager.addon_filter = 'All'
1245                 context.window_manager.addon_search = info["name"]
1246                 break
1247
1248         # incase a new module path was created to install this addon.
1249         bpy.utils.refresh_script_paths()
1250
1251         # TODO, should not be a warning.
1252         # self.report({'WARNING'}, "File installed to '%s'\n" % path_dest)
1253         return {'FINISHED'}
1254
1255     def invoke(self, context, event):
1256         wm = context.window_manager
1257         wm.fileselect_add(self)
1258         return {'RUNNING_MODAL'}
1259
1260
1261 class WM_OT_addon_remove(bpy.types.Operator):
1262     "Disable an addon"
1263     bl_idname = "wm.addon_remove"
1264     bl_label = "Remove Add-On"
1265
1266     module = StringProperty(name="Module", description="Module name of the addon to remove")
1267
1268     @staticmethod
1269     def path_from_addon(module):
1270         for mod in addon_utils.modules(USERPREF_PT_addons._addons_fake_modules):
1271             if mod.__name__ == module:
1272                 filepath = mod.__file__
1273                 if os.path.exists(filepath):
1274                     if os.path.splitext(os.path.basename(filepath))[0] == "__init__":
1275                         return os.path.dirname(filepath), True
1276                     else:
1277                         return filepath, False
1278         return None, False
1279
1280     def execute(self, context):
1281         path, isdir = WM_OT_addon_remove.path_from_addon(self.module)
1282         if path is None:
1283             self.report('WARNING', "Addon path %r could not be found" % path)
1284             return {'CANCELLED'}
1285
1286         # incase its enabled
1287         addon_utils.disable(self.module)
1288
1289         import shutil
1290         if isdir:
1291             shutil.rmtree(path)
1292         else:
1293             os.remove(path)
1294
1295         context.area.tag_redraw()
1296         return {'FINISHED'}
1297
1298     # lame confirmation check
1299     def draw(self, context):
1300         self.layout.label(text="Remove Addon: %r?" % self.module)
1301         path, isdir = WM_OT_addon_remove.path_from_addon(self.module)
1302         self.layout.label(text="Path: %r" % path)
1303
1304     def invoke(self, context, event):
1305         wm = context.window_manager
1306         return wm.invoke_props_dialog(self, width=600)
1307
1308
1309 class WM_OT_addon_expand(bpy.types.Operator):
1310     "Display more information on this add-on"
1311     bl_idname = "wm.addon_expand"
1312     bl_label = ""
1313
1314     module = StringProperty(name="Module", description="Module name of the addon to expand")
1315
1316     def execute(self, context):
1317         module_name = self.module
1318
1319         # unlikely to fail, module should have already been imported
1320         try:
1321             # mod = __import__(module_name)
1322             mod = USERPREF_PT_addons.module_get(module_name)
1323         except:
1324             import traceback
1325             traceback.print_exc()
1326             return {'CANCELLED'}
1327
1328         info = addon_utils.module_bl_info(mod)
1329         info["show_expanded"] = not info["show_expanded"]
1330         return {'FINISHED'}
1331
1332 if __name__ == "__main__":  # only for live edit.
1333     bpy.utils.register_module(__name__)