• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sunilnair · Jun 10, 2016 at 01:06 PM · meshmecanimhumanoidskinnedmeshrendererboneweights

Binding a mecanim skeleton to a new mesh

Hi,

I am trying to add the skeleton to a new mesh in my scene. The original mesh contains the skeleton and the model moves according to the bone position and rotation. I now have a new mesh without a skeleton. I want to skin this new mesh to this existing skeleton. Below is my code where I copy the bone weights and add the skinned mesh renderer.

It doesn't work. It deforms my new mesh terribly. Just for background, my new mesh is just an x-scaled version of the old mesh without the skeleton attached.

  void Start () 
     {
         vertices = mesh.vertices;
 
         //Get original mesh bone weights
         weights=new BoneWeight[mesh.vertexCount];
         weights = mesh.boneWeights;
         
         // Get original mesh Renderer
         top = this.transform.Find("Tops").gameObject;
         SkinnedMeshRenderer meshRend = top.GetComponent<SkinnedMeshRenderer> ();
 
         //Get original mesh Skeleton
         Transform[] originalMeshBones;
         originalMeshBones = meshRend.bones;
         Matrix4x4[] bindPoses = mesh.bindposes;
         for(int i = 0; i < 16; i++){
             bindPoses[i] = originalMeshBones[i].worldToLocalMatrix * transform.localToWorldMatrix;
         }
 
         //Blend Mesh settings; tshirt is the blend mesh game object          
         tshirt.AddComponent<Animator>();
         tshirt.GetComponent<Animator> ().avatar = humanAvatar;//human avatar is the original mesh avatar
         tshirt.GetComponent<Animator> ().applyRootMotion = true;
 
 
         tshirt = tshirt.transform.Find ("Tops").gameObject;
 
                //add skinned mesh renderer component to the new(blend) mesh
         blendMeshRend = tshirt.AddComponent<SkinnedMeshRenderer> ();
         
                 //copy the bone weights and bind poses and bones
         BoneWeight[] blendMeshWeights=new BoneWeight[mesh.vertexCount];
         blendMesh.boneWeights = weights;
         Debug.Log ("vertex 2: " + blendMesh.boneWeights [2].weight2 + "vertex 100:" + blendMesh.boneWeights [100].weight0);
 
         blendMesh.bindposes = bindPoses;
         Debug.Log ("bind poses 0"+blendMesh.bindposes [7].m00);
         blendMeshRend.bones = originalMeshBones;
 
         //tshirt.transform.parent = this.transform;
         blendMeshRend.sharedMesh = blendMesh;
         blendMeshRend.rootBone = meshRend.rootBone;
     }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Jun 13, 2016 at 11:34 AM 0
Share

Both rigs need to have to same hierarchy and number of bones.

http://answers.unity3d.com/questions/44355/shared-skeleton-and-animation-state.html

avatar image sunilnair · Jun 13, 2016 at 11:45 AM 0
Share

I have just one rig: the main skeleton. I need to bind a mesh (which is not skinned currently) to this skeleton.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xXx12aithexXx · Jun 10, 2016 at 03:18 PM

why not just bind the bones from a 3D editor ? and switch out the mesh .... I like were you're going with this so I'm guessing there is a reason to make it more complex?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sunilnair · Jun 13, 2016 at 11:20 AM 0
Share

Hi, Thanks for your comment. I need to change the bone positions of the skeleton in my script at run time. Since I update the skeleton in script, I cannot bind it in a 3D editor before. Now, if I change the joint positions with the new mesh attached, it will further deform the mesh. This is what I want to:

  1. Update the bone positions of the skeleton.

  2. Bind the new mesh (which correctly matches the new bone positions) to this skeleton

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Wrong bindPoses with generated mesh 1 Answer

Clothing My Mecanim Avatar 0 Answers

Editing mecanim animation from script 0 Answers

Modifying SkinnedMeshRenderer.sharedMesh.uv Doesnt Reset after Stopping game. 1 Answer

Mecanim: undesired mesh intersections 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges