martes, 3 de junio de 2014

Duplicate a serie of objects in blender 2.70 with python

import bpy

# Extracted from the Bret Battey blog:Bat Hat Media

# Find the objects with the text ".A." on its name,
# make a coy of everyone,
# change the text in copy for".B.",
# and select the new objects

scene= bpy.context.scene

for n in bpy.data.objects:
    if (n.name.find(".A."))!= -1 :
        pos=n.name.find(".A.");
        newname=n.name[0:pos]+".B."+n.name[pos+3:];
        copiedobject=n;
        copiedMesh=copiedobject.data;
        mesh = bpy.data.meshes.new(newname);# Create new mesh
        ob_new = bpy.data.objects.new(newname, mesh);# Create new object associated with the mesh
     
         # Copy data block from the old object into the new object
        ob_new.data = copiedobject.data.copy();
        ob_new.scale = copiedobject.scale;
        ob_new.location = copiedobject.location;
        ob_new.rotation_euler = copiedobject.rotation_euler;

        # Link new object to the given scene and select it
        scene.objects.link(ob_new);
        ob_new.select();
     
        print (n.name," COPIED with name: ",newname);