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