
#coding=UTF-8
import xml.etree.ElementTree as ET
from xml.dom import minidom
#from xml.dom.minidom import parse
import os

class bone_node(object):
	ObjectName = ""
	LimbName = ""
	LinkIndex = 1 
	copy_type = 0
	NodeName = None

	customized_object = None
	control_object = None
	ctr_transform = None
	cust_transform = None
	
	def __init__(self,LimbName = "",LinkIndex = "",NodeName ="",ObjectName=""):
		self.LimbName = LimbName
		self.LinkIndex = LinkIndex
		self.NodeName = NodeName
		self.ObjectName = ObjectName

	@property
	def __dict__(self):
		
		dict_ = {}
		dict_["objectName"] = u"{0}".format(self.ObjectName) 
		dict_["LimbName"] = u"{0}".format(self.LimbName) 
		dict_["LinkIndex"] = u"{0}".format(self.LinkIndex) 
		dict_["copy_type"] = u"{0}".format(self.copy_type) 
		dict_["NodeName"] = u"{0}".format(self.NodeName)
		
		return dict_
	
class ui_button(bone_node):
	x = 0
	y = 0
	w = 0
	h = 0 
	displayText = ""
	
	border_radius = 5 
	
	copy_type1 = 0
	transform = None
	OffsetTm = None
	Offsetbm = None

	color_index = 1
	
	
	@property
	def __dict__(self):
		dict_ = {}
		dict_["objectName"] = u"{0}".format(self.ObjectName) 
		dict_["LimbName"] = u"{0}".format(self.LimbName) 
		dict_["LinkIndex"] = u"{0}".format(self.LinkIndex) 
		dict_["copy_type"] = u"{0}".format(self.copy_type) 
		dict_["customized_object"] = u"{0}".format(self.customized_object) 
		dict_["control_object"] = u"{0}".format(self.control_object) 
		dict_["copy_type"] = u"{0}".format(self.copy_type1) 
		dict_["OffsetTm"] = u"{0}".format(self.OffsetTm)
		dict_["Offsetbm"] = u"{0}".format(self.Offsetbm)
		dict_["ctr_transform"] = u"{0}".format(self.ctr_transform)
		dict_["cust_transform"] = u"{0}".format(self.cust_transform) 
		return dict_
	@property
	def to_action(self):
		dict_ = {}
		# dict_["objectName"] = f"{self.ObjectName}" 
		# dict_["LimbName"] = f"{self.LimbName}" 
		# dict_["LinkIndex"] = f"{self.LinkIndex}" 
		# dict_["copy_type"] = f"{self.copy_type}" 
		# dict_["copy_type1"] = f"{self.copy_type1}" 
		
		# dict_["customized_object"] = f"{self.customized_object}" 
		# dict_["control_object"] = f"{self.control_object}" 
		# dict_["OffsetTm"] = f"{self.OffsetTm}" 
		# dict_["Offsetbm"] = f"{self.Offsetbm}" 
		# dict_["cust_transform"] = f"{self.cust_transform}" 
		# dict_["ctr_transform"] = f"{self.ctr_transform}" 

		return dict_

def button_to_node(bt):
	itme = bone_node()
	itme.ObjectName = bt.objectName()
	itme.LimbName = bt.property('LimbName')
	itme.LinkIndex = bt.property('LinkIndex')
	itme.copy_type = bt.property("copy_type")
	#itme.customized_object = bt.property('customized_object')
	#itme.control_object = bt.property('control_object')

	
	return itme

def open_ui_map(filepath):
	file = open(filepath, 'r')
	lines = file.readlines()
	result = []
	for line in lines:
		st01 = line.strip('[').rstrip().strip(']').replace("'",'')
		st = st01.rstrip().split(",")
		result.append(st)

	file.close()

	buttons = []
	for line in result:
		widget = ui_button()
		widget.w = int(line[1].lstrip())
		widget.h = int(line[0])
		widget.x = int(line[3].lstrip())
		widget.y = int(line[4].lstrip())
		widget.displayText = line[6].lstrip()
		widget.ObjectName = " {0}".format(line[5].lstrip())

		widget.LimbName = line[8].lstrip()
		widget.LinkIndex = line[9].lstrip()
		widget.border_radius = int(line[7].lstrip())
		widget.copy_type = int(line[10].lstrip())
		widget.transform = None
		widget.OffsetTm = None
		widget.color_index = int(line[7].lstrip())
		buttons.append(widget)

	return buttons
	
def save_xml(filepath,data,type_name):
	
	rootNode = ET.Element("Root" , {"name":type_name,"childNum":"{0}".format(len(data)),"version":"2"})
	for i in data:
		child = ET.Element(i.LimbName ,i.__dict__)
		rootNode.append(child)
	
	xml_str = ET.tostring(rootNode, encoding='utf-8')
	formatted_xml = minidom.parseString(xml_str).toprettyxml(indent="  ")

	with open(filepath, mode='w') as f:
		f.write(formatted_xml)
		f.seek(0)  # 这里需要将文件指针指向文件头，否则下面的read函数无法读取文件。

	formatted_xml = None
	xml_str = None
	rootNode = None


def set_actor_button_by_node(cust_btn,control_bnt,node):
	cust_btn.setProperty("NodeName",node.customized_object)
	control_bnt.setProperty("NodeName",node.control_object)
	
	cust_btn.setProperty("copy_type",int(node.copy_type))
	cust_btn.setProperty("transform",node.cust_transform)
	cust_btn.setProperty("OffsetTm",node.OffsetTm)
	
	control_bnt.setProperty("copy_type",int(node.copy_type1))
	control_bnt.setProperty("transform",node.ctr_transform)
	control_bnt.setProperty("OffsetTm",node.Offsetbm)
	

def set_button_by_ui_button(btn,node):
	btn.setProperty("NodeName",node.NodeName)
	btn.setProperty("copy_type",int(node.copy_type))

def new_ui_buuton_form_child(child):
	n = ui_button()
	n.LimbName =  child.tag
	n.LinkIndex = child.attrib.get("LinkIndex","1")
	n.ObjectName = child.attrib.get("objectName","")
	n.copy_type = child.attrib.get("copy_type","0")
	n.OffsetTm = child.attrib.get("OffsetTm","")
	n.Offsetbm = child.attrib.get("Offsetbm","")
	n.copy_type1 = child.attrib.get("copy_type1","0")

	n.ctr_transform = child.attrib.get("ctr_transform","")
	n.cust_transform = child.attrib.get("cust_transform","")

	n.control_object = child.attrib.get("control_object","")
	n.customized_object = child.attrib.get("customized_object","")
	return n 


def opne_actor_xml(filepath):
	tree = ET.parse(filepath)
	rootNode = tree.getroot()
	nodes = []

	xml_ver = rootNode.attrib.get("version","1")
	xml_type = rootNode.attrib.get("name","None")

	if xml_type in "action":
		pass
	else:
		return nodes
	
	# if xml_ver in "2":
	# 	pass
	# else:

	for child in rootNode:
		nodes.append(new_ui_buuton_form_child(child))

	return nodes
			


def open_xml(filepath,type_name=""):
	tree = ET.parse(filepath)
	rootNode = tree.getroot()
	nodes = []
	
	xml_ver = rootNode.get("version","1")
	xml_type = rootNode.attrib.get("name","None")

	if xml_type in "anatomy":
		if xml_ver in "2":
			for child in rootNode:
				n = bone_node(child.tag,child.attrib.get("LinkIndex","1"),child.attrib.get("NodeName",""),child.attrib.get("objectName",""))
				n.copy_type =  child.attrib.get("copy_type","1")
				nodes.append(n)
	
	
	if xml_type in ["control","customized"]:
		
		for child in rootNode:
			n = bone_node(child.tag,child.attrib.get("LinkIndex",""),"",child.attrib.get("objectName",""))
			n.copy_type = child.attrib.get("copy_type",0)
			
			att_name = "{0}_object".format(xml_type)
			nodeName = child.attrib.get(att_name,"")
			if len(nodeName) > 0:
				n.NodeName =  nodeName
				nodes.append(n)



	return nodes


if __name__ == "__main__":
	# test_ui = r"C:\ProgramData\Autodesk\ApplicationPlugins\NDToolsBox\Joe_Anim_Picker\Map_Biped.txt"
	# a = open_ui_map(test_ui)
	# print(a)
	
	# test = r"C:\ProgramData\Autodesk\ApplicationPlugins\NDToolsBox\Joe_Anim_Picker\master\actor_blender_biped.xml"
	# a = opne_actor_xml(test)
	# for i in a :
	# 	print("{0} ,{1} , {2} ,{3}".format(i.ObjectName,i.LinkIndex,i.control_object,i.customized_object))

	test = r"C:\ProgramData\Autodesk\ApplicationPlugins\NDToolsBox\Joe_Anim_Picker\master\control_Bip001.xml"
	a = open_xml(test)
	for i in a :
		print("{0} ,{1} , {2} ,{3},{4}".format(i.ObjectName,i.LinkIndex,i.control_object,i.customized_object,i.NodeName))