public static void Import(FullModelData fmd, string path) { AnimationFile animationFile = new AnimationFile(); animationFile.Read(path); foreach (AnimationFileObject animationObject in animationFile.Objects) { Object3D object3D = fmd.GetObject3DByHash(new HashName(animationObject.Name)); Log.Default.Info("Trying to add animation to " + animationObject.Name); if (object3D != null) { Log.Default.Info("Found " + animationObject.Name); object3D.Animations.Clear(); // Kill the old anims. if (animationObject.RotationKeyframes.Count > 0) { QuatLinearRotationController quatLinearRotationController = AddRotations(object3D, animationObject.RotationKeyframes); fmd.AddSection(quatLinearRotationController); } if (animationObject.PositionKeyframes.Count > 0) { LinearVector3Controller linearVector3Controller = AddPositions(object3D, animationObject.PositionKeyframes); fmd.AddSection(linearVector3Controller); } } else { Log.Default.Info("Not Found " + animationObject.Name); } } }
public static string ExportFile(FullModelData data, string path) { AnimationFile animationFile = new AnimationFile(); foreach (Object3D object3D in data.SectionsOfType <Object3D>()) { if (object3D.Animations.Count > 0) { AnimationFileObject animationFileObject = new AnimationFileObject(object3D.HashName.String); foreach (IAnimationController animationController in object3D.Animations) { if (animationController is LinearVector3Controller) { LinearVector3Controller linearVector3Controller = (LinearVector3Controller)animationController; animationFileObject.PositionKeyframes = new List <Keyframe <Vector3> >(linearVector3Controller.Keyframes); } else if (animationController is QuatLinearRotationController) { QuatLinearRotationController quatLinearRotationController = (QuatLinearRotationController)animationController; animationFileObject.RotationKeyframes = new List <Keyframe <Quaternion> >(quatLinearRotationController.Keyframes); } } animationFile.Objects.Add(animationFileObject); } } animationFile.Write(path); return(path); }
public override void Execute(ScriptState state) { var filepath = state.ResolvePath(File); state.Log.Status("Writing animation script to {0}", filepath); var xws = new XmlWriterSettings() { Indent = true }; using XmlWriter xw = XmlWriter.Create(filepath, xws); xw.WriteStartDocument(true); xw.WriteStartElement("modelscript"); foreach (var obj in state.Data.SectionsOfType <Object3D>()) { if (obj.Animations.Count == 0) { continue; } xw.WriteStartElement("animate"); xw.WriteAttributeString("object", obj.Name); foreach (var anim in obj.Animations) { if (anim == null) { xw.WriteStartElement("null"); xw.WriteEndElement(); continue; } xw.WriteStartElement(anim switch { LinearFloatController _ => "float", LinearVector3Controller _ => "vector3", QuatLinearRotationController _ => "quaternion", _ => throw new Exception($"Unrecognised animation controller type in {obj.Name}") });
public static LinearVector3Controller AddPositions(Object3D targetObject, IList <Keyframe <Vector3> > keyframes) { LinearVector3Controller linearVector3Controller = new LinearVector3Controller(); linearVector3Controller.Keyframes = new List <Keyframe <Vector3> >(keyframes); linearVector3Controller.KeyframeLength = linearVector3Controller.Keyframes.Max(kf => kf.Timestamp); if (targetObject.Animations.Count == 0) { targetObject.Animations.Add(linearVector3Controller); } else if (targetObject.Animations.Count == 2 && targetObject.Animations[0].GetType() == typeof(QuatLinearRotationController) && targetObject.Animations[1] == null) { targetObject.Animations[1] = linearVector3Controller; } else { throw new Exception($"Failed to insert animation in {targetObject.Name}: unrecognised controller list shape"); } return(linearVector3Controller); }