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