Keep getting .poll() Context is Incorrect Error even though I set the correct context

I keep getting "Context is incorrect" errors even though I use an override.

Here is the .blend: http://pasteall.org/blend/index.php?id=50312

It has one of my models I am coding some Animation Managing Tools for. I am trying to unlink multiple actions from an object. For this I use a custom class object to refer to the actions.

I made a function that changes the active action for the object and then calls the function for pressing the X next to its name in the Dopesheet Editor "bpy.ops.action.unlink()".

For this I set the Execution Context to the Dopesheet Editor.

Now the strange thing is, that the first action gets unlinked but at the second it raises the error.

Debug Prints show that the area is set to the Dopesheet Editor.

Set action to None

Would simply use

if ob.animation_data:
ob.animation_data.action = None


rather than

bpy.ops.action.unlink()


The former only needs reference to an object.

If you want to remove all animation from an object, actions, drivers and NLA then use

ob.animation_data_clear()


The error from question is caused by clearing the action on the object on first iteration and henceforth having no action to remove, it doesn't poll.

• Yes -- using a function or direct data manipulation is always preferred over calling an operator. – dr. Sybren Sep 9 '18 at 9:52
• @dr.Sybren unfortunately no on this occasion. Some questions are too cryptic. – batFINGER Sep 10 '18 at 4:41
• That .animation_data_clear() also removes the drivers could be potentially unwanted though. – John McDon Sep 11 '18 at 9:29

I tried the answer from batFINGER but this was not solving the problem entirely.

I went through everything since I did not really understand the NLA Editor before but now this is how it works, so here is a little

1. There is the Action Editor(AE) of the Dopesheet Editor and the NLA Strips in the NLA Editor Tracks. Any of both can have users of an action.
2. An action can only have 1 user in the AE, that is for the currently active action action in animation_data of an object.
This is the action that is currently shown in the AE, when you unlink it with the X it is gone from the Action Editor and action should be None.
The AE does not have users for all (other) actions that are shown in the list of the AE.
3. An action also has a user for each NLA Strip in the NLA Tracks of an object. Those are shown in the NLA Editor.
So If you want to unlink an action from an object you will have to .remove() the NLA Strip from nla_track.strips().
I think this is also the official way to remove an action from an object, so when you append it to another file it won't append the actions. It seems though that it does append a single action that is open in the AE.
Also it might be the actions that are included in an .fbx but I did not test this yet.
4. Now something about the behaviour of the AE and NLA Editor:
4.1
If you have an action open in the AE and you then create a new action with the + it will automatically create a muted and locked NLA Track with an NLA Strip of the first action in the NLA Editor.
This will then add an NLA Track to animation_data.nla_tracks and an NLA Strip to that track.
Also if an action has no other Users and you then switch to another action in the AE it will create a track and strip in that manner.
When you hit the Number to create a new User in the AE it will not do so.
4.2
The active action of an object that is currently open in its AE is shown in orange on top of the NLA Tracks in the NLA Editor.
The little arrows next to it will use the bpy.ops.action.pushdown() function which will make an NLA Track with an NLA Strip for the currently active action on top of the NLA Tracks.
You can only do this for actions that have any keyframes.
4.3
All actions that are played in the timeline are the NLA Tracks of an object, including the active action which is the one open in the AE or orange on top of the NLA Tracks in the NLA Editor.

Alright this is it so far, I hope it could help anyone.

Generally speaking if you want to Unlink Actions from an object you should remove the NLA Strips.