1
$\begingroup$

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 )

bpy.context.scene.collection.objects.link( o )

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)
$\endgroup$

1 Answer 1

2
$\begingroup$

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 )

bpy.context.scene.collection.objects.link( o )

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)
$\endgroup$
3
  • 1
    $\begingroup$ That was the trick, thank you so much :)! $\endgroup$ Commented Apr 28, 2021 at 7:11
  • 2
    $\begingroup$ or objs.target = o $\endgroup$
    – batFINGER
    Commented Apr 28, 2021 at 10:11
  • $\begingroup$ Very good point @batFINGER! :-D $\endgroup$ Commented Apr 28, 2021 at 11:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .