macroScript TransferMaterialsUI
	category:"ProgressiveFX"
	tooltip:"Transfer Materials"
	(

	rollout CopyPasteRlt "Transfer Materials" (
		group "Slots" (
			radiobuttons LocationRbt "Location" labels:#("local", "project", "network") columns:3 default:1 tooltip:"Where should be materials stored."
			multiListBox SlotList "" width:250 height:3 align:#center items:#("Material slot 1","Material slot 2","Material slot 3") selection:#(1)
			)
		group "Copy" (
			radiobuttons ObjectsRbt "Objects" labels:#("all", "selection") columns:1 default:1 across:2 tooltip:"Object to export."
			radiobuttons MethodRbt "Method" labels:#("by name", "by vertex count", "by transform") columns:1 default:1 tooltip:"Method for matching object."
			edittext RemoveStringEdt "remove string from name" align:#center labelontop:true
			label BlankLbl1 ""
			checkbox CopyPropsCbx "Include object properties" checked:true across:2 align:#center
			checkbox CopyModsCbx "Include modifiers" checked:true align:#center
			hyperlink NoteLbl "!! EditPoly and maybe other modifiers are not supported !!" color:red align:#center enabled:false
			label BlankLb2 ""
			button CopyMatBtn "COPY MATERIAL" width:300 align:#center tooltip:"Copy materials to clipboard." across:1
			)
		group "Paste" (
			radiobuttons ApplyRbt "Apply to" labels:#("scene", "selection") columns:1 default:1 tooltip:"On which object should be applied copied materials."
			label BlankLb3 ""
			button PasteMatBtn "PASTE MATERIAL" width:300 align:#center tooltip:"Pase material from clipboard." across:1
			)

		on CopyPasteRlt open do (
			SlotList.selection = 1
			)

		on MethodRbt changed state do (
			if MethodRbt.state == 1 do RemoveStringEdt.enabled = true
			if MethodRbt.state == 2 do RemoveStringEdt.enabled = false
			if MethodRbt.state == 3 do RemoveStringEdt.enabled = false
			)

		on CopyMatBtn pressed do (

			if LocationRbt.state == 1 do LocationDir = (getdir #temp)
			if LocationRbt.state == 2 do LocationDir = (getdir #matlib)
			if LocationRbt.state == 3 do LocationDir = @"Z:\shared_files"

			if doesFileExist LocationDir then (

				for slot in SlotList.selection do (

					msFile = (LocationDir + (@"\material_slot" + (slot as string) + ".ms"))
					matFile = (LocationDir + (@"\material_slot" + (slot as string) + ".mat"))

					deleteFile msFile
					deleteFile matFile
					
					file = openfile msFile mode:"a+"
				
					if ObjectsRbt.state == 1 do toExport = geometry
					if ObjectsRbt.state == 2 do toExport = selection

					for obj in toExport do (
						if RemoveStringEdt.text.count > 0 then (
							ObjectName = replace obj.name (findString obj.name RemoveStringEdt.text) RemoveStringEdt.text.count ""

							format "--%\n" ObjectName  to:file
							)
						else (
							ObjectName = obj.name
							format "--%\n" ObjectName  to:file
							)


						if MethodRbt.state == 1 do (
							format "for obj in PFX_TM_Global where (matchpattern obj.name pattern:\"*%*\") do (\n" ObjectName to:file
							)

						if MethodRbt.state == 2 do (
							format "for obj in PFX_TM_Global where (getPolygonCount obj)[2] == % do (\n" (getPolygonCount obj)[2]  to:file
							)

						if MethodRbt.state == 3 do (
							format "for obj in PFX_TM_Global where obj.transform == % do (\n" obj.transform  to:file
							)
						
						if not obj.material == undefined do (
							format "	obj.material = currentMaterialLibrary[\"%\"]\n" obj.material.name to:file
							appendifunique currentMaterialLibrary obj.material
						)
						
						if CopyPropsCbx.checked do (
							format "	obj.visibility = %\n" obj.visibility to:file
							format "	obj.renderable = %\n" obj.renderable to:file
							format "	obj.inheritVisibility = %\n" obj.inheritVisibility to:file
							format "	obj.primaryVisibility = %\n" obj.primaryVisibility to:file
							format "	obj.secondaryVisibility = %\n" obj.secondaryVisibility to:file
							format "	obj.receiveShadows = %\n" obj.receiveShadows to:file
							format "	obj.castShadows = %\n" obj.castShadows to:file
							format "	obj.applyAtmospherics = %\n" obj.applyAtmospherics to:file
							format "	obj.renderOccluded = %\n" obj.renderOccluded to:file
							format "	obj.gbufferchannel = %\n" obj.gbufferchannel to:file
							
							userprops = "" + getUserPropBuffer obj
							if not userprops == "" do (
							format "	setUserPropBuffer obj \"%\"\n" userprops to:file
							)
						)
						
						if CopyModsCbx.checked do (
							modsArray = #()
							if not obj.modifiers == undefined do (
								for m in obj.modifiers do (
									if classof m != Edit_Poly do (
										modStr = "("
										modStr = modStr + ((classof m) as string)
										for p in (getPropNames m) do (
											pname = p as string
											pval = (getProperty m p) as string
											if pname == "gizmo" do continue
											if pname == "texmap" do continue
											modStr = modStr + " " + pname + ":" + pval
											)
										modStr = modStr + ")"
										append modsArray modStr
										)
									)
									
								for m = modsArray.count to 1 by -1 do (
									format "	addmodifier obj %\n" modsArray[m] to:file
									)
							)
							)
						
						format ")\n" to:file
					)

					saveMaterialLibrary matFile
					close file
					print ("Materials copied to slot" + (slot as string))
				
					)
				)
			else (
				messageBox "Invalid path." title:"ERROR"
				)
			)
			
		on PasteMatBtn pressed do (
			if LocationRbt.state == 1 do LocationDir = (getdir #temp)
			if LocationRbt.state == 2 do LocationDir = (getdir #matlib)
			if LocationRbt.state == 3 do LocationDir = @"Z:\shared_files"
			
			if doesFileExist LocationDir then (

				for slot in SlotList.selection do (

					msFile = (LocationDir + (@"\material_slot" + (slot as string) + ".ms"))
					matFile = (LocationDir + (@"\material_slot" + (slot as string) + ".mat"))

					loadMaterialLibrary matFile
					if ApplyRbt.state == 1 do global PFX_TM_Global = geometry
					if ApplyRbt.state == 2 do global PFX_TM_Global = selection
					filein msFile
					print ("Materials pasted to slot" + (slot as string))
					)
				)
			else (
				messageBox "Invalid path." title:"ERROR"
				)
			)
		
		)
	TransferMaterialsRlt = newRolloutFloater "Transfer Materials" 350 500
	addrollout CopyPasteRlt TransferMaterialsRlt

	cui.RegisterDialogBar TransferMaterialsRlt minSize:[350,500] maxSize:[350,500] style:#(#cui_dock_vert,#cui_floatable,#cui_handles,#cui_max_sized)

	)