6
$\begingroup$

This question already has an answer here:

I would like to duplicate a mesh object, but on a particular object. Right now this is working:

import bpy 

for i in range (0,5):
    bpy.ops.object.duplicate(linked=0,mode='TRANSLATION') 
    bpy.data.objects['cube.001'].animation_data_clear()

The way I want it to do it though is that the script should only duplicate a particular object in the scene that I enter through as a string name. ATM its just duplicating the 1st object and duplicating the duplicates of the duplicates.

$\endgroup$

marked as duplicate by p2or, Polosson, brasshat, David Jan 17 '16 at 19:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

migrated from stackoverflow.com Jan 17 '16 at 18:09

This question came from our site for professional and enthusiast programmers.

17
$\begingroup$

The operator bpy.ops.object.duplicate() will duplicate the selected objects and then make the new duplicate objects selected while de-selecting the original objects. The operator is designed for user interaction which actually leaves the user moving the new duplicates when it is done (when started through the gui).

A better way to create a copy of an object by script is to use the copy() method available to most datablocks and then link it the the scene with scene.objects.link(). Note that the new object will use the same mesh datablock as the original, you can duplicate that as well while you are creating copies, you may also want to consider other data such as particle systems and materials. Instead of starting with the active object you can also specify an exact object with src_obj = bpy.data.objects['Cube']

import bpy

scn = bpy.context.scene
src_obj = bpy.context.active_object

for i in range (0,5):
    new_obj = src_obj.copy()
    new_obj.data = src_obj.data.copy()
    new_obj.animation_data_clear()
    scn.objects.link(new_obj)
$\endgroup$
  • $\begingroup$ this does not copy the rigidbody $\endgroup$ – sliders_alpha May 8 '18 at 21:26
  • $\begingroup$ @sliders_alpha Actually the rigid body setting do get copied, but the new object doesn't get added to the rigid body group. Try adding bpy.data.groups['RigidBodyWorld'].objects.link(new_obj) inside the loop. $\endgroup$ – sambler May 9 '18 at 5:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.