Friday, August 10, 2012

Duplicating Objects in Blender 2.6 Python Scripting

Duplicating objects with Blender 2.6 Python scripting is not quite as straightforward as one might hope, and for me a web search failed to return a simple, clear solution. If found my best solution by searching through the Blender scripts addons folder for files containing the 'copy()' function.

One has to create a new mesh, then copy the data of the source object, then link to the scene. Here's the short function I'm using to do this.

# The following function is adapted from 
# Nick Keeline "Cloud Generator" addNewObject 
# from object_cloud_gen.py (an addon that comes with the Blender 2.6 package)
#
def duplicateObject(scene, name, copyobj):

    # Create new mesh
    mesh = bpy.data.meshes.new(name)

    # Create new object associated with the mesh
    ob_new = bpy.data.objects.new(name, mesh)

    # Copy data block from the old object into the new object
    ob_new.data = copyobj.data.copy()
    ob_new.scale = copyobj.scale
    ob_new.location = copyobj.location

    # Link new object to the given scene and select it
    scene.objects.link(ob_new)
    ob_new.select = True

    return ob_new

5 comments:

  1. Thank very much for this :). It was usefully for me. It's glad to known that many people can make the life easy with little things. Thanks very much again and again :):). Have a good day

    ReplyDelete
  2. Agreed, it's really useful that someone should simply outline the pretty non-intuitive way of duplicating objects for the beginning blender scripter like myself

    ReplyDelete
  3. Thank U very much. Really Usefull code. You right when you say than " a web search failed to return a simple, clear solution."... I have spend a lot of hours trying to get information about it. Realy the blender documentation about API sucks.

    I'm thinking to go to Maya and abandonate Blender:

    (sorry for my english.)
    Now I'm following you in google ====b

    ReplyDelete
  4. This will do it pretty easy:

    bpy.ops.object.duplicate_move_linked()


    ReplyDelete