2
$\begingroup$

I'm trying to go through each shapekey on an object and rename them with script I'd run through the text editor. For example, if I have 3 shapekeys, and I would like to change the names of shapekey 1 > mouthOpen, shapekey 2 > mouthClose, and shapekey 3 > mouthKiss. How would I code a for loop to assign the correct names to the correct shapekey?

I've found this code so far:

import bpy, re
# get the selected object
selected_object = bpy.context.object

# get its shapekeys
shape_keys = selected_object.data.shape_keys.key_blocks

# loop through shapekeys and replace the names
for index, key in enumerate(shape_keys):
    if key.name != "Basis":
        key.name = "NewName" + str(index)

This code almost works. But, it renames ALL shapekeys to "NewName", and I don't know how to change the code to allow me to assign names by index.

Thank you very much for your time!

$\endgroup$

1 Answer 1

4
$\begingroup$

you can try this little changes: make a list of names use it based on the index-1, or just add 'Basis' as first item; the try: except: thing is in case you have more shapekeys than names, it will report an error otherwise

import bpy, re
# get the selected object
selected_object = bpy.context.object

# get its shapekeys
shape_keys = selected_object.data.shape_keys.key_blocks

names = ['mouthOpen', 'mouthClose', 'mouthKiss']

# loop through shapekeys and replace the names
for index, key in enumerate(shape_keys):
    if key.name != "Basis":
        try: key.name = names[index - 1]
        except: pass
$\endgroup$
1
  • $\begingroup$ That worked!!! Thank you-!! $\endgroup$
    – GoodNight
    Commented Feb 24, 2022 at 8:07

You must log in to answer this question.

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