Notifications
Clear all

[Closed] Saving user preferences???

hi all,

I’m writing a script that will be used by a few different users.

the script saves renders to a default directory.

I would like to give the users the ability to choose any directory on their machine.

the directory is obviously stored as a string

So here’s the Part I cant figure: How do I save that string back in the script file ( .ms ) so next time they run it, their preference is saved?

or is there another, better way?

thanks guys

3 Replies

Use createFile() to make a MyScriptSettings.ini file in the $userScripts folder, then use setINIsetting() and getINIsetting() to save/retrieve whatever settings you want.

1 Reply
 JHN
(@jhn)
Joined: 11 months ago

Posts: 0

No need to call createFile, if you use setInisetting on a file that doesn’t exist it automaticly tries to create one!

-Johan

Have a look at this tool. It reads and writes to an ini in the Max Root folder. I am having to rewrite this since the firm moved to Windows 7 and a strict security policy for Program Files (limited user access). I will have to change it to point to the Users\user.name\appdata\ etc. folder. Lots of find and replace in my future!


 
 /* 
 ----Future Improvements
 Create Grid from selected polygon/face
 
  */
  
 (
 	theINI = getdir #maxroot + "ZGF_Tools.ini"
 	
 	
 	--Functions------------------------------------
 	fn gridCycle = (
 		
 		local new_grid = undefined
 		local next_grid_index = 1
 		local current_grid = activegrid
 		local grids_array = #()
 
 		grids_array = for g in helpers where classof g == grid collect g
 		append grids_array undefined
 		(
 			if current_grid == undefined then 
 				(
 				current_grid = grids_array[1]
 					activegrid = current_grid
 				)
 				else
 				(
 				for i = 1 to grids_array.count do 
 					(
 					if grids_array[i] == current_grid then next_grid_index = (i+1)   
 					)
 					
 					if next_grid_index > grids_array.count then
 					(
 					new_grid = grids_array[1]
 					)
 					else
 					(
 					new_grid = grids_array[next_grid_index]
 					)
 				activegrid = new_grid
 				toolMode.coordsys #grid
 				)
 			)
 			
 	)
 
 	fn gridMod gridSpacing gridSize= (
 	
 		for g in helpers where classof g == grid do
 		(
 			g.grid = gridSpacing
 			g.length = gridSize
 			g.width = gridSize
 		)
 		SetGridSpacing gridSpacing
 		gridPrefs.perspViewGridExtents = gridSize/gridSpacing
 	)
 
 	fn alignSelToGrid obs currentGrid = (
 		if currentGrid != undefined then 
 		(
 			gridXForm = currentGrid.transform
 		)
 		else (
 			gridXForm = (matrix3 [1,0,0] [0,1,0] [0,0,1] [0,0,0])
 			)
 		undo "Align To Grid" on
 		(			
 			for i in obs do
 			(
 				i.transform = gridXForm
 			)
 		)	
 	)
 	
 	fn createGridAtObject obj = (
 		undo "Create Grid at Selection" on
 			if obj != undefined do
 			(
 				objSize = (obj.max - obj.min) + ((obj.max - obj.min) *.05)
 				newGrid = grid()
 				newGrid.width = objSize.x
 				newGrid.length = objSize.y
 				maxSize = (if (objSize.x > objSize.y) then objSize.x else objSize.y)
 				newGrid.grid = maxSize / 50
 				newGrid.pos = obj.pos
 				newGrid.rotation = obj.rotation
 				newGrid.name = ("Grid_"+obj.name)
 				select newGrid
 				activegrid = newGrid
 			)
 	)
 	-- UI ------------------------------------------
 		
 	try destroydialog zgfGridTools catch()
 	
 	local gridToolsBMP = (getDir #ui) + "icons" + "\gridTools.bmp"
 	
 	rollout zgfGridTools "Grid Tools" (	
 			
 			spinner spn_spacing "Spacing" range:[1,10000,60] type:#integer fieldWidth:40 align:#left offset:[-10,0] 
 			spinner spn_size "Size" range:[1,10000,1000] type:#integer fieldWidth:40 align:#left offset:[9,0]
 			button btn_gridCycle "Cycle Grids" width:120 align:#left offset:[-10,0] 
 			button btn_setView "Set Viewport to Grid" width:120 align:#left offset:[-10,0] 
 			button btn_alignToGrid "Align Selection to Grid" width: 120 align:#left offset:[-10,0] 
 			button btn_gridAtSelection "Create Grid from Sel." width:120 align:#left offset:[-10,0]
 
 		
 		
 			button btn_help "?" pos:[110,4] width:16 height:16
 			
 			--checkbutton gridToolsImage width:84 images:#(gridToolsBMP, undefined, 1, 1, 1, 1, 1) Pos:[116,0] border:false enabled:false
 	--Events------------------------------------------
 			--Grid Spacing Spinner
 			on spn_spacing buttonup do (
 				gridMod spn_spacing.value spn_size.value
 			)
 			
 			on spn_spacing entered do (
 				gridMod spn_spacing.value spn_size.value
 			)
 			
 			--Grid Size Spinner
 			on spn_size buttonup do (
 				gridMod spn_spacing.value spn_size.value
 			)
 			
 			on spn_size entered do (
 				gridMod spn_spacing.value spn_size.value
 			)
 			
 			--Cycle Grid Button
 			on btn_gridCycle pressed do (
 				if keyboard.controlPressed then 
 				(
 					max acthomegrid
 					btn_gridCycle.text = "World"
 
 				)
 				else if keyboard.shiftPressed then
 				(
 					max activate grid object
 					currentGrid = activegrid
 					if currentGrid != undefined then btn_gridCycle.text = currentGrid.name else btn_gridCycle.text = "World"
 				)
 
 				else (
 					gridCycle()
 					currentGrid = activegrid
 					if currentGrid != undefined then btn_gridCycle.text = currentGrid.name else btn_gridCycle.text = "World"
 				)
 			)
 			
 			--Set ViewPort to Grid
 			on btn_setView pressed do (
 				viewport.setType #view_grid
 			)
 			
 			--Align Selection to Grid Button
 			on btn_alignToGrid pressed do (
 				alignSelToGrid selection activegrid
 			)
 			
 			--Create Grid at Selection
 			on btn_GridAtSelection pressed do (
 				theSelection = selection[1]
 				createGridAtObject theSelection
 			)
 			--Help button
 			on btn_help pressed do (
 				try destroydialog helpInfo catch()
 				
 				rollout helpInfo "How to use Grid Tools"
 				(
 					group "Grid Spacing and Size:"
 					(
 						label lbl1a "Spinners change the Grid Spacing of all " align:#left offset:[-6,0]
 						label lbl1b "User Grids and Home Grid perspective extents." align:#left offset:[-6,0]
 					)
 					
 					group "Cycle Grids:"
 					(
 						label lbl2a "Cycle through active user Grids and Home Grid." align:#left offset:[-6,0]
 						label lbl2b "Hold Ctrl to set Home Grid current" align:#left offset:[-6,0]
 						label lbl2c "Hold Shift to set selected User Grid current" align:#left offset:[-6,0]
 					)
 					
 					group "Align Selection to Grid:"
 					(
 						label lbl3a "Align all selected objects to the current User Grid" align:#left offset:[-6,0]
 						label lbl3b "posistion, rotation and scale." align:#left offset:[-6,0]
 						label lbl3c "If Home Grid is current, objects are moved to" align:#left offset:[-6,0]
 						label lbl3d "world origin, 0,0,0." align:#left offset:[-6,0]
 					)
 
 				)
 					
 				global GridToolsHelp = newRolloutFloater "Grid Tools Help" 260 260
 				GridToolsHelp.pos = (GetDialogPos zgfGridTools) + [0,130]
 				addRollout helpInfo GridToolsHelp border:false
 				
 			)
 			
 			--On Moved
 			--on zgfGridTools moved do (GridToolsHelp.pos = (GetDialogPos zgfGridTools) + [0,130])
 			
 			--On Close
 			on zgfGridTools close do (
 				setINISetting theINI "Grid Tools" "Window Position" (GetDialogPos zgfGridTools as string)
 				
 				if GridToolsHelp != undefined do closeRolloutFloater GridToolsHelp -- Also close the Help floater
 				)
 		)
 		gtPos = execute (getINISetting theINI "Grid Tools" "Window Position")
 
 
 CreateDialog zgfGridTools width:130 pos:(if gtPos !=undefined  and gtPos != OK then gtPos else [100,100])
 	
 )