How to add an object and rename it

how to name an obect while creating it?
exaple:
em = bpy.ops.object.empty_add(type=‘PLAIN_AXES’, align=‘WORLD’, location=(5.60982, 4.35601, 6.84613), scale=(1, 1, 1))
this creates an empty with name “Empty”
I thought I can use
em.name = ‘someEmpty’
to rename but,
em = {‘FINISHED’}
I have to use
D.objects[‘Empty.001’].name = ‘someEmpty’ to rename but this name changes if theres an object with same name, while creating.
how to address this issue?
thanks.

The em = doesn’t hold the empty but the result of the operation…

AFAIK you can acess the actual object by the context:

print( D.objects[ bpy.context.object.name ] )

will give you your last object and

D.objects[ bpy.context.object.name ].name = 'whatever'

will change the name…

As @Okidoki mentions in his post you are storing the operation of creating an empty into the variable em instead of the end result.

Your objective can easily be achieved by storing the result into a variable directly after the operation has occurred. Blender forces the context to change when a new object is created, as such:

import bpy

bpy.ops.object.empty_add()

em = bpy.context.object

em.name = "someEmpty"

Oh yes… :sweat_smile: i was only refering to the way to navigate to the correct object property… and didn’t post a nice way to actually write good code…