I try to select an object and duplicate it with python, but I have some problems...
open the default blender project and type this code
the cube is dupplicated and not the Lamp...
I want to duplicate an object with python without selected it with the mouse.
Can someone give me help?
I think it is best to define selected object rather than active object.
Blender will take care of "active" object on its own. At any time, there is always supposed to be only one "active" object but can have several selected objects. If there is nothing selected, and you select "ob1" then "ob2", "ob2" is active object. If you then delete "ob2" then "ob1" (the last selected ob still in existence) is active etc... something like that, you get the idea. Sometimes active is not necessarily selected if you deselect all, there is still one active and active does not trigger selection to ON. I use:
print("Error selecting objects[" + obj_name + "]")
print(" -- verify: change mode to OBJECT and/or object exists")
The bpy.ops.object.select_name can have extend=True (your object added to already selected) or extend=False (everything deselected except your object)
i'd rather use
ob = bpy.data.objects["Lamp"].copy()
and if you don't want the duplicated object to refer to same datablock, add:
ob.data = bpy.data.objects["Lamp"].data.copy()
I'm sitting, waiting, wishing, building Blender in superstition...
Thanks a lot for your answers!