Duplicating an object (python)

Hi,

I have been trying to duplicating an object (shift+d) through python. All the code I have found doesn’t work in 2.54 :frowning:

Anyone have any code examples, or know a better search terms to use?

It is numerous bezeir curve I am trying to duplicate if that makes a difference.

Works,
Make your object ‘active’ (click e.g.) and in the script


import bpy
bpy.ops.object.duplicate_move()

does the job

Thanks very much; works perfectly.

I probably shouldn’t have been looking @ the object itself to duplicate I guess.

How I found out:
In the default below of the view3d window you can see “Object”
Clicking you see what Shift-D does: hold the cursor above duplicate, after a short time you can see the API command! :wink:

^ thanks have just started learning python, although python is easy finding things in 2.54 can be difficult at times.

Just started doing things the 2.5 way myself and I have to say it’s very impressive.

To duplicate many objects of the same type I would use something like:


import bpy

for ob in bpy.data.objects:
    if ob.type == 'CURVE':
        bpy.context.scene.objects.active = ob
        bpy.ops.object.duplicate_move()

Here you can see the 3 key parts of bpy in use. Objects are retrieved from data, the active object is set using the context and an operator from bpy.ops acts on the object.

I think this will duplicate all curve objects in memory. To operate only on curve objects in the current scene, I’d use the line

for ob in  bpy.context.scene.objects:

Have fun learning Python. Blender has a great API to explore. :slight_smile:

edit:

Haha just tried the above code to make sure it works. It does with no problems, but be careful how many times you hit the ‘Run script’ button. I got carried away and Blender hung for ages before it came back and I realised it had only gone up to Curve.999. :smiley: