##
#	\namespace	cross3d.softimage.softimagesceneobject
#
#	\remarks	The SoftimageSceneObject class provides the implementation of the AbstractSceneObject class as it applies
#				to Softimage
#
#	\author		douglas
#	\author		Blur Studio
#	\date		04/04/11
#

#------------------------------------------------------------------------------------------------------------------------

from cross3d import application
from cross3d.constants import ObjectType
from PySoftimage import xsi, xsiFactory, constants as xsiConstants
from win32com.client.dynamic import Dispatch as dynDispatch
from cross3d.abstract.abstractsceneobject import AbstractSceneObject

#------------------------------------------------------------------------------------------------------------------------

class SoftimageSceneObject(AbstractSceneObject):

	_nativeToAbstractObjectType = { 'light'         : ObjectType.Light,
									'camera'        : ObjectType.Camera,
									'Thinking'      : ObjectType.Particle | ObjectType.Thinking,
									'PF_Source'     : ObjectType.Particle,
									'FumeFX'		: ObjectType.FumeFX,
									'polymsh' 		: ObjectType.Geometry,
									'surfmsh'		: ObjectType.NurbsSurface,
									'crvlist'		: ObjectType.Curve,
									'#model'		: ObjectType.Model,
									'#Group'		: ObjectType.Group,
									'CameraInterest': ObjectType.CameraInterest }

	_abstractToNativeObjectType = dict((v,k) for k, v in _nativeToAbstractObjectType.iteritems())

	#------------------------------------------------------------------------------------------------------------------------
	# 												protected methods
	#------------------------------------------------------------------------------------------------------------------------
	def _findNativeChild(self, name, recursive=False, parent=None):
		"""
			\remarks	implements the AbstractSceneObject._findNativeChildren method to look up a specific native children for this object
			\return		<PySotimage.xsi.Object> nativeObject
		"""
		return self.nativePointer().FindChild(name, '', '', recursive)

	def _nativeChildren(self, recursive=False, wildcard='', type='', parent='', childrenCollector=[]):
		"""
			\remarks	implements the AbstractSceneObject._nativeChildren method to look up the native children for this object
			\param		recursive <bool> wildcard <string> type <string parent <string> childrenCollector <list>
			\sa			children
			\return		<list> [ <PySoftimage.xsi.Object> nativeObject, .. ]
		"""
		nativeType = ''
		if type != '':
			nativeType = self._nativeTypeOfObjectType(type)
		#return [ obj for obj in self._nativePointer.FindChildren( name, nativeType, parent, recursive ) ]
		return self._nativePointer.FindChildren2(wildcard, nativeType, '', recursive)

	def _nativeParent(self):
		"""
			\remarks	implements the AbstractSceneObject._nativeParent method to look up the native parent for this object
			\sa			parent, setParent, _setNativeParent
			\return		<PySoftimage.xsi.Object> nativeObject || None
		"""
		return self._nativePointer.Parent

	def _setNativeParent(self, nativeParent):
		"""
			\remarks	implements the AbstractSceneObject._setNativeParent method to set the native parent for this object
			\sa			parent, setParent, _nativeParent
			\param		<PySoftimage.xsi.Object> nativeObject || None
			\return		<bool> success
		"""
		if nativeParent is None:
			nativeParent = xsi.ActiveSceneRoot

		# Making sure the object is not already a child of the parent, otherwise Softimage throws an error
		if not self._nativePointer.Parent3DObject.IsEqualTo(nativeParent):
			nativeParent.AddChild(self._nativePointer)
		return True

	def _nativeModel(self):
		"""
			\remarks	implements the AbstractSceneObject._nativeModel method to look up the native model for this object
			\sa			children
			\return		<list> [ <PySotimage.xsi.Model> nativeObject, .. ]
		"""
		model = None
		obj = self.nativePointer()
		ignoreSceneRoot = True
		
		if str(obj.Type) == "#model":
			if ignoreSceneRoot is True and obj.Name == "Scene_Root":
				model = None
			else:
				model = obj
		else:
			try:
				if ignoreSceneRoot is True and obj.Model.Name == "Scene_Root":
					model = None
				else:
					model = obj.Model
			except AttributeError:
				pass

		return model

	def _setNativeModel(self, nativeModel):
		"""
		"""
		if self._nativePointer.Model.IsEqualTo(nativeModel):
			return True

		# Making sure the object is not already a child of the parent, otherwise Softimage throws an error
		if not self._nativePointer.Parent3DObject.IsEqualTo(nativeModel):
			nativeModel.AddChild(self._nativePointer)

		return True

	#------------------------------------------------------------------------------------------------------------------------
	# 												public methods
	#------------------------------------------------------------------------------------------------------------------------

	def isDeleted(self):
		return (self._nativePointer.Parent is None)

	def _constrainingNativeObjects(self):
		constraining = []
		for constraint in self.Kinematics.Constraints:
			constraining += [obj for obj in constraint.Constraining]
		return constraining

	def _constrainedNativeObjects(self):
		constraineds = []

		# TODO: Currently we only support "Pose Constraints".
		constraints = xsi.FindObjects(None, "{D42BBF71-3C47-11D2-8B42-00A024EE586F}")
		for constraint in constraints:

			# This is a weird thing when looking for all object type constraint.
			if constraint.FullName.startswith("TransientObjectContainer") or constraint.Parent3DObject is None:
				continue
				
			# Looping through constraining object.
			for constraining in constraint.Constraining:
				if constraining.isEqualTo(self._nativePointer):
					constraineds.append(constraint.Constrained)
					
		return constraineds
   
	def getCacheName(self, type):
		typeDic = {	"Pc":".pc2",
					"Tmc":".tmc",
					"Abc":".abc",
					"Icecache":".icecache"}


		obj = self._nativePointer
		name = obj.Fullname
		cacheName = name.replace("." , "")