2

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.

lhk
  • 27,458
  • 30
  • 122
  • 201

1 Answers1

1

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)]
sambler
  • 6,917
  • 1
  • 16
  • 23
  • The positions will usually be different but that is not guaranteed. Thank you for the tip on how to delete objects with a given name. that could be very useful. But it doesn't solve my specific problem where blender changes the names of objects. – lhk Jan 20 '16 at 09:37
  • 1
    But that loop is where you are getting the duplicates - for every position in positions you are creating an object with the name `plotname+"mesh"` - the first will get that name and the others will get the numeric extension added. – sambler Jan 20 '16 at 11:20
  • oh sorry. My question was not precise enough. the name changes in the loop are acceptable. the problem lies with the toplevel objects. they are changed, too. It doesn't matter if objects with "mesh" are renamed. I just need to ensure that the objects with the plotnames withput mesh aren't changed – lhk Jan 20 '16 at 12:29