[Closed] about object renaming
hello max user ~~
i need some help
i have many object
object name type is like below
01-box-01
01-sphere-01
02-ball-02
03-ball-03
like this
i have to rename for project
-
box-01 , sphere-01 , ball-02 , ball-03
like this -
01-box , 01-sphere , 02-ball , 03-ball
like this
like this , i have to subtract suffix , and prefix
how can make this script ~~~
To remove the prefix, and only if the prefix is fixed to two digits and a dash, you could say
for o in objects do o.name = substring o.name 4 -1
This will take the name starting from the 4th character to the end.
Since the prefix might have more than 2 digits and a dash, you could look for the dash first:
for o in objects do o.name = substring o.name ((findstring o.name "-")+1) -1
This finds the location of the first dash, then takes the rest of the name starting at the character after the dash.
It might be easier to use filterstring() to filter the name based on the dash.
For example
filterString "01-box-01" "-"
--returns
#("01","box","01")
Since we want to remove the prefix, you can take all elements from the array except the first one and build the name by adding the “-” between the pieces.
for o in objects do
(
theFS = filterString o.name "-"
if theFS.count > 2 do
(
theName = ""
for i = 2 to theFS.count-1 do theName += theFS[i] + "-"
theName += theFS[theFS.count]
o.name = theName
)
)
This filters the name using the dashes and checks whether the resulting array has 3 or more elements. If yes, it defines a new name as an empty string. It loops from the second element of the array to the second-last element of the array and collects the pieces into the new name, adding a dash at the end.
Then it adds the last element of the array without a dash and assigns the result to the object’s name
To do the same with the suffix, we will simply take a different range of the array:
for o in objects do
(
theFS = filterString o.name "-"
if theFS.count > 2 do
(
theName = ""
for i = 1 to theFS.count-2 do theName += theFS[i] + "-"
theName += theFS[theFS.count-1]
o.name = theName
)
)
Since we are checking the result of theFS, you can run this on ANY scene – only objects with two or more dashes in the name will be processed and the prefix/suffix will be removed correctly, while the other objects will not be touched…
I would just use the “Rename Objects…” under Tools.
However knowing how to do this using MaxScript can be good from a learning point of view