5
$\begingroup$

I have a script I am working on where I create and add a shape key to an object and add a driver to the value of the shape key. By adding the driver through python it doesn't have any keyframes like a driver would if you manually added it.

I want to be able to add keyframes to the driver's F-curves but I cannot find the data path to access it.

Here is a sample of my script where at the end I would want to insert keyframes:

import bpy

context = bpy.context
data = bpy.data
aobj = context.active_object
me = aobj.data
scn = context.scene
objs = data.objects

if me.shape_keys == None:
    bpy.ops.object.shape_key_add(from_mix=False)

bpy.ops.object.shape_key_add(from_mix=False)

current_key = me.shape_keys.key_blocks[1]

sk = me.shape_keys
cur_key = sk.key_blocks[1]
cur_key.driver_add('value', -1)
drivers = sk.animation_data.drivers

cur_driver = drivers[0]
cur_driver.modifiers.remove(cur_driver.modifiers[0])
$\endgroup$

1 Answer 1

4
$\begingroup$

I was trying to use keyframe_insert(), but what I needed to use was keyframe_points.add().

Here is a sample that adds a F-Curve with an ease-in and ease-out to the driver:

import bpy

context = bpy.context
data = bpy.data
aobj = context.active_object
me = aobj.data
scn = context.scene
objs = data.objects

if me.shape_keys == None:
    bpy.ops.object.shape_key_add(from_mix=False)

bpy.ops.object.shape_key_add(from_mix=False)

current_key = me.shape_keys.key_blocks[1]

sk = me.shape_keys
cur_key = sk.key_blocks[1]
cur_key.driver_add('value', -1)
drivers = sk.animation_data.drivers

cur_driver = drivers[0]
cur_driver.modifiers.remove(cur_driver.modifiers[0])

cur_driver.keyframe_points.add()
cur_driver.keyframe_points.add()

co = [(0.0, 0.0), (1.0, 1.0)]
handles = [[(-0.25, 0.0), (0.25, 0.0)],[(0.75, 1.0), (1.25,1.0)]]
for i in range(len(co)):
    cur_driver.keyframe_points[i].co = co[i]
    cur_driver.keyframe_points[i].handle_left_type = 'FREE'
    cur_driver.keyframe_points[i].handle_right_type = 'FREE'
    cur_driver.keyframe_points[i].handle_left = handles[i][0]
    cur_driver.keyframe_points[i].handle_right = handles[i][1]
$\endgroup$

You must log in to answer this question.

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