Notifications
Clear all

[Closed] Save each object in the scene in a different file

Hello,
i want to save each object inmy scene in a different file
my idea was to get get the object names in an array then from i run a variable from the array start to the end in a for loop and for each loop i get the object with the name assigned and save that object in a file with the same name
i’m used to code in java but i’m totally new to maxscript so i appreciate any help possible
thank you.

5 Replies

(
	global rol_saveObjectToFile
	try(destroyDialog rol_saveObjectToFile)catch()
	rollout rol_saveObjectToFile "SAVE"
	(
		local savePath = undefined
		
		button btn_browse "Browse" width:140
		button btn_save "SAVE" width:140
		
		on btn_browse pressed do
		(
			dir = getSavePath()
			if dir != undefined do
				savePath = dir
		)
		
		on btn_save pressed do
		(
			if savePath != undefined do
			(
				for o in objects do
				(
					saveNodes o (savePath + "\\" + o.name) quiet:true
				)
			)
		)
	)
	createdialog rol_saveObjectToFile
)

Thank you very much my friend
i really apreciate it :applause:

That worked just fine,
but can i make it only apply on groups?
right now it saves each group in a file but it saves also the objects inside the group too and i want to get rid of that

Well i managed to add a little condition and the code became like this

macroscript Save
category:"TestSection"
tooltip:"Save"
buttontext:"Save"
(
	global rol_saveObjectToFile
	try(destroyDialog rol_saveObjectToFile)catch()
	rollout rol_saveObjectToFile "SAVE"
	(
		local savePath = undefined
		
		button btn_browse "Browse" width:140
		button btn_save "SAVE" width:140
		
		on btn_browse pressed do
		(
			dir = getSavePath()
			if dir != undefined do
				savePath = dir
		)
		
		on btn_save pressed do
		(
			if savePath != undefined do
			(
				for o in objects where ( isgrouphead o ) do
				(
					saveNodes o (savePath + "\\" + o.name) quiet:true
				)
			)
		)
	)
	createdialog rol_saveObjectToFile
)

It works fine and saves the files with the group names but the problem in this code is that it’s only creating files with the name of the groups
there aren’t any meshes in the max file
(for exemple i made 4 groups: G1(boxes),G2(cones),G3(spheres),G4(tubes) and i after i run the scipt i found 4 empty max files with the names G1,G2,G3,G4 )


(
	global rol_saveObjectToFile
	try(destroyDialog rol_saveObjectToFile)catch()
	rollout rol_saveObjectToFile "SAVE GROUPS"
	(
		local savePath = undefined
		local allChildren = undefined
		
		button btn_browse "Browse" width:140
		button btn_save "SAVE" width:140
		
		function FindAllChildren obj =
		(
			if (isGroupHead obj) == false do
			(
				append allChildren obj
			)
			for c in obj.children do FindAllChildren c 
			--
			allChildren
		)
	
		on btn_browse pressed do
		(
			dir = getSavePath()
			if dir != undefined do
				savePath = dir
		)
		
		on btn_save pressed do
		(
			if savePath != undefined do
			(
				for o in objects where ( (isGroupHead o or isOpenGroupHead o) and o.parent == undefined )do
				(
					allChildren = #()
					saveNodes (FindAllChildren o) (savePath + "\\" + o.name) quiet:true
				)
			)
		)
	)
	createdialog rol_saveObjectToFile
)