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

flt_toolbar.py « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a707b87f84658955cc47678cdec1f330da7cc349 (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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
#!BPY

"""
Name: 'FLT Toolbar'
Blender: 240
Group: 'Misc'
Tooltip: 'Tools for working with FLT databases'
"""

__author__ = "Geoffrey Bantle"
__version__ = "1.0 11/21/07"
__email__ = ('scripts', 'Author, ')
__url__ = ('blender', 'blenderartists.org')

__bpydoc__ ="""\
This script provides tools for working with OpenFlight databases in Blender. OpenFlight is a
registered trademark of MultiGen-Paradigm, Inc.

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
reload(flt_properties)
from flt_properties import *

xrefprefix = ""
xrefstack = list()
vofsstack = list()
vquatstack = list()
prop_w = 256
prop_h = 256


#event codes
evcode = {
	"XREF_MAKE" : 100,
	"XREF_EDIT" : 101,
	"XREF_FILE" : 102,
	"XREF_PICK" : 103,
	"XREF_SELECT" : 104,
        "XREF_POP" : 105,
        "XREF_PREFIX" : 106,
	"FACE_NAME" : 200,
	"FACE_MAKESUB" : 201,
	"FACE_KILLSUB" : 202,
	"FACE_SELSUB" : 203,
	"SCENE_UPDATE" : 303,
	"IDPROP_COPY" : 501,
	"IDPROP_KILL" : 502,
	"CLIGHT_MAKE" : 700,
	"DFROMACT" : 701,
	"FIXCOL" : 702
}

XREF_PREFIX = None
XREF_MAKE = None
XREF_EDIT = None
XREF_SELECT = None
XREF_POP = None
FACE_MAKESUB = None
FACE_SELSUB = None
FACE_KILLSUB = None
IDPROP_KILL = None
IDPROP_COPY = None
SCENE_UPDATE = None
CLIGHT_MAKE = None
DFROMACT = None
FIXCOL = None


def RGBtoHSV( r, g, b):
	cmin = min( r, g, b )
	cmax = max( r, g, b )
	v = cmax				

	if(cmax!=0.0):
		s = (cmax-cmin)/cmax
	else:
		s = 0.0
		h = 0.0
	
	if(s == 0.0):
		h = -1.0
	else:
		cdelta = cmax-cmin
		rc = (cmax-r)/cdelta
		gc = (cmax-g)/cdelta
		bc = (cmax-b)/cdelta
		if(r==cmax):
			h = bc-gc
		else:
			if(g==cmax):
				h = 2.0+rc-bc
			else:
				h = 4.0+gc-rc
		h = h*60.0
		if(h<0.0):
			h += 360.0
			
		
	h = h/360.0
	if(h < 0.0): 
		h = 0.0
	return (h,s,v)


def update_state():
	state = dict()
	state["activeScene"] = Blender.Scene.GetCurrent()
	state["activeObject"] = state["activeScene"].objects.active
	if state["activeObject"] and not state["activeObject"].sel:
		state["activeObject"] = None
	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]
		
	#update editmode
	state["editmode"]	= Blender.Window.EditMode()

	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 idprops_append(object, typecode, props):
	object.properties["FLT"] = dict()
	object.properties["FLT"]['type'] = typecode 
	for prop in props:
		object.properties["FLT"][prop] = props[prop]
	object.properties["FLT"]['3t8!id'] = object.name

def idprops_kill(object):
	state = update_state()
	if object and object.properties.has_key('FLT'):
		object.properties.pop('FLT')

def idprops_copy(source):
	state = update_state()
	if source.properties.has_key('FLT'):
		for object in state["activeScene"].objects:
			if object.sel and object != source and (state["activeScene"].Layers & object.Layers):
				idprops_kill(object)
				object.properties['FLT'] = dict()
				for key in source.properties['FLT']:
					object.properties['FLT'][key] = source.properties['FLT'][key]

def unpack_color(color):
	return struct.unpack('>BBBB',struct.pack('>I',color))


def findColorKey(colordict, hsv):
	hdelta = 0.001
	for key in colordict:
		if not (((hsv[0] < (key[0] + hdelta)) and (hsv[0] > (key[0] - hdelta))) and ((hsv[1] < (key[1] + hdelta)) and (hsv[1] > (key[1] - hdelta)))):
			return key
	return None

def hsvsort(a, b):
	(index1, mag1) = a
	(index2, mag2) = b
	if mag1 > mag2:
		return 1
	elif mag1 < mag2:
		return -1
	return 0

def fix_colors():
	
	editmode = 0
	if Blender.Window.EditMode():
		Blender.Window.EditMode(0)
		editmode = 1
	state = update_state()
	
	scene = state["activeScene"]
	colors = None
	if state["activeScene"].properties.has_key('FLT'):
		try:
			colors = state["activeScene"].properties['FLT']['Color Palette']
		except:
			pass
	if not colors:
		return
	
	#first build a HSV version of our palette
	hsvpalette = list()
	for swatch in colors:
		color = unpack_color(swatch)
		hsv = RGBtoHSV(color[0] / 255.0, color[1] / 255.0, color[2] / 255.0)
		hsvpalette.append(hsv)
		
	#collect all of our meshes
	meshes = list()
	for object in scene.objects.context:
		if object.sel and object.type == 'Mesh':
			mesh = object.getData(mesh=True)
			if "FLT_COL" in mesh.faces.properties:
				meshes.append(mesh)
				
				
	#Now go through our meshes, and build a dictionary of face lists keyed according to (hue,saturation) of the baked color
	colordict = dict()
	for mesh in meshes:
		for face in mesh.faces:
			hsv = RGBtoHSV(face.col[0].r/255.0, face.col[0].g/255.0, face.col[0].b/255.0) #retrieve baked color
			if colordict.has_key((hsv[0],hsv[1])):
				colordict[(hsv[0],hsv[1])].append(face)
			else:
				colordict[(hsv[0],hsv[1])] = [face]				
	

	#for each color key in the color dict, build a list of distances from it to the values in hsvpalette and then quicksort them for closest match
	for key in colordict:
		maglist = list()
		for i, hsv in enumerate(hsvpalette):
			norm = Blender.Mathutils.Vector(hsv[0], hsv[1]) - Blender.Mathutils.Vector(key[0],key[1])
			maglist.append((i,norm.length))
		maglist.sort(hsvsort)
		print maglist[0]
		for face in colordict[key]:
			(index, intensity) = unpack_face_index(face.getProperty("FLT_COL"))
			newfindex = pack_face_index(maglist[0][0],intensity)
			face.setProperty("FLT_COL", int(newfindex))
	
	for mesh in meshes:
		update_mesh_colors(colors,mesh)	
	
	if editmode:
		Blender.Window.EditMode(1)	
	

def update_mesh_colors(colors, mesh):
	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]))
			
			if index == 0 and intensity == 0:
				color = (255,255,255)
				intensity  = 1.0
			#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 update_all():
	
	editmode = 0
	if Blender.Window.EditMode():
		Blender.Window.EditMode(0)
		editmode = 1
	state = update_state()
	colors = None
	if state["activeScene"].properties.has_key('FLT'):
		try:
			colors = state["activeScene"].properties['FLT']['Color Palette']
		except:
			pass
	if colors:
		#update the baked FLT colors for all meshes.
		for object in state["activeScene"].objects:
			if object.type == "Mesh":
				mesh = object.getData(mesh=True)
				update_mesh_colors(colors,mesh)
	if editmode:
		Blender.Window.EditMode(1)

#Change this to find the deep parent
def xref_create():
	global xrefprefix
	global xrefstack
	global vofsstack
	global vquatstack
	global prop_w
	global prop_h
	
	state = update_state()
	
	def findchildren(object):
		children = list()
		for candidate in state["activeScene"].objects:
			if candidate.parent == object:
				children.append(candidate)
		retlist = list(children)
		for child in children:
			retlist = retlist + findchildren(child)
		return retlist
		
	actObject = state["activeObject"]
        if actObject and xrefprefix:
                scenenames = list()
                for scene in Blender.Scene.Get():
                        scenenames.append(scene.name)

                if xrefprefix in scenenames:
                        #build a unique name for the xref...
                        suffix = 1
                        found = False
                        while not found:
                                candidate = xrefprefix + str(suffix)
                                if not candidate in scenenames:
                                        xrefname = candidate
                                        found = True
                                suffix+=1
                else:
                        xrefname = xrefprefix
                #create our XRef node
                xnode = state["activeScene"].objects.new('Empty')
                xnode.name = 'X:' + xrefname
                xnode.properties['FLT'] = dict()
                for prop in FLTXRef:
                        xnode.properties['FLT'][prop] = FLTXRef[prop]
                xnode.properties['FLT']['3t200!filename'] = xrefname + '.flt'
		xnode.properties['FLT']['type'] = 63
		xnode.enableDupGroup = True
		xnode.DupGroup = Blender.Group.New(xrefname) #this is dangerous... be careful!

                #copy rot and loc of actObject
                xnode.setLocation(actObject.getLocation())
                xnode.setEuler(actObject.getEuler())         
                
                #build the new scene
		xrefscene = Blender.Scene.New(xrefname)
		xrefscene.properties['FLT'] = dict()
		xrefscene.properties['FLT']['Filename'] = xrefname
		xrefscene.properties['FLT']['Main'] = 0

                #find the children of actObject so that we can add them to the group
		linkobjects = findchildren(actObject)
                linkobjects.append(actObject)
		for object in linkobjects:
                        xrefscene.objects.link(object)
                        state["activeScene"].objects.unlink(object)
                        xnode.DupGroup.objects.link(object)
                #clear rotation of actObject and location
                actObject.setLocation(0.0,0.0,0.0)
                actObject.setEuler(0.0,0.0,0.0)

                xrefscene.update(1)
                state["activeScene"].update(1)

def xref_select():
	state = update_state()
	candidates = list()
	scenelist = [scene.name for scene in Blender.Scene.Get()]
	for object in state["activeScene"].objects:
		if object.type == 'Empty' and object.enableDupGroup == True and object.DupGroup:
			candidates.append(object)
		
	for object in candidates:
		if object.DupGroup.name in scenelist:
			object.sel = 1

def xref_edit():
	global xrefprefix
	global xrefstack
	global vofsstack
	global vquatstack
	global prop_w
	global prop_h
	
	state = update_state()

	actObject = state["activeObject"]

	if actObject and actObject.type == 'Empty' and actObject.DupGroup:
#		if actObject.properties.has_key('FLT') and actObject.properties['FLT']['type'] == 63:
		for FLTscene in Blender.Scene.Get():
			if FLTscene.properties.has_key('FLT') and FLTscene.name == actObject.DupGroup.name:
				actObject.sel = 0
				xrefstack.append(state["activeScene"])
				vofsstack.append(Blender.Window.GetViewOffset())
				vquatstack.append(Blender.Window.GetViewQuat())
				FLTscene.makeCurrent()
				Blender.Window.SetViewOffset(0.0,0.0,0.0)

def xref_finish():
	global xrefprefix
	global xrefstack
	global vofsstack
	global vquatstack
	global prop_w
	global prop_h
	
	state = update_state() 
        if xrefstack:
                scene = xrefstack.pop()
                Blender.Window.SetViewQuat(vquatstack.pop())
                Blender.Window.SetViewOffset(vofsstack.pop())
                scene.makeCurrent()
                

def sortSub(a,b):
	aindex = a.getProperty("FLT_ORIGINDEX")
	bindex = b.getProperty("FLT_ORIGINDEX")
	
	if aindex > bindex:
		return 1
	elif aindex < bindex:
		return -1
	return 0

def subface_make():
	global xrefprefix
	global xrefstack
	global vofsstack
	global vquatstack
	global prop_w
	global prop_h
	
	editmode = 0
	if Blender.Window.EditMode():
		Blender.Window.EditMode(0)
		editmode = 1

	state = update_state()
	
	actmesh = state["activeMesh"]
	activeFace = state["activeFace"]
	if actmesh:
		if not "FLT_ORIGINDEX" in actmesh.faces.properties:
			actmesh.faces.addPropertyLayer("FLT_ORIGINDEX",Blender.Mesh.PropertyTypes["INT"])
			for i, face in enumerate(actmesh.faces):
				face.setProperty("FLT_ORIGINDEX",i)
		if not "FLT_SFLEVEL" in actmesh.faces.properties:
			actmesh.faces.addPropertyLayer("FLT_SFLEVEL",Blender.Mesh.PropertyTypes["INT"])
			
		#attach the subfaces to the active face. Note, this doesnt really work 100 percent properly yet, just enough for one level!
		if activeFace:
			#steps:
			#remove actface and selected faces from the facelist
			#quicksort facelist
			#append actface and subfaces to end of facelist.
			#generate new indices
			facelist = list()
			sublist = list()
			for face in actmesh.faces:
				facelist.append(face)
			for face in facelist:	
				if face == activeFace:
					face.setProperty("FLT_SFLEVEL",0)
					sublist.insert(0,face)
				elif face.sel:
					face.setProperty("FLT_SFLEVEL",1)
					sublist.append(face)
			for face in sublist:
				facelist.remove(face)
			facelist.sort(sortSub)
			for face in sublist:
				facelist.append(face)
			for i, face in enumerate(facelist):
				face.setProperty("FLT_ORIGINDEX",i)
		else:
			pass
	
	if editmode:
		Blender.Window.EditMode(1)

def subface_kill():
	global xrefprefix
	global xrefstack
	global vofsstack
	global vquatstack
	global prop_w
	global prop_h
	
	editmode = 0
	if Blender.Window.EditMode():
		Blender.Window.EditMode(0)
		editmode = 1
	state = update_state()
	
	actmesh = state["activeMesh"]
	if actmesh:
		if "FLT_ORIGINDEX" in actmesh.faces.properties and "FLT_SFLEVEL" in actmesh.faces.properties:
			for i,face in enumerate(actmesh.faces):
				face.setProperty("FLT_ORIGINDEX",i)
				face.setProperty("FLT_SFLEVEL",0)
	if editmode:
		Blender.Window.EditMode(1)

def subface_select():
	global xrefprefix
	global xrefstack
	global vofsstack
	global vquatstack
	global prop_w
	global prop_h
	
	editmode = 0
	if Blender.Window.EditMode():
		Blender.Window.EditMode(0)
		editmode = 1
	state = update_state()
	
	actmesh = state["activeMesh"]
	activeFace = state["activeFace"]
	if actmesh and activeFace:
		if "FLT_ORIGINDEX" in actmesh.faces.properties and "FLT_SFLEVEL" in actmesh.faces.properties:
			facelist = list()
			actIndex = None
			sublevel = None
			for face in actmesh.faces:
				facelist.append(face)
				facelist.sort(sortSub)
			for i, face in enumerate(facelist):
				if face == activeFace:
					actIndex = i
					sublevel = face.getProperty("FLT_SFLEVEL")+1
					break
			leftover = facelist[actIndex+1:]
			for face in leftover:
				if face.getProperty("FLT_SFLEVEL") == sublevel:
					face.sel = 1
				else:
					break
	if editmode:
		Blender.Window.EditMode(1)

def select_by_typecode(typecode):
	global xrefprefix
	global xrefstack
	global vofsstack
	global vquatstack
	global prop_w
	global prop_h
	
	state = update_state()
	
	for object in state["activeScene"].objects:
		if object.properties.has_key('FLT') and object.properties['FLT']['type'] == typecode and state["activeScene"].Layers & object.Layers:
				object.select(1)
def clight_make():
	state = update_state()
	actmesh = state["activeMesh"]
	actobj = state["activeObject"]
	
	if actobj and actmesh:
		actobj.properties['FLT'] = dict()
		actobj.properties['FLT']['type'] = 111
		for prop in FLTInlineLP:
			actobj.properties['FLT'][prop] = FLTInlineLP[prop]
	
		actmesh.verts.addPropertyLayer("FLT_VCOL", Blender.Mesh.PropertyTypes["INT"])
		for v in actmesh.verts:
			v.setProperty("FLT_VCOL", 83815)
			
def dfromact():
	state = update_state()
	actobj = state["activeObject"]
	actscene = state["activeScene"]
	dof = None
	
	for object in actscene.objects.context:
		if object.sel and (object != actobj):
			if not dof:
				dof = object
			else:
				break
	
	if not dof:
		return
	
	if 'FLT' not in dof.properties:
		dof.properties['FLT'] = dict()	
	
	#Warning! assumes 1 BU == 10 meters.
	#do origin
	dof.properties['FLT']['5d!ORIGX'] = actobj.getLocation('worldspace')[0]*10.0
	dof.properties['FLT']['6d!ORIGY'] = actobj.getLocation('worldspace')[1]*10.0
	dof.properties['FLT']['7d!ORIGZ'] = actobj.getLocation('worldspace')[2]*10.0
	#do X axis
	x = Blender.Mathutils.Vector(1.0,0.0,0.0)
	x = x * actobj.getMatrix('worldspace')
	x = x * 10.0
	dof.properties['FLT']['8d!XAXIS-X'] = x[0]
	dof.properties['FLT']['9d!XAXIS-Y'] = x[1]
	dof.properties['FLT']['10d!XAXIS-Z'] = x[2]
	#do X/Y plane
	x = Blender.Mathutils.Vector(1.0,1.0,0.0)
	x.normalize()
	x = x * actobj.getMatrix('worldspace')
	x = x * 10.0
	dof.properties['FLT']['11d!XYPLANE-X'] = x[0]
	dof.properties['FLT']['12d!XYPLANE-Y'] = x[1]
	dof.properties['FLT']['13d!XZPLANE-Z'] = x[2]
	
	
	
	
	
def event(evt,val):
	if evt == Draw.ESCKEY:
		Draw.Exit()
		
def but_event(evt):
	global xrefprefix
	global xrefstack
	global vofsstack
	global vquatstack
	global prop_w
	global prop_h
	global evcode
	
	state = update_state()
	
	#do Xref buttons
        if evt == evcode["XREF_PREFIX"]:
                xrefprefix = XREF_PREFIX.val
	if evt == evcode["XREF_EDIT"]:
		xref_edit()
	if evt == evcode["XREF_SELECT"]:
		xref_select()
	if evt == evcode["XREF_MAKE"]:
		xref_create()
	#do scene buttons				
	if evt == evcode["SCENE_UPDATE"]:
		update_all()
	#do face buttons
	if evt == evcode["FACE_MAKESUB"]:
		subface_make()
	if evt== evcode["FACE_KILLSUB"]:
		subface_kill()
	if evt== evcode["FACE_SELSUB"]:
		subface_select()
	#common buttons
	if evt == evcode["IDPROP_KILL"]:
		if state["activeObject"]:
			idprops_kill(state["activeObject"])
        if evt == evcode["IDPROP_COPY"]:
                if state["activeObject"]:
                        idprops_copy(state["activeObject"])
        if evt == evcode["XREF_POP"]:
                xref_finish()
	if evt == evcode["CLIGHT_MAKE"]:
		clight_make()
	if evt == evcode["DFROMACT"]:
		dfromact()
	if evt == evcode["FIXCOL"]:
		fix_colors()
	Draw.Redraw(1)
	Blender.Window.RedrawAll()


def box(x,y,w,h,c,mode):
        glColor3f(c[0],c[1],c[2])
	if mode == "outline":
                glBegin(GL_LINE_LOOP)
	else:
                glBegin(GL_POLYGON)
	glVertex2i(x,y)
	glVertex2i(x+w,y)
	glVertex2i(x+w,y+h)
	glVertex2i(x,y+h)
	glEnd()

def draw_postcommon(x,y,finaly):
	global sheetlabel
	global xrefprefix
	global xrefstack
	global vofsstack
	global vquatstack
	global prop_w
	global prop_h
	global evcode
	
	state = update_state()
	
	width = prop_w
        height = prop_h
        
	#draw the header
	glColor3f(0.15,0.15,0.15)
	glBegin(GL_POLYGON)
	glVertex2i(x-1,y)
	glVertex2i(x+width+1,y)
	glVertex2i(x+width+1,y-25)
	glVertex2i(x-1,y-25)
	glEnd()
	glColor3f(1,1,1)
	glRasterPos2i(x,y-20)
	sheetlabel = Blender.Draw.Text("FLT Tools Panel")
	#draw the box outline
	glColor3f(0,0,0)
	glBegin(GL_LINE_LOOP)
	glVertex2i(x-1,y)
	glVertex2i(x+1+width,y)
	glVertex2i(x+1+width,finaly-1)
	glVertex2i(x-1,finaly-1)
	glEnd()
	return finaly


def draw_propsheet(x,y):
	global XREF_PREFIX
	global XREF_MAKE
	global XREF_EDIT
	global XREF_SELECT
	global XREF_POP
	global FACE_MAKESUB
	global FACE_SELSUB
	global FACE_KILLSUB
	global IDPROP_KILL
	global IDPROP_COPY
	global SCENE_UPDATE
	global DFROMACT
	global FIXCOL
		
	global CLIGHT_MAKE
	global xrefprefix
	global xrefstack
	global vofsstack
	global vquatstack
	global prop_w
	global prop_h
	global evcode
	
	state = update_state()

	width = prop_w
	height = prop_h
	origx = x
	origy = y
	
         #draw Xref tools
        y = y-20
        XREF_PREFIX = Blender.Draw.String("XRef Name:",evcode["XREF_PREFIX"],x,y,width,20,xrefprefix,18,"Xref prefix name, Actual name is generated from this")
	y = y-20
	XREF_MAKE = Blender.Draw.PushButton("Make XRef",evcode["XREF_MAKE"],x,y,width,20,"Make External Reference")
        y = y-20
	XREF_EDIT = Blender.Draw.PushButton("Edit XRef",evcode["XREF_EDIT"],x,y,width,20,"Edit External Reference")
	y = y-20
	XREF_SELECT = Blender.Draw.PushButton("Select XRefs",evcode["XREF_SELECT"],x,y,width,20,"Select External References")
        y = y - 20
        XREF_POP = Blender.Draw.PushButton("Return to previous scene",evcode["XREF_POP"],x,y,width,20,"Go up one level in xref hierarchy")

        #Draw facetools
	y = y-20
	FACE_MAKESUB = Blender.Draw.PushButton("Make Subfaces",evcode["FACE_MAKESUB"],x,y,width,20,"Make subfaces")
	y = y-20
        FACE_SELSUB = Blender.Draw.PushButton("Select Subfaces",evcode["FACE_SELSUB"],x,y,width,20,"Select subfaces")
	y = y-20
	FACE_KILLSUB = Blender.Draw.PushButton("Kill Subfaces",evcode["FACE_KILLSUB"],x,y,width,20,"Kill subfaces")

        #Draw ID Property tools
	y = y - 20
	IDPROP_KILL = Blender.Draw.PushButton("Delete ID props",evcode["IDPROP_KILL"],x,y,width,20,"Delete ID props")
	y = y - 20
	IDPROP_COPY = Blender.Draw.PushButton("Copy to selected",evcode["IDPROP_COPY"],x,y,width,20, "Copy from active to all selected")
	
	y= y - 20
	CLIGHT_MAKE = Blender.Draw.PushButton("Make Light Point", evcode["CLIGHT_MAKE"],x,y,width,20,"Create inline light points from current mesh")
        #General tools
        y = y-20
	SCENE_UPDATE = Blender.Draw.PushButton("Update All",evcode["SCENE_UPDATE"],x,y,width,20,"Update all vertex colors")
	
	y=y-20
	DFROMACT = Blender.Draw.PushButton("Dof from Active", evcode["DFROMACT"],x,y,width,20,"Get Dof origin from active object")
	y=y-20
	FIXCOL = Blender.Draw.PushButton("Fix Colors", evcode["FIXCOL"],x,y,width,20,"Fix baked FLT colors of selected meshes") 	
	draw_postcommon(origx, origy,y)
		
def gui():
	#draw the propsheet/toolbox.
	psheety = 300
	#psheetx = psheety + 10
	draw_propsheet(0,psheety)
Draw.Register(gui,event,but_event)