Loop for unique names

Hello Dynamo Friends :slight_smile:

If i want to create elements (views or whatever) i often get errors because the name is already used.

I would like to know if i can Loop in a codeblock so that a name always gets a “_copy” suffix if it is in use, for an infinite amount.

Here is how i can do it for 3 “levels” by putting 3 codeblocks together.

Happy about any advice :slight_smile:

Creating a view should automatically name it with a new name. Then you can rename the view as needed. Can you show us (with node previews) how and when you’re getting this error?

Hello Nick :slight_smile:

There are many, many examples where i need this.

Creating schedules is one of them:

And naming the copys with numbers (copy_1, copy_2, etc) would be great.

edit:
So i`m using my workaround

If you’re providing the name then you need to provide a unique name. If you’re creating the name based on a list of objects, then you can just count the number of items you need to name and then add consecutive integers to each object.
image

1 Like

This looks like you already know of existing names that need to be duplicated. In that case, you can compare your existing names to the new names and just use an If statement to add “_Copy” or whatever to any name that already exists.

Hi @gerhard.p

The repeated names are in that Element.Name node? And from that you want to add the suffix into the repeated ones?

Hello,

Yes if the Name already exists i want to add _Copy_1.
Then i have to check if _Copy_1 alredy exists…and so on.

Here is an another attempt, it works but is not really a smooth solution.

I started some designscript code…but instead of just naming to _copy i have to add a count and a loop to check if copy1, copy2, copy3 etc exists…

image

Assuming that you have the repeated names in the Element.Name node. Would this help somehow?

seq = x==0? null: x==1? "Copy_"+1: "Copy_"+(1..x);
uniq = DSCore.List.UniqueItems(seq<1>);

or

lst_groups = DSCore.List.GroupByKey(x,x);
groups = Dictionary.ValueAtKey(lst_groups, "groups");
rest_itms = DSCore.List.RestOfItems(groups<1>);
count = DSCore.List.Count(rest_itms<1>);
suffix = count==0? null: count==1? "Copy_"+1: "Copy_"+(1..count);
appended  = rest_itms + "_" + DSCore.List.UniqueItems(suffix<1>);
first_itms = DSCore.List.FirstItem(groups<1>);
lst_names = DSCore.List.AddItemToFront(first_itms<1>,appended<1>);
1 Like

I got it now, i can create a list of all possible names and with set.difference i get the first name that is not in use :slight_smile:

Thanks for your help!

2 Likes

An alternative: since you know what name you want, you can check if the name exists already.

  • Get all views.
  • Get view names
  • Remove all _copy suffixes
  • Group the list by it’s own name (list and key are the same)
  • check if your desired name is in the list of unique keys
  • if not, use the desired name
  • if so, count the number of items in that group and append “_copy” to that count, and use that as he name
2 Likes