Welcome to mirror list, hosted at ThFree Co, Russian Federation.

flt_palettemanager.py « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c641a0a4f08a27c525750196243140d53e4979ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!BPY

"""
Name: 'FLT Palette Manager'
Blender: 240
Group: 'Misc'
Tooltip: 'Manage FLT colors'
"""

__author__ = "Geoffrey Bantle"
__version__ = "1.0 11/21/2007"
__email__ = ('scripts', 'Author, ')
__url__ = ('blender', 'elysiun')

__bpydoc__ ="""\

This script manages colors in OpenFlight databases. OpenFlight is a
registered trademark of MultiGen-Paradigm, Inc.

Todo:
-Figure out whats causing the PC speaker to beep when initializing...

Feature overview and more availible at:
http://wiki.blender.org/index.php/Scripts/Manual/FLTools
"""

# --------------------------------------------------------------------------
# flt_palettemanager.py version 0.1 2005/04/08
# --------------------------------------------------------------------------
# ***** BEGIN GPL LICENSE BLOCK *****
#
# Copyright (C) 2007: Blender Foundation
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------

import Blender.Draw as Draw
from Blender.BGL import *
import Blender
import flt_properties
import flt_defaultp as defaultp
from flt_properties import *


palette_size = 12
palette_x = 0
palette_y = 0

colors = list()
curint = 1.0
curswatch = 0
#make a default palette, not very useful.
cinc = 1.0 / 1024.0
cstep = 0.0
picker = None
ptt = ""
for i in xrange(1024):
	colors.append([cstep,cstep,cstep])
	cstep = cstep + cinc
def update_state():
	state = dict()
	state["activeScene"] = Blender.Scene.getCurrent()
	state["activeObject"] = state["activeScene"].getActiveObject()
	state["activeMesh"] = None
	if state["activeObject"] and state["activeObject"].type == 'Mesh':
		state["activeMesh"] = state["activeObject"].getData(mesh=True)
	
	state["activeFace"] = None
	if state["activeMesh"]:
		if state["activeMesh"].faceUV and state["activeMesh"].activeFace != None:
			state["activeFace"] = state["activeMesh"].faces[state["activeMesh"].activeFace]
		
	return state
	
def pack_face_index(index, intensity):
	return ((127*intensity)+(128*index))
def unpack_face_index(face_index):
	index = face_index / 128
	intensity = float(face_index - 128.0 * index) / 127.0
	return(index,intensity)

