3
$\begingroup$

I have a shape key which is controlled by a driver. The driver was created manualy by the user. How would I get the target of the driver from the shape key?

I know how to make a driver for a shape key:

import bpy

#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_id = bpy.context.object.active_shape_key.id_data.name

#Defines active object name.
active_object_name = bpy.context.active_object.name


meshKey = bpy.data.meshes[active_object_name].shape_keys
valDrive = meshKey.key_blocks[selected_shape_key_name].driver_add('value')

valDrive.driver.type = 'AVERAGE'
drvVar = valDrive.driver.variables.new()
drvVar.name = 'DriverName'
drvVar.type = 'TRANSFORMS'
drvVar.targets[0].id = bpy.data.objects['Armature']
drvVar.targets[0].bone_target = 'ShapeKeyController_L'
drvVar.targets[0].transform_space = 'LOCAL_SPACE'
drvVar.targets[0].transform_type = 'LOC_Y'


print("target name is", drvVar.targets[0].bone_target)

But I don't know how to edit/read an existing driver. I thought that it would be something like this but it is not:

import bpy

#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_id = bpy.context.object.active_shape_key.id_data.name

#Defines active object name.
active_object_name = bpy.context.active_object.name

meshKey = bpy.data.meshes[active_object_name].shape_keys
valDrive = meshKey.key_blocks[selected_shape_key_name]

print(valDrive.driver.type.targets[0].bone_target)
$\endgroup$

1 Answer 1

3
$\begingroup$

You can access it at shape_keys.animation_data.drivers.

import bpy

shape_key = bpy.context.object.active_shape_key
if shape_key is None:
    raise()

shape_keys = shape_key.id_data
if shape_keys.animation_data is None:
    raise()

for fc in shape_keys.animation_data.drivers:
    print(fc.data_path)
    print(fc.driver.type)

Hierarchy

enter image description here

Direct access

>>> 
>>> def fn(name):
...     if name == "":
...         print(False)
...     else:
...         print(name)
... 
>>> fn(bpy.data.shape_keys['Key'].animation_data.drivers[0].driver.variables[0].targets[0].bone_target)
False

>>> bpy.data.shape_keys['Key'].animation_data.drivers[0].driver.variables[0].targets[0].bone_target = "Bone.001"
>>> 
>>> fn(bpy.data.shape_keys['Key'].animation_data.drivers[0].driver.variables[0].targets[0].bone_target)
Bone.001
$\endgroup$
3
  • $\begingroup$ So if I wanted to get down to target <class bpy.types.DriverTarget> would I need to make a few for loops or how would I achive that? $\endgroup$
    – koko ze
    Jul 6, 2020 at 18:06
  • $\begingroup$ Sorry, it seems I didn't read it well. You can read/write by either method. $\endgroup$
    – tetii
    Jul 8, 2020 at 3:03
  • $\begingroup$ The "Direct access" explained a lot. But with it came a new problem/question. If you want to explain that too it is here $\endgroup$
    – koko ze
    Jul 11, 2020 at 22:10

You must log in to answer this question.

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