Copy bones orientation between different skeletons - c#

Hello,

I have a humanoid character. The character is animated by an animator or procedurally (for example by rotating its node in the editor). Let’s call him A.

I have a second character, called B. B has a different skeleton, with other axes, etc. I need to copy, by code, at runtime the transforms of skeleton A to replicate its animation on B.

I tested to compute and cache the rotation offset of each bone between A and B at startup and multiply it by the rotation of the animated character and write it to B, with no success. If you turn the head along the Yaw axis, it will turn around the Pitch axis on the second skeleton.

I attached the script to this post.

Any idea how to manage it?
Thanks in advance!

8929250–1223873–CopySkeleton.cs (1.57 KB)

Good luck with that. I’m finding that the API for Unity’s avatar system is badly documented and maybe not even finished.

The only way I’ve found to permanently modify a skeleton at runtime is to create a new avatar from scratch. Here was a repo that helped me out:
https://github.com/bkevelham/unity-avatar-generation

As for trying to change the bone rotations- I’m not sure that will even work because I don’t know if your meshes vertex weights will twist along with the bones. I guess you’ll just have to try it because I’m not sure what will happen.

1 Like

Name the bones correctly and animation system can do the orientation for you.
A range of factors may prevent one armature being able to copy another even with the same names. Import orientation of the bone, what it considers forward to be for example. Often an orientation imported into Unity inherits a -90* on the X. This can apply to all bone of an armature while another model may not have it. Thus the value required to have his head look the same way is not the same value, or not on the same axis. As you may had noticed.

a simple solution would be to keep adjusting and reimporting your model until you’ve aligned its bones correctly with the desired model.
Instead of writing script you can solve these problems by design. For example, removing the armature, and copying and pasting the other armature, and reparenting and re-weighting it.

The only way to achieve it correctly by code is to know how and why the bone imported differently or at a different rotation with a different forward. And if you knew that you wouldn’t need to code it. Because you could repair it. Yet if you did still want to code it then if you know the rough angle that they are off by, or which axis to switch the values from them that’s how it would be coded.

I agree it will probably be easier to fix your character rig in some sort of software like Blender, rather than trying to code it.

rigging is a hard learning curve that never ceases to frustrate :wink: same as the art of importing. My life I had imported and repaired the forward of so many models but each time I save as fbx and import I forget how i did it and have to trial and error a few times. This is blenders fault for not keeping a memory of export as fbx preferences. Though sometimes the blender exporter still isn’t correct you may have to ctrl+A and apply all transforms.

Thanks for the replies guys.
Unfortunately for my usage I really need to do it at runtime and by code :stuck_out_tongue: