Duplicate Object

Hi,

I guess this is quite a noob question, but I still do not get it to work.
I want to duplicate an object and get acces to this duplicated object.

Something like this:
New_object = duplicate_Object(“Cube”)

I found a way to duplicate an object when it is selected. But afte duplicating it, I do not know how I can assign this duplicated object to a new variable…

Any help would be realy great.

thanks dechgo

You will have to subscribe to the concept of operators which is a bit different from what you seem to be expecting. You will have to call “bpy.ops.object.duplicate()” and then get the duplicated object via “bpy.context.active_object”. If you have several objects selected before calling the operator, you can get the duplicates via “bpy.context.selected_objects”

Hi Felix,

thanks for your feedback.
I am not very familar with coding in Blender.
But I would prefer not to use any commands which refer to “selected” objects.
Since selecting objects should more or less only be necessery when you are working in the 3d view. With Python I would like to adress/call the objects directly…

But if it’s the only way, then I have to do it.

Greetings
dechgo

Here is a simple way to duplicate objects, datablocks, textures, images etc…


import bpy

ob = bpy.data.objects.get("Cube")
ob_dup = ob.copy()
print(ob_dup.name)
bpy.data.scenes[0].objects.link(ob_dup)

The .copy() will also preserve the entire modifier/constraint stack as well, which is nice if you are in need of an exact duplicate.

NOTE: The duplicate object is not linked to the scene unless you explicitly do so as shown above.

Hi Atom,

thanks for the code snippet.
Interesting that I have to explicit link it to the scene after I have copied it.
But this seems to be more straight forward then the work around with “selecting” the objects…

thanks again

I started out with the same mindset, but you won’t survive long. A lot of functionality is only available in operators, and operators work on context.

2 Likes

yeah, unfortunately some operations can only be done with operators, but most things can be re-created with low-level API.

Note: Atom’s code creates a linked duplicate, if you want an independant copy, also do:
ob_dup.data = ob.data.copy()

1 Like

Hi,

thanks for your feedbacks.
Then I have to get used to this kind of workflow, if it’s the way things are working in Python…

Thanks again for the fast help!

Just be aware it was a design choice made for blender, it’s not tied to python itself