Keyframing object constraint influence

I am writing a script that creates keyframes for an animation.

I have parented an object to another through a ChildOf constraint, and I would like (still in python) to insert keyframes to control the influence.

How can I do that? I can’t find a parameter list for keyframe_insert() to give me the answer.

Thanks.

the data_path is the property (in this case “influence”), and you need to call keyframe_insert on the parent object, e.g.:

bpy.context.object.constraints[‘Child Of’].keyframe_insert(data_path=“influence”)

keyframe_insert(data_path, index=-1, frame=bpy.context.scene.frame_current, group="")Insert a keyframe on the property given, adding fcurves and animation data when necessary.
[TABLE=“class: docutils field-list”]

[TH=“class: field-name field”]Parameters:[/TH]

  • data_path (string) – path to the property to key, analogous to the fcurve’s data path.
  • index (int) – array index of the property to key. Defaults to -1 which will key all indices or a single channel if the property is not an array.
  • frame (float) – The frame on which the keyframe is inserted, defaulting to the current frame.
  • group (str) – The name of the group the F-Curve should be added to if it doesn’t exist yet.

[TR=“class: field”]
[TH=“class: field-name”]Returns:[/TH]
Success of keyframe insertion.
[/TR]
[TR=“class: field”]
[TH=“class: field-name”]Return type:[/TH]
boolean
[/TR]
[/TABLE]
This is the most simple example of inserting a keyframe from python

You can also see artist website
import bpy

obj = bpy.context.object

set the keyframe at frame 1

obj.location = 3.0, 4.0, 10.0
obj.keyframe_insert(data_path=“location”, frame=1)

Note that when keying data paths which contain nested properties this must be done from the ID subclass, in this case the Armature rather then the bone.
import bpy
from bpy.props import PointerProperty

define a nested property

class MyPropGroup(bpy.types.PropertyGroup):
nested = bpy.props.FloatProperty(name=“Nested”, default=0.0)

register it so its available for all bones

bpy.utils.register_class(MyPropGroup)
bpy.types.Bone.my_prop = PointerProperty(type=MyPropGroup,
name=“MyProp”)

get a bone

obj = bpy.data.objects[“Armature”]
arm = obj.data

set the keyframe at frame 1

arm.bones[“Bone”].my_prop_group.nested = 10
arm.keyframe_insert(data_path=‘bones[“Bone”].my_prop.nested’,
frame=1,
group=“Nested Group”)

keyframe_insert(data_path, index=-1, frame=bpy.context.scene.frame_current, group="")Insert a keyframe on the property given, adding fcurves and animation data when necessary.
[TABLE=“class: docutils field-list”]

[TH=“class: field-name field”]Parameters:[/TH]

  • data_path (string) – path to the property to key, analogous to the fcurve’s data path.
  • index (int) – array index of the property to key. Defaults to -1 which will key all indices or a single channel if the property is not an array.
  • frame (float) – The frame on which the keyframe is inserted, defaulting to the current frame.
  • group (str) – The name of the group the F-Curve should be added to if it doesn’t exist yet.

[TR=“class: field”]
[TH=“class: field-name”]Returns:[/TH]
Success of keyframe insertion.
[/TR]
[TR=“class: field”]
[TH=“class: field-name”]Return type:[/TH]
boolean
[/TR]
[/TABLE]
This is the most simple example of inserting a keyframe from python

You can also see artist website
import bpy

obj = bpy.context.object

set the keyframe at frame 1

obj.location = 3.0, 4.0, 10.0
obj.keyframe_insert(data_path=“location”, frame=1)

Note that when keying data paths which contain nested properties this must be done from the ID subclass, in this case the Armature rather then the bone.
import bpy
from bpy.props import PointerProperty

define a nested property

class MyPropGroup(bpy.types.PropertyGroup):
nested = bpy.props.FloatProperty(name=“Nested”, default=0.0)

register it so its available for all bones

bpy.utils.register_class(MyPropGroup)
bpy.types.Bone.my_prop = PointerProperty(type=MyPropGroup,
name=“MyProp”)

get a bone

obj = bpy.data.objects[“Armature”]
arm = obj.data

set the keyframe at frame 1

arm.bones[“Bone”].my_prop_group.nested = 10
arm.keyframe_insert(data_path=‘bones[“Bone”].my_prop.nested’,
frame=1,
group=“Nested Group”)

Thanks – and how do I specify the numerical value that I wish to assign to the influence? Can I do it through another named parameter?

I think your asking if you can use a variable to assign the influence…

You can, so long as it reads back a number and not a path.

Sorry, I’m not following that. Can you be more specific, or give an example? A pointer to a relevant manual page could help too.

ob = bpy.context.object # could be any object
con = ob.constraints[‘Child Of’] # the object’s constraint of name “Child Of”

con.influence = 0.5
con.keyframe_insert(data_path=“influence”, frame=5)

con.influence = 0.8
con.keyframe_insert(data_path=“influence”, frame=20)

That’s excellent! Many thanks.

hey can u tell me how can i animate a object through scripting…???

I’m not an expert on this (as my questions show!), but in my experience there are at least two ways to do it. One is to write a script that creates keyframes using the keyframe_insert() function. You would run that script first to create the F-curves, and then the animation will play back (and render) the normal way.

Another way is to create a frame change handler – a script that will be triggered every time the frame changes. In that method, no keyframes are required – the data are changed on the fly by the script. I’m sure others can tell you more.

you can also access keyframe point directly (F-Curves), setting their .co property (x and y are time and value)

There are more options, but rather advanced:

  • A modal operator that runs in the background (‘PASS_THROUGH’) and changes data - a frame change handler is more appropriate in most cases though
  • Constraints / Drivers
  • PyDrivers (not yet supported by blender 2.6)