Blender changes the names of my objects

2k Views Asked by At

I'm using the python API for a "scatterplot" in blender. The data is a dictionary which maps names to lists of 3D points, so there are named clouds of points.

I need to look at each cloud individually and hide the others, this is my setup:

  • for each name in the dict, I create an empty object
  • for each 3D point belonging to this name, I create a small cube, position it and reparent it to the empty object.

Now I can hide the parent objects in the 3D view. The program works fine, but there is one strange problem: The names are important, I need to be able to find them in the sceneview. But blender changes the names. A name like "TopDown" becomes "TopDown.001". This happens despite the fact that there are no other objects with this name.

Here is my code:

for plotname, positions in points.items():

    bpy.ops.object.add(type='EMPTY')
    bpy.context.active_object.name = plotname
    bpy.context.active_object.location=(0,0,0)
    print(plotname)               #<---------------here the name is still correct

    for position in positions:
        me = bpy.data.meshes.new(plotname + 'Mesh')
        ob = bpy.data.objects.new(plotname+"Mesh", me)
        ob.location = (position[0], position[1], position[2])
        ob.show_name = True

        bpy.context.scene.objects.link(ob)

        me.from_pydata(verts_loc, [], faces)

        me.update(calc_edges=True)
        ob.parent=bpy.context.active_object

The actual program is a little longer, verts_loc and faces have been set up before this snippet. They represent a cube.

How can I ensure that plotname stays plotname and doesn't become plotname.001 ?

UPDATE

Clarification: It doesn't matter that points within the plot are renamed. Something like "plotnameMesh.001" and "plotnameMesh.002" is no problem. But the parent objects are renamed to. In fact the sole purpose of appending "Mesh" to the object names is to keep the toplevel plotname unique.

1

There are 1 best solutions below

3
On

The for position in positions: implies you are creating multiple objects at different locations for each plotname. One will keep the plotname while the others will get numeric extensions.

for position in positions:
    me = bpy.data.meshes.new(plotname + 'Mesh')
    ob = bpy.data.objects.new(plotname+"Mesh", me)

Is each position unique or are you adding multiple objects at each position?

You will also want to ensure you delete the previous creations before re-running your script.

When you have many objects with similar names you can use Select->Select Pattern to select them. In python you can do that with

[setattr(obj, 'select', True) for obj in bpy.data.objects if obj.name.startswith(plotname)]