# Update target with new same name object+suffix added

I have a script that automatically adds an empty as a target of a constrain which works as it should be, but when I run again the script it adds another empty with the suffix .001 and what I would like to is that the target updates automatically with that new empty with the suffix .001 and so on. Is that possible?

import bpy

empty_name = 'MyOwnEmpty'

o = bpy.data.objects.new( empty_name, None )

o.empty_display_size = 4
o.empty_display_type = 'ARROWS'

for x in bpy.context.selected_objects:
objs = x.constraints.new(type='COPY_LOCATION')
objs.use_offset = True
objs.target = bpy.context.scene.objects.get(empty_name)


When Blender creates a new object, if one of that name already exists then it adds the suffix to make it unique. You can pickup that name immediately after creation via the 'name' property.

In your case you simply need to use o.name in place of empty_name :

import bpy

empty_name = 'MyOwnEmpty'

o = bpy.data.objects.new( empty_name, None )

o.empty_display_size = 4
o.empty_display_type = 'ARROWS'

for x in bpy.context.selected_objects:
objs = x.constraints.new(type='COPY_LOCATION')
objs.use_offset = True
#objs.target = bpy.context.scene.objects.get(empty_name)
objs.target = bpy.context.scene.objects.get(o.name)

• That was the trick, thank you so much :)! Apr 28, 2021 at 7:11
• or objs.target = o Apr 28, 2021 at 10:11
• Very good point @batFINGER! :-D Apr 28, 2021 at 11:25