I have an animation in which a forcefield blows a bunch of chunks apart using the standard physics engine. This animation was then cached for each object, and from within the dope I selected all of them and scaled by "-1", invertng them and reversing the animation. I then manually re-alligned them to 0, and I had a perfect animation of all the pieces coming together.
I am now trying to create an addon that can do this automatically (not new to Python but new to doing it in Blender) and so I want to know what it is I have to call, a property of the object I assume, to get the keyframes in the dope sheet, so I can then scale them by -1.
Step 1: An object with two keyframes 100 frames apart
Step 2: Using "Bake Action", all the key frames from the first to the last are baked.
Step 3: As you can see, all keyframes are now in the Dope Sheet
Step 4: These are then scaled by -1
Step 5: Finally, they are then moved ("g") the total number of frames, in this case 100, in order to realign them.
This is an overview just for useful insights, but the core part I want to know is what I have to call to access the keyframes FROM the Dope Sheet, since IIRC the scaling doesn't work anywhere else.
This is just a basic mockup from what I understand the barebones to be (I found the context.object.animation_data.action
line online and am just using it as a sub-in for what I am looking for:
import bpy
class ReverseAnimationOperator(bpy.types.Operator):
"""Reverse Animation Operator"""
bl_idname = "object.animationReverse_operator"
bl_label = "Reverse Animation"
def execute(self, context):
context.object.animation_data.action = context.object.animation_data.action * -1
return {'FINISHED'}
def add_object_button(self, context):
self.layout.operator(
ReverseAnimationOperator.bl_idname,
text=ReverseAnimationOperator.__doc__,
icon='PLUGIN')
def register():
bpy.utils.register_class(ReverseAnimationOperator)
bpy.types.VIEW3D_MT_object.append(add_object_button)
if __name__ == "__main__":
register()
Blender throws up the error of "-1" being an invalid location.
Thank you in advance!
bl_idname
can contain only one period "." and no capitals. The default text of an operator button isbl_label
. Keyframe points of an fcurve are (frame, value) pairs. Will put together an answer on how to reverse keyframes. $\endgroup$