Explicitly setting object name to "X" actually sets it to "X.001"? I am using a python script to create and import some objects.

After creating an object I set it's name like so:

bpy.context.object.data.name = "MyObject"


and the new object is now named "MyObject"

However if I run the script again, the new object is now "MyObject.001" and the old object remains as "MyObject".

Even though I explicitly told python to name it to "MyObject" (and the rest of the script relies on that name) - but it's changing it anyways. 

My scrip is unaware of the fact that Blender is forcing the name to change.

How do I force Blender to change the name of the object to exactly what I set it to. I don't care if it indexes the old objects.

Just seems like bad default behavior in the first place.

Thanks.
Blender uses the name to uniquely reference the object - so if an objects already exists with that name it cannot allow you to create another one. Instead, it creates the object and adds the ‘.001’ suffix to make it unique (or 002, 003, etc until it finds an unused name).

One solution to your problem is for your script to first check whether that object already exists and if it does to either rename or delete the old object before creating the new one - this way your object should always be created with the name you specify.