macroScript zoa_path_editor category: "zoa scripts"
--------------------------------------
--	zoa path editor for maps, ies and vrayproxies
--	2009 by zoa                    
--	Balogh Andras Miklos      
--  	http://www.zoa.hu           
--------------------------------------
--   	v 1.01
--  	Last Modified On: 10/10/2010
--   	Created On: 17/11/209
-------------------------------------------------------------------------------
--   	Description:
--
--	Simple Path relinker application for maps, ies and vrayproxy objects. Features are: path text replacing and copy functions.
--	The two text fields below each section are for "original" and for "future" paths.
--	$ stand for string. Use it to copy a map path of a selected map to the desired text field.
-------------------------------------------------------------------------------
--	Notes and limitations:
--
--	HDRs not working yet
--	If a "bitmap map" doesn't have a map defined the script halts. - working on this one.
-------------------------------------------------------------------------------
--	Revision History:
--
--  v 1.01 - 24/11/2010 - fixed a bug with undefined maps - Thanks to Maelvon 
--	v 1.00 - 10/10/2010 - first public version released
-------------------------------------------------------------------------------
(
global main
global map_list= #()
global ies_list=#()
global proxy_list=#()
global proxy_file_list=#()
global missing_maps=#()
global temp_maps =#()
global name_list =#()
	
fn collect_map_list =
    (
    map_list=#()
    for m in getClassInstances BitmapTexture do --(if (findItem map_list mapfileN) == 0 do (append map_list m.filename))
        (
            local mapfileN = m.filename as string
            if (mapfileN != "undefined") do
                (
                local index = findItem map_list mapfileN
                if index == 0 do append map_list mapfileN
                )
        )
    sort map_list
    )
	
collect_map_list ()
if map_list.count <=30 then (list_height1 = map_list.count+1) else (list_height1 =30)
	
	
fn collect_proxy_list =
	(
	print "hahahahahaha"
	proxy_list=#()
	proxy_file_list=#()
	for obj in geometry where classof obj == VRayProxy do
		(
			ok=true
			for arg in proxy_list do 
				(
				if (areNodesInstances obj arg) do	ok=false
				)
			if ok do (
			append proxy_list obj
			appendifunique proxy_file_list obj.filename
			)
		)
	)
	
collect_proxy_list ()
if proxy_file_list.count <=30 then (list_height3 = proxy_file_list.count+1) else (list_height3 =30)


fn addmap mapfile =
		(
		appendifunique missing_maps mapfile
		)

	
fn search_missing_ =
	(
	missing_maps=#()
	enumeratefiles addmap #missing
	)
	
	
	
local instance_list = #()	
	
fn collect_ies_list =	
	(
	ies_list= #()
	light_list = for obj in lights where iskindof obj light collect obj
	name_list=#()
	instance_list = #()	
	
	for obj in light_list do
		(
			ok=true
			for arg in instance_list do 
				(
				
				if (areNodesInstances obj arg) do	(ok=false)
				)
			if ok do (append instance_list obj ; append name_list obj.name)
		)
	
	for obj in instance_list do
		(
		if ((classof obj==Target_Light) or  (classof obj==Free_Disc) or (classof obj==Free_Light) or (classof obj==Target_Disc)) do (appendifunique ies_list obj.webFile)
		if classof obj==VRayIES  do (appendifunique ies_list obj.ies_file)
		
		)
		
	)
	
collect_ies_list ()
if ies_list.count <=30 then (list_height2 = ies_list.count+1) else (list_height2 =30)
	
	
	
if main != undefined do (closeRolloutFloater Main)
Main = newRolloutFloater "map path editor" 545 (list_height1*13+115+list_height2*13+115+list_height3*13+115)
main.pos = [100,100]
rollout maps "maps" 
	(
	multiListBox map_list_box pos:[5,5] width:521 height:list_height1 items:map_list
	
	edittext old_path "" pos:[2,(list_height1*13+15)] width:473 height:17
	button old_path_browse "..." pos:[480,(list_height1*13+15)] width:20 height:20	
	button get_old_path "$" pos:[505,(list_height1*13+15)] width:20 height:20	
		
	button re_fresh "refresh" pos:[5,(list_height1*13+40)] width:100 height:20
	button sel_miss "select missings" pos:[110,(list_height1*13+40)] width:100 height:20
	button set_path "set new path" pos:[215,(list_height1*13+40)] width:100 height:20
	button change_path "replace with" pos:[320,(list_height1*13+40)] width:100 height:20
	button copy_file "copy files to" pos:[425,(list_height1*13+40)] width:100 height:20
	
	edittext new_path "" pos:[2,(list_height1*13+63)] width:473 height:17
	button new_path_browse "..." pos:[480,(list_height1*13+63)] width:20 height:20	
	button get_new_path "$" pos:[505,(list_height1*13+63)] width:20 height:20		
		
	
	on old_path_browse pressed do
		(
		local dir = getSavePath caption:"new path" initialDir:(old_path.text)
		if (dir != undefined) do (old_path.text = (dir+"\\" ))
		)
		
	on get_old_path pressed do
		(
		i= amin (map_list_box.selection as array)
		if i !=undefined do (local dir = getfilenamePath map_list[i])
		if (dir != undefined) do (old_path.text = (dir))
		)
		
	on new_path_browse pressed do
		(
		local dir = getSavePath caption:"new path" initialDir:(new_path.text)
		if (dir != undefined) do (new_path.text = (dir+"\\" ))
		)
		
	on get_new_path pressed do
		(
		i= amin (map_list_box.selection as array)
		if i !=undefined do (local dir = getfilenamePath map_list[i])
		if (dir != undefined) do (new_path.text = (dir))
		)
		
		
	on re_fresh pressed do		(collect_map_list (); map_list_box.items=map_list)--; map_count.text=map_list.count as string)	
		
	on sel_miss pressed do
		(	
		missing_maps=#()
		enumeratefiles addmap #missing
		temp_maps = #()
		for i =1 to  map_list_box.items.count do
			(
			for j = 1 to  missing_maps.count do
				(
				if missing_maps[j] == map_list_box.items[i] do ( append temp_maps i)
				)
			)
		map_list_box.selection=temp_maps as bitarray
		)		
		
	on set_path pressed do
		(
		for m in getClassInstances BitmapTexture do
			(

			for i in (map_list_box.selection as array) do
				(
				if m.filename == map_list[i] do ( m.fileName=(new_path.text) + (filenameFromPath m.fileName))
				)
			)
		 collect_map_list (); map_list_box.items=map_list
		map_list_box.selection=#{}
		)		
		
	on change_path  pressed do
		(
		for m in getClassInstances BitmapTexture do
			(
			for i in (map_list_box.selection as array) do
				(
				if m.filename == map_list[i] do 
					(	
					if (findstring m.filename old_path.text)!=undefined do
						(
						temp_text1=substring m.filename 1 ((findstring m.filename old_path.text)-1)
						temp_text2=new_path.text
						temp_text3=substring m.filename (old_path.text.count+(findstring m.filename old_path.text)) (m.filename.count-old_path.text.count-(findstring m.filename old_path.text)+1)
						m.fileName=temp_text1+temp_text2+temp_text3
						)
					)
				)
			)
		 collect_map_list (); map_list_box.items=map_list
		map_list_box.selection=#{}
		)
	
	on copy_file pressed do
		(
		for i in (map_list_box.selection as array) do
			(
			m=0
			for j =1 to map_list[i].count do (if map_list[i][j]=="\\" do m=j)
			file_name=substring map_list[i] (m+1) (map_list[i].count-m)
			copyFile map_list[i] (new_path.text+file_name)			
			)
		)	

	)
	
	

