There are two ways to create a copy of an object in a scene:
- By calling the duplicate operator,
- By using the object’s “copy()” method.
Creating a copy of an object using an operator
To make a copy of an object, just call the duplicate operator:
1 |
bpy.ops.object.duplicate(linked=False) |
The copied object must be selected and active.
The “linked” parameter specifies which copy to make: full (False) – the new object will be completely independent, or linked (True) – an instance of the object is created, the data of which refers to the data of the original object.
Creating a copy of an object using the “copy()” method
We can make a copy of an object without using operators.
By calling the “copy()” method on the original object, we will get its copy.
1 |
obj_copy = bpy.context.active_object.copy() |
Adding a copy of the object to the collection, we will see it in our scene.
1 |
bpy.context.collection.objects.link(obj_copy) |
Using the “copy()” method, we will get an instance of an object. To make a full copy, we need to call the same “copy()” method for the object data and for the object animation data.
1 2 3 4 |
obj_copy.data = obj_copy.data.copy() if obj_copy.animation_data: obj_copy.animation_data.action = obj_copy.animation_data.action.copy() |
Let’s define a function with the necessary calls:
1 2 3 4 5 6 7 8 |
def duplicate(obj, data=True, actions=True, collection=None): obj_copy = obj.copy() if data: obj_copy.data = obj_copy.data.copy() if actions and obj_copy.animation_data: obj_copy.animation_data.action = obj_copy.animation_data.action.copy() collection.objects.link(obj_copy) return obj_copy |
In the input parameters, we will pass:
- obj – the object to be copied
- data and actions – True or False to make a full copy or linked (instance)
- collection – the collection to place a copy of the object
Now, to create a copy of the currently active object, we just need to call the created function:
1 2 3 4 5 |
obj_copy = duplicate( obj=bpy.context.active_object, data=True, actions=True ) |
* .blend file with a code example for my Patreon subscribers
hi I improved a free script, where I use the info presented there. but copying animation data (and the blender operator does it by default, too) cause a bug in my script. this is logical, because calculating a new location, we need to modify the animation too. I will dig it more later, if I’m able to do it…this is difficult to talk in a foreign language, but I’m surprised by the lack of common projects in blender chats… having several persons on some common projects should accelerate things a lot. a lot of tools are not good enough. I mean even simple tools. I don’t imagine more complicated ones. or later… but maybe advanced programmers are more interested by those ones. here an example of the script I improved https://github.com/1C0D/Blender_InstanceBetween
but adding more tools around circular arrays would be great too.
but in general, no matter what is the tool, if it is useful, it can be improved, in a simple way at first. and with several persons, it’s more easy to find solutions.
Perhaps this happens because working with Blender addons is quite specific.
I think if your work is interesting and useful, someone will definitely join you.
thks for your support. your site is so useful too