1
$\begingroup$

I have four meshes with shape keys. Two cubes and two spheres. Across all meshes, the shape key names are identical and transform the meshes in similar ways. The shape keys on the first cube and the first sphere are animated with keyframes. Using this script (curtesey of @Harry McKenzie), it is possible to copy over the animated shape keys from Cube 1 to Cube 2 and from Sphere 1 to Sphere 2, because they have matching vertex counts respectively.

def copy_shape_keys(src, tgt):
    # Throw error if vertex count doesn't match
    if len(src.data.vertices) != len(tgt.data.vertices):
        raise RuntimeError('Objects have different vertex count!')
    
    # Delete all shape keys of the target mesh
    if tgt.data.shape_keys:
        for key in tgt.data.shape_keys.key_blocks:
            tgt.shape_key_remove(key)

    keys = src.data.shape_keys
    
    for key in keys.key_blocks:
        # Rebuild shape keys of target mesh using vertex data from source mesh
        k = tgt.shape_key_add(name=key.name, from_mix=False)
        for i, v in enumerate(src.data.vertices):
            k.data[i].co = key.data[i].co
        k.value = key.value
        
        # Create new action for every animated shape key
        data = tgt.data.shape_keys.animation_data_create()
        data.action = bpy.data.actions.new(name=key.name+"_action")
 
        # Copy fcurves from source to target
        for fc in keys.animation_data.action.fcurves:
            tgt_fc = data.action.fcurves.new(data_path=fc.data_path)
            for kf in fc.keyframe_points:
                tgt_fc.keyframe_points.insert(kf.co.x, kf.co.y)

source_obj = bpy.data.objects.get("Cube 1")
target_obj = bpy.data.objects.get("Cube 2")
copy_shape_keys(source_obj, target_obj)

Because this script relies on a perfect topology match, it fails to copy the animated shape keys from Cube 1 to any sphere or from Sphere 1 to any cube.

Animated shape keys on the right

How can this script be altered to allow all scenarios to work? In other words: How can this script be altered to only look at the shape key names, and not vertex data from other meshes, to copy over keyframes?

This question is a follow up to this thread.

$\endgroup$
4
  • 1
    $\begingroup$ It is not possible. Blender requires both shapes to have the same vertex count. Just create or dissolve vertices on the other shape to make them match count. $\endgroup$
    – Harry McKenzie
    Commented Aug 10, 2023 at 1:35
  • 1
    $\begingroup$ Adding to @HarryMcKenzie's comment, the relevant doc part docs.blender.org/manual/en/latest/animation/shape_keys/… $\endgroup$
    – taiyo
    Commented Aug 10, 2023 at 8:27
  • $\begingroup$ Is the only way to access shape key animations truly to copy all data from the ground up? Could there not be a more naive approach of just looking at the shape key value fields in the UI? It is easy to do manually by copy and pasting the keyframes into the timeline. $\endgroup$ Commented Aug 10, 2023 at 9:02
  • $\begingroup$ I would also like to add that using the script which was provided in the question of the original thread, it is possible to copy static shape key values from a cube to a sphere. Why can't this be extended to animated shape key values? $\endgroup$ Commented Aug 10, 2023 at 16:00

0

You must log in to answer this question.

Browse other questions tagged .