Hi. I have been trying to add some animation to my blendshapes through a text document that is separated through commas. I have been able to read the lines and everything just fine. the only thing is that I can not reference the blendshapes on the skinned mesh.
So I guess my problem is that I am not able to reference to the correct mesh renderer. Or that it is the meta file that does not work.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class ReadLines : EditorWindow {
public Object csvFile;
public string text;
public GameObject animobject;
public AnimationClip anim_obj;
public Keyframe[] ks;
public string[] bs_name;
[MenuItem("Window/Import Blendshapes")]
public static void ShowWindow()
{
GetWindow<ReadLines>("Import Blendshapes");
}
void OnGUI()
{
GUILayout.Label("Insert text here.", EditorStyles.boldLabel);
csvFile = EditorGUILayout.ObjectField(csvFile, typeof(TextAsset), true);
anim_obj = EditorGUILayout.ObjectField("Animation", anim_obj, typeof(AnimationClip), true) as AnimationClip;
animobject = EditorGUILayout.ObjectField("Mesh to get blendshapes", animobject, typeof(GameObject), true) as GameObject;
AnimationClip anim = anim_obj;
if(GUILayout.Button("Read Lines"))
{
AnimationCurve curve;
AnimationClip clip = anim;
clip.legacy = false;
text = csvFile.ToString();
string[] lines = text.Split("
"[0]);
bs_name = new string[lines.Length];
ks = new Keyframe[lines.Length];
int y = 0;
foreach (var i in lines)
{
string[] parts = i.Split(","[0]);
foreach(var x in parts)
{
//Debug.Log(parts[1]);
//Debug.Log(parts[2]);
//Debug.Log(parts[3]);
float kf = float.Parse(parts[0]);
string timestamp = parts[1];
bs_name[y] = "Blend Shape." + parts[2];
float bs_amount = float.Parse(parts[3]);
ks[y] = new Keyframe(kf, bs_amount);
}
y++;
}
curve = new AnimationCurve(ks);
foreach (string bs in bs_name)
{
clip.SetCurve(animobject.name, typeof(SkinnedMeshRenderer), bs, curve);
}
}
}
}