object selection and duplication with python

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
Posts: 0
Joined: Mon Oct 15, 2012 4:51 pm

object selection and duplication with python

Post by envif » Mon Oct 15, 2012 4:59 pm

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: Select all

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?

Posts: 0
Joined: Thu Mar 29, 2012 6:26 am

Post by wretch1958 » Tue Oct 16, 2012 7:58 pm

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: Select all

	bpy.ops.object.select_name(name=obj_name, extend=False)
	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)

Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Wed Oct 17, 2012 5:26 pm

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...

Posts: 0
Joined: Mon Oct 15, 2012 4:51 pm

Post by envif » Tue Oct 23, 2012 4:38 pm

Thanks a lot for your answers!

Post Reply