#!BPY

"""
Name: 'Toxic export'
Blender: 233
Group: 'Export'
"""

#####################################################################################################################
#Toxic EXPORT SCRIPT - VERY MUCH WORK IN PROGRESS and my first python script : )
#
#
#For use with Blender 2.33 and Windows (for now, toxic only runs under Windows anyway)
#
# 
# - Make sure you use the Blender startup camera only and all objects have materials #assigned.
#
# - A default settings file is included (without the direct lighting tag for speed). 
#
# - If you want to change settings, you have to do it manually in the script (until a GUI is #implemented)
#
# - If there are any problems, please check the Blender console.
#
#Koba - 2004
#
#Thanks to chris for his objexport code for obj_io_modif228.py.
#See comments in script for copyright information.
#
#
#####################################################################################################################

import Blender
import math
import os
from Blender import *
from Blender.NMesh import *
from Blender.BGL import *
from Blender.Draw import *



#####################################################################################################################

##########

#Initialising Tabs
settingtab = Create (1)
lightingtab = Create(0)
outputtab = Create(0)

#Initialising Custom Render Sliders

customx = Create(640)
customy = Create(490)
customreturn = Create(1)

#Initialising Settings buttons

setbutton1 = Create(1)
setbutton2 = Create(0)
setbutton3 = Create(0)
setbutton4 = Create(0)

settex1 = Create("Blender default")
settex2 = Create("C:\\Toxic export")

setslider1 = Create(8)
setslider2 = Create(144)
setslider3 = Create(4)
setslider4 = Create(4)
setslider5 = Create(16)
setslider6 = Create (3)
setslider7 = Create(2)

setmenu1 = Create(1)
setmenu2 = Create(1)
setmenu3 = Create(1)

#Initialising Lighting Buttons

lightslider1 = Create(10)
lightslider2 = Create(200000)
lightslider3 = Create(200)
lightslider4 = Create(400)
lightslider5 = Create(4)
lightslider6 = Create(1)
lightslider7 = Create(144)
lightslider8 = Create(144)
lightslider9 = Create(1)
lightslider10 = Create(1)
lightslider11 = Create(1)
lightslider12 = Create(1)
lightslider13 = Create(1)
lightslider14 = Create(1)
lightslider15 = Create(1)
lightslider16 = Create(1)
lightslider17 = Create(1)
lightslider18 = Create(1)
lightslider19 = Create(20000)
lightslider20 = Create(200)
lightslider21 = Create(400)

lightmenu1 = Create(1)
lightmenu2 = Create(1)
lightmenu3 = Create(1)
lightmenu4 = Create(1)
lightmenu5 = Create(1)
lightmenu6 = Create(1)
lightmenu7 = Create(1)
lightmenu8 = Create(1)

lightbutton1 = Create(1)
lightbutton2 = Create(0)
lightbutton3 = Create(0)
lightbutton4 = Create(0)
lightbutton5 = Create(0)
lightbutton6 = Create(0)
lightbutton7 = Create(0)
lightbutton8 = Create(0)
lightbutton9 = Create(0)

##

###
def Handler(ev, val):
	if ev == ESCKEY:
		Exit()

def Customrender():

	global customx
	global customy
	global customreturn

	customx = Number("X Size", 101 , 40, 400, 99, 15, customx.val, 1, 1024, "Set area light samples")
	customy = Number("Y size", 102 , 40, 299, 99, 15, customy.val, 1, 20, "Y resolution")
	customreturn = Button("Return",103, 20,250,100,30)

			
def Settings():
	
	global settingtab
	global lightingtab
	global outputtab
	
	global setbutton1
	global setbutton2
	global setbutton3
	global setbutton4

	global settex1
	global settex2
	
	global setslider1
	global setslider2
	global setslider3
	global setslider4
	global setslider5
	global setslider6
	global setslider7
		
	global setmenu1
	global setmenu2
	global setmenu3
	
	settingtab = Toggle("Settings", 1, 180, 410, 100, 18, settingtab.val, "Setting file options")
	lightingtab = Toggle("Lighting", 2, 330, 410, 100, 18, lightingtab.val, "Lighting options")
	outputtab = Toggle("Output", 3, 480, 410, 100, 18, outputtab.val, "Output options")
	
	settex1 = String("Scene name: ", 4,250,300,200,20,settex1.val,40,"Please type a name for your scene")
	settex2 = String("Scene name: ", 5,250,330,250,20,settex2.val,40,"Please specify the output folder")

	setslider1 = Number("Raydepth", 6 , 250, 240, 100, 15, setslider1.val, 1, 20, "Raydepth")
		
	setmenu1 = Menu("on%x1|off%x2", 8, 250, 270, 124, 18, setmenu1.val, "Specular reflections")
	setmenu2 = Menu("Supersampling%x1|Whitted Adaptive%x2", 9, 250, 170, 124, 18, setmenu2.val, "Pixel sampling scheme")
	setmenu3 = Menu("320 x 240%x1|480 x 320%x2|640 x 480%x3|640 x 512%x4|640 x 512%x5|768 x 470%x6|1024 x 576%x7|1024 x 768%x8|1280 x 960%x9|Custom%x10", 10, 912, 138, 124, 18, setmenu3.val, "Render resolution")
	
	if setmenu2.val ==1:

		setbutton1=Toggle("Random",11,250,140,87, 20, setbutton1.val,"Random Sampling")
			
		if setbutton1.val == 1:
			setbutton2.val = 0
			setbutton3.val = 0
			setslider2 = Number("Samples:", 1,380,110, 130, 22, setslider2.val,0.0,2048,"Samples taken")
		
		setbutton2 = Toggle("Regular",12,250,120,87, 20, setbutton2.val,"Regular Sampling")
	
		if setbutton2.val == 1:
			setbutton1.val = 0
			setbutton3.val = 0

			setslider3 = Number("Width:", 1,380,120, 130, 22, setslider3.val,1,2048,"Width") 
			setslider4 = Number("Height:", 1,380,100, 130, 22, setslider4.val,1,2048,"Height")
			

		setbutton3 = Toggle("Stratified",13,250,100,87,20,setbutton3.val,"Stratified Sampling") 
		
		if setbutton3.val ==1:
			setbutton1.val = 0
			setbutton2.val = 0

			setslider3 = Number("Width:", 1,380,120, 130, 22, setslider3.val,1,2048,"Width") 
			setslider4 = Number("Height:", 1,380,100, 130, 22, setslider4.val,1,2048,"Height")

	if setmenu2.val ==2:

		setslider6 = Number("Maxdepth:", 1,250,120, 130, 22, setslider6.val,1,2048,"Width") 
		setslider7 = Number("CThreshold:", 1,250,100, 130, 22, setslider7.val,1,2048,"Height")

		 

	Button("Intoxicate!",999, 200,50,100,30)
	
