ScriptSpot home

Forum topic · General Scripting

copy object with unique name + only 2 digits

By claire · 2017-03-02

Description

Hi Guys,

Really confused with this one. Tried lots of varients now, but cant seem to work out what's going wrong. Say you have a Box (it's name "Box_01", only 2 digits not the normal 3). I have written a few lines of code to copy this node and give it a unique name but I only want to stick to the original 2 digit suffix i.e "Box_02, Box_03" etc.

Please can you have a quick look and see if there are any obvious mistakes. Currently the new copy jumps from "Box_01" to "Box_03 then "Box_05" etc!!!!

newObj = copy $
newObj.name = uniquename $.name numDigits:2
select newObj

Many thanks in advance!!! :-)

Comments (10)
claire

Swordslayer,

Your ideas lead me to try this which works perfectly, Thanks!

newObj = copy $ name:$.name
newObj.name = uniquename $.name numDigits:2
select newObj

Reply to claire
Branko Živković

You can try this one also

--main function
fn uniqueNodeName node = if isValidNode node do
(
fn filtername nodeName suffix = (e = filterstring nodeName "_" ; e[2] = suffix ; e)
local oldName = filtername node.name "01", newName = oldName[1]+"_01"
while (getNodeByName newname) != undefined do
(
newName = oldName[1] + "_" + (string = formattedPrint ((trimleft oldName[2] "0") as integer + 1) format:"02d")
oldName = filtername newName string
)
newName
)
select (copy $ name:(uniqueNodeName $))

Thanks Barigazy,

Just tried your script and this works great too. Always intresting seeing more advanced options!

Reply to claire
claire

Hi Barigazy,

Appolgies I just tried it with another test name "Geometry_Box_v01" (chances are my names will have a few underscores at least) and when I run it creates a copy called "Geometry_v01", removing the middle section of the name. Is it posible to remove anything after the last instance of "_" and then add on the "v01" to create "Geometry_Box_v01", "Geometry_Box_v02", "Geometry_Box_v03" etc.

Thank you for your help!

Reply to claire
Branko Živković

Both fn's are only for your example only. You need to adjust theses for every different case. Anyway ...

fn uniqueNodeName node = if isValidNode node do
(
fn filtername nodeName suffix = (e = filterstring (substituteString nodeName "_v" "#") "#" ; e[2] = suffix ; e)
local oldName = filtername node.name "01", newName = oldName[1]+"_v01"
while (getNodeByName newname) != undefined do
(
newName = oldName[1] + "_v" + (string = formattedPrint ((trimleft oldName[2] "0") as integer + 1) format:"02d")
oldName = filtername newName string
)
newName
)
select (copy $ name:(uniqueNodeName $))

Barigazy,

This is perfect, thank you very much for your help!!! For my needs the count will never go over 99 so no worries.

Thanks again!

Reply to Branko Živković
Benjamin von Cramon

Q: editing MEL script

Excuse my noobness, but I'm running into a similar situation, believe this code from barigazy would do the trick, please confirm. I'm running a script to import multiple fbx's from a folder, here's that script: When I load macro_batch_exporter_importer.mcr I see 3DS finding and trying to open each the fbx's, but at the end I find but a single mesh with the generic name, meshNode. It would appear 3DS sees all these mesh parts with the same generic name and loads just one. So barigazy, that's what your code addresses? If so, how would I implement it with this .mse script that I can't open to see/edit the code? Thanks!

Vojtech Cada

.

Looks like the plus one increase is from the copy command, and you're incrementing it another time with uniqueName. Try using a placeholder name for the copied object instead (sth. like copy $ name:"temp" or copy $ prefix:#placeholder).

fajar

mine working correctly