Previous Thread  Next Thread

chat icon object selection and duplication with python

envif

Posted: Mon Oct 15, 2012 3:59 pm
Joined: 15 Oct 2012
Posts: 5
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

Code:

bpy.context.scene.objects.active=bpy.data.objects["Lamp"]
bpy.ops.object.duplicate_move()


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?
Thank's
Reply with quote


wretch1958

Posted: Tue Oct 16, 2012 6:58 pm
Joined: 29 Mar 2012
Posts: 21
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:

Code:

try:
   bpy.ops.object.mode_set(mode='OBJECT')
   bpy.ops.object.select_name(name=obj_name, extend=False)
except:
   print("Error selecting objects[" + obj_name + "]")
   print(" -- verify: change mode to OBJECT and/or object exists")
[/code]
The bpy.ops.object.select_name can have extend=True (your object added to already selected) or extend=False (everything deselected except your object)
Reply with quote


CoDEmanX

Posted: Wed Oct 17, 2012 4:26 pm
Joined: 05 Apr 2009
Posts: 731
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...
Reply with quote


envif

Posted: Tue Oct 23, 2012 3:38 pm
Joined: 15 Oct 2012
Posts: 5
Thanks a lot for your answers!
Reply with quote


 
Jump to:  
Powered by phpBB © 2001, 2005 phpBB Group