#TEXT
	
def Lighting():
	
	global settingtab
	global lightingtab
	global outputtab
	
	global lightslider1
	global lightslider2
	global lightslider3
	global lightslider4
	global lightslider5
	global lightslider6
	global lightslider7
	global lightslider8
	global lightslider9
	global lightslider10
	global lightslider11
	global lightslider12
	global lightslider13
	global lightslider14
	global lightslider15
	global lightslider16
	global lightslider17
	global lightslider18
	global lightslider19
	global lightslider20
	global lightslider21

	global lightmenu1
	global lightmenu2
	global lightmenu3
	global lightmenu4
	global lightmenu5
	global lightmenu6
	global lightmenu7
	global lightmenu8
	
	global lightbutton1
	global lightbutton2
	global lightbutton3
	global lightbutton4
	global lightbutton5
	global lightbutton6
	global lightbutton7
	global lightbutton8
	global lightbutton9

	global lightbutton
	settingtab = Toggle("Settings", 1, 180, 410, 100, 18, settingtab.val, "Setting file options")
	lightingtab = Toggle("Lighting", 2, 330, 410, 100, 18, lightingtab.val, "Lighting options")
	outputtab = Toggle("Output", 3, 480, 410, 100, 18, outputtab.val, "Output options")
	
	lightmenu1 = Menu("on%x1|off%x2", 14, 200, 370, 160, 18, lightmenu1.val, "Enable/Disable direct lighting")
	
	lightmenu3 = Menu("on%x1|off%x2", 16, 400, 370, 80, 18, lightmenu3.val, "Enable/Disable indirect lighting")
	lightmenu4 = Menu("on%x1|off%x2", 17, 300, 205, 160, 18, lightmenu4.val, "Enable/Disable Primary gathering")
	lightmenu5 = Menu("on%x1|off%x2", 18, 500, 205, 160, 18, lightmenu5.val, "Enable/Disable Secondary gathering")
	lightmenu6 = Menu("on%x1|off%x2", 19, 400, 270, 160, 18, lightmenu6.val, "Enable/Disable Radiance precomputation")
	lightmenu7 = Menu("on%x1|off%x2", 16, 490, 370, 80, 18, lightmenu7.val, "Enable/Disable caustics")

	lightslider2 = Number(" ", 22, 400,350, 80, 15, lightslider2.val, 1, 1000000, "Number of photons to fire")
	lightslider3 = Number(" ", 23, 400, 330, 80, 15, lightslider3.val, 1, 1000, "Maximum photons in radiance estimate")
	lightslider4 = Number(" ", 24, 400, 310, 80, 15, lightslider4.val, 1, 1000, "Maximum distance")
	lightslider5 = Number("Spacing:", 25, 400, 250, 160, 15, lightslider5.val, 1, 20, "Spacing")
	lightslider6 = Number("Maximum distance: 0.", 26, 400, 230, 160, 15, lightslider6.val, 1, 20, "Maximum search distance")
	lightslider9 = Number("Distance Threshold: 0.0", 27, 500, 60, 160, 15, lightslider9.val, 1, 20, "Distance threshold")

	lightslider19 = Number(" ", 22, 490,350, 80, 15, lightslider19.val, 1, 1000000, "Number of photons to fire (caustics)")
	lightslider20 = Number(" ", 23, 490, 330, 80, 15, lightslider20.val, 1, 1000, "Maximum photons in radiance estimate (caustics)")
	lightslider21 = Number(" ", 24, 490, 310, 80, 15, lightslider21.val, 1, 1000, "Maximum distance (caustics)")
	

	#Direct sampling

	lightbutton1=Toggle("Random",51,250,340,87, 20, lightbutton1.val,"Random Sampling")
	
	if lightbutton1.val == 1:

		lightslider10 = Number("Samples:", 100,220,270, 130, 22, lightslider10.val,0.0,2048,"Samples taken")
		
	lightbutton2 = Toggle("Regular",52,250,320,87, 20, lightbutton2.val,"Regular Sampling")
		
	if lightbutton2.val == 1:

		lightslider11 = Number("Width:", 100,220,270, 130, 22, lightslider11.val,1,2048,"Width") 
		lightslider12 = Number("Height:", 100,220,250, 130, 22, lightslider12.val,1,2048,"Height")
			
	lightbutton3 = Toggle("Stratified",53,250,300,87,20,lightbutton3.val,"Stratified Sampling") 
		
	if lightbutton3.val == 1:

		lightslider11 = Number("Width:", 100,220,270, 130, 22, lightslider11.val,1,2048,"Width") 
		lightslider12 = Number("Height:", 100,220,250, 130, 22, lightslider12.val,1,2048,"Height")
	
	#Primary gathering sampling
	
	lightbutton4=Toggle("Random",54,330,170,87, 20, lightbutton4.val,"Random Sampling")
	
	if lightbutton4.val == 1:

		lightslider13 = Number("Samples:", 100,310,100, 130, 22, lightslider13.val,0.0,2048,"Samples taken")
		
	lightbutton5 = Toggle("Regular",55,330,150,87, 20, lightbutton5.val,"Regular Sampling")
		
	if lightbutton5.val == 1:

		lightslider14 = Number("Width:", 100,310,100, 130, 22, lightslider14.val,1,2048,"Width") 
		lightslider15 = Number("Height:", 100,310,80, 130, 22, lightslider15.val,1,2048,"Height")
			
	lightbutton6 = Toggle("Stratified",56,330,130,87,20,lightbutton6.val,"Stratified Sampling") 
		
	if lightbutton6.val == 1:

		lightslider14 = Number("Width:", 100,310,100, 130, 22, lightslider14.val,1,2048,"Width") 
		lightslider15 = Number("Height:", 100,310,80, 130, 22, lightslider15.val,1,2048,"Height")

	#Secondary gathering sampling

	lightbutton7=Toggle("Random",57,530,170,87, 20, lightbutton7.val,"Random Sampling")
	
	if lightbutton7.val == 1:

		lightslider16 = Number("Samples:", 100,510,100, 130, 22, lightslider16.val,0.0,2048,"Samples taken")
		
	lightbutton8 = Toggle("Regular",58,530,150,87, 20, lightbutton8.val,"Regular Sampling")
		
	if lightbutton8.val == 1:

		lightslider17 = Number("Width:", 100,510,100, 130, 22, lightslider17.val,1,2048,"Width") 
		lightslider18 = Number("Height:", 100,510,80, 130, 22, lightslider18.val,1,2048,"Height")
			
	lightbutton9 = Toggle("Stratified",59,530,130,87,20,lightbutton9.val,"Stratified Sampling") 
		
	if lightbutton9.val == 1:

		lightslider17 = Number("Width:", 100,510,100, 130, 22, lightslider17.val,1,2048,"Width") 
		lightslider18 = Number("Height:", 100,510,80, 130, 22, lightslider18.val,1,2048,"Height")
	
	Button("Intoxicate!",999, 200,50,100,30)

