2
$\begingroup$

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'):
            bpy.ops.object.shape_key_remove(all=False)
        else:
            bpy.ops.object.shape_key_remove(all=True)
$\endgroup$
4
  • $\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
    Commented Mar 8, 2022 at 9:19
  • $\begingroup$ yes, I need to do automation in cleaning up rig model for Readyplayerme avatar :) $\endgroup$ Commented 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
    Commented Mar 8, 2022 at 11:36
  • $\begingroup$ Thanks, man! :) $\endgroup$ Commented Mar 8, 2022 at 11:55

1 Answer 1

3
$\begingroup$
# 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}")
    else:
        print(f"Key: {bl.name} (removed)")
        # set active index before remove
        oj.active_shape_key_index = ind
        bpy.ops.object.shape_key_remove()
$\endgroup$
5
  • $\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$ Commented Mar 8, 2022 at 14:27
  • 1
    $\begingroup$ if b.name == "mouthOpen":   change to   If b.name in {"mouthOpen", "basis"}: $\endgroup$
    – X Y
    Commented 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$ Commented Mar 9, 2022 at 3:50
  • $\begingroup$ Try the new one, it should work. $\endgroup$
    – X Y
    Commented 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$ Commented 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 .