Add MULTIPLE actions to an armature

I have made a direct interface with a Kinect2 sensor, via DLL, for MakeHuman. While recording, sensor data is buffered in an array. Once recording has stopped, and an armature is made the active object, a button operation can transfer / re-target the data to the armature.

The problem is I cannot manage to get a single action, meaning there is one named armatureNameAction and another my name (with code & no keyframes) like this:

import bpy
from mathutils import Vector

action = bpy.data.actions.new(name = 'my name')

ob = bpy.context.object
bone = ob.pose.bones['K2-Root']
value = Vector((1, 1, 2))
bone.location = value
bone.keyframe_insert('location', frame = 1)


This can be fixed by not even creating an action & just assigning the name of armature the action like this:

import bpy
from mathutils import Vector

ob = bpy.context.object
bone = ob.pose.bones['K2-Root']
value = Vector((1, 1, 2))
bone.location = value
bone.keyframe_insert('location', frame = 1)

ob.animation_data.action.name = 'my name'


With this way, how does the person make another sensor session & add a 2nd action?

If calling key_frame_insert directly from a bone, the easy way, and it always writes to an existing action unless it needs to create it, then the answer is to just manipulate the facts on the ground.

In the simple 2nd example, you can simply assign the armature's current action to None, ob.animation_data.action = None. These examples only have just one bone & frame though. When I place that in the real code, there is also an empty my name.001 created. If I try adding ob.animation_data.action = actionto the first example, the same empty action is also created in real code.

Though that empty action would get removed on save / reload, it looks half-assed. With some help from here, if you un-link the action, and rename after the new keyframes are added, you get one action every time.

import bpy
from mathutils import Vector

ob = bpy.context.object
if ob.animation_data and ob.animation_data.action:
# save last action, so it does not get removed without user doing something
ob.animation_data.action.use_fake_user = True

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

bone = ob.pose.bones['K2-Root']
value = Vector((1, 1, 2))
bone.location = value
bone.keyframe_insert('location', frame = 1)

ob.animation_data.action.name = 'my name'

• This does not change the answer, but my implementation of re-targeting is done by duplicating a temporary armature, which gets a .001 name. It gets a rest pose of the first frame of the sensor data. Local space copy_rotation constraints to original then do the re-targeting. This is probably causing the empty .001 actions, and why the simple examples work. – Jeff Palmer Aug 15 '19 at 13:22