Making A Copy Of An Object Using The Blender Python API

There are two ways to create a copy of an object in a scene:

  1. By calling the duplicate operator,
  2. By using the object’s “copy()” method.

Content on Patreon
Creating a copy of an object using an operator

To make a copy of an object, just call the duplicate operator:

Python bpy.ops.object.duplicate(linked=False)
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.

Python obj_copy = bpy.context.active_object.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.

Python bpy.context.collection.objects.link(obj_copy)
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.

Python obj_copy.data = obj_copy.data.copy() if obj_copy.animation_data: obj_copy.animation_data.action = obj_copy.animation_data.action.copy()
1234 obj_copy.data=obj_copy.data.copy() ifobj_copy.animation_data:obj_copy.animation_data.action=obj_copy.animation_data.action.copy()

Let’s define a function with the necessary calls:

Python 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
12345678 defduplicate(obj,data=True,actions=True,collection=None):obj_copy=obj.copy()ifdata:obj_copy.data=obj_copy.data.copy()ifactions andobj_copy.animation_data:obj_copy.animation_data.action=obj_copy.animation_data.action.copy()collection.objects.link(obj_copy)returnobj_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:

Python obj_copy = duplicate( obj=bpy.context.active_object, data=True, actions=True )
12345 obj_copy=duplicate(obj=bpy.context.active_object,data=True,actions=True)

* .blend file with a code example for my Patreon subscribers

Tag » How To Duplicate Objects In Blender