This started with this question.
I have a list of shape keys where some have and some don't have a driver. How do I get an index of a selected shape key driver? I thought of inputing just the index of the shape key but that of course doesn't work because they don't matchup.
My end game is to input the index of the selected shape key and get the name of the target bone which drives the selected shape key
Here is the code that I have currently (I currently get the name of the bone on the driver index 0 because I set it to that but of course I want that to be dynamic):
import bpy
#Defines the Basis name.
selected = bpy.context.object.active_shape_key_index
bpy.context.object.active_shape_key_index = 0
basis_name = bpy.context.object.active_shape_key.name
bpy.context.object.active_shape_key_index = selected
#Defines the selected shape key index.
selected_shape_key_index = bpy.context.object.active_shape_key_index
#Defines the selected shape key name.
selected_shape_key_name = bpy.context.object.active_shape_key.name
#Defines the selected shape key collection.
selected_shape_key_collection = bpy.context.object.active_shape_key.id_data.name
#x is the index of shape key with a driver. In my scene since I have 2 shape keys with drivers the index goes from 0 to 1.
x = 0
print(bpy.data.shape_keys["ShapeKeyCollection"].id_data.animation_data.drivers[x].driver.variables[0].targets[0].bone_target)
This is the little test/example file (Blender 2.9 alpha):
.replace
parts. $\endgroup$ – koko ze Jul 12 '20 at 19:36