import maya.OpenMaya as OpenMaya

class Vector(OpenMaya.MVector):

	def __init__(self, *args, **kwargs ):
		
		if len(args)==0:
			super(Vector,self).__init__()

		elif len(args)==1:
			if isinstance(args[0],list) and len(args[0])==3:
				super(Vector,self).__init__(args[0][0],args[0][1],args[0][2])
			elif isinstance(args[0],OpenMaya.MVector):
				super(Vector,self).__init__(args[0])
			elif isinstance(args[0],Vector):
				super(Vector,self).__init__(args[0].x,args[0].y,args[0].z)

		elif len(args)==3:
			super(Vector,self).__init__(args[0],args[1],args[2])

	def __str__(self):
		return '[%s,%s,%s]' % ( self.x, self.y, self.z )
		#return '['+str(self.x)+', '+str(self.y)+', '+str(self.z)+']'

	def __sub__(self,value):
		if isinstance(value,OpenMaya.MVector):
			return Vector(self.x-value.x,self.y-value.y,self.z-value.z)

	def __mul__(self, value):
		if isinstance(value, OpenMaya.MMatrix):
			#return Vector(OpenMaya.MVector(self.x,self.y,self.z)*value)
			return Vector(super(Vector,self).__mul__(value))

class Matrix(OpenMaya.MMatrix):

	def __init__(self, *args):
		# constructor
		if not args:
			super(Matrix,self).__init__()
		# initialize
		else:
			if len(args)==1 and isinstance(args[0],OpenMaya.MMatrix):
				super(Matrix,self).__init__(args[0])

			if len(args)==16:
				mMatrix = OpenMaya.MMatrix()
				OpenMaya.MScriptUtil().createMatrixFromList(args,mMatrix)
				super(Matrix,self).__init__(mMatrix)

			if len(args)==4:

				v0 = Vector(args[0])
				v1 = Vector(args[1])
				v2 = Vector(args[2])
				v3 = Vector(args[3])

				m = [v0.x,v0.y,v0.z,0,v1.x,v1.y,v1.z,0,v2.x,v2.y,v2.z,0,v3.x,v3.y,v3.z,0]
				mMatrix = OpenMaya.MMatrix()
				OpenMaya.MScriptUtil().createMatrixFromList(m,mMatrix)
				super(Matrix,self).__init__(mMatrix)

	def __str__(self):
		return '[%s]' % ','.join( [str(f) for f in self.get()] )
		#return '['+','.join([str(f) for f in self.get()])+']'
		#return '['+str(self(0,0))+', '+str(self(0,1))+', '+str(self(0,2))+', '+str(self(0,3))+', '+str(self(1,0))+', '+str(self(1,1))+', '+str(self(1,2))+', '+str(self(1,3))+', '+str(self(2,0))+', '+str(self(2,1))+', '+str(self(2,2))+', '+str(self(2,3))+', '+str(self(3,0))+', '+str(self(3,1))+', '+str(self(3,2))+', '+str(self(3,3))+']'

	def __mul__(self, value):
		if isinstance(value, OpenMaya.MMatrix):
			#m = [self(0,0),self(0,1),self(0,2),self(0,3),self(1,0),self(1,1),self(1,2),self(1,3),self(2,0),self(2,1),self(2,2),self(2,3),self(3,0),self(3,1),self(3,2),self(3,3)]
			#mMatrix = OpenMaya.MMatrix()
			#OpenMaya.MScriptUtil().createMatrixFromList(m,mMatrix)	
			#return Matrix(mMatrix*value)

			return Matrix(super(Matrix,self).__mul__(value))

	def __getitem__(self,index):

		if index>=0 and index<4:
			return self(0,index)
		if index>=4 and index<8:
			return self(1,index+4)
		if index>=8 and index<12:
			return self(2,index+8)
		if index>=12 and index<16:
			return self(3,index+12)

	def get(self):
		"""
		Returns the Matrix as a list
		"""
		return [self(0,0),self(0,1),self(0,2),self(0,3),self(1,0),self(1,1),self(1,2),self(1,3),self(2,0),self(2,1),self(2,2),self(2,3),self(3,0),self(3,1),self(3,2),self(3,3)]

	def inverse(self):

		#m = [self(0,0),self(0,1),self(0,2),self(0,3),self(1,0),self(1,1),self(1,2),self(1,3),self(2,0),self(2,1),self(2,2),self(2,3),self(3,0),self(3,1),self(3,2),self(3,3)]
		#mMatrix = OpenMaya.MMatrix()
		#OpenMaya.MScriptUtil().createMatrixFromList(m,mMatrix)
		#return Matrix(mMatrix.inverse())

		return Matrix(super(Matrix,self).inverse())

	def getTranslate(self):

		return Vector(self(3,0),self(3,1),self(3,2))

	def getRotate(self):

		eRotate = OpenMaya.MTransformationMatrix(self).rotation().asEulerRotation()
		return Vector(eRotate.x*57.2958,eRotate.y*57.2958*pi,eRotate.z*57.2958)

	def getScale(self):

		mMatrix = OpenMaya.MTransformationMatrix(self).asScaleMatrix()
		return Vector(mMatrix(0,0),mMatrix(1,1),mMatrix(2,2))