def Output():

	global settingtab
	global lightingtab
	global outputtab

	settingtab = Toggle("Settings", 1, 180, 410, 100, 18, settingtab.val, "Setting file options")
	lightingtab = Toggle("Lighting", 2, 330, 410, 100, 18, lightingtab.val, "Lighting options")
	outputtab = Toggle("Output", 3, 480, 410, 100, 18, outputtab.val, "Output options")

	glRasterPos2f(200, 2) 
	Text("NOTHING HERE YET")

def Action(ev):

	#Tabs actions

	if ev == 1:
		settingtab.val = 1
		lightingtab.val = 0
		outputtab.val = 0

		Register(Settings,Handler,Action)

	if ev == 2:
		settingtab.val = 0
		lightingtab.val = 1
		outputtab.val = 0

		Register(Lighting, Handler, Action)

	if ev == 3:
		settingtab.val = 0
		lightingtab.val = 0
		outputtab.val = 1

		Register(Output, Handler, Action)
		
	if ev ==9:

		Register(Settings,Handler,Action)

#Settings 
	if ev == 11:

		setbutton2.val = 0
		setbutton3.val = 0
		Register(Settings,Handler,Action)

	if ev == 12:

		setbutton1.val = 0
		setbutton3.val = 0
		Register(Settings,Handler,Action)

	if ev == 13:

		setbutton1.val = 0
		setbutton2.val = 0
		Register(Settings,Handler,Action)

	if setmenu3.val == 10:

		Register(Customrender,Handler,Action)

#Direct lighting sampling 

	if ev == 51:

		lightbutton2.val = 0
		lightbutton3.val = 0
		Register(Lighting,Handler,Action)

	if ev == 52:

		lightbutton1.val = 0
		lightbutton3.val = 0
		Register(Lighting,Handler,Action)

	if ev == 53:

		lightbutton1.val = 0
		lightbutton2.val = 0
		Register(Lighting,Handler,Action)

#Primary gather sampling

	if ev == 54:

		lightbutton5.val = 0
		lightbutton6.val = 0
		Register(Lighting,Handler,Action)

	if ev == 55:

		lightbutton4.val = 0
		lightbutton6.val = 0
		Register(Lighting,Handler,Action)

	if ev == 56:

		lightbutton4.val = 0
		lightbutton5.val = 0
		Register(Lighting,Handler,Action)

#Secondary gathering

	if ev == 57:

		lightbutton8.val = 0
		lightbutton9.val = 0
		Register(Lighting,Handler,Action)

	if ev == 58:

		lightbutton7.val = 0
		lightbutton9.val = 0
		Register(Lighting,Handler,Action)

	if ev == 59:

		lightbutton7.val = 0
		lightbutton8.val = 0
		Register(Lighting,Handler,Action)

#Other values

	if setmenu3.val == 10:
		Register(Customrender,Handler,Action)

	if ev == 101:
		width = customx.val

	if ev == 102:
		height = customy.val
	
	if ev == 103:
		Register(Settings,Handler,Action)

	if ev == 999:

		fl = open("""C:\Toxic Export\Blender default\export.settings.xml""" , 'w')
		global fl
		
		Header()

#Pixel sampling

		if setmenu2.val == 1:
			if setbutton1.val == 1:
				Pixeltype = "RandomSampling"
				Pixrand(Pixeltype, setslider2)
			if setbutton2.val == 1:
				Pixeltype = "RegularSampling"
				Pixregandstrat(Pixeltype, setslider3, setslider4)
			if setbutton3.val == 1:
				Pixeltype = "StratifiedSampling"
				Pixregandstrat(Pixeltype, setslider3, setslider4)

		if setmenu2.val == 2:
			Pixeltype = "WhittedAdaptiveSampling"
			Pixwhitted (Pixeltype, setslider7 , setslider6)

#COMPONENTS

		Opencomp()		
		if setmenu1.val == 1:
			Specular(setslider1)

#Directlighting
		
		if lightmenu1.val ==1:
			if lightbutton1.val ==1:
				Directtype = "RandomSampling"
				Directrand(Directtype, lightslider10)
			if lightbutton2.val ==1:
				Directtype = "RegularSampling"
				Directregandstrat(Directtype, lightslider11, lightslider12)
			if lightbutton3.val ==1:
				Directtype = "StratifiedSampling"
				Directregandstrat(Directtype, lightslider11, lightslider12)
			
#Indirectlighting
	
		if lightmenu3.val ==1:
			Photonsandestimate(lightslider2, lightslider3, lightslider4)

			if lightmenu6.val ==1:
				Radiancepre (lightslider5, lightslider6)

#Primary Gathering

			if lightmenu4.val ==1:

				if lightbutton4.val ==1:
					Primarytype = "RandomSampling"
					Primaryrand(Primarytype, lightslider13)

				if lightbutton5.val ==1:
					Primarytype = "RegularSampling"
					Primaryregandstrat (Primarytype, lightslider14, lightslider15)

				if lightbutton6.val ==1:
					Primarytype = "StratifiedSampling"
					Primaryregandstrat (Primarytype, lightslider14, lightslider15)

#Secondary Gathering

			if lightmenu5.val ==1:

				if lightbutton7.val ==1:
					Secondarytype = "RandomSampling"
					Threshold (lightslider9)
					Secondaryrand(Secondarytype, lightslider16)

				if lightbutton8.val ==1:
					Secondarytype = "RegularSampling"
					Threshold (lightslider9)
					Secondaryregandstrat (Secondarytype, lightslider17, lightslider18)

				if lightbutton9.val ==1:
					Secondarytype = "StratifiedSampling"
					Secondaryregandstrat (Secondarytype, lightslider17, lightslider18)
				
				Closeindirect()
		
#Caustics
				if lightmenu7.val == 1:
					CausticsPhotons (lightslider19)
					CausticsRest (lightslider20, lightslider21)


#Closing tags

		Outputtag()
		Closetags()
		fl.close()

		filenamebat = """%s\\run.bat""" % (Directory)
		fb = open(filenamebat, 'w')

		stb = """cd\\

		cd %s
		toxic export.xml -o %s""" % (Directory, Output)
		fb.write(stb)
		fb.close()

		os.startfile(filenamebat)
			
#The xml functions

def Header():

	set = """<?xml version="1.0" encoding="UTF-8"?>\n<!-- \n -->"""
	set +="""\n<ToxicSceneSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../schemas/toxicsettings.xsd">"""
	writer(set)

#Pixel sampling

def Pixrand(Pixeltype, n):
	
	set = """\n\t<Rendering>"""
	set += """\n\t\t<PixelSampling>"""
	set += """\n\t\t\t<Supersampling>"""
	set += """\n\t\t\t\t<%s samples="%s" />""" % (Pixeltype, setslider2.val)
	set += """\n\t\t\t</Supersampling>"""
	set += """\n\t\t</PixelSampling>"""
	writer(set)
	

def Pixregandstrat(Pixeltype,n,m):

	set = """\n\t<Rendering>"""
	set += """\n\t\t<PixelSampling>"""
	set += """\n\t\t\t<Supersampling>"""
	set += """\n\t\t\t<%s width="%s" height ="%s" />""" % (Pixeltype,setslider3.val,setslider4.val)
	set += """\n\t\t\t</Supersampling>"""
	set += """\n\t\t</PixelSampling>"""
	writer(set)

def Pixwhitted (Pixeltype, n ,m):
	
	set = """\n\t<Rendering>"""
	set += """\n\t\t<PixelSampling>"""
	set += """\n\t\t\t<%s contrastthreshold ="%s" maxdepth = "%s" />""" % (Pixeltype,setslider7.val,setslider6.val)
	set += """\n\t\t</PixelSampling>"""
	writer(set)

#COMPONENTS

def Opencomp():

	set = """\n\t\t<Components>"""
	writer(set)

def Specular(setslider1):

	set = """\n\t\t\t<SpecularReflections maxdepth = "%s"/>""" % (setslider1.val)
	writer(set)

#Direct Lighting

def Directrand (Directtype, m):

	set = """\n\t\t\t<DirectLighting>"""
	set += """\n\t\t\t\t<ArealightSampling>"""
	set += """\n\t\t\t\t\t<%s samples = "%s" />""" % (Directtype, lightslider10.val)
	set += """\n\t\t\t\t</ArealightSampling>"""
	set += """\n\t\t\t</DirectLighting>"""
	writer(set)

def Directregandstrat (Directtype, m , n):
	
	set = """\n\t\t\t<DirectLighting>"""
	set += """\n\t\t\t\t<ArealightSampling>"""
	set += """\n\t\t\t\t\t<%s width = "%s" height = "%s" />""" % (Directtype, lightslider11.val, lightslider12.val)
	set += """\n\t\t\t\t</ArealightSampling>"""
	set += """\n\t\t\t</DirectLighting>"""	
	writer(set)

#Indirectlighting

def Photonsandestimate (m , n , o):

	set = """\n\t\t\t<IndirectLighting>"""
	set += """\n\t\t\t\t<PhotonTracing photons = "%s"/>""" % (lightslider2.val)
	set += """\n\t\t\t\t<RadianceEstimate maxphotons = "%s" maxdistance = "%s"/>""" % (lightslider3.val , lightslider4.val)
	writer(set)

def Radiancepre (m , n):
	
	set = """\n\t\t\t\t<RadiancePrecomputation spacing = "%s" maxsearchdistance ="0.%s"/>""" % (lightslider5.val, lightslider6.val)
	writer(set)

#Primary Gathering

def Primaryrand (Primarytype, m):

	set = """\n\t\t\t\t<PrimaryFinalGathering>"""
	set += """\n\t\t\t\t\t<%s  samples = "%s"/>""" % (Primarytype, lightslider13.val)
	set += """\n\t\t\t\t<PrimaryFinalGathering>"""
	writer(set)

def Primaryregandstrat (Primarytype, m , n):
	
	set = """\n\t\t\t\t<PrimaryFinalGathering>"""
	set += """\n\t\t\t\t\t<%s  width = "%s" height = "%s"/>""" % (Primarytype, lightslider14.val, lightslider15.val)
	set += """\n\t\t\t\t<PrimaryFinalGathering>"""
	writer(set)

#Secondary Gathering

def Threshold(m):

	set = """\n\t\t\t\t<SecondaryFinalGathering distancethreshold = "%s">""" % (lightslider9.val)
	writer(set)

def Secondaryrand (Secondarytype, m):

	set = """\n\t\t\t\t\t<%s samples = "%s"/>""" % (Secondarytype, lightslider16.val)
	set += """\n\t\t\t\t<SecondaryFinalGathering>"""
	writer(set)


def Secondaryregandstrat (Secondarytype, m , n):
	
	set = """\n\t\t\t\t\t<%s  width = "%s" height = "%s"/>""" % (Secondarytype, lightslider17.val, lightslider18.val)
	set += """\n\t\t\t\t<SecondaryFinalGathering>"""
	writer(set)

#Caustics

def CausticsPhotons (m):

	set = """\n\t\t\t<Caustics>"""
	set += """\n\t\t\t\t<PhotonTracing photons = "%s"/>""" % (lightslider19.val)
	writer(set)

def CausticsRest (m , n):

	set = """\n\t\t\t\t<RadianceEstimate maxphotons= "%s" maxdistance = "%s"/>""" % (lightslider20.val, lightslider21.val)
	set += """\n\t\t\t</Caustics>"""
	writer(set)

