Search Unity

Setting up bone hierarchy for SkinnedMeshRenderer manually in script

Discussion in 'Scripting' started by Smidgens, Oct 22, 2018.

  1. Smidgens

    Smidgens

    Joined:
    Nov 2, 2014
    Posts:
    25
    I've been trying to find something on this topic without luck.

    What I want to do is instantiate a model through script and set up the bone hierarchy and Skinned Renderer component by hand.

    I've been wondering about the following for example:
    1) Is it possible to retrieve the bone hierarchy information from somewhere? Like say,
    to use HumanBodyBones to retrieve the transform data for a specific bone given a Mesh, Avatar definition, or both?

    2) How does the SkinnedMeshRenderer component map the mesh to the bones? Where is the vertex to bone information stored? The Mesh? The Avatar definition? ​

    To be clear: I do not want to simply instantiate a prefab. I'm curious about how much of this stuff it is possible to do procedurally through scripts.
     
    swimswim likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    You are in luck... it can be done!

    Unity3D is absolutely awesome for this stuff... and I had saved this in my hotlinks section a while back:

    http://docs.unity3d.com/ScriptReference/Mesh-bindposes.html

    Best of all after that sample codelet runs you can pause it and sniff around the scene and see what's going on.

    Glorious!

    NOTE: there is an annoying (infuriating?) detail about the above example code: it produces a mesh facing AWAY from the camera. Spin the object this script is on by 180 around the Y axis, or put your camera on the other side. Or just wind your triangles the opposite way so they face at you.
     
    swimswim and Smidgens like this.
  3. Smidgens

    Smidgens

    Joined:
    Nov 2, 2014
    Posts:
    25
    You magnificent hunk you - this is indeed what I was looking for, and then some. Thanks!
     
    swimswim and Kurt-Dekker like this.