def event(evt,val):
	global palette_size
	global palette_x
	global palette_y
	global colors
	global curint
	global curswatch
	
	areas = Blender.Window.GetScreenInfo()
	curarea = Blender.Window.GetAreaID()
	curRect = None
	editmode = 0

	for area in areas:
		if area['id'] == curarea:
			curRect = area['vertices']
			break
		
	if evt == Draw.LEFTMOUSE:
		mval = Blender.Window.GetMouseCoords()
		rastx = mval[0] - curRect[0]
		rasty = mval[1] - curRect[1]
		
		swatchx = (rastx -palette_x) / palette_size #+state["palette_x"]
		swatchy = (rasty -palette_y) / palette_size #+state["palette_y"]
                if rastx > palette_x and rastx < (palette_x + palette_size * 32) and rasty > palette_y and rasty < (palette_y+ palette_size* 32):
                        if swatchx < 32 and swatchy < 32:
                                curswatch = (swatchx * 32) + swatchy
                                Draw.Redraw(1)
		
                elif swatchy < 34 and swatchx < 32:
                        curint = 1.0 - (float(rastx-palette_x)/(palette_size*32.0))
                        Draw.Redraw(1)
	
	#copy current color and intensity to selected faces.
	elif evt == Draw.CKEY:
		
		if Blender.Window.EditMode():
			Blender.Window.EditMode(0)
			editmode = 1
		state = update_state()
		
		#retrieve color from palette
		color = struct.unpack('>BBBB',struct.pack('>I',colors[curswatch]))
		actmesh = state["activeMesh"]
		if actmesh: 
			if(Blender.Window.GetKeyQualifiers() != Blender.Window.Qual["CTRL"]):
				selfaces = list()
				for face in actmesh.faces:
					if face.sel:
						selfaces.append(face)
				
				if not "FLT_COL" in actmesh.faces.properties:
					actmesh.faces.addPropertyLayer("FLT_COL",Blender.Mesh.PropertyTypes["INT"])
					for face in actmesh.faces:
						face.setProperty("FLT_COL",127) #default
				try:
					actmesh.activeColorLayer = "FLT_Fcol"
				except:
					actmesh.addColorLayer("FLT_Fcol")
					actmesh.activeColorLayer = "FLT_Fcol"
				

				for face in selfaces:
					#First append packed index + color and store in face property
					face.setProperty("FLT_COL",int(pack_face_index(curswatch,curint))) 
					#Save baked color to face vertex colors
					for col in face.col:
						col.r = int(color[0] * curint)
						col.g = int(color[1] * curint)
						col.b = int(color[2] * curint)
						col.a = int(color[3] * curint)
			else:
				if Blender.Mesh.Mode() == Blender.Mesh.SelectModes['VERTEX']:
					if not 'FLT_VCOL' in actmesh.verts.properties:
						actmesh.verts.addPropertyLayer("FLT_VCOL",Blender.Mesh.PropertyTypes["INT"])
						for vert in actmesh.verts:
							vert.setProperty("FLT_VCOL",127)
					else:
						for vert in actmesh.verts:
							if vert.sel:
								vert.setProperty("FLT_VCOL",int(pack_face_index(curswatch,curint)))
			
			if editmode:
				Blender.Window.EditMode(1)
			
			Blender.Window.RedrawAll()
	
	#grab color and intensity from active face
	elif evt == Draw.VKEY:
		if Blender.Window.EditMode():
			Blender.Window.EditMode(0)
			editmode = 1
		state = update_state()
		
		actmesh = state["activeMesh"]
		activeFace = state["activeFace"]
		
		
		if activeFace:
			if not "FLT_COL" in actmesh.faces.properties:
				actmesh.faces.addPropertyLayer("FLT_COL",Blender.Mesh.PropertyTypes["INT"])
				for face in actmesh.faces:
					face.setProperty("FLT_COL",127) #default
			try:
				actmesh.activeColorLayer = "FLT_Fcol"
			except:
				actmesh.addColorLayer("FLT_Fcol")
				actmesh.activeColorLayer = "FLT_Fcol"
			tcol = activeFace.getProperty("FLT_COL")
			(index,intensity) = unpack_face_index(tcol)
			curswatch = index
			curint = intensity
			
		if editmode:
			Blender.Window.EditMode(1)
		
		Blender.Window.RedrawAll()
			
	elif evt == Draw.ESCKEY:
		Draw.Exit()
	
	if editmode:
		Blender.Window.EditMode(1)

def update_all():
	global colors
	state = update_state()
	#update the baked FLT colors for all meshes.
	for object in state["activeScene"].objects:
		if object.type == "Mesh":
			mesh = object.getData(mesh=True)
			if 'FLT_COL' in mesh.faces.properties:
				mesh.activeColorLayer = "FLT_Fcol"
				for face in mesh.faces:
					(index,intensity) = unpack_face_index(face.getProperty('FLT_COL'))
					color = struct.unpack('>BBBB',struct.pack('>I',colors[index]))
					#update the vertex colors for this face
					for col in face.col:
						col.r = int(color[0] * intensity)
						col.g = int(color[1] * intensity)
						col.b = int(color[2] * intensity)
						col.a = 255


def but_event(evt):
	global palette_size
	global palette_x
	global palette_y
	global colors
	global curint
	global curswatch
	global picker
	state = update_state()

	if evt == 1:
 		if picker.val:
			rval = (int(picker.val[0]*255),int(picker.val[1]*255),int(picker.val[2]*255),255)
			rval = struct.pack('>BBBB',rval[0],rval[1],rval[2],rval[3])
			rval = struct.unpack('>i',rval)
			colors[curswatch] = rval[0]	
			#go cd through all meshes and update their FLT colors
			update_all()

	Draw.Redraw(1)
def init_pal():
	global palette_size
	global palette_x
	global palette_y
	global colors
	global curint
	global curswatch
	
	state = update_state()

	if not state["activeScene"].properties.has_key('FLT'):
		state["activeScene"].properties['FLT'] = dict()

	try:
		colors = state["activeScene"].properties['FLT']['Color Palette']
	except:
		state["activeScene"].properties['FLT']['Color Palette'] = defaultp.pal
		colors = state["activeScene"].properties['FLT']['Color Palette']

