The operator bpy.ops.object.duplicate()
will duplicate the selected objects and then make the new duplicate objects selected while de-selecting the original objects. The operator is designed for user interaction which actually leaves the user moving the new duplicates when it is done (when started through the gui).
A better way to create a copy of an object by script is to use the copy()
method available to most datablocks and then link it to a collection using CollectionObjects.link()
in Blender 2.8x or to the scene using scene.objects.link()
in Blender 2.7x. Note that the new object will use the same mesh datablock as the original, you can duplicate that as well while you are creating copies, you may also want to consider other data such as particle systems and materials. Instead of starting with the active object you can also specify an exact object with src_obj = bpy.data.objects['Cube']
Blender 2.8x
import bpy
C = bpy.context
src_obj = bpy.context.active_object
for i in range (0,5):
new_obj = src_obj.copy()
new_obj.data = src_obj.data.copy()
new_obj.animation_data_clear()
C.collection.objects.link(new_obj)
Blender 2.7x
import bpy
scn = bpy.context.scene
src_obj = bpy.context.active_object
for i in range (0,5):
new_obj = src_obj.copy()
new_obj.data = src_obj.data.copy()
new_obj.animation_data_clear()
scn.objects.link(new_obj)