- Home /
How do I get started with Blend Shapes?
How do I get started with Blend Shapes?
Hello I don't have the option ‘Add Curve’ in the animation window... Do you know why ? Thanks
Answer by christinam · Nov 13, 2013 at 06:47 PM
Prepare Artwork
So the first thing to do would be to get your Blend Shapes setup in Maya. The following links are useful guides to getting some basic Blend Shapes ready:
Export your selection to fbx ensuring the animation box is checked and blend Shapes under deformed models is checked.
Import your fbx file into Unity (assets->import new assets->[name of file].fbx).
Drag the asset into the hierarchy window. If you select your object in the hierarchy and look in the inspector, you will see your Blend Shapes are listed under the
SkinnedMeshRenderer
component. Here you can adjust the influence of the blend shape to the default shape, 0 means the blend shape has no influence and 100 means the blend shape has full influence.
Scripting Access
It’s also possible to set the blend weights through code. Useful functions are GetBlendShapeWeight
and SetBlendShapeWeight
which you can read about in these doc pages:
http://docs.unity3d.com/Documentation/ScriptReference/SkinnedMeshRenderer.html
You can also check how many blend shapes a Mesh
has on it by accessing the blendShapeCount
variable along with other useful functions, the docs can be found here:
http://docs.unity3d.com/Documentation/ScriptReference/Mesh.html
Here is an example of code which blends a default shape into two other Blend Shapes over time when attached to a gameobject
that has 3 or more blend shapes:
//Using C#
using UnityEngine;
using System.Collections;
public class BlendShapeExample : MonoBehaviour
{
int blendShapeCount;
SkinnedMeshRenderer skinnedMeshRenderer;
Mesh skinnedMesh;
float blendOne = 0f;
float blendTwo = 0f;
float blendSpeed = 1f;
bool blendOneFinished = false;
void Awake ()
{
skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer> ();
skinnedMesh = GetComponent<SkinnedMeshRenderer> ().sharedMesh;
}
void Start ()
{
blendShapeCount = skinnedMesh.blendShapeCount;
}
void Update ()
{
if (blendShapeCount > 2) {
if (blendOne < 100f) {
skinnedMeshRenderer.SetBlendShapeWeight (0, blendOne);
blendOne += blendSpeed;
} else {
blendOneFinished = true;
}
if (blendOneFinished == true && blendTwo < 100f) {
skinnedMeshRenderer.SetBlendShapeWeight (1, blendTwo);
blendTwo += blendSpeed;
}
}
}
}
Create Animations In Unity
It is also possible to use the Animation window in Unity to create a blend animation, here are the steps:
Open the Animation window under Window->Animation.
On the left of the window click ‘Add Curve’ and add a Blend Shape which will be under Skinned Mesh Renderer.
From here you can manipulate the keyframes and Blend Weights to create the required animation. The documentation has more details about the curve editors and animation window.
Once you are finished editing your animation you can click play in the editor window or the animation window to preview your animation.
Is this compatible with Blender's Shape$$anonymous$$eys?
Here is a code example which uses GUI sliders to adjust the different weights of three Blend Shapes:
void OnGUI()
{
float horizontalSliderPos= skinned$$anonymous$$eshRenderer.GetBlendShapeWeight(0);
float horizontalSliderPos2 = skinned$$anonymous$$eshRenderer.GetBlendShapeWeight(1);
float horizontalSliderPos3 = skinned$$anonymous$$eshRenderer.GetBlendShapeWeight(2);
horizontalSliderPos = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), horizontalSliderPos, 0.0F, 100.0F);
skinned$$anonymous$$eshRenderer.SetBlendShapeWeight(0, horizontalSliderPos);
horizontalSliderPos2 = GUI.HorizontalSlider(new Rect(25, 75, 100, 30), horizontalSliderPos2, 0.0F, 100.0F);
skinned$$anonymous$$eshRenderer.SetBlendShapeWeight(1, horizontalSliderPos2);
horizontalSliderPos3 = GUI.HorizontalSlider(new Rect(25, 125, 100, 30), horizontalSliderPos3, 0.0F, 100.0F);
skinned$$anonymous$$eshRenderer.SetBlendShapeWeight(2, horizontalSliderPos3);
}
Hi this is awsome. I am pretty new though and i have an hard time understanding how this is working. especially line 10 and 11. is there some way we could talk about this? i would really love to learn this and the unity page is not doing it for me im afraid.
I also get this error when i try to make my own sliders: NullReferenceException sliders.OnGUI () (at Assets/sliders.cs:11)
Edit: I fixed the issue by adding your awake function. althought im still not sure what that does and why its needed. i also cannot read it in the unity reference pages. would love to understand and learn more about this topic.
Hey ChoChoEnzo! So on line 10 and 11 I am setting up my variables, in this case the type of the variables are 'Skinned$$anonymous$$eshRenderer' and '$$anonymous$$esh', they are Unity specific types just like 'GameObject'. The Awake function is only called once during the lifetime of the script instance and is mostly used for setting up variables, here is the page which explains in more detail:
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.Awake.html
In the Awake function I am assigning my currently null variables to be the Skinned$$anonymous$$eshRenderer and $$anonymous$$esh attached to the current object respectively, I suggest you read about the GetComponent function:
http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponent.html
I hope this answers your questions but feel free to ask more!
Your answer
![](https://koobas.hobune.stream/wayback/20220613121820im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Blend Shapes on mobile devices? 1 Answer
Negative blend shape values? 0 Answers
Allowing the player to draw something that turn in 2d shape with collison and solid color 1 Answer
Blending just the arms animation 1 Answer
Help with custom shader 0 Answers