[Closed] Challenge: Make all Object Names Unique
--create some random boxes
for i = 1 to 100 do
(
box name:("TestBox_" + (random 1 20) as string)
)
--my code for finding matches
theObjs = sort(for o in objects collect o.name)
matches = #()
for i = 1 to (theObjs.count - 1) where theObjs[i] == theObjs[i + 1] do appendifunique matches theObjs[i]
--my code for renaming matches
for m in matches do
(
for o in objects where o.name == m do o.name = uniquename m
)
Can anyone see any issues with this, or care to have a go at seeing if they can make it faster/better?
for i = 1 to 100 do
(
box name:("TestBox_" + (random 1 20) as string)
)
for i = 1 to 100 do
(
box name:("testbox_" + (random 1 20) as string)
)
Finding matching names with different cases… a bit more tricky…
for o in aObjs where matchpattern o.name pattern:m ignoreCase:true do o.name = uniqueName m
Unfortunately uniqueName doesn’t have an ‘ignoreCase’ parameter so you still get matches.
#1
if you want just all unique names:
for k=1 to objects.count do objects[k].name = "base_name" + k as string
#2
if you want to keep ‘base names’ and have only unique indexes:
done = #()
mapped fn trimObjectID obj = (obj.name = trimright obj.name "0123456789")
trimObjectID objects
for obj in objects where finditem done obj == 0 do
(
objs = getnodebyname obj.name all:on
for k=1 to objs.count do objs[k].name += k as string
join done objs
)
/*
delete objects
for k=1 to 1000 do dummy name:(bit.intaschar (random 65 70) + (random 1 10) as string))
/*
#3
if you want to increase index for dups:
<...>
similar to #2 but don’t trim indexes at the beginning
Ok so practical example…
Get thousands of objects in with some with intelligent name like, “DOOR” or “WINDOW” and then literally thousands called “_shell” so the challenge is to rename the “_shell” without renaming the others.
shells = $'*_shell*'
for k=1 to shells.count do shells[k].name = <somefunctiontosubstitute> obj.name (k as string)
Aha but the problem is they aren’t all called “_shell” there are other objects with clashing names, and there are too many for us to find manually…
…and like I said earlier the case can’t be assumed…
#(“_Shell”, “_shell”) we need to make them truly unique.
But that would also get anything called “Test_Shell” which might be unique so we don’t want to rename it to “_Shell_1203” for example.
Again the challenge is to only rename the objects that have duplicates names in the scene, regardless of case.