- Home /
Need help with editor script to copy animation data into individual curves
I'm trying to code an editor script to take an animation I build in the Animation Editor, and copy its individual curves into the AnimationCurve public variables of a script attached to a GameObject. (I'm doing this as I want access to the curve data itself, but it's easier to build the animations in the editor.) I'm new to editor scripts. I've had a look at this answer, to get me started. So far I've modified it to the following:
using UnityEditor; using UnityEngine; using System.Collections;
public class CopyCurves { const string duplicatePostfix = "_copy";
[MenuItem("Assets/Copy Animations To Curves")] static void CopyCurvesToIndividualCurves() { // Get selected AnimationClip AnimationClip imported = Selection.activeObject as AnimationClip; if (imported == null) { Debug.Log("Selected object is not an AnimationClip"); return; }
// Copy curves from imported to copy
AnimationClipCurveData[] curveDatas = AnimationUtility.GetAllCurves(imported, true);
for (int i = 0; i < curveDatas.Length; i++)
{
if (curveDatas[i].path=="Joint_0" && curveDatas[i].propertyName=="m_LocalRotation.x") {
AnimationCurve anim = curveDatas[i].curve;
System.Object army = GameObject.Find("/Arm").GetComponent<ArmCurves>();
Keyframe[] keyz = anim.keys;
foreach (Keyframe key in keyz) {
army.joint0x.AddKey(key); // THIS IS THE PROBLEM LINE
}
}
}
Debug.Log("Copying curves is done");
}
}
The GameObject's script ("ArmCurves") is simply this:
using UnityEngine; using System.Collections;
public class ArmCurves : MonoBehaviour { public AnimationCurve joint0x; }
I get the following error on comilation: Assets/Editor/CopyCurves.cs(36,38): error CS1061: Type object' does not contain a definition for
joint0x' and no extension method joint0x' of type
object' could be found (are you missing a using directive or an assembly reference?)
Firstly, can an editor script access variables belonging to a script attached to a GameObject? Secondly, am I mistaken that I can copy the curve data into the exposed AnimationCurve and see it in the curve editor after running this script? And thirdly, if 1 and 2 are okay, what am I doing wrong in the script above?
Answer by Ejlersen · May 10, 2011 at 06:09 PM
I haven't played that much with animations, but I'm quite sure the reason is that your army
variable has been set to the type System.Object
. It should be of the type ArmCurves
, so cast it to that and you should get permission to access joint0x
.
About you questions:
1) Yes, however you can't access scripts in the Editor folder from other scripts (not in the Editor folder). 2) Yes and no, you can copy it into the exposed AnimationCurve joint0x
variable, however I don't think it will be viewable in the Animation editor. You can apply it to the animation on the game object and it should be viewable from there. Try taking a look at AnimationUtility.
Your answer
Follow this Question
Related Questions
AnimationClip.SetCurve not setting a curve... 0 Answers
EDITOR: How to Change Animation Curve Colors 0 Answers
Curve fields in editor scripts 1 Answer
Why can't I edit animation curves imported from Maya? 1 Answer
Can't add property to animation 4 Answers