Duplicate children with override

Hello! First time posting here, sorry. I’ve been visiting this place for a while and found a lot of very useful tips and solutions. But my search skills finally failed me and I can’t seem to find an answer to my problem.
I wanted to write this simple duplicating script and broken as it was it served its purpose. Sort of. But now it’s time to tidy things up a bit and I don’t really know much about either Python or Blender.

So, reducing the problem to 2 objects and 2 lines…
Suppose I have a parent object and its child. Both selected. If I type

bpy.ops.object.duplicate()

everything works exactly as one would expect: New parent and a child created and selected.
But this

override = bpy.context.copy()
bpy.ops.object.duplicate(override)

results in reshuffling of the children somehow:

duplecatedChild

Selection is expected, but what with this parenting? Also the children are linked. But if I make more duplicates only the latest two will stay linked.

What am I doing wrong? Or rather what causes this behavior? Is copy of the context different from the actual thing? Is it even a right question?
Please advise. Thank you

hate to answer your question with another question- but why use bpy.ops with an override at all, why not just work with the data itself?

new_obj = some_object.copy()
new_data = some_object.data.copy() # you can skip this and the line below if you want a 'linked duplicate' that shares the same data.
new_obj.data = new_data 
bpy.context.collection.objects.link(new_obj)
new_obj.parent = whatever_object_you_want

working with bpy.ops within a python script is unavoidable sometimes, but if you can avoid it you should.

1 Like

Well, the idea was to write a quick and dirty solution, keep it as simple as possible and take advantage of the things that already there. It was like a duct tape for my latest model =D ‘Duplicate’ is almost exactly what I’m going for because it already:

  • keeps things in their collections,
  • adjusts child’s modifier target object,
  • doesn’t mess up child’s transform.

I mean, the only problem so far was it didn’t work properly with an override for some reason.
But I suppose with data I’ll have more control… whether I want it or not.

Thank you for your reply! Now that I look at it from different perspective, I might start from scratch.