I want to duplicate an existing animation and bake it with visual transforms applied, something like what the default nla baker does. For simplicity's sake say I need to do this for a single bone. This is how I tried to do it.
sourceAction = bpy.data.actions["myAction"]
action = bpy.data.actions.new("newAction")
fcurves = action.fcurves
poseBone = object.pose.bones["bone"]
bone = object.data.bones["bone"]
curveLocX = fcurves.new('pose.bones["bone"].location', 0, "bone")
curveLocY = fcurves.new('pose.bones["bone"].location', 1, "bone")
curveLocZ = fcurves.new('pose.bones["bone"].location', 2, "bone")
curveRotX = fcurves.new('pose.bones["bone"].rotation_euler', 0, "bone")
curveRotY = fcurves.new('pose.bones["bone"].rotation_euler', 1, "bone")
curveRotZ = fcurves.new('pose.bones["bone"].rotation_euler', 2, "bone")
#iterate through each frame somehow
#get the matrix for current frame
matrix = bone.matrix_local.inverted()*poseBone.matrix
loc = matrix.translation
rot = matrix.to_euler()
curveLocX.keyframe_points.insert(frame, loc.x)
curveLocY.keyframe_points.insert(frame, loc.y)
curveLocZ.keyframe_points.insert(frame, loc.z)
curveRotX.keyframe_points.insert(frame, rot.x)
curveRotY.keyframe_points.insert(frame, rot.y)
curveRotZ.keyframe_points.insert(frame, rot.z)
I'm not sure how to iterate through the frames to get the correct matrix for that frame. Also I'm not sure if there's a better way to get the visual transform channels.
convert_space()is a really neat function btw. – Apr 03 '20 at 16:30flatten()function do exactly? I can't figure it out. – Apr 03 '20 at 17:13foreach_setto set frame, value pairs on keyframe,kf.co = (f, v)needs to be a flat list eg[f0, v0, f1, v1, f2, v2]]Flatten takes two arrays[f0, f1, f2]and[v0, v1, v2]and interweaves (ravels, or flattens) them. – batFINGER Apr 03 '20 at 17:20[(x, y, z), (a, b, c), (d, e, f)] array to[(x, a, d), (y, b, e), (z, c, f)]giving all the x, y and z values. In this case I have use.Tor transpose. Can also reshape, or column stack. I'm a bit of a noob at numpy. – batFINGER Apr 03 '20 at 17:26c[0::2] = ais set values starting at 0 and every 2nd item onwards of c to a. – batFINGER Apr 03 '20 at 17:39animation_datato set each animation as active, and parse them by moving through the timeline right? – Apr 03 '20 at 17:51frames = np.arange(action.frame_range[0], action.frame_range[1] + 1), (and name new action accordingly) or just hard code it. – batFINGER Apr 03 '20 at 18:14ob.animation_data.action = action1, getting the visual transforms into a dictionary and baking it, and then proceeding to the next animationob.animation_data.action = action2. I haven't implemented it yet but I think this is correct. – Apr 03 '20 at 20:50