Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to make the bones be in bindpose?

Discussion in 'Animation' started by Steven-1, Aug 30, 2022.

  1. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    455
    Is there a way to make a skinned mesh have it's armature be in bind pose?
    I can't seem to find anything.

    Preferably I'd like to do this in code
     
  2. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    455
    Couldn't find a simple function or setting (which is odd IMO, as this should be an easy thing for Unity to add), But I managed to do it like this:

    Code (CSharp):
    1.  var bindposes = smr.sharedMesh.bindposes;
    2.                      var bones = smr.bones;
    3.                      var parents = new Transform[bones.Length];
    4.                      for (int i = 0; i < bones.Length; i++)
    5.                      {
    6.                          parents[i] = bones[i].parent;
    7.                          bones[i].parent = null;
    8.                      }
    9.                      for (int i = 0; i < bones.Length; i++)
    10.                      {
    11.                          bones[i].localScale = bindposes[i].inverse.ExtractScale();
    12.                          bones[i].rotation = bindposes[i].inverse.ExtractRotation();
    13.                          bones[i].position = bindposes[i].inverse.ExtractPosition();
    14.                      }
    15.                      for (int i = 0; i < bones.Length; i++)
    16.                      {
    17.                          bones[i].parent = parents[i];
    18.                      }
    using these extension methods:
    (which I got from https://forum.unity.com/threads/how-to-assign-matrix4x4-to-transform.121966/)

    Code (CSharp):
    1. public static class MatrixExtensions
    2. {
    3.      public static Quaternion ExtractRotation(this Matrix4x4 matrix)
    4.      {
    5.          Vector3 forward;
    6.          forward.x = matrix.m02;
    7.          forward.y = matrix.m12;
    8.          forward.z = matrix.m22;
    9.          Vector3 upwards;
    10.          upwards.x = matrix.m01;
    11.          upwards.y = matrix.m11;
    12.          upwards.z = matrix.m21;
    13.          return Quaternion.LookRotation(forward, upwards);
    14.      }
    15.      public static Vector3 ExtractPosition(this Matrix4x4 matrix)
    16.      {
    17.          Vector3 position;
    18.          position.x = matrix.m03;
    19.          position.y = matrix.m13;
    20.          position.z = matrix.m23;
    21.          return position;
    22.      }
    23.      public static Vector3 ExtractScale(this Matrix4x4 matrix)
    24.      {
    25.          Vector3 scale;
    26.          scale.x = new Vector4(matrix.m00, matrix.m10, matrix.m20, matrix.m30).magnitude;
    27.          scale.y = new Vector4(matrix.m01, matrix.m11, matrix.m21, matrix.m31).magnitude;
    28.          scale.z = new Vector4(matrix.m02, matrix.m12, matrix.m22, matrix.m32).magnitude;
    29.          return scale;
    30.      }
    31. }