2.5: change a user preferences settings from .enabled to .active, it's nicer
[blender.git] / release / scripts / 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 shutil
23
24
25 def ui_items_general(col, context):
26     """ General UI Theme Settings (User Interface)
27     """
28
29     row = col.row()
30
31     subsplit = row.split(percentage=0.95)
32
33     padding = subsplit.split(percentage=0.15)
34     colsub = padding.column()
35     colsub = padding.column()
36     colsub.row().prop(context, "outline")
37     colsub.row().prop(context, "item", slider=True)
38     colsub.row().prop(context, "inner", slider=True)
39     colsub.row().prop(context, "inner_sel", slider=True)
40
41     subsplit = row.split(percentage=0.85)
42
43     padding = subsplit.split(percentage=0.15)
44     colsub = padding.column()
45     colsub = padding.column()
46     colsub.row().prop(context, "text")
47     colsub.row().prop(context, "text_sel")
48     colsub.prop(context, "shaded")
49     subsub = colsub.column(align=True)
50     subsub.active = context.shaded
51     subsub.prop(context, "shadetop")
52     subsub.prop(context, "shadedown")
53
54     col.separator()
55
56
57 def opengl_lamp_buttons(column, lamp):
58     split = column.split(percentage=0.1)
59
60     if lamp.enabled == True:
61         split.prop(lamp, "enabled", text="", icon='OUTLINER_OB_LAMP')
62     else:
63         split.prop(lamp, "enabled", text="", icon='LAMP_DATA')
64
65     col = split.column()
66     col.active = lamp.enabled
67     row = col.row()
68     row.label(text="Diffuse:")
69     row.prop(lamp, "diffuse_color", text="")
70     row = col.row()
71     row.label(text="Specular:")
72     row.prop(lamp, "specular_color", text="")
73
74     col = split.column()
75     col.active = lamp.enabled
76     col.prop(lamp, "direction", text="")
77
78
79 class USERPREF_HT_header(bpy.types.Header):
80     bl_space_type = 'USER_PREFERENCES'
81
82     def draw(self, context):
83         layout = self.layout
84         layout.template_header(menus=False)
85
86         userpref = context.user_preferences
87
88         layout.operator_context = 'EXEC_AREA'
89         layout.operator("wm.save_homefile", text="Save As Default")
90
91         layout.operator_context = 'INVOKE_DEFAULT'
92
93         if userpref.active_section == 'INPUT':
94             op = layout.operator("wm.keyconfig_export")
95             op.filepath = "keymap.py"
96             op = layout.operator("wm.keyconfig_import")
97             op.filepath = "keymap.py"
98         elif userpref.active_section == 'ADDONS':
99             op = layout.operator("wm.addon_install")
100             op.filepath = "*.py"
101         elif userpref.active_section == 'THEMES':
102             op = layout.operator("ui.reset_default_theme")
103
104
105 class USERPREF_PT_tabs(bpy.types.Panel):
106     bl_label = ""
107     bl_space_type = 'USER_PREFERENCES'
108     bl_region_type = 'WINDOW'
109     bl_show_header = False
110
111     def draw(self, context):
112         layout = self.layout
113
114         userpref = context.user_preferences
115
116         layout.prop(userpref, "active_section", expand=True)
117
118
119 class USERPREF_MT_interaction_presets(bpy.types.Menu):
120     bl_label = "Presets"
121     preset_subdir = "interaction"
122     preset_operator = "script.execute_preset"
123     draw = bpy.types.Menu.draw_preset
124
125
126 class USERPREF_MT_splash(bpy.types.Menu):
127     bl_label = "Splash"
128
129     def draw(self, context):
130         layout = self.layout
131         split = layout.split()
132         row = split.row()
133         row.label("")
134         row = split.row()
135         row.label("Interaction:")
136         row.menu("USERPREF_MT_interaction_presets", text=bpy.types.USERPREF_MT_interaction_presets.bl_label)
137
138
139 class USERPREF_PT_interface(bpy.types.Panel):
140     bl_space_type = 'USER_PREFERENCES'
141     bl_label = "Interface"
142     bl_region_type = 'WINDOW'
143     bl_show_header = False
144
145     def poll(self, context):
146         userpref = context.user_preferences
147         return (userpref.active_section == 'INTERFACE')
148
149     def draw(self, context):
150         layout = self.layout
151
152         userpref = context.user_preferences
153         view = userpref.view
154
155         row = layout.row()
156
157         col = row.column()
158         col.label(text="Display:")
159         col.prop(view, "tooltips")
160         col.prop(view, "display_object_info", text="Object Info")
161         col.prop(view, "use_large_cursors")
162         col.prop(view, "show_view_name", text="View Name")
163         col.prop(view, "show_playback_fps", text="Playback FPS")
164         col.prop(view, "global_scene")
165         col.prop(view, "object_origin_size")
166
167         col.separator()
168         col.separator()
169         col.separator()
170
171         col.prop(view, "show_mini_axis", text="Display Mini Axis")
172         sub = col.column()
173         sub.active = view.show_mini_axis
174         sub.prop(view, "mini_axis_size", text="Size")
175         sub.prop(view, "mini_axis_brightness", text="Brightness")
176
177         col.separator()
178         col.separator()
179         col.separator()
180
181         col.label(text="Properties Window:")
182         col.prop(view, "properties_width_check")
183
184         row.separator()
185         row.separator()
186
187         col = row.column()
188         col.label(text="View Manipulation:")
189         col.prop(view, "auto_depth")
190         col.prop(view, "zoom_to_mouse")
191         col.prop(view, "rotate_around_selection")
192         col.prop(view, "global_pivot")
193
194         col.separator()
195
196         col.prop(view, "auto_perspective")
197         col.prop(view, "smooth_view")
198         col.prop(view, "rotation_angle")
199
200         col.separator()
201         col.separator()
202
203         col.label(text="2D Viewports:")
204         col.prop(view, "view2d_grid_minimum_spacing", text="Minimum Grid Spacing")
205         col.prop(view, "timecode_style")
206
207         row.separator()
208         row.separator()
209
210         col = row.column()
211         #Toolbox doesn't exist yet
212         #col.label(text="Toolbox:")
213         #col.prop(view, "use_column_layout")
214         #col.label(text="Open Toolbox Delay:")
215         #col.prop(view, "open_left_mouse_delay", text="Hold LMB")
216         #col.prop(view, "open_right_mouse_delay", text="Hold RMB")
217         col.prop(view, "use_manipulator")
218         sub = col.column()
219         sub.active = view.use_manipulator
220         sub.prop(view, "manipulator_size", text="Size")
221         sub.prop(view, "manipulator_handle_size", text="Handle Size")
222         sub.prop(view, "manipulator_hotspot", text="Hotspot")
223
224         col.separator()
225         col.separator()
226         col.separator()
227
228         col.label(text="Menus:")
229         col.prop(view, "open_mouse_over")
230         col.label(text="Menu Open Delay:")
231         col.prop(view, "open_toplevel_delay", text="Top Level")
232         col.prop(view, "open_sublevel_delay", text="Sub Level")
233
234         col.separator()
235
236         col.prop(view, "show_splash")
237
238
239 class USERPREF_PT_edit(bpy.types.Panel):
240     bl_space_type = 'USER_PREFERENCES'
241     bl_label = "Edit"
242     bl_region_type = 'WINDOW'
243     bl_show_header = False
244
245     def poll(self, context):
246         userpref = context.user_preferences
247         return (userpref.active_section == 'EDITING')
248
249     def draw(self, context):
250         layout = self.layout
251
252         userpref = context.user_preferences
253         edit = userpref.edit
254
255         row = layout.row()
256
257         col = row.column()
258         col.label(text="Link Materials To:")
259         col.prop(edit, "material_link", text="")
260
261         col.separator()
262         col.separator()
263         col.separator()
264
265         col.label(text="New Objects:")
266         col.prop(edit, "enter_edit_mode")
267         col.label(text="Align To:")
268         col.prop(edit, "object_align", text="")
269
270         col.separator()
271         col.separator()
272         col.separator()
273
274         col.label(text="Undo:")
275         col.prop(edit, "global_undo")
276         col.prop(edit, "undo_steps", text="Steps")
277         col.prop(edit, "undo_memory_limit", text="Memory Limit")
278
279         row.separator()
280         row.separator()
281
282         col = row.column()
283         col.label(text="Grease Pencil:")
284         col.prop(edit, "grease_pencil_manhattan_distance", text="Manhattan Distance")
285         col.prop(edit, "grease_pencil_euclidean_distance", text="Euclidean Distance")
286         #col.prop(edit, "grease_pencil_simplify_stroke", text="Simplify Stroke")
287         col.prop(edit, "grease_pencil_eraser_radius", text="Eraser Radius")
288         col.prop(edit, "grease_pencil_smooth_stroke", text="Smooth Stroke")
289         col.separator()
290         col.separator()
291         col.separator()
292         col.label(text="Playback:")
293         col.prop(edit, "use_negative_frames")
294
295         row.separator()
296         row.separator()
297
298         col = row.column()
299         col.label(text="Keyframing:")
300         col.prop(edit, "use_visual_keying")
301         col.prop(edit, "keyframe_insert_needed", text="Only Insert Needed")
302
303         col.separator()
304
305         col.prop(edit, "use_auto_keying", text="Auto Keyframing:")
306
307         sub = col.column()
308
309         # sub.active = edit.use_auto_keying # incorrect, timeline can enable
310         sub.prop(edit, "auto_keyframe_insert_keyingset", text="Only Insert for Keying Set")
311         sub.prop(edit, "auto_keyframe_insert_available", text="Only Insert Available")
312
313         col.separator()
314
315         col.label(text="New F-Curve Defaults:")
316         col.prop(edit, "keyframe_new_interpolation_type", text="Interpolation")
317         col.prop(edit, "keyframe_new_handle_type", text="Handles")
318         col.prop(edit, "insertkey_xyz_to_rgb", text="XYZ to RGB")
319
320         col.separator()
321         col.separator()
322         col.separator()
323
324         col.label(text="Transform:")
325         col.prop(edit, "drag_immediately")
326
327         row.separator()
328         row.separator()
329
330         sculpt = context.tool_settings.sculpt
331         col = row.column()
332         col.label(text="Paint and Sculpt:")
333         col.prop(edit, "sculpt_paint_use_unified_size", text="Unify Size")
334         col.prop(edit, "sculpt_paint_use_unified_strength", text="Unify Strength")
335         row = col.row(align=True)
336         row.label("Overlay Color:")
337         row.prop(edit, "sculpt_paint_overlay_col", text="")
338         col.prop(sculpt, "use_openmp", text="Threaded Sculpt")
339         col.prop(sculpt, "show_brush")
340
341         col.separator()
342         col.separator()
343         col.separator()
344
345         col.label(text="Duplicate Data:")
346         col.prop(edit, "duplicate_mesh", text="Mesh")
347         col.prop(edit, "duplicate_surface", text="Surface")
348         col.prop(edit, "duplicate_curve", text="Curve")
349         col.prop(edit, "duplicate_text", text="Text")
350         col.prop(edit, "duplicate_metaball", text="Metaball")
351         col.prop(edit, "duplicate_armature", text="Armature")
352         col.prop(edit, "duplicate_lamp", text="Lamp")
353         col.prop(edit, "duplicate_material", text="Material")
354         col.prop(edit, "duplicate_texture", text="Texture")
355         #col.prop(edit, "duplicate_fcurve", text="F-Curve")
356         col.prop(edit, "duplicate_action", text="Action")
357         col.prop(edit, "duplicate_particle", text="Particle")
358
359
360 class USERPREF_PT_system(bpy.types.Panel):
361     bl_space_type = 'USER_PREFERENCES'
362     bl_label = "System"
363     bl_region_type = 'WINDOW'
364     bl_show_header = False
365
366     def poll(self, context):
367         userpref = context.user_preferences
368         return (userpref.active_section == 'SYSTEM')
369
370     def draw(self, context):
371         layout = self.layout
372
373         userpref = context.user_preferences
374         system = userpref.system
375
376         split = layout.split()
377
378
379         # 1. Column
380         column = split.column()
381         colsplit = column.split(percentage=0.85)
382
383         col = colsplit.column()
384         col.label(text="General:")
385         col.prop(system, "dpi")
386         col.prop(system, "frame_server_port")
387         col.prop(system, "scrollback", text="Console Scrollback")
388         col.prop(system, "auto_execute_scripts")
389         col.prop(system, "tabs_as_spaces")
390
391         col.separator()
392         col.separator()
393         col.separator()
394
395         col.label(text="Sound:")
396         col.row().prop(system, "audio_device", expand=True)
397         sub = col.column()
398         sub.active = system.audio_device != 'NONE'
399         #sub.prop(system, "enable_all_codecs")
400         sub.prop(system, "audio_channels", text="Channels")
401         sub.prop(system, "audio_mixing_buffer", text="Mixing Buffer")
402         sub.prop(system, "audio_sample_rate", text="Sample Rate")
403         sub.prop(system, "audio_sample_format", text="Sample Format")
404
405         col.separator()
406         col.separator()
407         col.separator()
408
409         col.label(text="Screencast:")
410         col.prop(system, "screencast_fps")
411         col.prop(system, "screencast_wait_time")
412         col.separator()
413         col.separator()
414         col.separator()
415
416         #column = split.column()
417         #colsplit = column.split(percentage=0.85)
418
419         # No translation in 2.5 yet
420         #col.prop(system, "language")
421         #col.label(text="Translate:")
422         #col.prop(system, "translate_tooltips", text="Tooltips")
423         #col.prop(system, "translate_buttons", text="Labels")
424         #col.prop(system, "translate_toolbox", text="Toolbox")
425
426         #col.separator()
427
428         #col.prop(system, "use_textured_fonts")
429
430
431         # 2. Column
432         column = split.column()
433         colsplit = column.split(percentage=0.85)
434
435         col = colsplit.column()
436         col.label(text="OpenGL:")
437         col.prop(system, "clip_alpha", slider=True)
438         col.prop(system, "use_mipmaps")
439         col.prop(system, "use_vbos")
440         #Anti-aliasing is disabled as it breaks broder/lasso select
441         #col.prop(system, "use_antialiasing")
442         col.label(text="Window Draw Method:")
443         col.prop(system, "window_draw_method", text="")
444         col.label(text="Textures:")
445         col.prop(system, "gl_texture_limit", text="Limit Size")
446         col.prop(system, "texture_time_out", text="Time Out")
447         col.prop(system, "texture_collection_rate", text="Collection Rate")
448
449         col.separator()
450         col.separator()
451         col.separator()
452
453         col.label(text="Sequencer:")
454         col.prop(system, "prefetch_frames")
455         col.prop(system, "memory_cache_limit")
456
457
458         # 3. Column
459         column = split.column()
460
461         column.label(text="Solid OpenGL lights:")
462
463         split = column.split(percentage=0.1)
464         split.label()
465         split.label(text="Colors:")
466         split.label(text="Direction:")
467
468         lamp = system.solid_lights[0]
469         opengl_lamp_buttons(column, lamp)
470
471         lamp = system.solid_lights[1]
472         opengl_lamp_buttons(column, lamp)
473
474         lamp = system.solid_lights[2]
475         opengl_lamp_buttons(column, lamp)
476
477         column.separator()
478         column.separator()
479         column.separator()
480
481         column.label(text="Color Picker Type:")
482         column.row().prop(system, "color_picker_type", text="")
483
484         column.separator()
485         column.separator()
486         column.separator()
487
488         column.prop(system, "use_weight_color_range", text="Custom Weight Paint Range")
489         sub = column.column()
490         sub.active = system.use_weight_color_range
491         sub.template_color_ramp(system, "weight_color_range", expand=True)
492
493
494 class USERPREF_PT_theme(bpy.types.Panel):
495     bl_space_type = 'USER_PREFERENCES'
496     bl_label = "Themes"
497     bl_region_type = 'WINDOW'
498     bl_show_header = False
499
500     @staticmethod
501     def _theme_generic(split, themedata):
502
503         row = split.row()
504
505         subsplit = row.split(percentage=0.95)
506
507         padding1 = subsplit.split(percentage=0.15)
508         padding1.column()
509
510         subsplit = row.split(percentage=0.85)
511
512         padding2 = subsplit.split(percentage=0.15)
513         padding2.column()
514
515         colsub_pair = padding1.column(), padding2.column()
516
517         props_type = {}
518
519         for i, prop in enumerate(themedata.rna_type.properties):
520             attr = prop.identifier
521             if attr == "rna_type":
522                 continue
523
524             props_type.setdefault((prop.type, prop.subtype), []).append(prop.identifier)
525
526         for props_type, props_ls in sorted(props_type.items()):
527             for i, attr in enumerate(props_ls):
528                 colsub_pair[i % 2].row().prop(themedata, attr)
529
530     def poll(self, context):
531         userpref = context.user_preferences
532         return (userpref.active_section == 'THEMES')
533
534     def draw(self, context):
535         layout = self.layout
536
537         theme = context.user_preferences.themes[0]
538
539         split_themes = layout.split(percentage=0.2)
540         split_themes.prop(theme, "theme_area", expand=True)
541
542         split = layout.split(percentage=0.4)
543
544
545         layout.separator()
546         layout.separator()
547
548         split = split_themes.split()
549
550         if theme.theme_area == 'USER_INTERFACE':
551             col = split.column()
552
553             ui = theme.user_interface.wcol_regular
554             col.label(text="Regular:")
555             ui_items_general(col, ui)
556
557             ui = theme.user_interface.wcol_tool
558             col.label(text="Tool:")
559             ui_items_general(col, ui)
560
561             ui = theme.user_interface.wcol_radio
562             col.label(text="Radio Buttons:")
563             ui_items_general(col, ui)
564
565             ui = theme.user_interface.wcol_text
566             col.label(text="Text:")
567             ui_items_general(col, ui)
568
569             ui = theme.user_interface.wcol_option
570             col.label(text="Option:")
571             ui_items_general(col, ui)
572
573             ui = theme.user_interface.wcol_toggle
574             col.label(text="Toggle:")
575             ui_items_general(col, ui)
576
577             ui = theme.user_interface.wcol_num
578             col.label(text="Number Field:")
579             ui_items_general(col, ui)
580
581             ui = theme.user_interface.wcol_numslider
582             col.label(text="Value Slider:")
583             ui_items_general(col, ui)
584
585             ui = theme.user_interface.wcol_box
586             col.label(text="Box:")
587             ui_items_general(col, ui)
588
589             ui = theme.user_interface.wcol_menu
590             col.label(text="Menu:")
591             ui_items_general(col, ui)
592
593             ui = theme.user_interface.wcol_pulldown
594             col.label(text="Pulldown:")
595             ui_items_general(col, ui)
596
597             ui = theme.user_interface.wcol_menu_back
598             col.label(text="Menu Back:")
599             ui_items_general(col, ui)
600
601             ui = theme.user_interface.wcol_menu_item
602             col.label(text="Menu Item:")
603             ui_items_general(col, ui)
604
605             ui = theme.user_interface.wcol_scroll
606             col.label(text="Scroll Bar:")
607             ui_items_general(col, ui)
608
609             ui = theme.user_interface.wcol_progress
610             col.label(text="Progress Bar:")
611             ui_items_general(col, ui)
612
613             ui = theme.user_interface.wcol_list_item
614             col.label(text="List Item:")
615             ui_items_general(col, ui)
616
617             ui = theme.user_interface.wcol_state
618             col.label(text="State:")
619
620             row = col.row()
621
622             subsplit = row.split(percentage=0.95)
623
624             padding = subsplit.split(percentage=0.15)
625             colsub = padding.column()
626             colsub = padding.column()
627             colsub.row().prop(ui, "inner_anim")
628             colsub.row().prop(ui, "inner_anim_sel")
629             colsub.row().prop(ui, "inner_driven")
630             colsub.row().prop(ui, "inner_driven_sel")
631
632             subsplit = row.split(percentage=0.85)
633
634             padding = subsplit.split(percentage=0.15)
635             colsub = padding.column()
636             colsub = padding.column()
637             colsub.row().prop(ui, "inner_key")
638             colsub.row().prop(ui, "inner_key_sel")
639             colsub.row().prop(ui, "blend")
640
641
642             ui = theme.user_interface
643             col.separator()
644             col.separator()
645
646             split = col.split(percentage=0.93)
647             split.prop(ui, "icon_file")
648
649             layout.separator()
650             layout.separator()
651
652         else:
653             self._theme_generic(split, getattr(theme, theme.theme_area.lower()))
654
655
656 class USERPREF_PT_file(bpy.types.Panel):
657     bl_space_type = 'USER_PREFERENCES'
658     bl_label = "Files"
659     bl_region_type = 'WINDOW'
660     bl_show_header = False
661
662     def poll(self, context):
663         userpref = context.user_preferences
664         return (userpref.active_section == 'FILES')
665
666     def draw(self, context):
667         layout = self.layout
668
669         userpref = context.user_preferences
670         paths = userpref.filepaths
671
672         split = layout.split(percentage=0.7)
673
674         col = split.column()
675         col.label(text="File Paths:")
676
677         colsplit = col.split(percentage=0.95)
678         col1 = colsplit.split(percentage=0.3)
679
680         sub = col1.column()
681         sub.label(text="Fonts:")
682         sub.label(text="Textures:")
683         sub.label(text="Texture Plugins:")
684         sub.label(text="Sequence Plugins:")
685         sub.label(text="Render Output:")
686         sub.label(text="Scripts:")
687         sub.label(text="Sounds:")
688         sub.label(text="Temp:")
689         sub.label(text="Image Editor:")
690         sub.label(text="Animation Player:")
691
692         sub = col1.column()
693         sub.prop(paths, "fonts_directory", text="")
694         sub.prop(paths, "textures_directory", text="")
695         sub.prop(paths, "texture_plugin_directory", text="")
696         sub.prop(paths, "sequence_plugin_directory", text="")
697         sub.prop(paths, "render_output_directory", text="")
698         sub.prop(paths, "python_scripts_directory", text="")
699         sub.prop(paths, "sounds_directory", text="")
700         sub.prop(paths, "temporary_directory", text="")
701         sub.prop(paths, "image_editor", text="")
702         subsplit = sub.split(percentage=0.3)
703         subsplit.prop(paths, "animation_player_preset", text="")
704         subsplit.prop(paths, "animation_player", text="")
705
706         col = split.column()
707         col.label(text="Save & Load:")
708         col.prop(paths, "use_relative_paths")
709         col.prop(paths, "compress_file")
710         col.prop(paths, "load_ui")
711         col.prop(paths, "filter_file_extensions")
712         col.prop(paths, "hide_dot_files_datablocks")
713
714         col.separator()
715         col.separator()
716
717         col.label(text="Auto Save:")
718         col.prop(paths, "save_version")
719         col.prop(paths, "recent_files")
720         col.prop(paths, "save_preview_images")
721         col.prop(paths, "auto_save_temporary_files")
722         sub = col.column()
723         sub.active = paths.auto_save_temporary_files
724         sub.prop(paths, "auto_save_time", text="Timer (mins)")
725
726 from space_userpref_keymap import InputKeyMapPanel
727
728
729 class USERPREF_PT_input(InputKeyMapPanel):
730     bl_space_type = 'USER_PREFERENCES'
731     bl_label = "Input"
732
733     def poll(self, context):
734         userpref = context.user_preferences
735         return (userpref.active_section == 'INPUT')
736
737     def draw_input_prefs(self, inputs, layout):
738         # General settings
739         row = layout.row()
740         col = row.column()
741
742         sub = col.column()
743         sub.label(text="Presets:")
744         subrow = sub.row(align=True)
745         subrow.menu("USERPREF_MT_interaction_presets", text=bpy.types.USERPREF_MT_interaction_presets.bl_label)
746         subrow.operator("wm.interaction_preset_add", text="", icon='ZOOMIN')
747         sub.separator()
748
749         sub.label(text="Mouse:")
750         sub1 = sub.column()
751         sub1.active = (inputs.select_mouse == 'RIGHT')
752         sub1.prop(inputs, "emulate_3_button_mouse")
753         sub.prop(inputs, "continuous_mouse")
754
755         sub.label(text="Select With:")
756         sub.row().prop(inputs, "select_mouse", expand=True)
757
758         sub = col.column()
759         sub.label(text="Double Click:")
760         sub.prop(inputs, "double_click_time", text="Speed")
761
762         sub.separator()
763
764         sub.prop(inputs, "emulate_numpad")
765
766         sub.separator()
767
768         sub.label(text="Orbit Style:")
769         sub.row().prop(inputs, "view_rotation", expand=True)
770
771         sub.label(text="Zoom Style:")
772         sub.row().prop(inputs, "zoom_style", text="")
773         if inputs.zoom_style == 'DOLLY':
774             sub.row().prop(inputs, "zoom_axis", expand=True)
775             sub.prop(inputs, "invert_zoom_direction")
776
777         #sub.prop(inputs, "use_middle_mouse_paste")
778
779         #col.separator()
780
781         sub = col.column()
782         sub.label(text="Mouse Wheel:")
783         sub.prop(inputs, "wheel_invert_zoom", text="Invert Wheel Zoom Direction")
784         #sub.prop(view, "wheel_scroll_lines", text="Scroll Lines")
785
786         col.separator()
787         ''' not implemented yet
788         sub = col.column()
789         sub.label(text="NDOF Device:")
790         sub.prop(inputs, "ndof_pan_speed", text="Pan Speed")
791         sub.prop(inputs, "ndof_rotate_speed", text="Orbit Speed")
792         '''
793
794         row.separator()
795
796     def draw(self, context):
797         layout = self.layout
798
799         #import time
800
801         #start = time.time()
802
803         userpref = context.user_preferences
804         wm = context.manager
805
806         inputs = userpref.inputs
807
808         split = layout.split(percentage=0.25)
809
810         # Input settings
811         self.draw_input_prefs(inputs, split)
812
813         # Keymap Settings
814         self.draw_keymaps(context, split)
815
816         #print("runtime", time.time() - start)
817
818
819 class USERPREF_PT_addons(bpy.types.Panel):
820     bl_space_type = 'USER_PREFERENCES'
821     bl_label = "Addons"
822     bl_region_type = 'WINDOW'
823     bl_show_header = False
824
825     def poll(self, context):
826         userpref = context.user_preferences
827         return (userpref.active_section == 'ADDONS')
828
829     @staticmethod
830     def _addon_list():
831         import sys
832         modules = []
833         loaded_modules = set()
834         paths = bpy.utils.script_paths("addons")
835         # sys.path.insert(0, None)
836         for path in paths:
837             # sys.path[0] = path
838             modules.extend(bpy.utils.modules_from_path(path, loaded_modules))
839
840         # del sys.path[0]
841         return modules
842
843     def draw(self, context):
844         layout = self.layout
845
846         userpref = context.user_preferences
847         used_ext = {ext.module for ext in userpref.addons}
848
849         # collect the categories that can be filtered on
850         addons = [(mod, addon_info_get(mod)) for mod in self._addon_list()]
851
852         cats = {info["category"] for mod, info in addons}
853         cats.discard("")
854
855         cats = ["All", "Enabled", "Disabled"] + sorted(cats)
856
857         bpy.types.Scene.EnumProperty(items=[(cat, cat, cat + " addons") for cat in cats],
858             name="Category", attr="addon_filter", description="Filter add-ons by category")
859         bpy.types.Scene.StringProperty(name="Search", attr="addon_search",
860             description="Search within the selected filter")
861
862         split = layout.split(percentage=0.2)
863         col = split.column()
864         col.prop(context.scene, "addon_filter", text="Filter", expand=True)
865         col.prop(context.scene, "addon_search", text="", icon='VIEWZOOM')
866
867         col = split.column()
868
869         filter = context.scene.addon_filter
870         search = context.scene.addon_search.lower()
871
872         for mod, info in addons:
873             module_name = mod.__name__
874
875             is_enabled = module_name in used_ext
876
877             # check if add-on should be visible with current filters
878             if (filter == "All") or \
879                     (filter == info["category"]) or \
880                     (filter == "Enabled" and is_enabled) or \
881                     (filter == "Disabled" and not is_enabled):
882
883
884                 if search and search not in info["name"].lower():
885                     if info["author"]:
886                         if search not in info["author"].lower():
887                             continue
888                     else:
889                         continue
890
891                 # Addon UI Code
892                 box = col.column().box()
893                 colsub = box.column()
894                 row = colsub.row()
895
896                 row.operator("wm.addon_expand", icon='TRIA_DOWN' if info["expanded"] else 'TRIA_RIGHT', emboss=False).module = module_name
897
898                 rowsub = row.row()
899                 rowsub.active = is_enabled
900                 rowsub.label(text=info["name"], icon='ERROR' if info["warning"] else 'BLENDER')
901
902                 if is_enabled:
903                     row.operator("wm.addon_disable", icon='CHECKBOX_HLT', text="", emboss=False).module = module_name
904                 else:
905                     row.operator("wm.addon_enable", icon='CHECKBOX_DEHLT', text="", emboss=False).module = module_name
906
907                 # Expanded UI (only if additional infos are available)
908                 if info["expanded"]:
909                     if info["description"]:
910                         split = colsub.row().split(percentage=0.15)
911                         split.label(text='Description:')
912                         split.label(text=info["description"])
913                     if info["location"]:
914                         split = colsub.row().split(percentage=0.15)
915                         split.label(text='Location:')
916                         split.label(text=info["location"])
917                     if info["author"]:
918                         split = colsub.row().split(percentage=0.15)
919                         split.label(text='Author:')
920                         split.label(text=info["author"])
921                     if info["version"]:
922                         split = colsub.row().split(percentage=0.15)
923                         split.label(text='Version:')
924                         split.label(text=info["version"])
925                     if info["warning"]:
926                         split = colsub.row().split(percentage=0.15)
927                         split.label(text="Warning:")
928                         split.label(text='  ' + info["warning"], icon='ERROR')
929                     if info["wiki_url"] or info["tracker_url"]:
930                         split = colsub.row().split(percentage=0.15)
931                         split.label(text="Internet:")
932                         if info["wiki_url"]:
933                             split.operator("wm.url_open", text="Link to the Wiki", icon='HELP').url = info["wiki_url"]
934                         if info["tracker_url"]:
935                             split.operator("wm.url_open", text="Report a Bug", icon='URL').url = info["tracker_url"]
936
937                         if info["wiki_url"] and info["tracker_url"]:
938                             split.separator()
939                         else:
940                             split.separator()
941                             split.separator()
942
943         # Append missing scripts
944         # First collect scripts that are used but have no script file.
945         module_names = {mod.__name__ for mod, info in addons}
946         missing_modules = {ext for ext in used_ext if ext not in module_names}
947
948         if missing_modules and filter in ("All", "Enabled"):
949             col.column().separator()
950             col.column().label(text="Missing script files")
951
952             module_names = {mod.__name__ for mod, info in addons}
953             for ext in sorted(missing_modules):
954                 # Addon UI Code
955                 box = col.column().box()
956                 colsub = box.column()
957                 row = colsub.row()
958
959                 row.label(text=ext, icon='ERROR')
960                 row.operator("wm.addon_disable").module = ext
961
962 from bpy.props import *
963
964
965 def addon_info_get(mod, info_basis={"name": "", "author": "", "version": "", "blender": "", "location": "", "description": "", "wiki_url": "", "tracker_url": "", "category": "", "warning": "", "expanded": False}):
966     addon_info = getattr(mod, "bl_addon_info", {})
967
968     # avoid re-initializing
969     if "_init" in addon_info:
970         return addon_info
971
972     if not addon_info:
973         mod.bl_addon_info = addon_info
974
975     for key, value in info_basis.items():
976         addon_info.setdefault(key, value)
977
978     if not addon_info["name"]:
979         addon_info["name"] = mod.__name__
980
981     addon_info["_init"] = None
982     return addon_info
983
984
985 class WM_OT_addon_enable(bpy.types.Operator):
986     "Enable an addon"
987     bl_idname = "wm.addon_enable"
988     bl_label = "Enable Add-On"
989
990     module = StringProperty(name="Module", description="Module name of the addon to enable")
991
992     def execute(self, context):
993         module_name = self.properties.module
994
995         try:
996             mod = __import__(module_name)
997             mod.register()
998         except:
999             import traceback
1000             traceback.print_exc()
1001             return {'CANCELLED'}
1002
1003         ext = context.user_preferences.addons.new()
1004         ext.module = module_name
1005
1006         # check if add-on is written for current blender version, or raise a warning
1007         info = addon_info_get(mod)
1008
1009         if info.get("blender", (0, 0, 0)) > bpy.app.version:
1010             self.report("WARNING','This script was written for a newer version of Blender and might not function (correctly).\nThe script is enabled though.")
1011
1012         return {'FINISHED'}
1013
1014
1015 class WM_OT_addon_disable(bpy.types.Operator):
1016     "Disable an addon"
1017     bl_idname = "wm.addon_disable"
1018     bl_label = "Disable Add-On"
1019
1020     module = StringProperty(name="Module", description="Module name of the addon to disable")
1021
1022     def execute(self, context):
1023         import traceback
1024         module_name = self.properties.module
1025
1026         try:
1027             mod = __import__(module_name)
1028             mod.unregister()
1029         except:
1030             traceback.print_exc()
1031
1032         addons = context.user_preferences.addons
1033         ok = True
1034         while ok: # incase its in more then once.
1035             ok = False
1036             for ext in addons:
1037                 if ext.module == module_name:
1038                     addons.remove(ext)
1039                     ok = True
1040                     break
1041
1042         return {'FINISHED'}
1043
1044
1045 class WM_OT_addon_install(bpy.types.Operator):
1046     "Install an addon"
1047     bl_idname = "wm.addon_install"
1048     bl_label = "Install Add-On..."
1049
1050     module = StringProperty(name="Module", description="Module name of the addon to disable")
1051
1052     filepath = StringProperty(name="File Path", description="File path to write file to")
1053     filter_folder = BoolProperty(name="Filter folders", description="", default=True, options={'HIDDEN'})
1054     filter_python = BoolProperty(name="Filter python", description="", default=True, options={'HIDDEN'})
1055
1056     def execute(self, context):
1057         import traceback
1058         import zipfile
1059         pyfile = self.properties.filepath
1060
1061         path_addons = bpy.utils.script_paths("addons")[-1]
1062
1063         #check to see if the file is in compressed format (.zip)
1064         if zipfile.is_zipfile(pyfile):
1065             try:
1066                 file_to_extract = zipfile.ZipFile(pyfile, 'r')
1067
1068                 #extract the file to "addons"
1069                 file_to_extract.extractall(path_addons)
1070
1071             except:
1072                 traceback.print_exc()
1073                 return {'CANCELLED'}
1074
1075         else:
1076             path_dest = os.path.join(path_addons, os.path.basename(pyfile))
1077
1078             if os.path.exists(path_dest):
1079                 self.report({'WARNING'}, "File already installed to '%s'\n" % path_dest)
1080                 return {'CANCELLED'}
1081
1082             #if not compressed file just copy into the addon path
1083             try:
1084                 shutil.copyfile(pyfile, path_dest)
1085
1086             except:
1087                 traceback.print_exc()
1088                 return {'CANCELLED'}
1089
1090         # TODO, should not be a warning.
1091         # self.report({'WARNING'}, "File installed to '%s'\n" % path_dest)
1092         return {'FINISHED'}
1093
1094     def invoke(self, context, event):
1095         paths = bpy.utils.script_paths("addons")
1096         if not paths:
1097             self.report({'ERROR'}, "No 'addons' path could be found in " + str(bpy.utils.script_paths()))
1098             return {'CANCELLED'}
1099
1100         wm = context.manager
1101         wm.add_fileselect(self)
1102         return {'RUNNING_MODAL'}
1103
1104
1105 class WM_OT_addon_expand(bpy.types.Operator):
1106     "Display more information on this add-on"
1107     bl_idname = "wm.addon_expand"
1108     bl_label = ""
1109
1110     module = StringProperty(name="Module", description="Module name of the addon to expand")
1111
1112     def execute(self, context):
1113         module_name = self.properties.module
1114
1115         # unlikely to fail, module should have already been imported
1116         try:
1117             mod = __import__(module_name)
1118         except:
1119             import traceback
1120             traceback.print_exc()
1121             return {'CANCELLED'}
1122
1123         info = addon_info_get(mod)
1124         info["expanded"] = not info["expanded"]
1125         return {'FINISHED'}
1126
1127
1128 classes = [
1129     USERPREF_HT_header,
1130     USERPREF_PT_tabs,
1131     USERPREF_PT_interface,
1132     USERPREF_PT_theme,
1133     USERPREF_PT_edit,
1134     USERPREF_PT_system,
1135     USERPREF_PT_file,
1136     USERPREF_PT_input,
1137     USERPREF_PT_addons,
1138
1139     USERPREF_MT_interaction_presets,
1140     USERPREF_MT_splash,
1141
1142     WM_OT_addon_enable,
1143     WM_OT_addon_disable,
1144     WM_OT_addon_install,
1145     WM_OT_addon_expand]
1146
1147
1148 def register():
1149     register = bpy.types.register
1150     for cls in classes:
1151         register(cls)
1152
1153
1154 def unregister():
1155     unregister = bpy.types.unregister
1156     for cls in classes:
1157         unregister(cls)
1158
1159 if __name__ == "__main__":
1160     register()