Find objects with identical names and add incremental numbering

Hi, I'm looking for a bit of maxscript to scan the names of objects in selection and add an increment for any identical names.

Lets say I have 9 objects with names like this:

table_1 table_1 table_1
door_1 door_1 door_1
chair_1 chair_1 chair_1

I would like to finish with this:

table_1 table_2 table_3
door_1 door_2 door_3
chair_1 chair_2 chair_3

I DONT want this (this is what I keep getting):

table_1 table_2 table_3
door_4 door_5 door_6
chair_7 chair_8 chair_9

By doing this, I can then make further modifications of my own to the objects, now that they have unique names.

Thanks so much in advance and i really appreciate any help! :)

Comments

Comment viewing options

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

...

Try this fn

fn incrementalRename nodes str: =
(
	local nameArr = #() ; objArr = #()
	for o in nodes do
	(
		shortname = trimright o.name str
		idx = findItem nameArr shortname
		if idx != 0 then append objArr[idx] o else
		(
			append nameArr shortname
			append objArr #(o)
		)
	)
	for i = 1 to nameArr.count do
	(
		for j = 1 to objArr[i].count do objArr[i][j].name = nameArr[i] + j as string
	)
)

Select some objects and run below code

incrementalRename selection str:"0123456789"

bga

barigazy's picture

...

In cases when you have thousand objects on the scene then this fn is more practical

fn incrementalRename nodes str: zeros: =
(
	local nameArr = #() ; objArr = #()
	for o in nodes do
	(
		shortname = trimright o.name str
		idx = findItem nameArr shortname
		if idx != 0 then append objArr[idx] o else
		(
			append nameArr shortname
			append objArr #(o)
		)
	)
	for i = 1 to nameArr.count do
	(
		for j = 1 to objArr[i].count do objArr[i][j].name = nameArr[i] + (formattedPrint j format:("0" + zeros as string + "d"))
	)
)

Argument "zeros" means how meny zeros ei. numbers you want to be unique number.
If we run next line of code, your let say 56 chair object will be named "chair_0056"

incrementalRename selection str:"0123456789" zeros:4

bga

fajar's picture

how boout this

how bout using this as started

(
local match =  MatchPattern 
local fmtPnt = formattedPrint
local fmt = format
 
local NamesObjCollection = #("door","table","chair") -- predefined object that you want search
 
fn replaceDigitText inputString:"" SubtituteString:"" = 
(
	local newString 
	if inputString!="" or inputString!=unsupplied do 
	(
		local findInt = @"\d+" -- regex symbol for finding any digit in the text
		local regex = (dotNetClass "System.Text.RegularExpressions.Regex")
		if (regex.IsMatch inputString findInt) do 
		(
			newString = regex.Replace inputString findInt SubtituteString
		)		
	)
	newString
)
 
mapped fn ChangeObjectName objNames selOnly:false totalDigit:3 = 
(
	fmt "Category : % \n" objNames
	local replaceText = replaceDigitText
	local grName,newNumber,oldName,tempName,newName
	local theObj = #() 
 
	if totalDigit<2 do (totalDigit = 3)
 
	case selOnly of 
	(
		true  : (theObj = selection as array) -- for selection only
		false : (theObj = objects as array) -- for entire object
	)
 
	if theObj.count!= 0 do 
	(
		local grName = (for i in theObj where (match i.name pattern:("*"+objNames+"*") ignoreCase:true) collect i)
 
		if (x = grName.count)!=0 do 
		(
			for k=1 to x do 
			(
				newNumber = fmtPnt k format:("0"+totalDigit+"d")
				oldName = grName[k].name
				tempName = (oldName+ newNumber)
				newName = replaceText inputString:oldName SubtituteString:newNumber
 
				if newName!=undefined then 
				(
					grName[k].name = newName
					fmt "OldName : %| NewName : % \n" oldName newName
				)
				else  
				(
					grName[k].name = tempName
					fmt "OldName : %| NewName : % \n" oldName tempName
				)
			)
			fmt "-------------- \n"
		) 
	)
)	
 
clearListener()
-- ChangeObjectName NamesObjCollection ---** this is how u using it
)

Hope that help you a little.

miauu's picture

.

Post your code.

Comment viewing options

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