append numbers to name

append numbers to name

joegunn3d
Enthusiast Enthusiast
420 Views
6 Replies
Message 1 of 7

append numbers to name

joegunn3d
Enthusiast
Enthusiast
Thought I'd post this since it seems I can't find an example of what I'm trying to do.

Basically, if I have a bunch of objects that are already named correctly. I just want to append a unique number to them.

thanks
max2008-11
0 Likes
421 Views
6 Replies
Replies (6)
Message 2 of 7

Steve_Curley
Mentor
Mentor
Give us an actual example of what the names are now and what you want them to be. It matters (different coding) if you want "1" on the end or "01" like Max usually does itself.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 3 of 7

joegunn3d
Enthusiast
Enthusiast
Lets say my objects are name liked this:

BoxOpen01

SLight12

RingMapWhat_0012_X01

Now after I select all my objects I would like something like this:

BoxOpen01_01

SLight12_02

RingMapWhat_0012_X01_03

Thanks
max2008-11
0 Likes
Message 4 of 7

Steve_Curley
Mentor
Mentor
That's going to be tricky, unless you were to encode the original names in the script.

If you just select them then there's no guarantee that they will be in the right order, so you might end up with
SLight12_01
RingMapWhat_0012_X01_02
BoxOpen01_03

If having them in alphabetical order would be sufficient:-
BoxOpen01_01
RingMapWhat_0012_X01_02
SLight12_03
then it could be done by sorting the selection before making any changes.
If the order is irrelevent then it's easier.

This example does sort them first. Select the objects before running it.

(
ar = for obj in selection collect obj.name
sort ar

for t = 1 to ar.count do
(
newName = stringstream ""
n = getNodeByName ar
format "%_%" ar (formattedprint t format:"2.2d") to:newName
n.name = newName as string
)
)

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 5 of 7

joegunn3d
Enthusiast
Enthusiast
Thanks a lot Steve your great! I ended up using some crappy code in the mean time which works. I don't mind the order in which they get numbered just as long as they get numbered.

(
for i in selection do
(
i.name = i.name + (random 1 100) as string
)
)

I'm going to take a look at what you posted and replace my crazy code...

Thanks again!
max2008-11
0 Likes
Message 6 of 7

Steve_Curley
Mentor
Mentor
In that case:-

(
for t = 1 to selection.count do
(
newName = stringstream ""
format "%_%" selection.name (formattedprint t format:"2.2d") to:newName
selection.name = newName as string
)
)


Problem with "somenumber as string" is that you won't get the leading zero which leads to inconsistently named objects. Apart from that there's nothing really "crappy" with your code ;)

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 7 of 7

joegunn3d
Enthusiast
Enthusiast
Thanks for the reply. That looks like clean code for what I need. I appreciate it.

Thanks again!
max2008-11
0 Likes