private float GetOutputValue(IO_MayaANIM anim, AnimData data, float value) { if (data.output == OutputType.angular) { if (anim.header.angularUnit == "deg") { return((float)(value * Math.PI / 180)); } } return(value); }
public SBAnimation ImportSBAnimation(string FileName, SBSkeleton skeleton) { IO_MayaANIM anim = new IO_MayaANIM(); anim.Open(FileName); var animation = new SBAnimation(); animation.Name = Path.GetFileNameWithoutExtension(FileName); animation.FrameCount = anim.header.endTime - 1; foreach (var node in anim.Bones) { SBTransformAnimation a = new SBTransformAnimation(); a.Name = node.name; animation.TransformNodes.Add(a); foreach (var att in node.atts) { if (!trackTypeConversion.ContainsKey(att.type)) { continue; } SBTransformTrack track = new SBTransformTrack(trackTypeConversion[att.type]); a.Tracks.Add(track); foreach (var key in att.keys) { InterpolationType itype = InterpolationType.Linear; float intan = 0; float outtan = 0; if (key.intan == "fixed") { itype = InterpolationType.Hermite; intan = key.t1; } if (key.outtan == "fixed") { itype = InterpolationType.Hermite; outtan = key.t2; } track.AddKey(key.input - header.startTime, GetOutputValue(anim, att, key.output), itype, intan , outtan); } } } return(animation); }
public static void ExportIOAnimationAsANIM(string fname, SBAnimation animation, SBSkeleton Skeleton, bool ordinal = false) { IO_MayaANIM anim = new IO_MayaANIM(); anim.header.endTime = animation.FrameCount + 1; // get bone order List <SBBone> BonesInOrder = getBoneTreeOrder(Skeleton); if (ordinal) { BonesInOrder = BonesInOrder.OrderBy(f => f.Name, StringComparer.Ordinal).ToList(); } foreach (SBBone b in BonesInOrder) { AnimBone animBone = new AnimBone() { name = b.Name }; anim.Bones.Add(animBone); // Add Tracks SBTransformAnimation node = null; foreach (var animNode in animation.TransformNodes) { if (animNode.Name.Equals(b.Name)) { node = animNode; break; } } if (node == null) { continue; } AddAnimData(animBone, node.Transform, ControlType.translate, TrackType.translateX); AddAnimData(animBone, node.Transform, ControlType.translate, TrackType.translateY); AddAnimData(animBone, node.Transform, ControlType.translate, TrackType.translateZ); AddAnimData(animBone, node.Transform, ControlType.rotate, TrackType.rotateX); AddAnimData(animBone, node.Transform, ControlType.rotate, TrackType.rotateY); AddAnimData(animBone, node.Transform, ControlType.rotate, TrackType.rotateZ); AddAnimData(animBone, node.Transform, ControlType.scale, TrackType.scaleX); AddAnimData(animBone, node.Transform, ControlType.scale, TrackType.scaleY); AddAnimData(animBone, node.Transform, ControlType.scale, TrackType.scaleZ); } anim.Save(fname); }
public static void ExportIOAnimationAsANIM(string fname, SBAnimation animation, SBSkeleton Skeleton) { IO_MayaANIM anim = new IO_MayaANIM(); anim.header.endTime = animation.FrameCount + 1; if (!MayaSettings.UseRadians) { anim.header.angularUnit = "deg"; } // get bone order List <SBBone> BonesInOrder = getBoneTreeOrder(Skeleton); if (MayaSettings.Maya2015) { BonesInOrder = BonesInOrder.OrderBy(f => f.Name, StringComparer.Ordinal).ToList(); } if (MayaSettings.RemoveRedundantFrames) { animation.Optimize(); } foreach (SBBone b in BonesInOrder) { AnimBone animBone = new AnimBone() { name = b.Name }; anim.Bones.Add(animBone); // Add Tracks SBTransformAnimation node = null; foreach (var animNode in animation.TransformNodes) { if (animNode.Name.Equals(b.Name)) { node = animNode; break; } } if (node == null) { continue; } //TODO: bake scale for compensate scale... foreach (var track in node.Tracks) { switch (track.Type) { case SBTrackType.TranslateX: AddAnimData(animBone, track.Keys, ControlType.translate, TrackType.translateX); break; case SBTrackType.TranslateY: AddAnimData(animBone, track.Keys, ControlType.translate, TrackType.translateY); break; case SBTrackType.TranslateZ: AddAnimData(animBone, track.Keys, ControlType.translate, TrackType.translateZ); break; case SBTrackType.RotateX: AddAnimData(animBone, track.Keys, ControlType.rotate, TrackType.rotateX); break; case SBTrackType.RotateY: AddAnimData(animBone, track.Keys, ControlType.rotate, TrackType.rotateY); break; case SBTrackType.RotateZ: AddAnimData(animBone, track.Keys, ControlType.rotate, TrackType.rotateZ); break; case SBTrackType.ScaleX: AddAnimData(animBone, track.Keys, ControlType.scale, TrackType.scaleX); break; case SBTrackType.ScaleY: AddAnimData(animBone, track.Keys, ControlType.scale, TrackType.scaleY); break; case SBTrackType.ScaleZ: AddAnimData(animBone, track.Keys, ControlType.scale, TrackType.scaleZ); break; } } } anim.Save(fname); }
public SBAnimation ImportSBAnimation(string FileName, SBSkeleton skeleton) { SBAnimation anim = new SBAnimation(); using (StreamReader r = new StreamReader(new FileStream(FileName, FileMode.Open))) { if (r.ReadLine() != "#SBAnimation Version 1") { return(anim); } while (!r.EndOfStream) { var line = r.ReadLine(); var args = line.Trim().Split(' '); switch (args[0]) { case "comment": break; case "FrameCount": anim.FrameCount = int.Parse(args[1]); break; case "visibility": SBVisibilityAnimation visAnim = new SBVisibilityAnimation(); visAnim.MeshName = args[1]; var visLien = r.ReadLine(); if (visLien == "{") { visLien = r.ReadLine(); while (!r.EndOfStream && visLien != "}") { visLien = visLien.Replace(" ", ""); var frame = visLien.Substring(0, visLien.IndexOf(":")).Trim(); var value = visLien.Substring(visLien.IndexOf(":") + 1, visLien.Length - (visLien.IndexOf(":") + 1)).Trim(); visAnim.Visibility.AddKey(float.Parse(frame), bool.Parse(value)); visLien = r.ReadLine(); } } anim.VisibilityNodes.Add(visAnim); break; case "bone": if (args[1].Contains(".anim")) { IO_MayaANIM manim = new IO_MayaANIM(); var bones = manim.ImportSBAnimation(Path.GetDirectoryName(FileName) + "/" + args[1], skeleton); anim.TransformNodes.AddRange(bones.TransformNodes); } break; case "material": SBMaterialAnimation matAnim = new SBMaterialAnimation(); matAnim.MaterialName = args[1]; matAnim.AttributeName = args[2]; var matLien = r.ReadLine(); if (matLien == "{") { matLien = r.ReadLine(); while (!r.EndOfStream && matLien != "}") { matLien = matLien.Replace(" ", ""); var frame = matLien.Substring(0, matLien.IndexOf(":")).Trim(); var value = matLien.Substring(matLien.IndexOf(":") + 1, matLien.Length - (matLien.IndexOf(":") + 1)).Trim(); var vector = value.Replace(")", "").Replace("(", "").Split(','); if (vector.Length != 4) { continue; } matAnim.Keys.AddKey(float.Parse(frame), new Vector4(float.Parse(vector[0]), float.Parse(vector[1]), float.Parse(vector[2]), float.Parse(vector[3]))); matLien = r.ReadLine(); } } anim.MaterialNodes.Add(matAnim); break; } } } return(anim); }