def Closeindirect():

	set = """\n\t\t\t</IndirectLighting>"""
	writer(set)

def Closetags():

	set = """\n</ToxicSceneSettings>"""
	writer(set)

def Outputtag():
	
	set = """\n\t\t</Components>"""
	set += """\n\t</Rendering>"""
	set += """\n\t<Output>"""
	set += """\n\t\t<GammaCorrection targetgamma="2.2" />"""
	set += """\n\t\t<!--<RenderArea x0="486" y0="640" x1="790" y1="704" />-->"""
	set += """\n\t</Output>"""
	writer(set)

def writer(n):
	fl.write(n)

Register(Settings, Handler, Action)


#################################### MAIN SCRIPT ################################################

UserDirectory = """C:\\Toxic Export"""
SceneName = "Blender default"
Output = """image.png"""

Directory = """%s\%s""" % (UserDirectory, SceneName)


#####################################################################################################################

filename = """%s\export.xml""" % (Directory)
fp = open(filename, 'w')

#####################################################################################################################

#Lots of functions for .xml output

def Object1(n):

	st = """\n\n\t\t<Object type= "mesh">"""
	st += """\n\t\t\t<Parameter name="href" value="%s.obj"/>""" % n
	writen(st)

def Object2(n):
	st = """\n\t\t\t<Parameter name="surfaceshader" value="%s"/>""" % n
	st += """\n\t\t</Object>"""
	writen(st)

def Camera(n):
	
	st = """\n\n\t\t<Object type="pinholecamera">"""
	st += """\n\t\t\t<Transform>"""
	st += """\n\t\t\t\t<Rotation angle="%f" axis="1.0 0.0 0.0"/>""" % (cam.RotX*180/math.pi)
	st += """\n\t\t\t\t<Rotation angle="%f" axis="0.0 1.0 0.0"/>""" % (cam.RotY*180/math.pi)
	st += """\n\t\t\t\t<Rotation angle="%f" axis="0.0 0.0 1.0"/>""" % (cam.RotZ*180/math.pi)
	st += """\n\t\t\t\t<Translation value= "%f %f %f"/>""" % (cam.LocX, cam.LocY, cam.LocZ)
	st += """\n\t\t\t</Transform>"""
	st += """\n\t\t</Object>"""
	writen(st)

def Shader1(n):

	st ="""\n\n\t\t<SurfaceShader name="%s">""" % namemat
	writen(st)

def Shader2(n):
	
	st = """\n\t\t\t<Reflectance>"""
	st += """\n\t\t\t\t<ConstantTexture value="%s %s %s"/>""" % (rgblist[0], rgblist[1], rgblist[2])
	st += """\n\t\t\t</Reflectance>"""
	st += """\n\t\t</SurfaceShader>"""
	writen(st)

def Pointlight1(n):
	st = """\n\n\t\t<Object type="pointlight">"""
	st += """\n\t\t\t<Parameter name="power" value="%s"/>"""  % (Energy)
	writen(st)

def Pointlight2(n):
	st = """\n\t\t\t<Transform>"""
	st += """\n\t\t\t\t<Translation value="%f %f %f"/>""" % (oblamp.LocX, oblamp.LocY, oblamp.LocZ)
	st += """\n\t\t\t</Transform>"""
	st += """\n\t\t</Object>"""
	writen(st)

def Emit1(n):
	st = """\n\n\t\t<Object type="%s">""" % (lamptype)
	writen(st)

def Emit2(n):
	st = """\n\t\t\t<Parameter name="surfaceshader" value="%s"/>"""  % (name)
	writen(st)

def Emit3(n):
	st = """\n\t\t\t<Transform>"""
	st += """\n\t\t\t\t<Translation value="%f %f %f"/>""" % (oblamp.LocX, oblamp.LocY, oblamp.LocZ)
	st += """\n\t\t\t</Transform>"""
	st += """\n\t\t</Object>"""
	writen(st)

def lampshade1(n):
	st ="""\n\n\t\t<SurfaceShader name="%s">""" % name
	writen(st)

def lampshade2(n):
	st = """\n\t\t\t<EDF type = "lambertian"/>"""
	st += """\n\t\t\t<RadiantExitance value= "%f %f %f"/>""" % (Energy,Energy,Energy)
	st += """\n\t\t\t<Reflectance>"""
	st += """\n\t\t\t\t<ConstantTexture value="0.0"/>"""
	st += """\n\t\t\t</Reflectance>"""
	st += """\n\n\t\t</SurfaceShader>"""
	writen(st) 	

def Defaultmat():
	st ="""\n\n\t\t<SurfaceShader name="The toxic default material">"""
	st += """\n\t\t\t<Reflectance>"""
	st += """\n\t\t\t\t<ConstantTexture value="1.0 1.0 1.0"/>"""
	st += """\n\t\t\t</Reflectance>"""
	st += """\n\t\t</SurfaceShader>"""
	writen(st)

def writen(n):
	fp.write(n)

#####################################################################################################################

#Header of .xml

st = """<?xml version="1.0" encoding="UTF-8"?>
<ToxicScene xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../schemas/toxicscene.xsd">"""

st += """\n\t<Frame>"""
writen(st)


#####################################################################################################################

#Removes stray vertices.

def removevert(n): 

   mesh = GetRaw(mname) 

   Vertices=mesh.verts[:] 

   vertlist=[] 

   for f in mesh.faces: 
      for vertice in f.v: 
         vertlist.append(vertice) 

   for v in Vertices: 
      if v not in vertlist: 
         mesh.verts.remove(v) 
   PutRaw(mesh,mname)

#Extracting Blender information and assigning to functions

objectlist = Blender.Object.Get()
lamplist = Blender.Lamp.Get()
listofmats = Blender.Material.Get()

noob = len(objectlist)
nola = len(lamplist)
matlen = len(listofmats)


if noob == 0:
	raise "You need objects in your scene"

curno = 0
matno = 0
a = objectlist[curno]
b = lamplist[curno]
defaultlim = 0

if matno<matlen:
	for mat in listofmats:			
		allmats = Blender.Material.Get()
		firstmats = allmats[matno]
		namemat = firstmats.getName()
		Shader1(namemat)
		rgbvals = Blender.Material.Get(namemat)
		rgblist = rgbvals.rgbCol
		Shader2(rgblist)
		matno = matno + 1

if curno<noob:
	for a in objectlist:
		if a.getType() == "Mesh":
			mname = a.getName()
			mesh = Blender.Object.Get(mname)
			meshdata = mesh.getData()
			matnames = meshdata.materials          #< Insert "removevert(mname) on next line for testing
			
			if len(matnames) == 0: #Assigning a material
				print "Assigning standard material"
				meshshade = "The toxic default material"
				if defaultlim == 0:
					Defaultmat()
					defaultlim = 1
					Object1(mname)
					Object2(meshshade)
				else:
					Object1(mname)
					Object2(meshshade)
			else:	
				assignedshader = matnames[0]
				meshshade = assignedshader.getName()
				Object1(mname)
				Object2(meshshade)

		curno = curno + 1
					
	for a in objectlist:
		if a.getType() == "Camera":
			mname = a.getName()
			cam = Blender.Object.Get(mname)
			Camera(cam)
			curno = curno + 1
		if a.getType() == "Lamp":
			mname = b.getName()
			oblampname = a.getName()
			print mname
			lamp = Blender.Lamp.Get(mname)
			lampint = lamp.getType()
			if lampint == 0:
				Energy = lamp.getEnergy()*30
				Pointlight1(Energy)
				obname = a.getName()
				oblamp = Blender.Object.Get(oblampname)
				Pointlight2(oblamp)
			if lampint == 1:
				lamptype = "square"
				name = lamp.getName()
				oblamp = Blender.Object.Get(oblampname)
				Energy = lamp.getEnergy()*30
				lampshade1 (name)
				lampshade2(Energy)
				Emit1(lamptype)
				Emit2(name)
				Emit3(oblamp)
			if lampint == 2:
				lamptype = "sphere"
				name = lamp.getName()
				oblamp = Blender.Object.Get(oblampname)
				Energy = lamp.getEnergy()*30
				lampshade1 (name)
				lampshade2(Energy)
				Emit1(lamptype)
				Emit2(name)
				Emit3(oblamp)
				

#####################################################################################################################
	
#Footer of .xml

st = """\n\n\t</Frame>"""
st += """\n</ToxicScene>"""
writen(st)

#####################################################################################################################
#.obj export code extracted from obj_io_modif228.py script (and small change to ObjExport function)
# All Rights Reserved
# chris@artdreamdesigns.com 

# ===============================
# Import our libraries
# ===============================

try:
    import nt
    os=nt
    os.sep='\\'
except:    
    import posix
    os=posix
    os.sep='/'

def isdir(path):
    try:
        st = os.stat(path)
        return 1 
    except:
        return 0
    
def split(pathname):
         k0=pathname.split(os.sep)
         directory=pathname.replace(k0[len(k0)-1],'')
         Name=k0[len(k0)-1]
         return directory, Name
        
def join(l0,l1):        
     return  l0+os.sep+l1
    
os.isdir=isdir
os.split=split
os.join=join

import math
import Blender
#import Blender210
from Blender import *
from Blender import NMesh
from Blender.Draw import *
from Blender.BGL import *
from Blender import Material

# ===============================
# Input Variables
# ===============================

listofthings = Blender.Object.Get()
noofthings = len(listofthings)
number = 0
meshnow = listofthings[number]

gAlert   = 0
type     = 1
exporttype = 2
returncode = 0
operation = "Export"
center = [0,0,0]
rotation = [0,0,0]
Transform = []
multiflag = 0
		

         
#================================
# MAIN SCRIPT
#================================
# Opens a file, writes data in it
# and closes it up.
#============
#========================================
def CreateMatrix(object, Transform):
#========================================
   Mx = []
   My = []
   Mz = []
   T1 = []
   Transform = []

   angle = object.RotX
   Mx.append([1, 0, 0])
   y = math.cos(angle)
   z = -math.sin(angle)
   Mx.append([0, y, z])
   y = math.sin(angle)
   z = math.cos(angle)
   Mx.append([0, y, z])

   angle = object.RotY
   x = math.cos(angle)
   z = math.sin(angle)
   My.append([x, 0, z])
   My.append([0, 1, 0])
   x = -math.sin(angle)
   z = math.cos(angle)
   My.append([x, 0, z])

   angle = object.RotZ
   x = math.cos(angle)
   y = -math.sin(angle)
   Mz.append([x, y, 0])
   x = math.sin(angle)
   y = math.cos(angle)
   Mz.append([x, y, 0])
   Mz.append([0, 0, 1])

   m0 = Mx[0]
   m1 = Mx[1]
   m2 = Mx[2]
   for row in My:
      x, y, z = row
      nx = x*m0[0] + y*m1[0] + z*m2[0]
      ny = x*m0[1] + y*m1[1] + z*m2[1]
      nz = x*m0[2] + y*m1[2] + z*m2[2]
      T1.append([nx, ny, nz])

   m0 = T1[0]
   m1 = T1[1]
   m2 = T1[2]
   for row in Mz:
     x, y, z = row
     nx = x*m0[0] + y*m1[0] + z*m2[0]
     ny = x*m0[1] + y*m1[1] + z*m2[1]
     nz = x*m0[2] + y*m1[2] + z*m2[2]
     Transform.append([nx, ny, nz])

   Transform.append([object.SizeX, object.SizeY, object.SizeZ])
   Transform.append([object.LocX, object.LocY, object.LocZ])

   return Transform

#======================================
def Alter(vect, Transform):
#======================================
   v2 = []
   nv = []

   x, y, z = vect
   sx, sy, sz = Transform[3]
   lx, ly, lz = Transform[4]

   v2.append(x*sx)
   v2.append(y*sy)
   v2.append(z*sz)

   for index in range(len(vect)):
      t = Transform[index]
      nv.append(v2[0]*t[0] + v2[1]*t[1] +v2[2]*t[2])

   nv[0] = nv[0]+lx
   nv[1] = nv[1]+ly
   nv[2] = nv[2]+lz

   return nv

#================================
def ExportFunction (exportName, type):
#================================       
      global gFilename 
      global gAlert

      try:
         FILE=open (exportName,"r")
         FILE.close()
         gAlert = 1
         Draw ()
      except IOError:

         directory, Name = os.split(gFilename.val)

         
         if os.isdir(directory):
            ExportFunctionOK(exportName, type)
            Draw ()
         else:
            gAlert = 5
            Draw ()

