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