static void Main(string[] args) { //コンソールアプリ作りたいわけじゃないのでハードコーディングしとく string InputPMD = "miku.pmd"; string InputVMD = "TrueMyHeart.vmd"; string OutputVMD = "tmh_bake.vmd"; MMDModel1 model = (MMDModel1)ModelManager.Read(InputPMD, MikuMikuDance.Model.CoordinateType.RightHandedCoordinate); MMDMotion2 motion = (MMDMotion2)MotionManager.Read(InputVMD, MikuMikuDance.Motion.CoordinateType.RightHandedCoordinate); motion = IKBaker.bake(motion, model); MotionManager.Write(OutputVMD, motion); }
/// <summary> /// ファイルからMMDモーションを読み込む /// </summary> /// <param name="filename">MMDモーションファイル</param> /// <param name="coordinate">変換先座標系</param> /// <returns>MMDモーションオブジェクト</returns> /// <param name="scale">スケーリング値</param> public static MMDMotion Read(string filename, CoordinateType coordinate, float scale = 1.0f) { //フルパス取得 filename = Path.GetFullPath(filename); //ファイルチェック if (!File.Exists(filename)) { throw new FileNotFoundException("MMDモーションファイル:" + filename + "が見つかりません"); } //戻り値用変数 MMDMotion result = null; //ファイルリーダー using (FileStream fs = new FileStream(filename, FileMode.Open)) { BinaryReader reader = new BinaryReader(fs); //マジック文字列 string magic = MMDMotion2.GetString(reader.ReadBytes(30)); if (magic.Substring(0, 20) != "Vocaloid Motion Data") { throw new FileLoadException("MMDモーションファイルではありません"); } //バージョン int version = Convert.ToInt32(magic.Substring(21)); if (version == 2) { result = new MMDMotion2(); } else { throw new FileLoadException("version=" + version.ToString() + "モデルは対応していません"); } result.Read(reader, coordinate, scale); if (fs.Length != fs.Position) { Console.WriteLine("警告:ファイル末尾以降に不明データ?"); } fs.Close(); } return(result); }
/// <summary> /// ファイルへの書き出し /// </summary> /// <param name="filename">ファイル名</param> /// <param name="motion">モーション</param> /// <param name="scale">スケーリング値</param> public static void Write(string filename, MMDMotion motion, float scale = 1f) { //フルパス取得 filename = Path.GetFullPath(filename); //ファイルリーダー using (FileStream fs = new FileStream(filename, FileMode.Create)) { BinaryWriter writer = new BinaryWriter(fs); //マジック文字列 if (motion is MMDMotion2) { writer.Write(MMDMotion2.GetBytes("Vocaloid Motion Data 0002", 25)); writer.Write((byte)0); writer.Write(MMDMotion2.GetBytes("JKLM", 4)); } else { new NotImplementedException("その他のバーションは未作成"); } motion.Write(writer, scale); fs.Close(); } }
private void startBake_Click(object sender, EventArgs e) { // 入力チェック if (!File.Exists(pmdFileName.Text)) { MessageBox.Show( "PMDファイルが見つかりません!:\n" + pmdFileName.Text, "PMD読み込みエラー", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!File.Exists(vmdFileName.Text)) { MessageBox.Show( "VMDファイルが見つかりません!:\n" + vmdFileName.Text, "VMD読み込みエラー", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // saveVMDの上書き確認 if (File.Exists(saveVmdName.Text)) { var result = MessageBox.Show( "VMDファイルが存在します。\n" + saveVmdName.Text + "\n上書きしてよろしいですか?", "上書き確認", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (result != System.Windows.Forms.DialogResult.Yes) { return; } // bakeでエラー出るっぽいので削除しておく File.Delete(saveVmdName.Text); } // Bake! try { MMDModel1 model = (MMDModel1)ModelManager.Read(pmdFileName.Text, MikuMikuDance.Model.CoordinateType.RightHandedCoordinate); MMDMotion2 motion = (MMDMotion2)MotionManager.Read(vmdFileName.Text, MikuMikuDance.Motion.CoordinateType.RightHandedCoordinate); motion = IKBaker.bake(motion, model); MotionManager.Write(saveVmdName.Text, motion); MessageBox.Show( "Bake完了", "Baked!!"); } catch { MessageBox.Show( "Bake中にエラーが発生しました", "未知のエラー", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }