macroscript zoa_proxy_switcher
	category: "zoa scripts"

--------------------------------------
--	zoa proxy switcher
--	2010 by zoa                    
--	Andras Onodi (onodi.andras@zoa.hu)
--   	www.zoa.hu                   
--------------------------------------
--   	v 1.20
--	Last Modified On: 02/09/2011
--   	Created On: 18/01/2010
-------------------------------------------------------------------------------
--	Thanks for the ideas and support:
--
--	László Sebő (laszlo.sebo@gmail.com)
--	András Balogh (balogh.andras@zoa.hu) - for almost rewriting the whole thing at v1.20
--	Andrei Kletskov "111" (andklv@rambler.ru)
-------------------------------------------------------------------------------
--   	Description:
--
--	Select a bunch of objects and put unique nodes to a specified directory as separate max files with the related vrayproxy data.
--	Instance copies will be replaced with the new vrayproxy object
--	Get the previously saved objects back to max whenever you wish.
-------------------------------------------------------------------------------
--	Notes and limitations:
--
--	Instanced objects must end "kakimaki00" or "kakimaki_00". Any numbers at the end.
--	If exporting instances: "objectname_xx" will be exported as objectname.max - without the numbers
--	Don't use the name "objectname_xy" if it's not an instance of "objectname_xz". Ohterwise the script will try to overwrite the "objectname.max" file.
--	Reference copys not supported yet. They do make the script crcash!
--	Never Tested on groups
--	Trying to get a maxobject back to an invisible layer doesn't work yet.
-------------------------------------------------------------------------------
--	Revision History:
--	v 1.20 - 02.09.2011 - new simplified UI, works with linked objects, works with any numberings, wire colors stay the same, local separators can be "_","-",".", " ", project path is being remembered (Thanks for the fixes Andras Miklos!!!)
--	v 1.01 - 28.01.2010 - Minor bugfixes: fixed the mysterious kakimaki-bug, fixed the path-paste bug.
--	v 1.00 - 22.01.2010 - Added The "Back To Max" Function, Added filtering for geometry/vrayproxy objects to avoid crashes
--	v 0.12 - 20.01.2010 - Added postexport functions: show as box, selects the newly created objects
--	v 0.11 - 19.01.2010 - Added functionality for "bunch of objects selection" - added the unique_list filter
--	v 0.10 - 18.01.2010 - Created the original script for saving 1 object to a vrayproxy and replacing the related instances
-------------------------------------------------------------------------------


(
--------------variables

	global unique_list = #()
	global instance_list = #()
	if project_path==undefined then ( global project_path= "C:\\")
	local numbers=#("0","1","2","3","4","5","6","7","8","9")
	local separators=#("_","-",".", " ")

--------------functions
	
fn create_unique_list obj_list=
	(
	unique_list=#()
	for i in obj_list do
		(
		isinlist= false
		for q in unique_list do
			(
			if areNodesInstances q i == true then (isinlist = true)
			)
			if isinlist == false then append unique_list i
		)
	)
	
fn get_instances obj =
	(
	instance_list = #()
	InstanceMgr.GetInstances obj &instances
	instance_list = instances
	)	
	
fn fn_2proxy obj=
	(
	new_name=obj.name
		
	if instance_list.count>1 then
		(
		last_letter=(substring new_name (new_name.count) 1)
		while (finditem numbers last_letter)!=0 do
			(
			new_name=(substring new_name 1 (new_name.count-1)	)
			last_letter=(substring new_name (new_name.count) 1)
			)
		if (finditem separators last_letter)!=0 then
			(
			new_name=(substring new_name 1 (new_name.count-1)	)
			)	
		)
		
	Undo On
	a= copy obj	
	a.name=new_name
	a.pos=[0,0,0]
	if a.parent!=undefined then (a.parent = undefined)

	local proxy_path = project_path + new_name + ".vrmesh"
	local max_path = project_path + new_name + ".max"
	
	if (doesfileexist max_path != false) then q = querybox ("filename: "+ max_path + "\nexists. Overwrite?")
		
		if (q == true) or (doesfileexist max_path == false) then
			(
			saveNodes a max_path
			select a
			vraymeshexport meshFile:proxy_path autoCreateProxies:true exportMultiple:true
			new_proxy = (getnodebyname ("VRayProxy_"+new_name as string))
			setUserProp new_proxy "max_node_path" max_path
			new_proxy.pivot = [0,0,0]
			)
			
	for i in instance_list do
		(
		i.layer.current=TRUE
		a= instance new_proxy
		a.transform = i.transform
		a.name = i.name
		a.material = i.material
		a.wirecolor = i.wirecolor
		setUserProp a "max_node_path" max_path
		if i.parent !=undefined then a.parent=i.parent
		for c in i.children do c.parent=a
		)
		
		delete new_proxy
		for i in instance_list do ( delete i )
	)
	
	
fn fn_2max obj=
	(
	if isMaxFile (getuserprop obj "max_node_path") then 				
		(
		print "1"
		Undo On
		mergeMAXFile (getuserprop obj "max_node_path") #select #noRedraw #useSceneMtlDups #mergeDups
		merged_node = $
		for i in instance_list do
			(

			i.layer.current=true
			a= instance merged_node
			a.name = i.name	
			a.transform = i.transform
			a.material = i.material
			a.wirecolor = i.wirecolor
			if i.parent !=undefined then a.parent=i.parent
			for c in i.children do c.parent=a
			)	
		delete merged_node
		for i in instance_list do ( delete i )
		)else (
		messageBox ("Uppps, the following file:\n" + (getuserprop m "max_node_path" as string) + "\n is not a valid max file.") title:"ZOA_ProxySwitcher Path Error"
		)
			
		
	)
	
	
	
--------------rollout
	
rollout ProxySwitcher "ProxySwitcher" width:308 height:60
	(
		edittext new_path text:"C:\\" pos:[3,5] width:270 height:20
		button btn_browse "..." pos:[280,5] width:20 height:20
		button btn_2proxy "Max To VrayProxy" pos:[5,35] width:95 height:20
		checkbox chk_showasbox "ShowAsBox" pos:[108,36] width:81 height:18 checked:true
		button btn_2max "VrayProxy To Max" pos:[205,35] width:95 height:20
		
	on ProxySwitcher open do
		(
		if project_path!=undefined then ( new_path.text=project_path)	else (global project_path=new_path.text)
		)
		
	on btn_selectunique pressed do
		(
			Undo On
			(
			global unique_list = #()
			create_unique_list selection
			select unique_list
			)
		)
		
	on btn_2proxy pressed do
		(
		create_unique_list selection
		for k in unique_list do
			(
			if classof k != VRayProxy then
				(
				get_instances k
				fn_2proxy k
				)
			)
		)	
		
	on new_path changed h do 
		(
		project_path = h
		)

	on btn_browse pressed do
		(
			local dir = getSavePath caption:"Path" initialDir:(new_path.text)
			if (dir != undefined) do (new_path.text = (dir+"\\" ))
			project_path = new_path.text
		)
		
	on btn_2max pressed do
		(
		create_unique_list selection
		for k in unique_list do
			(
			if classof k == VRayProxy then
				(
				get_instances k
				fn_2max k
				)
			)
		)	
	)	
	
try(destroyDialog ProxySwitcher)catch()
createdialog ProxySwitcher style:#( #style_sysmenu, #style_resizing, #style_minimizebox, #style_titlebar ) pos:[100,100]	
	
) 