I have a head model with a lot of shape keys that I don't need. I only need one shape key which is ("mouthOpen"). How can I delete all shape keys excluding the mouthopen ? I'm a beginner level at python.

This is my attempt:

bpy_obj = bpy.context.active_object

    index = bpy_obj.data.shape_keys.key_blocks.keys()
    for ob in index :
        if index.index('mouthOpen'):
  • $\begingroup$ Is it a prerequisite for your answer to have python involved? To my knowledge you can archieve this without having to code. $\endgroup$
    – DarkSoul
    Mar 8, 2022 at 9:19
  • $\begingroup$ yes, I need to do automation in cleaning up rig model for Readyplayerme avatar :) $\endgroup$ Mar 8, 2022 at 9:27
  • $\begingroup$ In that case I am of no use here. I have 0 coding skills. Sure someone else here knows how. Good luck! $\endgroup$
    – DarkSoul
    Mar 8, 2022 at 11:36
  • $\begingroup$ Thanks, man! :) $\endgroup$ Mar 8, 2022 at 11:55

1 Answer 1

# make sure you are in object mode first
import bpy

ojs = bpy.data.objects
oj = ojs["Cube"]
bpy.context.view_layer.objects.active = oj #set context.object

blocks = oj.data.shape_keys.key_blocks
for ind in reversed(range(len(blocks))):
    bl = blocks[ind]
    if bl.name in {"mouthOpen", "Basis"}:
        print(f"Key: {bl.name}")
        print(f"Key: {bl.name} (removed)")
        # set active index before remove
        oj.active_shape_key_index = ind
  • $\begingroup$ appreciate it! I've tried it but I don't know why it always deletes all and leaves the bottom shape key. And I realize I also need the Basis shape key and not only the "mouthOpen". I'm have to modify my question to delete all shape keys and leave the basis and the mouthOpen shape key. $\endgroup$ Mar 8, 2022 at 14:27
  • 1
    $\begingroup$ if b.name == "mouthOpen":   change to   If b.name in {"mouthOpen", "basis"}: $\endgroup$
    – X Y
    Mar 8, 2022 at 15:00
  • $\begingroup$ Hi, I've tried it again but it still removes all shape keys and leaves only the last bottom. I was thinking what if I achieve it by removing from index 2 - 50 because the basis and the mouthOpen is index 0 and 1. How to add _shape_key_index to the shape_key_remove $\endgroup$ Mar 9, 2022 at 3:50
  • $\begingroup$ Try the new one, it should work. $\endgroup$
    – X Y
    Mar 9, 2022 at 7:04
  • 1
    $\begingroup$ thank you!! that's sick! you reverse the range and set the active index..woww I have so much to learn !! $\endgroup$ Mar 9, 2022 at 8:16

You must log in to answer this question.

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