How to duplicate an action (animation) in python?

Hi everyone!

I’m currently trying to write a python addon that creates a copy of the active (animation) action and then iterates over the keyframes.

Right now, I’m stuck at something very simple: I want to create a copy of the current action from inside python. I know that calling ‘bpy.ops.action.new()’ copies the action, but everytime I type this in the python console or run a script containing this line, I get an error, claiming:

‘RuntimeError: Operator bpy.ops.action.new.poll() failed, context is incorrect’

So I tried changing the area/context before calling this line with this code:

import bpy

area = bpy.context.area
old_type = area.type

area.type = 'DOPESHEET_EDITOR'
bpy.context.space_data.mode = 'ACTION'

bpy.ops.action.new()

area.type = old_type

Now there’s no error anymore, but nothing happens. No new action is generated.
I’m kind of lost here, does anyone have any idea how to fix or circumvent this?
Any help would be much appreciated!

Don’t use the operator. If you want to duplicate an action, use

action_copy = bpy.data.actions[’…’].copy()