I'm trying to add some items to the scene and rename them but ran into some issues. Example code:
import bpy context = bpy.context name = "Cube" bpy.ops.mesh.primitive_cube_add(location = [1,2,3]) item = context.active_object item.name = name bpy.ops.mesh.primitive_cube_add(location = [10,20,30]) item = context.active_object item.name = name
I thought that this code would name the first cube to "Cube" and the second cube to Cube.001, but it actually does the opposite and I end up with the second cube (10,20,30) named Cube and the first is named Cube.001. Is this a bug?
Is there anyway around this other than manually looping over existing objects and generating a unique name?
I would be happy to use bmesh instead (as that will give me the expected behaviour), but I can't get that method to work either as I need to create a uv map as well. Example workaround:
import bpy import bmesh bm = bmesh.new() bmesh.ops.create_cube(bm, size=1.0, calc_uvs=True) mesh_name = "Cube" mesh_data = bpy.data.meshes.new(mesh_name) bm.to_mesh(mesh_data) bm.free() mesh_obj = bpy.data.objects.new(mesh_data.name, mesh_data) bpy.context.collection.objects.link(mesh_obj)
With this example the naming is correct, but I have no uv maps.