1
$\begingroup$

I have a character that consists of multiple meshes parented to an armature, and I am trying to duplicate it using a script. Basically, I am looking for the script analog of ⇧ Shift + D, like in this question.

I have tried copying the objects and their datas, and assigning parent to the armature, but they are not properly parented: moving bones does not deform the meshes.

copy_arm_data = bpy.data.objects["Arm"].data.copy()
copy_arm = bpy.data.objects["Arm"].copy()
copy_arm.data = copy_arm_data
copy_mesh1_data = bpy.data.objects["Mesh1"].data.copy()
copy_mesh1 = bpy.data.objects["Mesh1"].copy()
copy_mesh1.data = copy_mesh1_data
copy_mesh2_data = bpy.data.objects["Mesh2"].data.copy()
copy_mesh2 = bpy.data.objects["Mesh2"].copy()
copy_mesh2.data = copy_mesh2_data
    
copy_mesh1.parent = copy_arm
copy_mesh2.parent = copy_arm

What am I missing here?

$\endgroup$

1 Answer 1

1
+50
$\begingroup$

You need to take into account the modifiers part, as an armature is a modifier.

enter image description here

Your code seems correct except that. Add these lines, presuming the modifier is named "Armature' (simple default case):

copy_mesh1.modifiers['Armature'].object = copy_arm
copy_mesh2.modifiers['Armature'].object = copy_arm

But if this is not the case (not named "Armature"), prefer using a function like this one:

def replace_armature(o, old_arm, new_arm):
    o.parent = new_arm
    # Loop over modifiers
    for m in o.modifiers:
        # Check if armature and if this is the wanted 
        # armature to replace
        if m.type == 'ARMATURE' and m.object == old_arm:
            # Assign the new one
            m.object = new_arm

And you can call:

replace_armature(copy_mesh1, bpy.data.objects['Arm'], copy_arm)
replace_armature(copy_mesh2, bpy.data.objects['Arm'], copy_arm)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .