#!BPY

""" Registration info for Blender menus: <- these words are ignored
Name: 'Vertex Color Copy'
Blender: 232
Group: 'Object'
Tip: 'Copies the vertex color information from one object to another.'
"""

__author__ = ""
__url__ = ("blender",
"Script's homepage, http://vegastrike.sourceforge.net/wiki/HowTo:Radiosity_baking_in_Blender")
__version__ = "233"

__bpydoc__ = """\
"""

# WHAT DOES IT DO?
# This script copies the vertex color information from one mesh
# to another, and does so correctly assuming the meshes have the
# same geometry, vertex-wise.

import Blender
from Blender import *
from Blender.Draw import *
from Blender.BGL import *

fromObj = "Mesh.rad"		# Set this to the resulting mesh from radio calcuation
toObj = "Mesh.orig"		# Set this to the name of your original, UV-mapped mesh.
fromObjx=Create(fromObj)
toObjx=Create(toObj)


def copy_data():
	global fromObj, toObj
	me_from = Blender.Object.Get(fromObj).getData()
	me_to = Blender.Object.Get(toObj).getData()

	if not me_to and not me_from:
		print "ERROR: Source/destination object does not exist"
		Blender.Draw.PupMenu("ERROR%t|Source/destination object does not exist")

	elif len(me_to.verts) != len(me_from.verts):
		print "ERROR: Source and destination objects must have the same number of vertices"
		Blender.Draw.PupMenu("ERROR%t|Source and destination objects must have the same number of vertices")
	
	else:
		# make two lists sorted on world-space coordinates,
		# containing a face and vertex index i and j respectively
		
		# Transform objects to world space coordinates:
		me_to.transform(Blender.Object.Get(toObj).getMatrix())
		me_from.transform(Blender.Object.Get(fromObj).getMatrix())
	
		l_to = []
		l_from = []
	
		# format: [ (x,y,z, face index, vertex index), ... ]
	
		for i in range(len(me_to.faces)):
			for j in range(len(me_to.faces[i].v)):
				vert = me_to.faces[i].v[j].co
				l_to.append((vert[0], vert[1], vert[2], i, j))
			
			for j in range(len(me_from.faces[i].v)):
				vert = me_from.faces[i].v[j].co
				l_from.append((vert[0], vert[1], vert[2], i, j))
		
		# Sort the lists after vertex coordinates
		l_to.sort()	
		l_from.sort()
	
		for i in range(len(l_to)):
			to_f = l_to[i][3]
			from_f = l_from[i][3]
			to_v = l_to[i][4]
			from_v = l_from[i][4]
		
			me_to.faces[to_f].col[to_v] = me_from.faces[from_f].col[from_v]

		# Transform objects back to object space coordinates:
		me_to.transform(Blender.Object.Get(toObj).getInverseMatrix())
		me_from.transform(Blender.Object.Get(fromObj).getInverseMatrix())

		me_to.update()
		print "Copied vertex color information from object "+fromObj+" to object "+toObj+"."
		Blender.Draw.PupMenu("SUCESS%t|Copied vertex color information from object "+fromObj+" to object "+toObj+".")
  

def EVENT(evt,val):
   pass

def BUTTON(evt):
	if (evt==1):
		Exit()
	elif (evt==2):
		copy_data()
	Blender.Redraw()

def DRAW():
	global fromObj, toObj, fromObjx, toObjx

	glClear(GL_COLOR_BUFFER_BIT)
	glColor3f(0.1, 0.1, 0.15)    

	ligne=20

	Button ("Exit",1,20,1,80,ligne)
	Button ("Copy Data",2,102,1,80,ligne)

	glRasterPos2f(20, ligne*2-10)
	Text("Vertex Color Copy")
	#			String(name, event, x, y, width, height, initial, length, tooltip=None)
	glRasterPos2f(20, ligne*3-5)
	Text("To")
	toObj =		String('OB:', 4, 50, ligne*3-10, 200, 18, toObjx.val, 120, "The object to which the vertex colors should be copied." )
	glRasterPos2f(20, ligne*4-5)
	Text("From")
	fromObj =	String('OB:', 4, 50, ligne*4-10, 200, 18, fromObjx.val, 120, "The object from which the vertex colors should be copied." )
	toObjx=toObj
	fromObjx=fromObj
	toObj=toObjx.val
	fromObj=fromObjx.val


Register(DRAW,EVENT,BUTTON)