#===========================================================
def Standard(faces, Vertices, has_uvco, FILE, ObjName): 
#=========================================================== 
       global vertexcount
       global uvcount
       global multiflag

       uvPtrs = []
       uvList = []

       FILE.write("o %s\n\n" % (ObjName)) 
       FILE.write("g %s\n\n" % (ObjName)) 
 
       for v in Vertices: 
          vert = v.co 
          if multiflag  == 1:
             vert = Alter(vert, Transform) 
          x, y, z = vert
               
          FILE.write("v %s %s %s\n" % (x, y, z))

       uv_flag = 0
       for face in faces:
         for uv in face.uv:
            found = 0
            index = len(uvList)
            limit = 0
            if len(uvList)-200 > 0:
               limit = len(uvList)-200
            while index > limit and found == 0:
               uv_value = uvList[index-1]
               if uv[0] == uv_value[0] and uv[1] == uv_value[1]:
                  uvPtrs.append(index+uvcount)
                  found = 1
               index = index - 1
            if found == 0:
               uvList.append(uv)
               index = len(uvList)
               uvPtrs.append(index+uvcount)
               u, v = uv
               FILE.write("vt %s %s\n" % (u, v))
               uv_flag = 1

       if has_uvco and uv_flag == 0:
         for v in Vertices:
            u, v, z = v.uvco 
            u = (u-1)/2
            v = (v-1)/2
            FILE.write("vt %s %s\n" % (u, v))

       for v in Vertices: 
          x, y, z = v.no
          FILE.write("vn %s %s %s\n" % (x, y, z))

       p = 0
       uvindex = 0
       total = len(faces)

       for face in faces:
          p = p+1
          if (p%1000) == 0:
              print ("Progress = "+ str(p)+ " of "+ str(total) +" faces")

          FILE.write("f ")
          for index in range(len(face.v)):
             v = face.v[index].index + vertexcount
             if len(face.uv) > 0:
                FILE.write("%s/%s/%s " % (v+1, uvPtrs[uvindex], v+1))
                uvindex = uvindex+1
             elif has_uvco:
                FILE.write("%s/%s/%s " % (v+1, v+1, v+1))
             else:                     
                FILE.write("%s//%s " % (v+1, v+1))
          FILE.write("\n")

       vertexcount = vertexcount + len(Vertices)
       uvcount = uvcount + len(uvList)

       print("Export of " +str(ObjName)+ ".obj finished.\n")

#=========================================
def AddMeshMaterial(name, materialList, matindex):
#=========================================
    
   index = 0
   found = 0 
   limit = len(materialList)

   while index < limit:
     if materialList[index] == name:
        matindex = index 
        found = 1
        index = limit
     index = index + 1
   
   if found == 0:      
      materialList.append(name)
      matindex = len(materialList)-1
        
   return matindex

#=========================================
def AddGlobalMaterial (name, matindex):
#=========================================
    
   index = 0
   found = 0
   matindex  = 0
   MatList = Material.Get()
   limit = len(MatList)

   while index < limit:
     if MatList[index].name == name:
        matindex = index 
        found = 1
        index = limit
     index = index + 1

   if found == 0:
      material = Material.New(name)
      matindex = index
    
   return matindex

#================================
def ObjExport(FILE, type, Objects):
#================================
  global returncode
  global vertexcount
  global uvcount
  global Transform
  global multiflag
  global exporttype

  vertexcount = 0
  uvcount = 0
  returncode = 0
  FILE.write("# Wavefront OBJ (1.0) exported by lynx's OBJ import/export script\n\n")

  if Objects == []:
     print("You have not selected an object!")
     returncode = 4
  else:
     for object in Objects:
        MtlList = []
        if len(Objects) > 1 or exporttype > 1:
           Transform = CreateMatrix(object, Transform)
           multiflag = 1
        
        object.makeDisplayList()

        mesh = NMesh.GetRawFromObject(object.name)
        ObjName = mesh.name
        has_uvco = mesh.hasVertexUV()

        FILE.write("# Meshname:\t%s\n" % ObjName)

        faces = mesh.faces
        materials = mesh.materials
        Vertices = mesh.verts
        GlobalMaterials = Material.Get()

        if len(materials) > 1 and len(GlobalMaterials) > 0 and type < 4:
           CreateMtlFile(Name, materials, MtlList)

        # Total Vertices and faces; comment if not useful
        FILE.write("# Total number of Faces:\t%s\n" % len(faces))
        FILE.write("# Total number of Vertices:\t%s\n" % len(Vertices))

        FILE.write("\n")

        # print first image map for uvcoords to use
        # to be updated when we get access to other textures
        if mesh.hasFaceUV(): FILE.write("# UV Texture:\t%s\n\n" % mesh.hasFaceUV())

        if len(materials) > 1 and len(GlobalMaterials) > 0 and type < 3:
           UseLayers(faces, Vertices, MtlList, has_uvco, FILE, ObjName, Name)
        elif len(materials) > 1 and len(GlobalMaterials) > 0 and type == 3:
           UseMtl(faces, Vertices, MtlList, has_uvco, FILE, ObjName, Name)
        else:
           Standard(faces, Vertices, has_uvco, FILE, ObjName)




#================================================
def CreateMtlFile (name, MeshMaterials, MtlList):
#================================================
      global gFilename 

    # try to export materials
      directory, mtlname = os.split(gFilename.val)
      mtlname = name + ".mtl"
      filename = os.join(directory, mtlname)
      file = open(filename, "w")

      file.write("# Materials for %s.\n" % (name + ".obj"))
      file.write("# Created by Blender.\n")
      file.write("# These files must be in the same directory for the materials to be read correctly.\n\n")

      MatList = Material.Get()
      print str(MeshMaterials)

      MtlNList=[]
      for m in  MatList:
         MtlNList.append(m.name)

      counter = 1
      found = 0  

      for material in MeshMaterials:
         for mtl in MtlList:
            if material == mtl:
                found = 1

         MtlList.append(material) 

         if found == 0:
            file.write("newmtl %s \n" % material.name)
            index = 0
            print material, MatList
            while index < len(MatList):
               if material.name == MatList[index].name:
                  mtl = MatList[index]
                  index = len(MatList)
                  found = 1
               index = index + 1

            if found == 1:
               alpha = mtl.getAlpha()
               file.write("       Ka %s %s %s \n" % (round(1-alpha,5), round(1-alpha,5), round(1-alpha,5)))
               file.write("       Kd %s %s %s \n" % (round(mtl.R,5), round(mtl.G,5), round(mtl.B,5)))
               file.write("       Ks %s %s %s \n" % (round(mtl.specCol[0],5), round(mtl.specCol[1],5), round(mtl.specCol[2],5)))
               file.write("       illum 1\n")
               
            else:
               file.write("       Ka %s %s %s \n" % (0, 0, 0))
               file.write("       Kd %s %s %s \n" % (1, 1, 1))
               file.write("       Ks %s %s %s \n" % (1, 1, 1))
               file.write("       illum 1\n")

         found = 0

      file.flush()
      file.close()
 


