2
$\begingroup$

I'm trying to delete the driver of specific shape keys. I have used the code below, which deletes a shape key, using key_idx to select an entry key from the list.

fcurves.remove(fcurves[key_idx])

However, the 'index' values of that list are defined by the drivers rather than the shape keys. That is to say, fcurves.remove(fcurves[3]) will find the third shape-key-with-a-driver, and not the driver of the third shape key.

This means that I can't use key_idx = active_shape_key_index to delete a specific shape key, since I've no way of telling how many previous shape keys have drivers, if any.

There's also bpy.ops.anim.driver_button_remove(all=True), but that just returns {'CANCELLED'}

How can I specify a shape key driver to be deleted, when it's unknown at what 'driver index' that driver sits at?

$\endgroup$

1 Answer 1

2
$\begingroup$

Remove shape key and key driver by shape key index

import bpy

def kill_shape_key_by_ind(oj, ind):
    shape_keys = oj.data.shape_keys
    blocks = shape_keys.key_blocks

    if ind >= len(blocks):
        print("Out of index")
        return

    shape_key = blocks[ind]
    shape_key_name = shape_key.name

    drivers = shape_keys.animation_data.drivers
    dr = drivers.find(f'key_blocks["{shape_key_name}"].value')
    if dr is not None:
        drivers.remove(dr)

    ind = blocks.keys().index(shape_key_name)
    # set active shape key index before remove
    oj.active_shape_key_index = ind

    context_object = bpy.context.object
    bpy.context.view_layer.objects.active = oj
    bpy.ops.object.shape_key_remove()
    bpy.context.view_layer.objects.active = context_object


kill_shape_key_by_ind(bpy.data.objects["Cube"], 3)
$\endgroup$
1
  • 1
    $\begingroup$ Thank you, this works. $\endgroup$
    – Smail
    Mar 24 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.