Jason, from your merge 30325.
[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         col = row.column()
331         row = col.row(align=True)
332         row.label("Overlay Color:")
333         row.prop(edit, "sculpt_paint_overlay_col", text="")
334
335         col.separator()
336         col.separator()
337         col.separator()
338
339         col.label(text="Duplicate Data:")
340         col.prop(edit, "duplicate_mesh", text="Mesh")
341         col.prop(edit, "duplicate_surface", text="Surface")
342         col.prop(edit, "duplicate_curve", text="Curve")
343         col.prop(edit, "duplicate_text", text="Text")
344         col.prop(edit, "duplicate_metaball", text="Metaball")
345         col.prop(edit, "duplicate_armature", text="Armature")
346         col.prop(edit, "duplicate_lamp", text="Lamp")
347         col.prop(edit, "duplicate_material", text="Material")
348         col.prop(edit, "duplicate_texture", text="Texture")
349         #col.prop(edit, "duplicate_fcurve", text="F-Curve")
350         col.prop(edit, "duplicate_action", text="Action")
351         col.prop(edit, "duplicate_particle", text="Particle")
352
353
354 class USERPREF_PT_system(bpy.types.Panel):
355     bl_space_type = 'USER_PREFERENCES'
356     bl_label = "System"
357     bl_region_type = 'WINDOW'
358     bl_show_header = False
359
360     def poll(self, context):
361         userpref = context.user_preferences
362         return (userpref.active_section == 'SYSTEM')
363
364     def draw(self, context):
365         layout = self.layout
366
367         userpref = context.user_preferences
368         system = userpref.system
369
370         split = layout.split()
371
372
373         # 1. Column
374         column = split.column()
375         colsplit = column.split(percentage=0.85)
376
377         col = colsplit.column()
378         col.label(text="General:")
379         col.prop(system, "dpi")
380         col.prop(system, "frame_server_port")
381         col.prop(system, "scrollback", text="Console Scrollback")
382         col.prop(system, "auto_execute_scripts")
383         col.prop(system, "tabs_as_spaces")
384
385         col.separator()
386         col.separator()
387         col.separator()
388
389         col.label(text="Sound:")
390         col.row().prop(system, "audio_device", expand=True)
391         sub = col.column()
392         sub.active = system.audio_device != 'NONE'
393         #sub.prop(system, "enable_all_codecs")
394         sub.prop(system, "audio_channels", text="Channels")
395         sub.prop(system, "audio_mixing_buffer", text="Mixing Buffer")
396         sub.prop(system, "audio_sample_rate", text="Sample Rate")
397         sub.prop(system, "audio_sample_format", text="Sample Format")
398
399         col.separator()
400         col.separator()
401         col.separator()
402
403         col.label(text="Screencast:")
404         col.prop(system, "screencast_fps")
405         col.prop(system, "screencast_wait_time")
406         col.separator()
407         col.separator()
408         col.separator()
409
410         #column = split.column()
411         #colsplit = column.split(percentage=0.85)
412
413         # No translation in 2.5 yet
414         #col.prop(system, "language")
415         #col.label(text="Translate:")
416         #col.prop(system, "translate_tooltips", text="Tooltips")
417         #col.prop(system, "translate_buttons", text="Labels")
418         #col.prop(system, "translate_toolbox", text="Toolbox")
419
420         #col.separator()
421
422         #col.prop(system, "use_textured_fonts")
423
424
425         # 2. Column
426         column = split.column()
427         colsplit = column.split(percentage=0.85)
428
429         col = colsplit.column()
430         col.label(text="OpenGL:")
431         col.prop(system, "clip_alpha", slider=True)
432         col.prop(system, "use_mipmaps")
433         col.prop(system, "use_vbos")
434         #Anti-aliasing is disabled as it breaks broder/lasso select
435         #col.prop(system, "use_antialiasing")
436         col.label(text="Window Draw Method:")
437         col.prop(system, "window_draw_method", text="")
438         col.label(text="Textures:")
439         col.prop(system, "gl_texture_limit", text="Limit Size")
440         col.prop(system, "texture_time_out", text="Time Out")
441         col.prop(system, "texture_collection_rate", text="Collection Rate")
442
443         col.separator()
444         col.separator()
445         col.separator()
446
447         col.label(text="Sequencer:")
448         col.prop(system, "prefetch_frames")
449         col.prop(system, "memory_cache_limit")
450
451
452         # 3. Column
453         column = split.column()
454
455         column.label(text="Solid OpenGL lights:")
456
457         split = column.split(percentage=0.1)
458         split.label()
459         split.label(text="Colors:")
460         split.label(text="Direction:")
461
462         lamp = system.solid_lights[0]
463         opengl_lamp_buttons(column, lamp)
464
465         lamp = system.solid_lights[1]
466         opengl_lamp_buttons(column, lamp)
467
468         lamp = system.solid_lights[2]
469         opengl_lamp_buttons(column, lamp)
470
471         column.separator()
472         column.separator()
473         column.separator()
474
475         column.label(text="Color Picker Type:")
476         column.row().prop(system, "color_picker_type", text="")
477
478         column.separator()
479         column.separator()
480         column.separator()
481
482         column.prop(system, "use_weight_color_range", text="Custom Weight Paint Range")
483         sub = column.column()
484         sub.active = system.use_weight_color_range
485         sub.template_color_ramp(system, "weight_color_range", expand=True)
486
487
488 class USERPREF_PT_theme(bpy.types.Panel):
489     bl_space_type = 'USER_PREFERENCES'
490     bl_label = "Themes"
491     bl_region_type = 'WINDOW'
492     bl_show_header = False
493
494     @staticmethod
495     def _theme_generic(split, themedata):
496
497         row = split.row()
498
499         subsplit = row.split(percentage=0.95)
500
501         padding1 = subsplit.split(percentage=0.15)
502         padding1.column()
503
504         subsplit = row.split(percentage=0.85)
505
506         padding2 = subsplit.split(percentage=0.15)
507         padding2.column()
508
509         colsub_pair = padding1.column(), padding2.column()
510
511         props_type = {}
512
513         for i, prop in enumerate(themedata.rna_type.properties):
514             attr = prop.identifier
515             if attr == "rna_type":
516                 continue
517
518             props_type.setdefault((prop.type, prop.subtype), []).append(prop.identifier)
519
520         for props_type, props_ls in sorted(props_type.items()):
521             for i, attr in enumerate(props_ls):
522                 colsub_pair[i % 2].row().prop(themedata, attr)
523
524     def poll(self, context):
525         userpref = context.user_preferences
526         return (userpref.active_section == 'THEMES')
527
528     def draw(self, context):
529         layout = self.layout
530
531         theme = context.user_preferences.themes[0]
532
533         split_themes = layout.split(percentage=0.2)
534         split_themes.prop(theme, "theme_area", expand=True)
535
536         split = layout.split(percentage=0.4)
537
538
539         layout.separator()
540         layout.separator()
541
542         split = split_themes.split()
543
544         if theme.theme_area == 'USER_INTERFACE':
545             col = split.column()
546
547             ui = theme.user_interface.wcol_regular
548             col.label(text="Regular:")
549             ui_items_general(col, ui)
550
551             ui = theme.user_interface.wcol_tool
552             col.label(text="Tool:")
553             ui_items_general(col, ui)
554
555             ui = theme.user_interface.wcol_radio
556             col.label(text="Radio Buttons:")
557             ui_items_general(col, ui)
558
559             ui = theme.user_interface.wcol_text
560             col.label(text="Text:")
561             ui_items_general(col, ui)
562
563             ui = theme.user_interface.wcol_option
564             col.label(text="Option:")
565             ui_items_general(col, ui)
566
567             ui = theme.user_interface.wcol_toggle
568             col.label(text="Toggle:")
569             ui_items_general(col, ui)
570
571             ui = theme.user_interface.wcol_num
572             col.label(text="Number Field:")
573             ui_items_general(col, ui)
574
575             ui = theme.user_interface.wcol_numslider
576             col.label(text="Value Slider:")
577             ui_items_general(col, ui)
578
579             ui = theme.user_interface.wcol_box
580             col.label(text="Box:")
581             ui_items_general(col, ui)
582
583             ui = theme.user_interface.wcol_menu
584             col.label(text="Menu:")
585             ui_items_general(col, ui)
586
587             ui = theme.user_interface.wcol_pulldown
588             col.label(text="Pulldown:")
589             ui_items_general(col, ui)
590
591             ui = theme.user_interface.wcol_menu_back
592             col.label(text="Menu Back:")
593             ui_items_general(col, ui)
594
595             ui = theme.user_interface.wcol_menu_item
596             col.label(text="Menu Item:")
597             ui_items_general(col, ui)
598
599             ui = theme.user_interface.wcol_scroll
600             col.label(text="Scroll Bar:")
601             ui_items_general(col, ui)
602
603             ui = theme.user_interface.wcol_progress
604             col.label(text="Progress Bar:")
605             ui_items_general(col, ui)
606
607             ui = theme.user_interface.wcol_list_item
608             col.label(text="List Item:")
609             ui_items_general(col, ui)
610
611             ui = theme.user_interface.wcol_state
612             col.label(text="State:")
613
614             row = col.row()
615
616             subsplit = row.split(percentage=0.95)
617
618             padding = subsplit.split(percentage=0.15)
619             colsub = padding.column()
620             colsub = padding.column()
621             colsub.row().prop(ui, "inner_anim")
622             colsub.row().prop(ui, "inner_anim_sel")
623             colsub.row().prop(ui, "inner_driven")
624             colsub.row().prop(ui, "inner_driven_sel")
625
626             subsplit = row.split(percentage=0.85)
627
628             padding = subsplit.split(percentage=0.15)
629             colsub = padding.column()
630             colsub = padding.column()
631             colsub.row().prop(ui, "inner_key")
632             colsub.row().prop(ui, "inner_key_sel")
633             colsub.row().prop(ui, "blend")
634
635
636             ui = theme.user_interface
637             col.separator()
638             col.separator()
639
640             split = col.split(percentage=0.93)
641             split.prop(ui, "icon_file")
642
643             layout.separator()
644             layout.separator()
645
646         else:
647             self._theme_generic(split, getattr(theme, theme.theme_area.lower()))
648
649
650 class USERPREF_PT_file(bpy.types.Panel):
651     bl_space_type = 'USER_PREFERENCES'
652     bl_label = "Files"
653     bl_region_type = 'WINDOW'
654     bl_show_header = False
655
656     def poll(self, context):
657         userpref = context.user_preferences
658         return (userpref.active_section == 'FILES')
659
660     def draw(self, context):
661         layout = self.layout
662
663         userpref = context.user_preferences
664         paths = userpref.filepaths
665
666         split = layout.split(percentage=0.7)
667
668         col = split.column()
669         col.label(text="File Paths:")
670
671         colsplit = col.split(percentage=0.95)
672         col1 = colsplit.split(percentage=0.3)
673
674         sub = col1.column()
675         sub.label(text="Fonts:")
676         sub.label(text="Textures:")
677         sub.label(text="Texture Plugins:")
678         sub.label(text="Sequence Plugins:")
679         sub.label(text="Render Output:")
680         sub.label(text="Scripts:")
681         sub.label(text="Sounds:")
682         sub.label(text="Temp:")
683         sub.label(text="Image Editor:")
684         sub.label(text="Animation Player:")
685
686         sub = col1.column()
687         sub.prop(paths, "fonts_directory", text="")
688         sub.prop(paths, "textures_directory", text="")
689         sub.prop(paths, "texture_plugin_directory", text="")
690         sub.prop(paths, "sequence_plugin_directory", text="")
691         sub.prop(paths, "render_output_directory", text="")
692         sub.prop(paths, "python_scripts_directory", text="")
693         sub.prop(paths, "sounds_directory", text="")
694         sub.prop(paths, "temporary_directory", text="")
695         sub.prop(paths, "image_editor", text="")
696         subsplit = sub.split(percentage=0.3)
697         subsplit.prop(paths, "animation_player_preset", text="")
698         subsplit.prop(paths, "animation_player", text="")
699
700         col = split.column()
701         col.label(text="Save & Load:")
702         col.prop(paths, "use_relative_paths")
703         col.prop(paths, "compress_file")
704         col.prop(paths, "load_ui")
705         col.prop(paths, "filter_file_extensions")
706         col.prop(paths, "hide_dot_files_datablocks")
707
708         col.separator()
709         col.separator()
710
711         col.label(text="Auto Save:")
712         col.prop(paths, "save_version")
713         col.prop(paths, "recent_files")
714         col.prop(paths, "save_preview_images")
715         col.prop(paths, "auto_save_temporary_files")
716         sub = col.column()
717         sub.active = paths.auto_save_temporary_files
718         sub.prop(paths, "auto_save_time", text="Timer (mins)")
719
720 from space_userpref_keymap import InputKeyMapPanel
721
722
723 class USERPREF_PT_input(InputKeyMapPanel):
724     bl_space_type = 'USER_PREFERENCES'
725     bl_label = "Input"
726
727     def poll(self, context):
728         userpref = context.user_preferences
729         return (userpref.active_section == 'INPUT')
730
731     def draw_input_prefs(self, inputs, layout):
732         # General settings
733         row = layout.row()
734         col = row.column()
735
736         sub = col.column()
737         sub.label(text="Presets:")
738         subrow = sub.row(align=True)
739         subrow.menu("USERPREF_MT_interaction_presets", text=bpy.types.USERPREF_MT_interaction_presets.bl_label)
740         subrow.operator("wm.interaction_preset_add", text="", icon='ZOOMIN')
741         sub.separator()
742
743         sub.label(text="Mouse:")
744         sub1 = sub.column()
745         sub1.active = (inputs.select_mouse == 'RIGHT')
746         sub1.prop(inputs, "emulate_3_button_mouse")
747         sub.prop(inputs, "continuous_mouse")
748
749         sub.label(text="Select With:")
750         sub.row().prop(inputs, "select_mouse", expand=True)
751
752         sub = col.column()
753         sub.label(text="Double Click:")
754         sub.prop(inputs, "double_click_time", text="Speed")
755
756         sub.separator()
757
758         sub.prop(inputs, "emulate_numpad")
759
760         sub.separator()
761
762         sub.label(text="Orbit Style:")
763         sub.row().prop(inputs, "view_rotation", expand=True)
764
765         sub.label(text="Zoom Style:")
766         sub.row().prop(inputs, "zoom_style", text="")
767         if inputs.zoom_style == 'DOLLY':
768             sub.row().prop(inputs, "zoom_axis", expand=True)
769             sub.prop(inputs, "invert_zoom_direction")
770
771         #sub.prop(inputs, "use_middle_mouse_paste")
772
773         #col.separator()
774
775         sub = col.column()
776         sub.label(text="Mouse Wheel:")
777         sub.prop(inputs, "wheel_invert_zoom", text="Invert Wheel Zoom Direction")
778         #sub.prop(view, "wheel_scroll_lines", text="Scroll Lines")
779
780         col.separator()
781         ''' not implemented yet
782         sub = col.column()
783         sub.label(text="NDOF Device:")
784         sub.prop(inputs, "ndof_pan_speed", text="Pan Speed")
785         sub.prop(inputs, "ndof_rotate_speed", text="Orbit Speed")
786         '''
787
788         row.separator()
789
790     def draw(self, context):
791         layout = self.layout
792
793         #import time
794
795         #start = time.time()
796
797         userpref = context.user_preferences
798         wm = context.manager
799
800         inputs = userpref.inputs
801
802         split = layout.split(percentage=0.25)
803
804         # Input settings
805         self.draw_input_prefs(inputs, split)
806
807         # Keymap Settings
808         self.draw_keymaps(context, split)
809
810         #print("runtime", time.time() - start)
811
812
813 class USERPREF_PT_addons(bpy.types.Panel):
814     bl_space_type = 'USER_PREFERENCES'
815     bl_label = "Addons"
816     bl_region_type = 'WINDOW'
817     bl_show_header = False
818
819     def poll(self, context):
820         userpref = context.user_preferences
821         return (userpref.active_section == 'ADDONS')
822
823     @staticmethod
824     def _addon_list():
825         import sys
826         import time
827
828         modules = []
829         loaded_modules = set()
830         paths = bpy.utils.script_paths("addons")
831
832         if bpy.app.debug:
833             t_main = time.time()
834
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         if bpy.app.debug:
841             print("Addon Script Load Time %.4f" % (time.time() - t_main))
842
843         # del sys.path[0]
844         return modules
845
846     def draw(self, context):
847         layout = self.layout
848
849         userpref = context.user_preferences
850         used_ext = {ext.module for ext in userpref.addons}
851
852         # collect the categories that can be filtered on
853         addons = [(mod, addon_info_get(mod)) for mod in self._addon_list()]
854
855         cats = {info["category"] for mod, info in addons}
856         cats.discard("")
857
858         cats = ["All", "Enabled", "Disabled"] + sorted(cats)
859
860         bpy.types.Scene.EnumProperty(items=[(cat, cat, cat + " addons") for cat in cats],
861             name="Category", attr="addon_filter", description="Filter add-ons by category")
862         bpy.types.Scene.StringProperty(name="Search", attr="addon_search",
863             description="Search within the selected filter")
864
865         split = layout.split(percentage=0.2)
866         col = split.column()
867         col.prop(context.scene, "addon_filter", text="Filter", expand=True)
868         col.prop(context.scene, "addon_search", text="", icon='VIEWZOOM')
869
870         col = split.column()
871
872         filter = context.scene.addon_filter
873         search = context.scene.addon_search.lower()
874
875         for mod, info in addons:
876             module_name = mod.__name__
877
878             is_enabled = module_name in used_ext
879
880             # check if add-on should be visible with current filters
881             if (filter == "All") or \
882                     (filter == info["category"]) or \
883                     (filter == "Enabled" and is_enabled) or \
884                     (filter == "Disabled" and not is_enabled):
885
886
887                 if search and search not in info["name"].lower():
888                     if info["author"]:
889                         if search not in info["author"].lower():
890                             continue
891                     else:
892                         continue
893
894                 # Addon UI Code
895                 box = col.column().box()
896                 colsub = box.column()
897                 row = colsub.row()
898
899                 row.operator("wm.addon_expand", icon='TRIA_DOWN' if info["expanded"] else 'TRIA_RIGHT', emboss=False).module = module_name
900
901                 rowsub = row.row()
902                 rowsub.active = is_enabled
903                 rowsub.label(text=info["name"], icon='ERROR' if info["warning"] else 'BLENDER')
904
905                 if is_enabled:
906                     row.operator("wm.addon_disable", icon='CHECKBOX_HLT', text="", emboss=False).module = module_name
907                 else:
908                     row.operator("wm.addon_enable", icon='CHECKBOX_DEHLT', text="", emboss=False).module = module_name
909
910                 # Expanded UI (only if additional infos are available)
911                 if info["expanded"]:
912                     if info["description"]:
913                         split = colsub.row().split(percentage=0.15)
914                         split.label(text='Description:')
915                         split.label(text=info["description"])
916                     if info["location"]:
917                         split = colsub.row().split(percentage=0.15)
918                         split.label(text='Location:')
919                         split.label(text=info["location"])
920                     if info["author"]:
921                         split = colsub.row().split(percentage=0.15)
922                         split.label(text='Author:')
923                         split.label(text=info["author"])
924                     if info["version"]:
925                         split = colsub.row().split(percentage=0.15)
926                         split.label(text='Version:')
927                         split.label(text=info["version"])
928                     if info["warning"]:
929                         split = colsub.row().split(percentage=0.15)
930                         split.label(text="Warning:")
931                         split.label(text='  ' + info["warning"], icon='ERROR')
932                     if info["wiki_url"] or info["tracker_url"]:
933                         split = colsub.row().split(percentage=0.15)
934                         split.label(text="Internet:")
935                         if info["wiki_url"]:
936                             split.operator("wm.url_open", text="Link to the Wiki", icon='HELP').url = info["wiki_url"]
937                         if info["tracker_url"]:
938                             split.operator("wm.url_open", text="Report a Bug", icon='URL').url = info["tracker_url"]
939
940                         if info["wiki_url"] and info["tracker_url"]:
941                             split.separator()
942                         else:
943                             split.separator()
944                             split.separator()
945
946         # Append missing scripts
947         # First collect scripts that are used but have no script file.
948         module_names = {mod.__name__ for mod, info in addons}
949         missing_modules = {ext for ext in used_ext if ext not in module_names}
950
951         if missing_modules and filter in ("All", "Enabled"):
952             col.column().separator()
953             col.column().label(text="Missing script files")
954
955             module_names = {mod.__name__ for mod, info in addons}
956             for ext in sorted(missing_modules):
957                 # Addon UI Code
958                 box = col.column().box()
959                 colsub = box.column()
960                 row = colsub.row()
961
962                 row.label(text=ext, icon='ERROR')
963                 row.operator("wm.addon_disable").module = ext
964
965 from bpy.props import *
966
967
968 def addon_info_get(mod, info_basis={"name": "", "author": "", "version": "", "blender": "", "location": "", "description": "", "wiki_url": "", "tracker_url": "", "category": "", "warning": "", "expanded": False}):
969     addon_info = getattr(mod, "bl_addon_info", {})
970
971     # avoid re-initializing
972     if "_init" in addon_info:
973         return addon_info
974
975     if not addon_info:
976         mod.bl_addon_info = addon_info
977
978     for key, value in info_basis.items():
979         addon_info.setdefault(key, value)
980
981     if not addon_info["name"]:
982         addon_info["name"] = mod.__name__
983
984     addon_info["_init"] = None
985     return addon_info
986
987
988 class WM_OT_addon_enable(bpy.types.Operator):
989     "Enable an addon"
990     bl_idname = "wm.addon_enable"
991     bl_label = "Enable Add-On"
992
993     module = StringProperty(name="Module", description="Module name of the addon to enable")
994
995     def execute(self, context):
996         module_name = self.properties.module
997
998         try:
999             mod = __import__(module_name)
1000             mod.register()
1001         except:
1002             import traceback
1003             traceback.print_exc()
1004             return {'CANCELLED'}
1005
1006         ext = context.user_preferences.addons.new()
1007         ext.module = module_name
1008
1009         # check if add-on is written for current blender version, or raise a warning
1010         info = addon_info_get(mod)
1011
1012         if info.get("blender", (0, 0, 0)) > bpy.app.version:
1013             self.report("WARNING','This script was written for a newer version of Blender and might not function (correctly).\nThe script is enabled though.")
1014
1015         return {'FINISHED'}
1016
1017
1018 class WM_OT_addon_disable(bpy.types.Operator):
1019     "Disable an addon"
1020     bl_idname = "wm.addon_disable"
1021     bl_label = "Disable Add-On"
1022
1023     module = StringProperty(name="Module", description="Module name of the addon to disable")
1024
1025     def execute(self, context):
1026         import traceback
1027         module_name = self.properties.module
1028
1029         try:
1030             mod = __import__(module_name)
1031             mod.unregister()
1032         except:
1033             traceback.print_exc()
1034
1035         addons = context.user_preferences.addons
1036         ok = True
1037         while ok: # incase its in more then once.
1038             ok = False
1039             for ext in addons:
1040                 if ext.module == module_name:
1041                     addons.remove(ext)
1042                     ok = True
1043                     break
1044
1045         return {'FINISHED'}
1046
1047
1048 class WM_OT_addon_install(bpy.types.Operator):
1049     "Install an addon"
1050     bl_idname = "wm.addon_install"
1051     bl_label = "Install Add-On..."
1052
1053     module = StringProperty(name="Module", description="Module name of the addon to disable")
1054
1055     filepath = StringProperty(name="File Path", description="File path to write file to")
1056     filter_folder = BoolProperty(name="Filter folders", description="", default=True, options={'HIDDEN'})
1057     filter_python = BoolProperty(name="Filter python", description="", default=True, options={'HIDDEN'})
1058
1059     def execute(self, context):
1060         import traceback
1061         import zipfile
1062         pyfile = self.properties.filepath
1063
1064         path_addons = bpy.utils.script_paths("addons")[-1]
1065
1066         #check to see if the file is in compressed format (.zip)
1067         if zipfile.is_zipfile(pyfile):
1068             try:
1069                 file_to_extract = zipfile.ZipFile(pyfile, 'r')
1070
1071                 #extract the file to "addons"
1072                 file_to_extract.extractall(path_addons)
1073
1074             except:
1075                 traceback.print_exc()
1076                 return {'CANCELLED'}
1077
1078         else:
1079             path_dest = os.path.join(path_addons, os.path.basename(pyfile))
1080
1081             if os.path.exists(path_dest):
1082                 self.report({'WARNING'}, "File already installed to '%s'\n" % path_dest)
1083                 return {'CANCELLED'}
1084
1085             #if not compressed file just copy into the addon path
1086             try:
1087                 shutil.copyfile(pyfile, path_dest)
1088
1089             except:
1090                 traceback.print_exc()
1091                 return {'CANCELLED'}
1092
1093         # TODO, should not be a warning.
1094         # self.report({'WARNING'}, "File installed to '%s'\n" % path_dest)
1095         return {'FINISHED'}
1096
1097     def invoke(self, context, event):
1098         paths = bpy.utils.script_paths("addons")
1099         if not paths:
1100             self.report({'ERROR'}, "No 'addons' path could be found in " + str(bpy.utils.script_paths()))
1101             return {'CANCELLED'}
1102
1103         wm = context.manager
1104         wm.add_fileselect(self)
1105         return {'RUNNING_MODAL'}
1106
1107
1108 class WM_OT_addon_expand(bpy.types.Operator):
1109     "Display more information on this add-on"
1110     bl_idname = "wm.addon_expand"
1111     bl_label = ""
1112
1113     module = StringProperty(name="Module", description="Module name of the addon to expand")
1114
1115     def execute(self, context):
1116         module_name = self.properties.module
1117
1118         # unlikely to fail, module should have already been imported
1119         try:
1120             mod = __import__(module_name)
1121         except:
1122             import traceback
1123             traceback.print_exc()
1124             return {'CANCELLED'}
1125
1126         info = addon_info_get(mod)
1127         info["expanded"] = not info["expanded"]
1128         return {'FINISHED'}
1129
1130
1131 classes = [
1132     USERPREF_HT_header,
1133     USERPREF_PT_tabs,
1134     USERPREF_PT_interface,
1135     USERPREF_PT_theme,
1136     USERPREF_PT_edit,
1137     USERPREF_PT_system,
1138     USERPREF_PT_file,
1139     USERPREF_PT_input,
1140     USERPREF_PT_addons,
1141
1142     USERPREF_MT_interaction_presets,
1143     USERPREF_MT_splash,
1144
1145     WM_OT_addon_enable,
1146     WM_OT_addon_disable,
1147     WM_OT_addon_install,
1148     WM_OT_addon_expand]
1149
1150
1151 def register():
1152     register = bpy.types.register
1153     for cls in classes:
1154         register(cls)
1155
1156
1157 def unregister():
1158     unregister = bpy.types.unregister
1159     for cls in classes:
1160         unregister(cls)
1161
1162 if __name__ == "__main__":
1163     register()