Hey everyone, I would like to duplicate parented objects and move them, but when using python it weirdly doesn’t work.
Here I have two small cubes parented to an empty which is parented to the big cube. When I do the duplication and moving manually there is no problem (see top pic), but if I copy the command into the editor and run it, the duplication works, but the objects are misaligned. What am I doing wrong?
You can use bpy.types.ID.copy instead to duplicate your object, allowing access to manual attribute assignments to the duplicated object and its children.
import bpy
# VARIABLES
OBJ = bpy.data.objects["Main_Cube"]
TRANSFORM = [4,0,0]
# PARENT
OBJ_DUPLI = OBJ.copy()
bpy.context.collection.objects.link(OBJ_DUPLI)
# CHILDREN
for c in OBJ.children:
CHILD_DUPLI = c.copy()
CHILD_DUPLI.parent = OBJ_DUPLI
bpy.context.collection.objects.link(CHILD_DUPLI)
OBJ_DUPLI.location = TRANSFORM
2 Likes
Thanks so much for your reply @RPaladin Do you think it is possible to modify your solution to work when both small cubes are parented to an empty which is in turn parented to the big cube? I tried it, but the the elements are in the wrong position and it doesn’t copy the small cubes:
Sure. Simply duplicate the child duplicate process, as basically you’re hoping to duplicate the children of a child. The script can be easily tweaked to duplicate and attach more and more children if you wish as well.
Fyi I exaggerated the empty size so that it’s easier to see in the video.
import bpy
# VARIABLES
OBJ = bpy.data.objects["Main_Cube"]
TRANSFORM = [4,0,0]
# PARENT
OBJ_DUPLI = OBJ.copy()
bpy.context.collection.objects.link(OBJ_DUPLI)
# EMPTY CHILD
for c1 in OBJ.children:
CHILD_DUPLI = c1.copy()
CHILD_DUPLI.parent = OBJ_DUPLI
bpy.context.collection.objects.link(CHILD_DUPLI)
# CUBE CHILDREN
for c2 in c1.children:
CHILD_DUPLI = c2.copy()
CHILD_DUPLI.parent = OBJ_DUPLI
bpy.context.collection.objects.link(CHILD_DUPLI)
OBJ_DUPLI.location = TRANSFORM
1 Like
Amazing Thanks for your help @RPaladin !