0
$\begingroup$

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.

$\endgroup$
1
  • 1
    $\begingroup$ The best solution is to not rely on the name, see Leanders first suggestion. If you want to ensure an object is renamed you do it twice, the first time may get a numeric suffix, the second will rename the other object with the suffix and the current one without. $\endgroup$
    – sambler
    Jul 13, 2018 at 7:35

2 Answers 2

3
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

Blender creates the incremented suffixes automatically, which is a good thing: You don't have to handle existing objects with the same name yourself.

1. Just grab the modified name of the created object

This method is probably the most practical, you just assign the Blender assigned name to the variable which holds the name you're working with.

my_mesh_name = "my_mesh_name"
me = bpy.data.meshes.new(name = my_mesh_name)
created_object = bpy.data.objects.new(name = my_mesh_name, object_data = me)

my_mesh_name = created_object.name

2. Don't create new object, if the exist

Another way could be to use existing objects and/or meshes if they have the name you want to use.

def create_mesh_obj(bm, p_name = "my_mesh_name"):
    if p_name not in bpy.data.meshes:
        me_tmp = bpy.data.meshes.new(name = p_name)
    me = bpy.data.meshes[p_name]

    if p_name not in bpy.data.objects:
        ob = bpy.data.objects.new(name = p_name, object_data = me)
        scene.objects.link(ob)
    else:
        ob = bpy.data.objects[p_name]
        ob.data = me
    scene.update()
    return ob

3. Start from scratch

If there are any objects with the desired names, delete them beforehand.

my_mesh_name = "my_mesh_name"
for obj in bpy.data.objects:
    if obj.name == my_mesh_name:
        bpy.data.objects.remove(obj)
bpy.context.scene.update()
$\endgroup$
1
  • 1
    $\begingroup$ Working with the object names is a bad suggestion, there are too many cases and you can't control all of them. In general, best practice (also code wise) is using the object reference whenever possible in the first place. Also, consider that deleting the object beforehand can lead into crashes later on because its data block, which is still available after calling remove(). $\endgroup$
    – brockmann
    Jul 19, 2018 at 9:25

You must log in to answer this question.

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