Duplicate object and adding constraint

Hello everybody,

I am trying to write a script in which I copy an object many times and add a constraint to the duplicated object…

To duplicate the object, I select inside the script and use:

 
bpy.ops.object.duplicate()

My problem is now how can I assign this new object to my variable (e.g. dupli_object).
As far as I know to assign the object I can use this:

 
object_variable = bpy.data.objects['ObjectName']

To assign the constraint then I have to use:

 
dupli_object.constraints.new('FOLLOW_PATH')

Because the object is created inside a loop in the script I do not want to assign it with the name.
I hoped I can get acces to it when I use the Collection number from:

bpy.data.objects[nr]

Because I thought that the last created object will have the last number in the collection.
But I noticed now, that this is not true and so my code creates an error…
So the basic problem I have, how can I assign a variable the duplicated object, so that I can get acces to the constraints??

I hope I explained my problem good enough…
I would appreciate every help I can get.
Thanks in advance

Greetings dechgo

when you duplicate new object gets context, so add the constraint and then reselect first one like this…


import bpy
obj = bpy.context.object

for a in range(10):
    bpy.ops.object.duplicate()
    new_obj = bpy.context.object
    new_obj.constraints.new('FOLLOW_PATH')
    
    new_obj.select = False
    bpy.context.scene.objects.active = obj
    obj.select = True

Hi liero,

thanks for your help.

I think this is exactly what I was looking for. I’ll have to try it later…

Thank you very much!!!

Greetings dechgo

post a simple version of your code, it might seem like more work but if you can break down the problem to a small section of code that we can run, debugging it will be much more fun… rather than having us imagine what your code does or the way it does it.