
'JModel2Null
'javier@internarte.com
'Some Imports bring hierarchies into XSI as Models instead of Null Parents
'This script replaces the Models for Nulls keeping hierarchy, position,rotation and scale.
'Use: select all models you want cleaned up as nulls and run
'********************************************************************************
option Explicit



dim oSel, oItem, oChild, oChildren, oObj, oNul, index
dim oRoot,oNewKids,oPar,oName,oXpos,oYpos,oZpos,oXscl,oYscl,oZscl,oXRot,oYRot,oZrot

Set oRoot = ActiveProject.ActiveScene.Root

set oSel = CreateObject("XSI.Collection")		'collection of Initial Selection

oSel.additems selection
FOR index = 0 to oSel.Count -1
		set oItem = oSel(index)
		oName = oItem.Name
		if oItem.type = "#model" then
			oXpos = GetValue (oItem &".kine.global.pos.posx")
			oYpos = GetValue (oItem &".kine.global.pos.posy")
			oZpos = GetValue (oItem &".kine.global.pos.posz")

			oXscl = GetValue (oItem &".kine.global.scl.sclx")
			oYscl = GetValue (oItem &".kine.global.scl.scly")
			oZscl = GetValue (oItem &".kine.global.scl.sclz")
			
			oXrot = GetValue (oItem &".kine.global.rotx")
			oYrot = GetValue (oItem &".kine.global.roty")
			oZrot = GetValue (oItem &".kine.global.rotz")
		set oPar = oItem.parent
			GetPrim "Null", oName&"_m2n"
			set oNul = selection(0)
				setvalue oNul	&".kine.global.pos.posx", oXpos	
				setvalue oNul	&".kine.global.pos.posy", oYpos
				setvalue oNul	&".kine.global.pos.posz", oZpos
				setvalue oNul	&".kine.global.rotx", oXrot	
				setvalue oNul	&".kine.global.roty", oYrot
				setvalue oNul	&".kine.global.rotz", oZrot
				setvalue oNul	&".kine.global.scl.sclx", oXscl	
				setvalue oNul	&".kine.global.scl.scly", oYscl
				setvalue oNul	&".kine.global.scl.sclz", oZscl			
			set oNewKids = CreateObject("XSI.Collection")	
				
			set oChildren = oItem.findchildren
			
				for each oChild in oChildren
					set oPar = oChild.parent
					if oPar = oItem then
						oNewKids.add oChild
						logmessage oChild
					end if
					oNul.addChild oNewKids
				
				NEXT
				deleteobj oItem
		end if
			DeselectAll
			
NEXT
