# ns_TopNull_To_ObjectSRT
# Initial code generated by Softimage SDK Wizard
# Executed Thu Oct 24 10:43:06 UTC+0200 2013 by freelancer
# 
# Tip: To add a command to this plug-in, right-click in the 
# script editor and choose Tools > Add Command.
import win32com.client
from win32com.client import constants

null = None
false = 0
true = 1

def XSILoadPlugin( in_reg ):
	in_reg.Author = "Niclas Schlapmann"
	in_reg.Name = "ns_TopNull_To_ObjectSRT"
	in_reg.Major = 1
	in_reg.Minor = 0
	
	in_reg.RegisterCommand("ns_TopNull_To_ObjectSRT","ns_TopNull_To_ObjectSRT")
	in_reg.RegisterMenu(constants.siMenuTbGetPrimitiveID , "ns_TopNull_To_ObjectSRT_Menu", False, False)


	#RegistrationInsertionPoint - do not remove this line

	return true

def XSIUnloadPlugin( in_reg ):
	strPluginName = in_reg.Name
	Application.LogMessage(str(strPluginName) + str(" has been unloaded."),constants.siVerbose)
	return true
	
	
def ns_TopNull_To_ObjectSRT_Execute(  ):
	inputName = Application.XSIInputBox("Null Name(s)?", "Input Name", "null")
	oSelection = Application.Selection
	oName=[]
	oParentName=[]
	nulls=[]
	oTrans=[]
	oModelName=[]
	takeSRT = True

	try:
		#Gather
		for i in range(oSelection.count):
			oName.append(oSelection.Item(i).Name)
			oModelName.append(oSelection.Item(i).Model.Name)
			oParentName.append(oSelection.Item(i).Parent3DObject.Name)
			oTrans.append(oSelection.Item(i).Kinematics.Local.Transform)

		#create Nulls / SRT	
		Application.LogMessage(oModelName[i])
		Application.LogMessage(oParentName[i])

		for i in range(len(oName)):
			
			if oParentName[i] == "Scene_Root":
				
				nameresult = Application.GetPrim("Null", inputName, oModelName[i] + "." + "", "")
			else:
				
				nameresult = Application.GetPrim("Null", inputName, oModelName[i] + "." + oParentName[i], "")
				
			
			#set SRTs
			if takeSRT:
				nameresult.Kinematics.Local.Transform = oTrans[i]
			
			nulls.append(nameresult.Name)
			
		#parenting Object to Nulls
		selectString = ""
		for i in range(len(oName)):
			Application.ParentObj(oModelName[i] + "." + nulls[i], oModelName[i] + "." + oName[i])
			selectString = selectString + oModelName[i] + "." + nulls[i] + ","
		
		#selecting
		Application.SelectObj(selectString, "", "")

			
		
	except:
		Application.LogMessage("Upps!", 4)
			
	return true
	
def ns_TopNull_To_ObjectSRT_Menu_Init( in_ctxt ):
	oMenu = in_ctxt.Source
	oMenu.AddCommandItem("ns_TopNull_To_ObjectSRT","ns_TopNull_To_ObjectSRT")
	return true

	



	
	
	
	
	

	