rollout ies "ies" 
	(
	
	multiListBox ies_list_box pos:[5,5] width:521 height:list_height2 items:ies_list
	
	edittext old_path "" pos:[2,(list_height2*13+15)] width:473 height:17
	button old_path_browse "..." pos:[480,(list_height2*13+15)] width:20 height:20	
	button get_old_path "$" pos:[505,(list_height2*13+15)] width:20 height:20	
		
	button re_fresh "refresh" pos:[5,(list_height2*13+40)] width:100 height:20
	button sel_miss "select missings" pos:[110,(list_height2*13+40)] width:100 height:20
	button set_path "set new path" pos:[215,(list_height2*13+40)] width:100 height:20
	button change_path "replace with" pos:[320,(list_height2*13+40)] width:100 height:20
	button copy_file "copy files to" pos:[425,(list_height2*13+40)] width:100 height:20
	
	edittext new_path "" pos:[2,(list_height2*13+63)] width:473 height:17
	button new_path_browse "..." pos:[480,(list_height2*13+63)] width:20 height:20	
	button get_new_path "$" pos:[505,(list_height2*13+63)] width:20 height:20			
			
	on old_path_browse pressed do
		(
		local dir = getSavePath caption:"new path" initialDir:(old_path.text)
		if (dir != undefined) do (old_path.text = (dir+"\\" ))
		)
		
	on get_old_path pressed do
		(
		i= amin (ies_list_box.selection as array)
		if i !=undefined do (local dir = getfilenamePath ies_list[i])
		if (dir != undefined) do (old_path.text = (dir))
		)
		
	on new_path_browse pressed do
		(
		local dir = getSavePath caption:"new path" initialDir:(new_path.text)
		if (dir != undefined) do (new_path.text = (dir+"\\" ))
		)
		
	on get_new_path pressed do
		(
		i= amin (ies_list_box.selection as array)
		if i !=undefined do (local dir = getfilenamePath ies_list[i])
		if (dir != undefined) do (new_path.text = (dir))
		)
		
		
	on re_fresh pressed do		(collect_ies_list ())--; map_count.text=map_list.count as string)	
		
	on sel_miss pressed do
		(
		temp_ies = #()
		for i =1 to  ies_list_box.items.count do
			(
			if (doesfileexist ies_list_box.items[i])!=true do ( append temp_ies i)
			)
		ies_list_box.selection=temp_ies as bitarray
		)		
		
	on set_path pressed do
		(
		for obj in instance_list do
			(
			for i in (ies_list_box.selection as array) do
				(
				
				if ((classof obj==Target_Light) or  (classof obj==Free_Disc) or (classof obj==Free_Light) or (classof obj==Target_Disc)) do
					(
					if obj.webFile==ies_list[i] do 
						(
						obj.webFile=(new_path.text) + (getfilenamefile ies_list[i])+".ies"
						)					
					)
					
				if classof obj==VRayIES do 
					(
					if obj.ies_file==ies_list[i] do
						(
						obj.ies_file=(new_path.text) + (getfilenamefile ies_list[i])+".ies"
						)
					)
				)
			)
		 collect_ies_list (); ies_list_box.items=ies_list
		ies_list_box.selection=#{}
		)	
		
		
	on change_path  pressed do
		(
		for obj in instance_list do
			(
			for i in (ies_list_box.selection as array) do
				(
				
				if ((classof obj==Target_Light) or  (classof obj==Free_Disc) or (classof obj==Free_Light) or (classof obj==Target_Disc)) do
					(
					if obj.webFile==ies_list[i] do 
						(
						temp_text1=substring obj.webFile 1 ((findstring obj.webFile old_path.text)-1)
						temp_text2=new_path.text
						temp_text3=substring obj.webFile (old_path.text.count+(findstring obj.webFile old_path.text)) (obj.webFile.count-old_path.text.count-(findstring obj.webFile old_path.text)+1)
						obj.webFile=temp_text1+temp_text2+temp_text3
						)					
					)
					
				if classof obj==VRayIES do 
					(
					if obj.ies_file==ies_list[i] do
						(
						temp_text1=substring obj.ies_file 1 ((findstring obj.ies_file old_path.text)-1)
						temp_text2=new_path.text
						temp_text3=substring obj.ies_file (old_path.text.count+(findstring obj.ies_file old_path.text)) (obj.ies_file.count-old_path.text.count-(findstring obj.ies_file old_path.text)+1)
						obj.ies_file=temp_text1+temp_text2+temp_text3
						)
					)
				)
			)
		 collect_ies_list (); ies_list_box.items=ies_list
		ies_list_box.selection=#{}
		)	
	
	on copy_file pressed do
		(
		for i in (ies_list_box.selection as array) do
			(
			m=0
			for j =1 to ies_list[i].count do (if ies_list[i][j]=="\\" do m=j)
			file_name=substring ies_list[i] (m+1) (ies_list[i].count-m)
			copyFile ies_list[i] (new_path.text+file_name)			
			)
		)	
	)

	
	
	
	
rollout proxy "vray proxies" 
	(
	multiListBox proxy_list_box pos:[5,5] width:521 height:list_height3 items:proxy_file_list
	
	edittext old_path "" pos:[2,(list_height3*13+15)] width:473 height:17
	button old_path_browse "..." pos:[480,(list_height3*13+15)] width:20 height:20	
	button get_old_path "$" pos:[505,(list_height3*13+15)] width:20 height:20	
		
	button re_fresh "refresh" pos:[5,(list_height3*13+40)] width:100 height:20
	button sel_miss "select missings" pos:[110,(list_height3*13+40)] width:100 height:20
	button set_path "set new path" pos:[215,(list_height3*13+40)] width:100 height:20
	button change_path "replace with" pos:[320,(list_height3*13+40)] width:100 height:20
	button copy_file "copy files to" pos:[425,(list_height3*13+40)] width:100 height:20
	
	edittext new_path "" pos:[2,(list_height3*13+63)] width:473 height:17
	button new_path_browse "..." pos:[480,(list_height3*13+63)] width:20 height:20	
	button get_new_path "$" pos:[505,(list_height3*13+63)] width:20 height:20			
			
	on old_path_browse pressed do
		(
		local dir = getSavePath caption:"new path" initialDir:(old_path.text)
		if (dir != undefined) do (old_path.text = (dir+"\\" ))
		)
		
	on get_old_path pressed do
		(
		i= amin (proxy_list_box.selection as array)
		if i !=undefined do (local dir = getfilenamePath proxy_file_list[i])
		if (dir != undefined) do (old_path.text = (dir))
		)
		
	on new_path_browse pressed do
		(
		local dir = getSavePath caption:"new path" initialDir:(new_path.text)
		if (dir != undefined) do (new_path.text = (dir+"\\" ))
		)
		
	on get_new_path pressed do
		(
		i= amin (proxy_list_box.selection as array)
		if i !=undefined do (local dir = getfilenamePath proxy_file_list[i])
		if (dir != undefined) do (new_path.text = (dir))
		)
		
		
	on re_fresh pressed do		(collect_proxy_list (); proxy_list_box.items=proxy_file_list)
		
	on sel_miss pressed do
		(
		temp_proxies = #()
		for i =1 to  proxy_list_box.items.count do
			(
			if (doesfileexist proxy_list_box.items[i])!=true do ( append temp_proxies i)
			)
		proxy_list_box.selection=temp_proxies as bitarray
		)	
		
	on change_path  pressed do
		(
		for m in proxy_list do
			(
			for i in (proxy_list_box.selection as array) do
				(
				if m.filename == proxy_file_list[i] do 
					(	
					if (findstring m.filename old_path.text)!=undefined do
						(
						temp_text1=substring m.filename 1 ((findstring m.filename old_path.text)-1)
						temp_text2=new_path.text
						temp_text3=substring m.filename (old_path.text.count+(findstring m.filename old_path.text)) (m.filename.count-old_path.text.count-(findstring m.filename old_path.text)+1)					
						m.fileName=temp_text1+temp_text2+temp_text3
						)
					)
				)
			)
		 collect_proxy_list (); proxy_list_box.items=proxy_file_list
		proxy_list_box.selection=#{}
		)
	
	on set_path pressed do
		(
 		for m in proxy_list do
 			(
			for i in (proxy_list_box.selection as array) do
 				(
				if m.filename == proxy_file_list[i] do
					(
					m.fileName=(new_path.text) + (getfilenamefile proxy_file_list[i])+".vrmesh"
					)
				)
 			)
 		collect_proxy_list (); proxy_list_box.items=proxy_file_list
 		proxy_list_box.selection=#{}
		)

	on copy_file pressed do
		(
		for i in (proxy_list_box.selection as array) do
			(
			m=0
			for j =1 to proxy_file_list[i].count do (if proxy_file_list[i][j]=="\\" do m=j)
			file_name=substring proxy_file_list[i] (m+1) (proxy_file_list[i].count-m)
			copyFile proxy_file_list[i] (new_path.text+file_name)			
			)
		)	
		
		
		
		
	)



	


	
Addrollout maps main
Addrollout ies main
Addrollout proxy main
	

) 