I am currently developing a script that I hope will streamline the approach of adding keyframes to existing actions used by action constraints.
I am doing this by allowing the user to move an object to a new position, run an operator which will find the action attached to the action constraint for the selected object, load said action onto the object, insert a new keyframe into the action, before removing the action again.
This allows the user to add a new keyframe to the action without having to go through the process of loading and unloading the action manually.
The problem is that the action constraint seems to be adding any movement the user has made to the object and adding the action on top, creating an offset effect.
The annoying thing is that this code does work when entered line by line via the python console, but when run as a whole from the text editor I get the offset problem shown below (after having moved the object and controller 1 unit on the x axis and then running the script):
The Wiki explains that this is expected:
When the linked action affects some location properties, the owner’s existing location is added to the result of evaluating this constraint....
My existing (and simplified) code is below, hopefully the comments explain the steps, for test purposes it is presumed that the original x location is 0 and that the first keyframe is frame 1):
import bpy;
currentFrame = bpy.context.scene.frame_current;
object = bpy.data.objects['Cube'];
constraint = object.constraints[0];
currentLocation = object.location.copy(); #Store current position
object.location[0] = 0; #Undo movement
object.animation_data.action = constraint.action; #Assign action
object.location = currentLocation; #Move object
object.keyframe_insert(data_path="location", frame=currentFrame, index=0) #Insert keyframe
bpy.context.scene.frame_current = 1 #Return to first keyframe
object.animation_data.action = None; #Remove Action
bpy.context.scene.frame_current = currentFrame; #Return to initial frame
So, how do I avoid the offsetting issue when running the script from the text editor (which will later be an operator)?
I have included a blend file here, it is already set up with one keyframe in the action on frame 1. Setting a second keyframe on frame 10 by running the code line by line in the python console will show the correct result, running the script from the text editor will not.
bpy.context.scene.frame_current = NUMBER
, butbpy.context.scene.frame_set(NUMBER)
$\endgroup$