#=====================================================================
def UseLayers(faces, Vertices, MtlList, has_uvco, FILE, ObjName, Name): 
#===================================================================== 
       global vertexcount
       global uvcount
       global multiflag

       uvPtrs = []
       uvList = []

       FILE.write("mtllib %s\n\n" % (Name + ".mtl"))
       FILE.write("g %s\n\n" % (ObjName)) 

       for v in Vertices: 
          vert = v.co 
          if multiflag  == 1:
             vert = Alter(vert, Transform)   
          x, y, z = vert
          FILE.write("v %s %s %s\n" % (x, y, z))

       uv_flag = 0
       for m in range(len(MtlList)):
          for face in faces:
              if face.mat == m:
                 for uv in face.uv:
                    found = 0
                    index = len(uvList)
                    limit = 0
                    if len(uvList)-200 > 0:
                       limit = len(uvList)-200
                    while index > limit and found == 0:
                       uv_value = uvList[index-1]
                       if uv[0] == uv_value[0] and uv[1] == uv_value[1]:
                          uvPtrs.append(index+uvcount)
                          found = 1
                       index = index - 1
                    if found == 0:
                       uvList.append(uv)
                       index = len(uvList)
                       uvPtrs.append(index+uvcount)
                       u, v = uv
                       FILE.write("vt %s %s\n" % (u, v))
                       uv_flag = 1

       if has_uvco and uv_flag == 0:
         for v in Vertices:
            u, v, z = v.uvco
            u = (u-1)/2
            v = (v-1)/2
            FILE.write("vt %s %s\n" % (u, v))

       for v in Vertices: 
          x, y, z = v.no
          FILE.write("vn %s %s %s\n" % (x, y, z))

       total = len(faces)
       p = 0
       uvindex = 0
       for m in range(len(MtlList)):         
          FILE.write("usemtl %s\n" % (MtlList[m].name)) 
          for face in faces:
              if face.mat == m:
                p = p+1
                if (p%1000) == 0:
                   print ("Progress = "+ str(p)+ " of "+ str(total) +" faces")

                FILE.write("f ")
                for index in range(len(face.v)):
                   v = face.v[index].index + vertexcount 
                   if len(face.uv) > 0:
                      FILE.write("%s/%s/%s " % (v+1, uvPtrs[uvindex], v+1))
                      uvindex = uvindex+1
                   elif has_uvco:
                      FILE.write("%s/%s/%s " % (v+1, v+1, v+1))
                   else:
                      FILE.write("%s//%s " % (v+1, v+1))
                FILE.write("\n")

       vertexcount = vertexcount + len(Vertices) 
       print("Export of " +str(ObjName)+ ".obj using material layers finished.\n")

#==================================================================
def UseMtl(faces, Vertices, MtlList, has_uvco, FILE, ObjName, Name): 
#==================================================================
       global vertexcount
       global multiflag

       FILE.write("mtllib %s\n\n" % (Name + ".mtl")) 
       FILE.write("o %s\n\n" % (ObjName))
       
       index = 0
       VertexList = []
       for vertex in Vertices:
          VertexList.append(-1)
          index = index + 1
       print("number of vertices is " +str(len(VertexList)))

       Totalindex = 0
       ix = 0
       NewVertexList = []
       NewVertexCo = []
       for m in range(len(MtlList)):
           # Group name is the name of the mesh 
           if MtlList[m]:
              FILE.write("g %s\n" % (MtlList[m].name+str(m+1))) 
           else:
              FILE.write("g %s\n" % ("Null"+str(m+1)))
           FILE.write("s off\n\n") 
         
           FILE.write("usemtl %s\n\n" % (MtlList[m].name)) 
 
           for face in faces:
              if face.mat == m:
                 for vertex in face.v:
                    v = vertex.index 
                    if VertexList[v] < 0:
                       VertexList[v] = Totalindex
                       NewVertexList.append(v)
                       Totalindex = Totalindex + 1
 
           for v_old in NewVertexList:
              vert = Vertices[v_old].co
              if multiflag  == 1:
                vert = Alter(vert, Transform)
              x, y, z = vert
              FILE.write("v %s %s %s\n" % (x, y, z))
              NewVertexCo.append([x,y,z])

           if has_uvco:
              for v_old in NewVertexList:
                 u, v, z = Vertices[v_old].uvco
                 u = (u-1)/2
                 v = (v-1)/2              
                 FILE.write("vt %s %s\n" % (u, v))

           for v_old in NewVertexList:
              x, y, z = Vertices[v_old].no
              FILE.write("vn %s %s %s\n" % (x, y, z))
  
           for face in faces:
             if face.mat == m:
                FILE.write("f ")
                for index in range(len(face.v)):
                   v = face.v[index].index
                   v_new = VertexList[v] 
                   if has_uvco:
                      FILE.write("%s/%s/%s " % (v_new+1, v_new+1, v_new+1))
                   else:
                      FILE.write("%s//%s " % (v_new+1, v_new+1))
                FILE.write("\n")

           FILE.write("\n")

           NewVertexList = []
           print("Group " +str(m+1)+ " of " +str(len(MtlList))+ " finished.")
 
       print("Export of " +str(ObjName)+ ".obj using groups finished.\n")

#####################################################################################################################

#Code to export all objects correctly

def modobj(n):
	Filename = """%s\%s.obj""" % (Directory,nameis)
	FILE = open(Filename, 'w')
	Objectitem = Blender.Object.Get(nameis)
	Objects = [Objectitem]
	ObjExport (FILE, 5, Objects)

if number < noofthings:
	for a in listofthings:
		if a.getType() == "Mesh":
			nameis = a.getName()
			modobj(nameis)

####################################################################################################################

fp.close()
