6edaf2974ab04c3227e9d949163e509a5715884a
[blender.git] / release / scripts / flt_palettemanager.py
1 #!BPY
2
3 """
4 Name: 'FLT Palette Manager'
5 Blender: 240
6 Group: 'Misc'
7 Tooltip: 'Manage FLT colors'
8 """
9
10 __author__ = "Geoffrey Bantle"
11 __version__ = "1.0 11/21/2007"
12 __email__ = ('scripts', 'Author, ')
13 __url__ = ('blender', 'blenderartists.org')
14
15 __bpydoc__ ="""\
16
17 This script manages colors in OpenFlight databases. OpenFlight is a
18 registered trademark of MultiGen-Paradigm, Inc.
19
20 Todo:
21 -Figure out whats causing the PC speaker to beep when initializing...
22
23 Feature overview and more availible at:
24 http://wiki.blender.org/index.php/Scripts/Manual/FLTools
25 """
26
27 # --------------------------------------------------------------------------
28 # flt_palettemanager.py version 0.1 2005/04/08
29 # --------------------------------------------------------------------------
30 # ***** BEGIN GPL LICENSE BLOCK *****
31 #
32 # Copyright (C) 2007: Blender Foundation
33 #
34 # This program is free software; you can redistribute it and/or
35 # modify it under the terms of the GNU General Public License
36 # as published by the Free Software Foundation; either version 2
37 # of the License, or (at your option) any later version.
38 #
39 # This program is distributed in the hope that it will be useful,
40 # but WITHOUT ANY WARRANTY; without even the implied warranty of
41 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42 # GNU General Public License for more details.
43 #
44 # You should have received a copy of the GNU General Public License
45 # along with this program; if not, write to the Free Software Foundation,
46 # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
47 #
48 # ***** END GPL LICENCE BLOCK *****
49 # --------------------------------------------------------------------------
50
51 import Blender.Draw as Draw
52 from Blender.BGL import *
53 import Blender
54 import flt_properties
55 import flt_defaultp as defaultp
56 from flt_properties import *
57
58
59 palette_size = 12
60 palette_x = 0
61 palette_y = 0
62
63 colors = list()
64 curint = 1.0
65 curswatch = 0
66 #make a default palette, not very useful.
67 cinc = 1.0 / 1024.0
68 cstep = 0.0
69 picker = None
70 ptt = ""
71 for i in xrange(1024):
72         colors.append([cstep,cstep,cstep])
73         cstep = cstep + cinc
74 def update_state():
75         state = dict()
76         state["activeScene"] = Blender.Scene.getCurrent()
77         state["activeObject"] = state["activeScene"].getActiveObject()
78         state["activeMesh"] = None
79         if state["activeObject"] and state["activeObject"].type == 'Mesh':
80                 state["activeMesh"] = state["activeObject"].getData(mesh=True)
81         
82         state["activeFace"] = None
83         if state["activeMesh"]:
84                 if state["activeMesh"].faceUV and state["activeMesh"].activeFace != None:
85                         state["activeFace"] = state["activeMesh"].faces[state["activeMesh"].activeFace]
86                 
87         return state
88         
89 def pack_face_index(index, intensity):
90         return ((127*intensity)+(128*index))
91 def unpack_face_index(face_index):
92         index = face_index / 128
93         intensity = float(face_index - 128.0 * index) / 127.0
94         return(index,intensity)
95
96 def event(evt,val):
97         global palette_size
98         global palette_x
99         global palette_y
100         global colors
101         global curint
102         global curswatch
103         
104         areas = Blender.Window.GetScreenInfo()
105         curarea = Blender.Window.GetAreaID()
106         curRect = None
107         editmode = 0
108
109         for area in areas:
110                 if area['id'] == curarea:
111                         curRect = area['vertices']
112                         break
113                 
114         if evt == Draw.LEFTMOUSE:
115                 mval = Blender.Window.GetMouseCoords()
116                 rastx = mval[0] - curRect[0]
117                 rasty = mval[1] - curRect[1]
118                 
119                 swatchx = (rastx -palette_x) / palette_size #+state["palette_x"]
120                 swatchy = (rasty -palette_y) / palette_size #+state["palette_y"]
121                 if rastx > palette_x and rastx < (palette_x + palette_size * 32) and rasty > palette_y and rasty < (palette_y+ palette_size* 32):
122                         if swatchx < 32 and swatchy < 32:
123                                 curswatch = (swatchx * 32) + swatchy
124                                 Draw.Redraw(1)
125                 
126                 elif swatchy < 34 and swatchx < 32:
127                         curint = 1.0 - (float(rastx-palette_x)/(palette_size*32.0))
128                         Draw.Redraw(1)
129         
130         #copy current color and intensity to selected faces.
131         elif evt == Draw.CKEY:
132                 
133                 if Blender.Window.EditMode():
134                         Blender.Window.EditMode(0)
135                         editmode = 1
136                 state = update_state()
137                 
138                 #retrieve color from palette
139                 color = struct.unpack('>BBBB',struct.pack('>I',colors[curswatch]))
140                 actmesh = state["activeMesh"]
141                 if actmesh: 
142                         if(Blender.Window.GetKeyQualifiers() != Blender.Window.Qual["CTRL"]):
143                                 selfaces = list()
144                                 for face in actmesh.faces:
145                                         if face.sel:
146                                                 selfaces.append(face)
147                                 
148                                 if not "FLT_COL" in actmesh.faces.properties:
149                                         actmesh.faces.addPropertyLayer("FLT_COL",Blender.Mesh.PropertyTypes["INT"])
150                                         for face in actmesh.faces:
151                                                 face.setProperty("FLT_COL",127) #default
152                                 try:
153                                         actmesh.activeColorLayer = "FLT_Fcol"
154                                 except:
155                                         actmesh.addColorLayer("FLT_Fcol")
156                                         actmesh.activeColorLayer = "FLT_Fcol"
157                                 
158
159                                 for face in selfaces:
160                                         #First append packed index + color and store in face property
161                                         face.setProperty("FLT_COL",int(pack_face_index(curswatch,curint))) 
162                                         #Save baked color to face vertex colors
163                                         for col in face.col:
164                                                 col.r = int(color[0] * curint)
165                                                 col.g = int(color[1] * curint)
166                                                 col.b = int(color[2] * curint)
167                                                 col.a = int(color[3] * curint)
168                         else:
169                                 if Blender.Mesh.Mode() == Blender.Mesh.SelectModes['VERTEX']:
170                                         if not 'FLT_VCOL' in actmesh.verts.properties:
171                                                 actmesh.verts.addPropertyLayer("FLT_VCOL",Blender.Mesh.PropertyTypes["INT"])
172                                                 for vert in actmesh.verts:
173                                                         vert.setProperty("FLT_VCOL",127)
174                                         else:
175                                                 for vert in actmesh.verts:
176                                                         if vert.sel:
177                                                                 vert.setProperty("FLT_VCOL",int(pack_face_index(curswatch,curint)))
178                         
179                         if editmode:
180                                 Blender.Window.EditMode(1)
181                         
182                         Blender.Window.RedrawAll()
183         
184         #grab color and intensity from active face
185         elif evt == Draw.VKEY:
186                 if Blender.Window.EditMode():
187                         Blender.Window.EditMode(0)
188                         editmode = 1
189                 state = update_state()
190                 
191                 actmesh = state["activeMesh"]
192                 activeFace = state["activeFace"]
193                 
194                 
195                 if activeFace:
196                         if not "FLT_COL" in actmesh.faces.properties:
197                                 actmesh.faces.addPropertyLayer("FLT_COL",Blender.Mesh.PropertyTypes["INT"])
198                                 for face in actmesh.faces:
199                                         face.setProperty("FLT_COL",127) #default
200                         try:
201                                 actmesh.activeColorLayer = "FLT_Fcol"
202                         except:
203                                 actmesh.addColorLayer("FLT_Fcol")
204                                 actmesh.activeColorLayer = "FLT_Fcol"
205                         tcol = activeFace.getProperty("FLT_COL")
206                         (index,intensity) = unpack_face_index(tcol)
207                         curswatch = index
208                         curint = intensity
209                         
210                 if editmode:
211                         Blender.Window.EditMode(1)
212                 
213                 Blender.Window.RedrawAll()
214                         
215         elif evt == Draw.ESCKEY:
216                 Draw.Exit()
217         
218         if editmode:
219                 Blender.Window.EditMode(1)
220
221 def update_all():
222         global colors
223         state = update_state()
224         #update the baked FLT colors for all meshes.
225         for object in state["activeScene"].objects:
226                 if object.type == "Mesh":
227                         mesh = object.getData(mesh=True)
228                         if 'FLT_COL' in mesh.faces.properties:
229                                 mesh.activeColorLayer = "FLT_Fcol"
230                                 for face in mesh.faces:
231                                         (index,intensity) = unpack_face_index(face.getProperty('FLT_COL'))
232                                         color = struct.unpack('>BBBB',struct.pack('>I',colors[index]))
233                                         #update the vertex colors for this face
234                                         for col in face.col:
235                                                 col.r = int(color[0] * intensity)
236                                                 col.g = int(color[1] * intensity)
237                                                 col.b = int(color[2] * intensity)
238                                                 col.a = 255
239
240
241 def but_event(evt):
242         global palette_size
243         global palette_x
244         global palette_y
245         global colors
246         global curint
247         global curswatch
248         global picker
249         state = update_state()
250
251         if evt == 1:
252                 if picker.val:
253                         rval = (int(picker.val[0]*255),int(picker.val[1]*255),int(picker.val[2]*255),255)
254                         rval = struct.pack('>BBBB',rval[0],rval[1],rval[2],rval[3])
255                         rval = struct.unpack('>i',rval)
256                         colors[curswatch] = rval[0]     
257                         #go cd through all meshes and update their FLT colors
258                         update_all()
259
260         Draw.Redraw(1)
261 def init_pal():
262         global palette_size
263         global palette_x
264         global palette_y
265         global colors
266         global curint
267         global curswatch
268         
269         state = update_state()
270
271         if not state["activeScene"].properties.has_key('FLT'):
272                 state["activeScene"].properties['FLT'] = dict()
273
274         try:
275                 colors = state["activeScene"].properties['FLT']['Color Palette']
276         except:
277                 state["activeScene"].properties['FLT']['Color Palette'] = defaultp.pal
278                 colors = state["activeScene"].properties['FLT']['Color Palette']
279
280 def draw_palette():
281         global palette_size
282         global palette_x
283         global palette_y
284         global colors
285         global curint
286         global curswatch
287         global picker
288
289         state = update_state()
290         init_pal()
291
292         ssize = palette_size
293         xpos = palette_x
294         cid = 0
295
296         highlight = [(palette_x,palette_y),(palette_x+palette_size,palette_y),(palette_x+palette_size,palette_y+palette_size),(palette_x,palette_y+palette_size)]
297         for x in xrange(32):
298                 ypos = palette_y
299                 for y in xrange(32):
300                         color = struct.unpack('>BBBB',struct.pack('>I',colors[cid]))
301                         glColor3f(color[0]/255.0,color[1]/255.0,color[2]/255.0)
302                         glBegin(GL_POLYGON)
303                         glVertex2i(xpos,ypos)
304                         glVertex2i(xpos+ssize,ypos)
305                         glVertex2i(xpos+ssize,ypos+ssize)
306                         glVertex2i(xpos,ypos+ssize)                             
307                         glEnd()
308                         
309                         if curswatch == cid:
310                                 highlight[0] = (xpos,ypos)
311                                 highlight[1] = (xpos+ssize,ypos)
312                                 highlight[2] = (xpos+ssize,ypos+ssize)
313                                 highlight[3] = (xpos,ypos+ssize)
314                 
315                         glColor3f(0.0,0.0,0.0)
316                         glBegin(GL_LINE_LOOP)
317                         glVertex2i(xpos,ypos)
318                         glVertex2i(xpos+ssize,ypos)
319                         glVertex2i(xpos+ssize,ypos+ssize)
320                         glVertex2i(xpos,ypos+ssize)
321                         glVertex2i(xpos,ypos)                           
322                         glEnd()                 
323                                 
324                         
325                         cid = cid + 1
326                         ypos = ypos + ssize
327                 
328                 xpos = xpos + ssize
329         
330         #draw intensity gradient
331         color = struct.unpack('>BBBB',struct.pack('>I',colors[curswatch]))
332         color = [color[0]/255.0,color[1]/255.0,color[2]/255.0]
333         colsteps = [color[0]/255.0,color[1]/255.0,color[2]/255.0]
334         stripwidth = (palette_size * 32.0) / 256
335         strippad = palette_size / 2.0
336         
337         xpos = palette_x
338         grady = (palette_y + (palette_size * 32.0)) + strippad
339         for x in xrange(256):
340                 color[0] = color[0] - colsteps[0]
341                 color[1] = color[1] - colsteps[1] 
342                 color[2] = color[2] - colsteps[2]
343
344                 glColor3f(color[0], color[1] ,color[2])
345                 glBegin(GL_POLYGON)
346                 glVertex2f(xpos,grady)
347                 glVertex2f(xpos+stripwidth,grady)
348                 glVertex2f(xpos+stripwidth,grady+palette_size)
349                 glVertex2f(xpos,grady+palette_size)
350                 glEnd()
351                 xpos = xpos + stripwidth
352                 
353         #draw intensity slider bar
354         #xposition ==  512 - ((curint) * 512)
355         xpos = ((palette_size*32) * (1.0 - curint)) + palette_x
356         glColor3f(1.0,1.0,1.0)
357         glBegin(GL_LINE_LOOP)
358         glVertex2i(xpos-6,grady-1)
359         glVertex2i(xpos+6,grady-1)
360         glVertex2i(xpos+6,grady+palette_size+1)
361         glVertex2i(xpos-6,grady+palette_size+1)
362         #glVertex2i(xpos-6,grady+7)
363         glEnd()
364
365         #draw color picker
366         color = struct.unpack('>BBBB',struct.pack('>I',colors[curswatch]))
367         pickcol = (color[0]/255.0,color[1]/255.0,color[2]/255.0)
368         picker = Blender.Draw.ColorPicker(1,highlight[0][0]+1,highlight[0][1]+1,ssize-2,ssize-2,pickcol,ptt)
369
370         #draw highlight swatch
371         glColor3f(1.0,1.0,1.0)
372         glBegin(GL_LINE_LOOP)
373         glVertex2i(highlight[0][0],highlight[0][1])
374         glVertex2i(highlight[1][0],highlight[1][1])
375         glVertex2i(highlight[2][0],highlight[2][1])
376         glVertex2i(highlight[3][0],highlight[3][1])
377         glVertex2i(highlight[0][0],highlight[0][1])
378         glEnd()                 
379
380 def gui():
381         glClearColor(0.5,0.5,0.5,1.0)
382         glClear(GL_COLOR_BUFFER_BIT)
383         draw_palette()
384
385
386 init_pal()
387 Draw.Register(gui,event,but_event)
388