bl_info cleanup
[blender-addons-contrib.git] / space_view3d_quickPrefs.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 bl_info = {
20     "name": "QuickPrefs",
21     "author": "Sean Olson",
22     "version": (2,1),
23     "blender": (2, 6, 4),
24     "location": "3DView->Properties Panel (N-Key)",
25     "description": "Add often changed User Preference options to Properties panel.",
26     "warning": "",
27     "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"\
28                 "Scripts/3D_interaction/QuickPrefs",
29     "tracker_url": "http://projects.blender.org/tracker/index.php?"\
30                    "func=detail&aid=27822",
31     "category": "3D View"}
32
33 import os
34     
35 import bpy
36 from bpy.types import Menu, Panel
37 from rna_prop_ui import PropertyPanel
38 from bpy.app.handlers import persistent
39
40 import bpy_extras
41
42 #Global Variables
43 recursive  = False
44 renaming   = True
45 lastindex  = 0
46 lastname   = ""
47 debug = 0
48 defaultfilepath=os.path.join(bpy.utils.script_paths(subdir="addons")[0], "quickprefpresets")
49
50
51 ##########################################
52 ####### Import/Export Functions ##########
53 ##########################################
54
55 #import all grabs all files in the given directory and imports them
56 @persistent
57 def gllightpreset_importall():
58     path = bpy.context.scene.gllightpreset_importdirectory
59     directorylisting = os.listdir(path)
60
61     for infile in directorylisting:
62         if not os.path.isdir(infile):           #check if the file is a directory
63             if '.preset' in infile:                     #check that this is a .preset file
64                 thefile=os.path.join(path, infile)      #join the filename with the path
65                 gllightpreset_importsingle(thefile)     #import it!
66        
67 #import single takes a given filename and imports it
68 def gllightpreset_importsingle(filename):
69     
70     if not os.path.isdir(filename):           #check if the file is a directory
71         if '.preset' in filename:               #check to make sure this is a preset file
72             readfile=open(filename, 'r')
73             
74             name=readfile.readline()
75             name=name[:-1]
76             
77             illuminationString0=readfile.readline()
78             illuminationString0=illuminationString0[:-1]
79             
80             if illuminationString0=="True":
81                 illumination0=True
82             else:
83                 illumination0=False
84            
85             illuminationString1=readfile.readline()
86             illuminationString1=illuminationString1[:-1]
87             if illuminationString1=="True":
88                 illumination1=True
89             else:
90                 illumination1=False
91                 
92             illuminationString2=readfile.readline()
93             illuminationString2=illuminationString2[:-1]
94             if illuminationString2=="True":
95                 illumination2=True
96             else:
97                 illumination2=False
98             
99             #convert strings to booleans
100
101             str=readfile.readline()
102             strArray=str.split()
103             direction0x=strArray[0]
104             direction0y=strArray[1]
105             direction0z=strArray[2]
106
107             str=readfile.readline()
108             strArray=str.split()
109             direction1x=strArray[0]
110             direction1y=strArray[1]
111             direction1z=strArray[2]
112
113             str=readfile.readline()
114             strArray=str.split()
115             direction2x=strArray[0]
116             direction2y=strArray[1]
117             direction2z=strArray[2]
118
119             str=readfile.readline()
120             strArray=str.split()
121             diffuse0r=strArray[0]
122             diffuse0g=strArray[1]
123             diffuse0b=strArray[2]
124
125             str=readfile.readline()
126             strArray=str.split()
127             diffuse1r=strArray[0]
128             diffuse1g=strArray[1]
129             diffuse1b=strArray[2]
130
131             str=readfile.readline()
132             strArray=str.split()
133             diffuse2r=strArray[0]
134             diffuse2g=strArray[1]
135             diffuse2b=strArray[2]
136
137             str=readfile.readline()
138             strArray=str.split()
139             spec0r=strArray[0]
140             spec0g=strArray[1]
141             spec0b=strArray[2]
142
143             str=readfile.readline()
144             strArray=str.split()
145             spec1r=strArray[0]
146             spec1g=strArray[1]
147             spec1b=strArray[2]
148
149             str=readfile.readline()
150             strArray=str.split()
151             spec2r=strArray[0]
152             spec2g=strArray[1]
153             spec2b=strArray[2]
154             
155             #set the variables
156             system=bpy.context.user_preferences.system
157             system.solid_lights[0].use=illumination0
158             system.solid_lights[1].use=illumination1
159             system.solid_lights[2].use=illumination2
160
161             system.solid_lights[0].direction = (float(direction0x), float(direction0y), float(direction0z))
162             system.solid_lights[1].direction = (float(direction1x), float(direction1y), float(direction1z))
163             system.solid_lights[2].direction = (float(direction2x), float(direction2y), float(direction2z))
164
165             system.solid_lights[0].diffuse_color=(float(diffuse0r), float(diffuse0g), float(diffuse0b))
166             system.solid_lights[1].diffuse_color=(float(diffuse1r), float(diffuse1g), float(diffuse1b))
167             system.solid_lights[2].diffuse_color=(float(diffuse2r), float(diffuse2g), float(diffuse2b))
168
169             system.solid_lights[0].specular_color=(float(spec0r), float(spec0g), float(spec0b))
170             system.solid_lights[1].specular_color=(float(spec1r), float(spec1g), float(spec1b))
171             system.solid_lights[2].specular_color=(float(spec2r), float(spec2g), float(spec2b))
172             gllightpreset_add(name)
173             gllightpreset_save()
174
175 #Export all exports the entire contents of your presets list to the given file    
176 @persistent     
177 def gllightpreset_exportall(context):
178    for i in range(len(bpy.context.scene.gllightpreset)):
179         gllightpreset_exportsingle(i, True)
180              
181     
182 def gllightpreset_exportsingle(index, multiimport):
183     scn=bpy.context.scene
184     name=scn.gllightpreset[index].name
185     
186     illuminatedBool0=scn.gllightpreset[index].illuminated0
187     illuminatedBool1=scn.gllightpreset[index].illuminated1
188     illuminatedBool2=scn.gllightpreset[index].illuminated2
189     
190     #illuminations
191     if illuminatedBool0==True:
192         illuminated0="True"
193     else:
194         illuminated0="False"
195     
196     if illuminatedBool1==True:
197         illuminated1="True"
198     else:
199         illuminated1="False"
200         
201     if illuminatedBool2==True:
202         illuminated2="True"
203     else:
204         illuminated2="False"
205         
206     #direction light0
207     dirx=str(scn.gllightpreset[index].direction0[0])
208     diry=str(scn.gllightpreset[index].direction0[1])
209     dirz=str(scn.gllightpreset[index].direction0[2])
210     direction0=dirx + " " + diry + " " + dirz + " "
211     
212     #direction light1
213     dirx=str(scn.gllightpreset[index].direction1[0])
214     diry=str(scn.gllightpreset[index].direction1[1])
215     dirz=str(scn.gllightpreset[index].direction1[2])
216     direction1=dirx + " " + diry + " " + dirz + " "
217     
218     #direction light2
219     dirx=str(scn.gllightpreset[index].direction2[0])
220     diry=str(scn.gllightpreset[index].direction2[1])
221     dirz=str(scn.gllightpreset[index].direction2[2])
222     direction2=dirx + " " + diry + " " + dirz + " "
223     
224     #diffuse light0
225     difr=str(scn.gllightpreset[index].diffuse0[0])
226     difg=str(scn.gllightpreset[index].diffuse0[1])
227     difb=str(scn.gllightpreset[index].diffuse0[2])
228     diffuse0=difr + " " + difg + " " + difb + " "
229     
230     #diffuse light1
231     difr=str(scn.gllightpreset[index].diffuse1[0])
232     difg=str(scn.gllightpreset[index].diffuse1[1])
233     difb=str(scn.gllightpreset[index].diffuse1[2])
234     diffuse1=difr + " " + difg + " " + difb + " "
235     
236     #diffuse light2
237     difr=str(scn.gllightpreset[index].diffuse2[0])
238     difg=str(scn.gllightpreset[index].diffuse2[1])
239     difb=str(scn.gllightpreset[index].diffuse2[2])
240     diffuse2=difr + " " + difg + " " + difb + " "
241     
242     #specular light 0
243     specr=str(scn.gllightpreset[index].specular0[0])
244     specg=str(scn.gllightpreset[index].specular0[1])
245     specb=str(scn.gllightpreset[index].specular0[2])
246     specular0=specr + " " + specg + " " + specb + " "
247     
248     #specular light 1
249     specr=str(scn.gllightpreset[index].specular1[0])
250     specg=str(scn.gllightpreset[index].specular1[1])
251     specb=str(scn.gllightpreset[index].specular1[2])
252     specular1=specr + " " + specg + " " + specb + " "
253     
254     #specular light 2
255     specr=str(scn.gllightpreset[index].specular2[0])
256     specg=str(scn.gllightpreset[index].specular2[1])
257     specb=str(scn.gllightpreset[index].specular2[2])
258     specular2=specr + " " + specg + " " + specb + " "
259     
260     printstring=name+"\n"
261     printstring+=illuminated0 +"\n"+ illuminated1 +"\n"+ illuminated2+"\n"
262     printstring+=direction0  +"\n"+ direction1 +"\n"+ direction2 +"\n"
263     printstring+=diffuse0+"\n"+diffuse1 +"\n"+ diffuse2 +"\n"
264     printstring+=specular0+"\n"+specular1+"\n"+specular2+"\n"
265
266     if multiimport==True:
267         filepath = scn.gllightpreset_exportdirectory
268     else:
269         filepath = scn.gllightpreset_exportfile
270
271     if not os.path.exists(filepath):
272         os.mkdir(filepath)
273     filepath=os.path.join(filepath, (name+".preset"))        
274     writefile = open(filepath, 'w') 
275     writefile.write(printstring) 
276
277 ##########################################
278 ####### General  Presets Functions #######
279 ##########################################
280
281 def gllightpreset_addPresets():
282     print('in addPresets')
283     system=bpy.context.user_preferences.system
284   
285     system.solid_lights[0].use=True
286     system.solid_lights[1].use=True
287     system.solid_lights[2].use=True
288   
289     system.solid_lights[0].direction = (-0.8920, 0.3000, 0.8999)
290     system.solid_lights[1].direction = (0.5880, 0.4600, 0.2480)
291     system.solid_lights[2].direction = (0.2159, -0.3920, -0.2159)
292     
293     system.solid_lights[0].diffuse_color=(0.8000, 0.8000, 0.8000)
294     system.solid_lights[1].diffuse_color=(0.4980, 0.5000, 0.6000)
295     system.solid_lights[2].diffuse_color=(0.7980, 0.8379, 1.0)
296     
297     system.solid_lights[0].specular_color=(0.5, 0.5, 0.5)
298     system.solid_lights[1].specular_color=(0.2000, 0.2, 0.2)
299     system.solid_lights[2].specular_color=(0.0659, 0.0, 0.0)
300     gllightpreset_add("(Default)")
301     gllightpreset_save()
302     
303     system.solid_lights[0].use=True
304     system.solid_lights[1].use=True
305     system.solid_lights[2].use=True
306     
307     system.solid_lights[0].direction = (-0.3207, 0.4245, 0.8466)
308     system.solid_lights[1].direction = (0.4259, -0.0555, 0.9030)
309     system.solid_lights[2].direction = (-0.4485, 0.2400, -0.8609)
310     
311     system.solid_lights[0].diffuse_color=(0.4292, 0.0457, 0.0457)
312     system.solid_lights[1].diffuse_color=(0.7873, 0.4424, 0.2893)
313     system.solid_lights[2].diffuse_color=(0.7483, 0.7168, 0.6764)
314     
315     system.solid_lights[0].specular_color=(0.2000, 0.2000, 0.2000)
316     system.solid_lights[1].specular_color=(0.0, 0.0, 0.0)
317     system.solid_lights[2].specular_color=(0.0, 0.0, 0.0)
318     gllightpreset_add("Orange Clay")
319     gllightpreset_save()
320     
321     system.solid_lights[0].use=True
322     system.solid_lights[1].use=True
323     system.solid_lights[2].use=True
324     
325     system.solid_lights[0].direction=(-0.06666, 0.8222, 0.5652)
326     system.solid_lights[1].direction=(-0.8555, 0.1111, 0.5056)
327     system.solid_lights[2].direction=(0.9000, 0.1555, 0.4071)
328     
329     system.solid_lights[0].diffuse_color=(0.1000, 0.1000, 0.1000)
330     system.solid_lights[1].diffuse_color=(1.0, 0.9734, 0.8713)
331     system.solid_lights[2].diffuse_color=(0.7835, 0.9215, 1.0)
332     
333     system.solid_lights[0].specular_color=(0.0, 0.0, 0.0)
334     system.solid_lights[1].specular_color=(0.0, 0.0, 0.0)
335     system.solid_lights[2].specular_color=(0.0, 0.0, 0.0)
336     gllightpreset_add("Softblend")
337     gllightpreset_save()
338     
339     system.solid_lights[0].use = True
340     system.solid_lights[1].use = True
341     system.solid_lights[2].use = True
342
343     system.solid_lights[0].direction = (0.3666, 0.2888, 0.8843)
344     system.solid_lights[1].direction = (0.1111, 0.0888, 0.9898)
345     system.solid_lights[2].direction = (-0.5142, 0.5142, -0.6864)
346
347     system.solid_lights[0].diffuse_color = (0.6618, 0.5481, 0.5054)
348     system.solid_lights[1].diffuse_color = (0.3672, 0.2371, 0.1752)
349     system.solid_lights[2].diffuse_color = (1.0, 0.9574, 0.9058)
350
351     system.solid_lights[0].specular_color = (0.4969, 0.4886, 0.4820)
352     system.solid_lights[1].specular_color = (0.0, 0.0, 0.0)
353     system.solid_lights[2].specular_color = (1.0, 1.0, 1.0)
354     gllightpreset_add("Skin")
355     gllightpreset_save()
356
357     system.solid_lights[0].use = True
358     system.solid_lights[1].use = False
359     system.solid_lights[2].use = False
360
361     system.solid_lights[0].direction = (0.0111, 0.4000, 0.9164)
362     system.solid_lights[1].direction = (0.1111, 0.0888, 0.9898)
363     system.solid_lights[2].direction = (-0.5142, 0.5142, -0.6864)
364
365     system.solid_lights[0].diffuse_color = (1.0, 0.5912, 0.4148)
366     system.solid_lights[1].diffuse_color = (0.4962, 0.4962, 0.4962)
367     system.solid_lights[2].diffuse_color = (1.0, 0.9574, 0.9058)
368
369     system.solid_lights[0].specular_color = (0.2036, 0.4206, 0.7873)
370     system.solid_lights[1].specular_color = (0.0, 0.0, 0.0)
371     system.solid_lights[2].specular_color = (1.0, 1.0, 1.0)
372     gllightpreset_add("Candy")
373     gllightpreset_save();
374     
375     system.solid_lights[0].use = True
376     system.solid_lights[1].use = True
377     system.solid_lights[2].use = True
378
379     system.solid_lights[0].direction = (-0.6124, 0.6046, 0.5092)
380     system.solid_lights[1].direction = (0.1627, 0.5271, 0.8340)
381     system.solid_lights[2].direction = (-0.5858, 0.7811, -0.2160)
382
383     system.solid_lights[0].diffuse_color = (0.0, 0.3564, 0.4292)
384     system.solid_lights[1].diffuse_color = (0.7873, 0.0038, 0.7197)
385     system.solid_lights[2].diffuse_color = (0.1994, 0.2014, 0.2330)
386
387     system.solid_lights[0].specular_color = (0.0946, 0.0946, 0.0946)
388     system.solid_lights[1].specular_color = (0.0, 0.0, 0.0)
389     system.solid_lights[2].specular_color = (0.0, 0.0, 0.0)
390     gllightpreset_add("Purpleboogers.com")
391     gllightpreset_save();
392     
393     system.solid_lights[0].use = True
394     system.solid_lights[1].use = False
395     system.solid_lights[2].use = False
396
397     system.solid_lights[0].direction = (0.0111, 0.4000, 0.9164)
398     system.solid_lights[1].direction = (0.1111, 0.0888, 0.9898)
399     system.solid_lights[2].direction = (-0.5142, 0.5142, -0.6864)
400
401     system.solid_lights[0].diffuse_color = (0.8000, 0.8000, 0.8000)
402     system.solid_lights[1].diffuse_color = (0.4962, 0.4962, 0.4962)
403     system.solid_lights[2].diffuse_color = (1.0, 0.9574, 0.9058)
404
405     system.solid_lights[0].specular_color = (0.4540, 0.4540, 0.4540)
406     system.solid_lights[1].specular_color = (0.0, 0.0, 0.0)
407     system.solid_lights[2].specular_color = (1.0, 1.0, 1.0)
408     gllightpreset_add("Dark Gray")
409     gllightpreset_save()
410
411     system.solid_lights[0].use = True
412     system.solid_lights[1].use = False
413     system.solid_lights[2].use = False
414
415     system.solid_lights[0].direction = (0.0333, 0.2444, 0.9690)
416     system.solid_lights[1].direction = (0.1111, 0.08888, 0.9898)
417     system.solid_lights[2].direction = (-0.5142, 0.5142, -0.6864)
418
419     system.solid_lights[0].diffuse_color = (0.8000, 0.6670, 0.6020)
420     system.solid_lights[1].diffuse_color = (0.4962, 0.4962, 0.4962)
421     system.solid_lights[2].diffuse_color = (1.0, 0.9574, 0.9058)
422
423     system.solid_lights[0].specular_color = (0.3281, 0.3281, 0.3281)
424     system.solid_lights[1].specular_color = (0.0, 0.0, 0.0)
425     system.solid_lights[2].specular_color = (1.0, 1.0, 1.0)
426     gllightpreset_add("Turntable")
427     gllightpreset_save()
428
429     system.solid_lights[0].use = True
430     system.solid_lights[1].use = True
431     system.solid_lights[2].use = True
432
433     system.solid_lights[0].direction = (-0.2555, 0.5444, 0.7989)
434     system.solid_lights[1].direction = (0.5666, -0.7333, 0.3756)
435     system.solid_lights[2].direction = (-0.5142, 0.5142, -0.6864)
436
437     system.solid_lights[0].diffuse_color = (1.0, 0.9475, 0.3194)
438     system.solid_lights[1].diffuse_color = (0.3851, 0.2978, 0.1612)
439     system.solid_lights[2].diffuse_color = (1.0, 1.0, 1.0)
440
441     system.solid_lights[0].specular_color = (0.2740, 0.2740, 0.2740)
442     system.solid_lights[1].specular_color = (0.0, 0.0, 0.0)
443     system.solid_lights[2].specular_color = (0.0, 0.0, 0.0)
444     gllightpreset_add("Yellow Clay")
445     gllightpreset_save()
446     print('exit addpresets')
447 ##########################################
448 ####### LightPreset Functions #######
449 ##########################################
450
451 def gllightpreset_name(self, context):
452     global recursive
453     global renaming
454     global lastname
455     if recursive==True: return
456     recursive=True
457
458     tmp=self.name
459     self.name=self.name+"@temporaryname"
460     self.name=gllightpreset_checkname(tmp)
461     tmp=self.name
462     if renaming==True: gllightpreset_rename(lastname, tmp)
463     #gllightpreset_sort()
464
465     recursive=False
466     
467 def gllightpreset_checkname(name):
468     collection=bpy.context.scene.gllightpreset
469     if name=="": name="Preset"
470
471     if not name in collection:
472         return name
473
474     pos = name.rfind(".")
475     if pos==-1:
476         name=name+".001"
477         pos=len(name)-4
478
479     for i in range(1, 1000):
480         nr="00"+str(i)
481         tmp=name[:pos+1]+nr[len(nr)-3:]
482         if not tmp in collection:
483             return tmp        
484     return "Preset.???"
485     
486 def gllightpreset_rename(old, new):
487     for o in bpy.context.scene.objects:
488         if o.get("gllightpreset", "Default")==old:
489             o.gllightpreset=new
490
491 @persistent            
492 def gllightpreset_index(self, context):
493     global recursive
494     if recursive==True: return
495     recursive=True
496
497     scene=bpy.context.scene
498     if scene.gllightpreset_index > len(scene.gllightpreset)-1:
499         scene.gllightpreset_index = len(scene.gllightpreset)-1
500
501     recursive=False
502
503 def gllightpreset_add(name=""):
504     global renaming
505     renaming=False
506
507     entry=bpy.context.scene.gllightpreset.add()
508     bpy.context.scene['gllightpreset_index']=len(bpy.context.scene.gllightpreset)-1
509     entry.name=gllightpreset_checkname(name)
510     
511     renaming=True
512     gllightpreset_save()
513        
514 def gllightpreset_delete():
515     index=bpy.context.scene.gllightpreset_index
516     name=bpy.context.scene.gllightpreset[index].name
517     
518     for o in bpy.context.scene.objects:
519         if o.get("gllightpreset", "Default")==name:
520             o.gllightpreset="Default"
521     
522     bpy.context.scene.gllightpreset.remove(index)
523    
524     
525 def gllightpreset_save():
526     index=bpy.context.scene.gllightpreset_index
527     name=bpy.context.scene.gllightpreset[index].name
528     
529     bpy.context.scene.gllightpreset[index].illuminated0 = bpy.context.user_preferences.system.solid_lights[0].use
530     bpy.context.scene.gllightpreset[index].illuminated1 = bpy.context.user_preferences.system.solid_lights[1].use
531     bpy.context.scene.gllightpreset[index].illuminated2 = bpy.context.user_preferences.system.solid_lights[2].use
532     
533     bpy.context.scene.gllightpreset[index].direction0 = bpy.context.user_preferences.system.solid_lights[0].direction
534     bpy.context.scene.gllightpreset[index].direction1 = bpy.context.user_preferences.system.solid_lights[1].direction
535     bpy.context.scene.gllightpreset[index].direction2 = bpy.context.user_preferences.system.solid_lights[2].direction
536     
537     bpy.context.scene.gllightpreset[index].diffuse0 = bpy.context.user_preferences.system.solid_lights[0].diffuse_color
538     bpy.context.scene.gllightpreset[index].diffuse1 = bpy.context.user_preferences.system.solid_lights[1].diffuse_color
539     bpy.context.scene.gllightpreset[index].diffuse2 = bpy.context.user_preferences.system.solid_lights[2].diffuse_color
540     
541     bpy.context.scene.gllightpreset[index].specular0 = bpy.context.user_preferences.system.solid_lights[0].specular_color
542     bpy.context.scene.gllightpreset[index].specular1 = bpy.context.user_preferences.system.solid_lights[1].specular_color
543     bpy.context.scene.gllightpreset[index].specular2 = bpy.context.user_preferences.system.solid_lights[2].specular_color
544         
545 #select the current light    
546 def gllightpreset_select():
547     index=bpy.context.scene.gllightpreset_index
548     name=bpy.context.scene.gllightpreset[index].name
549     
550     bpy.context.user_preferences.system.solid_lights[0].use=bpy.context.scene.gllightpreset[index].illuminated0
551     bpy.context.user_preferences.system.solid_lights[1].use=bpy.context.scene.gllightpreset[index].illuminated1
552     bpy.context.user_preferences.system.solid_lights[2].use=bpy.context.scene.gllightpreset[index].illuminated2
553     
554     bpy.context.user_preferences.system.solid_lights[0].direction=bpy.context.scene.gllightpreset[index].direction0
555     bpy.context.user_preferences.system.solid_lights[1].direction=bpy.context.scene.gllightpreset[index].direction1
556     bpy.context.user_preferences.system.solid_lights[2].direction=bpy.context.scene.gllightpreset[index].direction2
557     
558     bpy.context.user_preferences.system.solid_lights[0].diffuse_color=bpy.context.scene.gllightpreset[index].diffuse0
559     bpy.context.user_preferences.system.solid_lights[1].diffuse_color=bpy.context.scene.gllightpreset[index].diffuse1
560     bpy.context.user_preferences.system.solid_lights[2].diffuse_color=bpy.context.scene.gllightpreset[index].diffuse2
561     
562     bpy.context.user_preferences.system.solid_lights[0].specular_color=bpy.context.scene.gllightpreset[index].specular0
563     bpy.context.user_preferences.system.solid_lights[1].specular_color=bpy.context.scene.gllightpreset[index].specular1
564     bpy.context.user_preferences.system.solid_lights[2].specular_color=bpy.context.scene.gllightpreset[index].specular2
565     
566 #sort alphabetically
567 def gllightpreset_sort():
568     collection=bpy.context.scene.gllightpreset
569     count=len(collection)
570     for i in range(0, count):
571         for j in range(i+1, count):
572             if collection[i].name > collection[j].name:
573                 collection.move(j, i)
574
575 #Add default setting
576 def gllightpreset_addDefault():
577     print('adding default presets')  
578     gllightpreset_addPresets()
579     gllightpreset_sort();
580     bpy.context.scene['gllightpreset_index']=0
581     gllightpreset_select()
582
583 ##########################################
584 ####### Persistant functions##############
585 ##########################################       
586
587
588 #This function decides where to load presets from - The order goes: BPY>File>Defaults
589 @persistent
590 def gllightpreset_chooseLoadLocation(context):
591
592     filepath=bpy.context.scene.gllightpreset_importdirectory
593
594     print(bpy.context.scene.gllightpreset)
595
596     if len(bpy.context.scene.gllightpreset)==0:                 #is it in bpy?
597           if not os.path.exists(filepath):                      #is it in the folder?
598                print('quickprefs: loading default presets')
599                gllightpreset_addDefault()                       #it's not, add the default
600           else:                                                 #the folder exists
601                directorylisting = os.listdir(filepath)
602                if len(directorylisting)==0:                     #is the folder empty?
603                     print('quickprefs: loading default presets')
604                     gllightpreset_addDefault()                  #the folder is empty, add default
605                else:                                            #the folder is not empty
606                     print('quickprefs: loading preset folder')
607                     gllightpreset_loadpresets(1)                #go ahead and load it    
608     print('quickprefs: loading from bpy')
609
610 #This function scans for changes of the index of the selected preset and updates the selection if needed
611 @persistent
612 def gllightpreset_scan(context):
613     global lastindex
614     if debug==0:
615         if lastindex!=bpy.context.scene['gllightpreset_index']:
616             lastindex=bpy.context.scene['gllightpreset_index']
617             gllightpreset_select()
618
619 #This function loads all the presets from a given folder (stored in bpy)
620 @persistent
621 def gllightpreset_loadpresets(context):
622        gllightpreset_importall()
623        bpy.context.scene['gllightpreset_index']=0
624        gllightpreset_select()
625            
626 ##########################################
627 ####### GUI ##############################
628 ##########################################
629
630 #This function defines the layout of the openGL lamps
631 def opengl_lamp_buttons(column, lamp):
632     split = column.split(percentage=0.1)
633
634     split.prop(lamp, "use", text="", icon='OUTLINER_OB_LAMP' if lamp.use else 'LAMP_DATA')
635
636     col = split.column()
637     col.active = lamp.use
638     row = col.row()
639     row.label(text="Diffuse:")
640     row.prop(lamp, "diffuse_color", text="")
641     row = col.row()
642     row.label(text="Specular:")
643     row.prop(lamp, "specular_color", text="")
644
645     col = split.column()
646     col.active = lamp.use
647     col.prop(lamp, "direction", text="")
648
649 class gllightpreset(bpy.types.PropertyGroup):
650
651     props=bpy.props
652     name = props.StringProperty(update=gllightpreset_name)
653     
654     illuminated0 = props.BoolProperty(default = True)
655     illuminated1 = props.BoolProperty(default = True)
656     illuminated2 = props.BoolProperty(default = True)
657     
658     direction0 = props.FloatVectorProperty(name="",  default=(-0.8920, 0.3000, 0.8999))
659     direction1 = props.FloatVectorProperty(name="",  default=(0.5880, 0.4600, 0.2480))
660     direction2 = props.FloatVectorProperty(name="",  default=(0.2159, -0.3920, -0.2159))
661     
662     diffuse0 = props.FloatVectorProperty(name="",  default=(0.8000, 0.8000, 0.8000))
663     diffuse1 = props.FloatVectorProperty(name="",  default=(0.4980, 0.5000, 0.6000))
664     diffuse2 = props.FloatVectorProperty(name="",  default=(0.7980, 0.8379, 1.0))
665     
666     specular0 = props.FloatVectorProperty(name="",  default=(0.5, 0.5, 0.5))
667     specular1 = props.FloatVectorProperty(name="",  default=(0.2000, 0.2000, 0.2000))
668     specular2 = props.FloatVectorProperty(name="",  default=(0.0659, 0.0, 0.0))
669
670     count = props.IntProperty(name="", default=0)
671     count2 = props.IntProperty(name="", default=0)
672
673 class SCENE_OT_gllightpreset(bpy.types.Operator):
674     bl_label ="Preset Action"
675     bl_idname="gllightpreset.action"
676
677     #alias
678     
679
680     button=bpy.props.StringProperty(default="")
681
682     def execute(self, context):
683         scn=bpy.context.scene
684         button=self.button
685            
686         
687         if   button=="add":
688             gllightpreset_add()
689         elif button=="delete":   
690             if len(scn.gllightpreset)>1:
691                 gllightpreset_delete()
692             else:
693                 self.report({'INFO'}, "Must have at least 1 Preset")
694                 
695         elif button=="save":     
696             gllightpreset_save()
697             self.report({'INFO'}, "Saved Preset to Blend File.")
698         elif button=="sort":     gllightpreset_sort()
699         elif button=="select":   gllightpreset_select()
700         elif button=="export":   
701             gllightpreset_exportsingle(scn.gllightpreset_index, False)
702             self.report({'INFO'}, "Exported Preset to: "+scn.gllightpreset_exportfile)
703         elif button=="exportall":
704             gllightpreset_exportall(1)
705             self.report({'INFO'}, "Exported Presets to: "+scn.gllightpreset_exportdirectory)
706         elif button=="import":   
707             if not os.path.isdir(scn.gllightpreset_importfile):
708                 if not scn.gllightpreset_importdirectory=="":
709                     gllightpreset_importsingle(scn.gllightpreset_importfile)
710                 else:
711                     self.report({'INFO'}, "Please choose a valid preset file")
712             else:
713                 self.report({'INFO'}, "Please choose a valid preset file")
714         elif button=="importall":
715             gllightpreset_importall()
716             self.report({'INFO'}, "Imported Presets from: "+scn.gllightpreset_importdirectory)
717         elif button=="defaults":
718                 gllightpreset_addDefault()
719   
720         if scn.gllightpreset_index > len(scn.gllightpreset)-1:
721             scn.gllightpreset_index = len(scn.gllightpreset)-1
722
723         return {"FINISHED"}
724          
725 #Panel for tools
726 class PANEL(bpy.types.Panel):
727     bl_label = 'Quick Preferences'
728     bl_space_type = 'VIEW_3D'
729     bl_context = "render"
730     bl_region_type = 'UI'
731     bl_options = {'DEFAULT_CLOSED'}
732
733     
734     def draw(self, context):
735         global lastname
736     
737         #aliases
738         scn = bpy.context.scene
739         system = context.user_preferences.system
740         inputs = context.user_preferences.inputs
741         edit = context.user_preferences.edit
742         layout = self.layout
743         split = layout.split()
744         
745 #Setup the OpenGL lights box
746 #Draw the Lights
747         box = layout.box().column(align=False)
748         box.prop(scn,'lights')
749         if scn.lights:
750
751             split = box.split(percentage=0.1)
752             split.label()
753             split.label(text="Colors:")
754             split.label(text="Direction:")
755
756             lamp = system.solid_lights[0]
757             opengl_lamp_buttons(box, lamp)
758
759             lamp = system.solid_lights[1]
760             opengl_lamp_buttons(box, lamp)
761
762             lamp = system.solid_lights[2]
763             opengl_lamp_buttons(box, lamp)
764
765             box.separator()
766             
767 #Draw the selection box        
768             split = box.split(percentage=0.7)
769             col = split.row()
770             col.template_list(scn, "gllightpreset", scn, "gllightpreset_index", rows=5, maxrows=5)
771
772 #Draw the buttons
773             split = split.split()
774             col = split.column()
775             col.operator("gllightpreset.action", icon="ZOOMIN", text="Add").button="add"
776             col.operator("gllightpreset.action", icon="ZOOMOUT", text="Delete").button="delete"
777             col.operator("gllightpreset.action", icon="FILE_TICK", text="Save to Blend").button="save"
778             col.operator("gllightpreset.action", icon="SORTALPHA", text="Sort").button="sort"
779             col.operator("gllightpreset.action", icon="SHORTDISPLAY", text="Add Defaults").button="defaults"                     
780             col=box.row()
781             col=col.column()
782 #Draw the text box            
783             count=len(scn.gllightpreset)           
784             if count>0:
785                 entry=scn.gllightpreset[scn.gllightpreset_index]
786                 lastname=entry.name
787                 
788                 if entry.count==0: 
789                     col.prop(entry, "name", text="")
790                 if entry.count> 0: 
791                     col.prop(entry, "name", text="")
792                 if scn.objects.active != None:
793                     name=scn.objects.active.get("gllightpreset", "Default")
794 #Draw the import/export part of the box                
795                 col.prop(scn,'importexport')
796                 if scn.importexport:
797                     split = box.split(percentage=0.5)
798                     col = split.column()
799                     col.label("Import Directory or File")
800                     col.prop(scn, 'gllightpreset_importfile')
801                     col.prop(scn, 'gllightpreset_importdirectory')
802                     col.label("Export Directory or File")
803                     col.prop(scn, 'gllightpreset_exportfile')
804                     col.prop(scn, 'gllightpreset_exportdirectory')
805                                 
806                     split = split.split()
807                     col = split.column()
808                     
809                     col.label("")
810                     col.operator("gllightpreset.action", icon="IMPORT", text="Import File").button="import"
811                     col.operator("gllightpreset.action", icon="IMPORT", text="Import All").button="importall"  
812                     col.label("")
813                     col.operator("gllightpreset.action", icon="EXPORT", text="Export Selection").button="export"
814                     col.operator("gllightpreset.action", icon="EXPORT", text="Export All").button="exportall"
815                     
816                     
817 #The second box containing interface options is here
818         #interface options   
819         box = layout.box().column(align=True)
820         box.prop(scn,'interface')
821         
822         
823         if scn.interface:
824             #Select with
825             boxrow=box.row()
826             boxrow.label(text="Select With:")
827             boxrow=box.row()
828             boxrow.prop(inputs, "select_mouse", expand=True)
829
830             #Orbit
831             boxrow=box.row()
832             boxrow.label(text="Orbit Style:")
833             boxrow=box.row()
834             boxrow.prop(inputs, "view_rotate_method", expand=True)
835         
836             #continuous grab
837             boxrow=box.row()
838             boxrow.prop(inputs, "use_mouse_continuous")
839
840             #Color Picker
841             boxrow=box.row()
842             boxrow.label(text="Color Picker Type")
843             boxrow=box.row()
844             boxrow.row().prop(system, "color_picker_type", text="")
845
846             #Align To
847             boxrow=box.row()
848             boxrow.label(text="Align New Objects To:")
849             boxrow=box.row()
850             boxrow.prop(edit, "object_align", text="")
851          
852 ##########################################
853 ####### Registration Functions ############
854 ##########################################
855 def register():
856     #aliases
857     scn=bpy.types.Scene
858     handler=bpy.app.handlers
859
860     #register classes
861     bpy.utils.register_class(PANEL)
862     bpy.utils.register_class(gllightpreset)
863     bpy.utils.register_class(SCENE_OT_gllightpreset)
864         
865     #strings for file IO
866     scn.gllightpreset_importfile = bpy.props.StringProperty(name = "", subtype='FILE_PATH', default=defaultfilepath)
867     scn.gllightpreset_importdirectory = bpy.props.StringProperty(name = "", subtype='DIR_PATH', default=defaultfilepath)
868     scn.gllightpreset_exportfile = bpy.props.StringProperty(name = "", subtype='FILE_PATH', default=defaultfilepath)    
869     scn.gllightpreset_exportdirectory = bpy.props.StringProperty(name = "", subtype='DIR_PATH', default=defaultfilepath)
870
871     #Set up values for UI stuff
872     scn.lights = bpy.props.BoolProperty(name='Lights', default=True)
873     scn.lightPresets = bpy.props.BoolProperty(name='Light Presets', default=False)
874     scn.interface = bpy.props.BoolProperty(name='Interface', default=False)
875     scn.importexport = bpy.props.BoolProperty(name='Show Import/Export Options', default=False)    
876     bpy.types.Object.gllightpreset = bpy.props.StringProperty()
877    
878     #important storage of stuff
879     scn.gllightpreset = bpy.props.CollectionProperty(type=gllightpreset)
880     scn.gllightpreset_index = bpy.props.IntProperty(min=0, default=0, update=gllightpreset_index)
881     
882
883     #handlers    
884     #handler.load_post.append(gllightpreset_chooseLoadLocation)     #was crashing blender on new file load - comment for now
885     handler.scene_update_pre.append(gllightpreset_scan)
886
887     #let the fun begin!
888     gllightpreset_chooseLoadLocation(1)
889     
890            
891 # unregistering and removing menus
892 def unregister():
893     bpy.utils.unregister_class(PANEL)
894     bpy.utils.unregister_class(gllightpreset)
895     bpy.utils.unregister_class(SCENE_OT_gllightpreset)
896
897
898 if __name__ == "__main__":
899     register()
900