I didn't think baking animation worked by randomly selecting joints, I thought you had to select the hierarchy first, you would basically have to manually select the first joint you created in your chain, for instance the pelvis joint, then run your script, starting with selecting the hierarchy, then baking the animation…