Objects divider. Advice needed

Hello!

I need to divide max-file into multiple files by names of objects and groups. Script works fine with objects, but not with groups, it operates with group contents and saves each sub-object separately. I whant to save groupped objects into one file with name of group.

if (selection.count > 0) do(
 
	for obj in selection do (
 
		CenterPivot obj 
		obj.pivot.z = obj.min.z
 
		obj.pos = [0, 0, 0]
 
		nameofmaxfile = maxFilePath + obj.name + ".max"		
 
		savenodes obj (nameofmaxfile)	
	)
)

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
miauu's picture

.

Try this:

(
 
	function IsGroupHeadMember head node act:off = 
	(
		while isvalidnode node and isgroupmember node and not (act = (node.parent == head)) do node = node.parent
		act
	)
	function GetAllGroupMembers head = if isGroupHead head do
	(
		for node in (join #() head) where IsGroupHeadMember head node collect node
	)
 
	function FindTopMostGroupHead obj = 
	(			
		if isGroupMember obj then
		(
			while obj != undefined and (not isGroupHead obj or isGroupMember obj) do
				(obj = obj.parent)			
		)
		else
		(
			obj
		)
		obj
	)
 
	if selection.count != 0 do
	(
		selObjsArr = selection as array
		rootObjsArr = #()
		for o in selObjsArr do
		(
			appendIfUnique rootObjsArr (FindTopMostGroupHead o)
		)
		if rootObjsArr.count != 0 do
		(
			for obj in rootObjsArr do 
			(
				objToSave = if isGroupHead obj then (GetAllGroupMembers obj) else obj
				CenterPivot obj 
				obj.pivot.z = obj.min.z 
				obj.pos = [0, 0, 0] 
				nameOfMaxFile = maxFilePath + obj.name + ".max"		 
				saveNodes objToSave (nameOfMaxFile)	
			)
		)
	)
)
mpashnin's picture

Thank you so much! It works

Thank you so much! It works as i whant!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.