def draw_palette():
	global palette_size
	global palette_x
	global palette_y
	global colors
	global curint
	global curswatch
        global picker

	state = update_state()
	init_pal()

	ssize = palette_size
	xpos = palette_x
	cid = 0

	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)]
	for x in xrange(32):
		ypos = palette_y
		for y in xrange(32):
			color = struct.unpack('>BBBB',struct.pack('>I',colors[cid]))
			glColor3f(color[0]/255.0,color[1]/255.0,color[2]/255.0)
			glBegin(GL_POLYGON)
			glVertex2i(xpos,ypos)
			glVertex2i(xpos+ssize,ypos)
			glVertex2i(xpos+ssize,ypos+ssize)
			glVertex2i(xpos,ypos+ssize)				
			glEnd()
			
			if curswatch == cid:
				highlight[0] = (xpos,ypos)
				highlight[1] = (xpos+ssize,ypos)
				highlight[2] = (xpos+ssize,ypos+ssize)
				highlight[3] = (xpos,ypos+ssize)
		
			glColor3f(0.0,0.0,0.0)
			glBegin(GL_LINE_LOOP)
			glVertex2i(xpos,ypos)
			glVertex2i(xpos+ssize,ypos)
			glVertex2i(xpos+ssize,ypos+ssize)
			glVertex2i(xpos,ypos+ssize)
			glVertex2i(xpos,ypos)				
			glEnd()			
				
			
			cid = cid + 1
			ypos = ypos + ssize
		
		xpos = xpos + ssize
	
	#draw intensity gradient
	color = struct.unpack('>BBBB',struct.pack('>I',colors[curswatch]))
	color = [color[0]/255.0,color[1]/255.0,color[2]/255.0]
	colsteps = [color[0]/255.0,color[1]/255.0,color[2]/255.0]
	stripwidth = (palette_size * 32.0) / 256
	strippad = palette_size / 2.0
	
	xpos = palette_x
	grady = (palette_y + (palette_size * 32.0)) + strippad
	for x in xrange(256):
		color[0] = color[0] - colsteps[0]
		color[1] = color[1] - colsteps[1] 
		color[2] = color[2] - colsteps[2]

		glColor3f(color[0], color[1] ,color[2])
		glBegin(GL_POLYGON)
		glVertex2f(xpos,grady)
		glVertex2f(xpos+stripwidth,grady)
		glVertex2f(xpos+stripwidth,grady+palette_size)
		glVertex2f(xpos,grady+palette_size)
		glEnd()
		xpos = xpos + stripwidth
		
	#draw intensity slider bar
	#xposition ==  512 - ((curint) * 512)
	xpos = ((palette_size*32) * (1.0 - curint)) + palette_x
	glColor3f(1.0,1.0,1.0)
	glBegin(GL_LINE_LOOP)
	glVertex2i(xpos-6,grady-1)
	glVertex2i(xpos+6,grady-1)
	glVertex2i(xpos+6,grady+palette_size+1)
	glVertex2i(xpos-6,grady+palette_size+1)
	#glVertex2i(xpos-6,grady+7)
	glEnd()

	#draw color picker
	color = struct.unpack('>BBBB',struct.pack('>I',colors[curswatch]))
	pickcol = (color[0]/255.0,color[1]/255.0,color[2]/255.0)
	picker = Blender.Draw.ColorPicker(1,highlight[0][0]+1,highlight[0][1]+1,ssize-2,ssize-2,pickcol,ptt)

	#draw highlight swatch
	glColor3f(1.0,1.0,1.0)
	glBegin(GL_LINE_LOOP)
	glVertex2i(highlight[0][0],highlight[0][1])
	glVertex2i(highlight[1][0],highlight[1][1])
	glVertex2i(highlight[2][0],highlight[2][1])
	glVertex2i(highlight[3][0],highlight[3][1])
	glVertex2i(highlight[0][0],highlight[0][1])
	glEnd()			

def gui():
	glClearColor(0.5,0.5,0.5,1.0)
	glClear(GL_COLOR_BUFFER_BIT)
	draw_palette()


init_pal()
Draw.Register(gui,event,but_event)