Why does the name of a definition have suffix #1?

Created a definition by loading a .skp file. I wanted to set the name of the definition. But Sketchup always appended #1 to the name. For example:

active_mod = Sketchup::active_model
cdef = active_mod.definitions.load(filename)
cinst = active_mod.active_entities.add_instance(
cdef.name = 'House'

I wanted the name as ‘House’, but the result name always was ‘House #1’. What’s the problem with my code?

"House" is already the name of an existing definition in the model’s DefinitionList collection.

ComponentDefinition name must be unique as it is used as a key.

You can easily test this before your code:

if active_mod.definitions['House']

The collection’s #[] method will return nil if the definition name has not yet been used.

The absolute necessity for uniqueness of member names applies for all of the SketchUp API’s collection classes that allow access by name via their #[] method.

So generally, for any collection member object, if you attempt to set the member’s name property to a name that is already in use, then the API will “difference” the name by adding " #n" where n is an integer that forces the name to be unique.*

* This also applies to manual user actions through the GUI when bringing resources into the model such as definitions, materials, etc.

If you wanted to reuse the name "House", you would first need to delete the old definition using that name.

dlist = active_mod.definitions
if old_house = dlist['House']
  dlist.remove(old_house) if old_house.instances.empty

If you do not care if old_house had instances, then calling dlist.remove(old_house) will delete those instances from the model or the entities of any other definition (whether used or not.)

ADD: the other option is to rename the old_house definition.

1 Like