2
$\begingroup$

I have used the mesh data transfer addon by Maurizio Memoli to project a large number of shape keys onto a target mesh from a template object. However, not all of the shape keys in the template object transform regions that influence the target object, resulting in a high number of redundant shape keys that are identical to the basis shape.

Does there exist an addon or script that can iterate though an object's shape keys and automatically delete shapes that are identical to the basis? This would save a lot of time across multiple objects.

$\endgroup$

1 Answer 1

9
$\begingroup$

Try this. You must be in object mode, and it affects all selected objects.

import bpy
import numpy as np

# Tolerance to small differences, change it if you want
tolerance = 0.001

assert bpy.context.mode == 'OBJECT', "Must be in object mode!"

for ob in bpy.context.selected_objects:
    if ob.type != 'MESH': continue
    if not ob.data.shape_keys: continue
    if not ob.data.shape_keys.use_relative: continue

    kbs = ob.data.shape_keys.key_blocks
    nverts = len(ob.data.vertices)
    to_delete = []

    # Cache locs for rel keys since many keys have the same rel key
    cache = {}

    locs = np.empty(3*nverts, dtype=np.float32)

    for kb in kbs:
        if kb == kb.relative_key: continue

        kb.data.foreach_get("co", locs)

        if kb.relative_key.name not in cache:
            rel_locs = np.empty(3*nverts, dtype=np.float32)
            kb.relative_key.data.foreach_get("co", rel_locs)
            cache[kb.relative_key.name] = rel_locs
        rel_locs = cache[kb.relative_key.name]

        locs -= rel_locs
        if (np.abs(locs) < tolerance).all():
            to_delete.append(kb.name)

    for kb_name in to_delete:
        ob.shape_key_remove(ob.data.shape_keys.key_blocks[kb_name])
$\endgroup$
2
  • 1
    $\begingroup$ Suggest a small change to avoid re-processing same mesh and hence same shapekeys set. $\endgroup$
    – batFINGER
    Sep 9, 2021 at 4:44
  • $\begingroup$ Thank you scurest! That script works like a charm. $\endgroup$
    – Frosferes
    Sep 10, 2021 at 7:42

You must log in to answer this question.

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