1
$\begingroup$

My question is how do I reference a the fcurve modifier on a shape key driver in Python? I have the following code at the moment:

fcurve =bpy.context.active_object.data.shape_keys.key_blocks["new"].driver_add("value")
#this creates a new Input Variable
var = fcurve.driver.variables.new()

#this selects the first input variable
tar0 = var.targets[0]
#this changes the prop type to Key
tar0.id_type = "KEY"
#this changes the prop type to select from the dropdown Key
tar0.id = bpy.data.shape_keys["Key"]
#this puts in the data path of shape key 1 as the Path
tar0.data_path = sk1_datapath

So this code creates a driver and an input variable and loads it with a shape key. Now my question is how do I create points and edit the handles of the points on the driver like I would in the Driver Editor window normally in blender?

$\endgroup$

1 Answer 1

1
$\begingroup$

So I figured out how to reference the driver's fcurve to add points through fcurve.keyframe_points.insert(0,0). Please be aware the keyframe_points.insert function doesn't return a proper reference the keyframe and you do need to iterate over the fcurve.keyframe_points to return the points successfully for editing interpolation and handles.

For further reference please read the blender api here for properties of the blender Keyframe class: https://docs.blender.org/api/current/bpy.types.Keyframe.html#bpy.types.Keyframe

Here's a working demo that works without any other code:

import bpy
#note: This demo starts of with a cube selected
#The cube has two shape keys named "new" and "test"
#What this code achieves is: 
#Summary: Creates a driver on the shape key "new" and edits the fcurve 
#to add three points and changes the interpolation to SINE and changes the easing

#this is a hardcoded shape key value
#"test" is the name of the shape key
#this is how you reference shape keys using key_blocks followed by name of shape key
sk1 = bpy.context.active_object.data.shape_keys.key_blocks["test"]
#this is how you get the data path from a shape key
sk1_datapath = sk1.path_from_id('value')
#this function adds a generator modifier to the fcurve, remember to delete it
fcurve = bpy.context.active_object.data.shape_keys.key_blocks["new"].driver_add("value")

#this creates a new Input Variable
var = fcurve.driver.variables.new()

#this selects the first input variable
tar0 = var.targets[0]
#this changes the prop type to Key
tar0.id_type = "KEY"
#this changes the prop type to select from the dropdown Key
tar0.id = bpy.data.shape_keys["Key"]
#this puts in the data path of shape key 1 as the Path
tar0.data_path = sk1_datapath

#remove the generator modifier that for some reason is created when using driver_add("value")
fcurve.modifiers.remove(fcurve.modifiers[0])

##Create a new F-Curve by inserting points for the driver
# Add three hardcoded keyframe points
fcurve.keyframe_points.insert(0,0)
fcurve.keyframe_points.insert(0.5,0.5)
fcurve.keyframe_points.insert(1,0)

#this does not work
#point1 = fcurve.keyframe_points.insert(0,0)
#the keyframe_points.insert function does not return the points properly
#so we grab the points manually. You can use a for loop to iterate through.
point1 = fcurve.keyframe_points[0]
point2 = fcurve.keyframe_points[1]
point3 = fcurve.keyframe_points[2]

#change the interpolation and easing of the points
point1.interpolation = "SINE"
point1.easing = "EASE_OUT"

#point 2 and 3 we want to use auto easing which is the default option
#so we don't have to set it
point2.interpolation = "SINE"
point3.interpolation = "SINE"

#this is how to adjust the handles if you need
#point1.handle_left = (0,0.5)

Hope this helps!

$\endgroup$

You must log in to answer this question.

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