<commit_before>﻿using NMotiveTools;
using NMotive;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Linq;

class TakToBvhBatch
{
    [STAThreadAttribute]
    public static void Main( string[] args )
    {
        // Print Dependencies.
        //DependenciesDisplayer.PrintDependencies(typeof(TakToBvhBatch).Assembly);
        string logFilePath = Path.GetFullPath(@".\TakToBvhBatch.log");
        using (StreamWriter loger = new StreamWriter(logFilePath))
        {
			int loadingErros = 0;
			int bvhExportErrors = 0;
			int videoExportErrors = 0;

            // Select Directory containing Takes.
            string takeFileDirectory = SelectTakesRootDirectory();
            if (Directory.Exists(takeFileDirectory))
            {
                // Create take processors : to trajectorize, fill gaps, filter.
                IEnumerable<TakeProcessor> takeProcessors = CreateTakeProcessors();
                
				// Exporters
                BVHExporter bvhExporter = CreateBVHExporter();
				VideoExporter videoExporter = new VideoExporter();

				// Process each take.
				IEnumerable<string> takeFiles = Directory.EnumerateFiles(takeFileDirectory, "*.tak", SearchOption.AllDirectories);
				foreach (string takeFile in takeFiles) 
				{
					string takeFileFullPath = Path.GetFullPath(takeFile);
					Log(loger, string.Format("==> Loading take : {0}", takeFileFullPath));
					try
					{
						Take take = new Take(takeFileFullPath);
						Log(loger, "\t==> Take loaded successfully !");
						bvhExportErrors += ExportBVHs(take, takeProcessors, bvhExporter, loger);
						videoExportErrors += ExportVideo(take, videoExporter, loger);
						// Takes can use a lot of memory. Might not want to wait around for the garbage collector to do its clean up.
						take.Dispose();
					}
					catch (Exception e)
					{
						Log(loger, String.Format("\tError : failed to load take {0}: {1}", takeFileFullPath, e.Message));
						++loadingErros;
					}
					Log(loger, "\n================================================================================");
                }
            }
            else
            {
                Log(loger, "No Directory selected ! Abort...");
            }

			int totalErrors = loadingErros + bvhExportErrors + videoExportErrors;
			if (totalErrors > 0)
			{
				Log(loger, "==> Batch finished !");
				Log(loger, string.Format("{0} Take Load Error(s)", loadingErros));
				Log(loger, string.Format("{0} BVH Export Error(s)", bvhExportErrors));
				Log(loger, string.Format("{0} Video Export Error(s)", videoExportErrors));
			}
			else
			{
				Log(loger, "==> Batch finished with success (no errors) !");
			}

			Console.WriteLine("See {0} for details", logFilePath);
            Console.WriteLine("\nAppuyer sur Entrée pour quitter...");
            Console.ReadLine();
        }
    }

    #region Private Methods
    static void Log(StreamWriter pWriter, string message)
    {
        pWriter.WriteLine(message);
        Console.WriteLine(message);
    }

	static BVHExporter CreateBVHExporter()
	{
		BVHExporter exporter = new BVHExporter();
		exporter.ExcludeFingers = false;
		exporter.ExcludeToes = false;
		exporter.HandsDownward = false;
		exporter.MotionBuilderNames = true;
		exporter.SingleJointTorso = false;
		exporter.Units = LengthUnits.Units_Centimeters;
		return exporter;
	}

	static int ExportBVHs(Take pTake, IEnumerable<TakeProcessor> pTakeProcessors, BVHExporter pExporter, StreamWriter pLoger)
	{
		int nbErrors = 0;
		Log(pLoger, "==> Exporting BVH files (1 per skeleton)... !");
		IEnumerable<Node> activeSkels = pTake.Scene.AllSkeletons().Where(skel => skel.Active);
		if (activeSkels.Count() > 0)
		{
			// We will export BVHs only if all processors succeed.
			bool allProcessesSucceeded = true;
			foreach (TakeProcessor processor in pTakeProcessors)
			{
				try
				{
					Result processResult = processor.Process(pTake);
					if (!processResult.Success)
					{
						Log(pLoger, String.Format("\tError : process {0} failed : {1}", processor.Name, processResult.Message));
						allProcessesSucceeded = false;
						++nbErrors;
						break;
					}
				}
				catch (Exception e)
				{
					Log(pLoger, String.Format("\tError : process {0} failed : {1}", processor.Name, e.Message));
					allProcessesSucceeded = false;
					++nbErrors;
					break;
				}
			}

			if (allProcessesSucceeded)
			{
				string takeDir = Path.GetDirectoryName(pTake.FileName);
				// Exporting all active skeletons to a bvh file.
				for (int i = 0; i < activeSkels.Count(); ++i)
				{
					Node skeleton = activeSkels.ElementAt(i);

					string bvhFilePath = Path.GetFileNameWithoutExtension(pTake.FileName) + "_" + skeleton.Name + "." + pExporter.Extension;
					bvhFilePath = Path.GetFullPath(Path.Combine(takeDir, bvhFilePath));
					pExporter.SkeletonName = skeleton.Name;
					try
					{
						Result exportResult = pExporter.Export(pTake, bvhFilePath, true);
						if (exportResult.Success)
						{
							Log(pLoger, String.Format("\t==> BVH {0}/{1} exported successfully to file {2}", i+1, activeSkels.Count(), bvhFilePath));
						}
						else
						{
							Log(pLoger, String.Format("\tError : cannot export BVH {0}/{1} : {2} => {3}", i+1, activeSkels.Count(), bvhFilePath, exportResult.Message));
							++nbErrors;
						}
					}
					catch (Exception e)
					{
						Log(pLoger, String.Format("\tError : cannot export BVH {0}/{1} : {2} => {3}", i+1, activeSkels.Count(), bvhFilePath, e.Message));
						++nbErrors;
					}
				}<commit_msg>_Fixed BVH export<commit_after>﻿using NMotiveTools;
using NMotive;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Linq;

class TakToBvhBatch
{
    [STAThreadAttribute]
    public static void Main( string[] args )
    {
        // Print Dependencies.
        //DependenciesDisplayer.PrintDependencies(typeof(TakToBvhBatch).Assembly);
        string logFilePath = Path.GetFullPath(@".\TakToBvhBatch.log");
        using (StreamWriter loger = new StreamWriter(logFilePath))
        {
			int loadingErros = 0;
			int bvhExportErrors = 0;
			int videoExportErrors = 0;

            // Select Directory containing Takes.
            string takeFileDirectory = SelectTakesRootDirectory();
            if (Directory.Exists(takeFileDirectory))
            {
                // Create take processors : to trajectorize, fill gaps, filter.
                IEnumerable<TakeProcessor> takeProcessors = CreateTakeProcessors();
                
				// Exporters
                BVHExporter bvhExporter = CreateBVHExporter();
				VideoExporter videoExporter = new VideoExporter();

				// Process each take.
				IEnumerable<string> takeFiles = Directory.EnumerateFiles(takeFileDirectory, "*.tak", SearchOption.AllDirectories);
				foreach (string takeFile in takeFiles) 
				{
					string takeFileFullPath = Path.GetFullPath(takeFile);
					Log(loger, string.Format("==> Loading take : {0}", takeFileFullPath));
					try
					{
						Take take = new Take(takeFileFullPath);
						Log(loger, "\t==> Take loaded successfully !");
						bvhExportErrors += ExportBVHs(take, takeProcessors, bvhExporter, loger);
						videoExportErrors += ExportVideo(take, videoExporter, loger);
						// Takes can use a lot of memory. Might not want to wait around for the garbage collector to do its clean up.
						take.Dispose();
					}
					catch (Exception e)
					{
						Log(loger, String.Format("\tError : failed to load take {0}: {1}", takeFileFullPath, e.Message));
						++loadingErros;
					}
					Log(loger, "\n================================================================================");
                }
            }
            else
            {
                Log(loger, "No Directory selected ! Abort...");
            }

			int totalErrors = loadingErros + bvhExportErrors + videoExportErrors;
			if (totalErrors > 0)
			{
				Log(loger, "==> Batch finished !");
				Log(loger, string.Format("{0} Take Load Error(s)", loadingErros));
				Log(loger, string.Format("{0} BVH Export Error(s)", bvhExportErrors));
				Log(loger, string.Format("{0} Video Export Error(s)", videoExportErrors));
			}
			else
			{
				Log(loger, "==> Batch finished with success (no errors) !");
			}

			Console.WriteLine("See {0} for details", logFilePath);
            Console.WriteLine("\nAppuyer sur Entrée pour quitter...");
            Console.ReadLine();
        }
    }

    #region Private Methods
    static void Log(StreamWriter pWriter, string message)
    {
        pWriter.WriteLine(message);
        Console.WriteLine(message);
    }

	static BVHExporter CreateBVHExporter()
	{
		BVHExporter exporter = new BVHExporter();
		exporter.ExcludeFingers = false;
		exporter.ExcludeToes = false;
		exporter.HandsDownward = false;
		exporter.MotionBuilderNames = true;
		exporter.SingleJointTorso = false;
		exporter.Units = LengthUnits.Units_Centimeters;
		return exporter;
	}

	static int ExportBVHs(Take pTake, IEnumerable<TakeProcessor> pTakeProcessors, BVHExporter pExporter, StreamWriter pLoger)
	{
		int nbErrors = 0;
		Log(pLoger, "==> Exporting BVH files (1 per skeleton)... !");
		IEnumerable<Node> activeSkels = pTake.Scene.AllSkeletons().Where(skel => skel.Active);
		if (activeSkels.Count() > 0)
		{
			// We will export BVHs only if all processors succeed.
			bool allProcessesSucceeded = true;
			foreach (TakeProcessor processor in pTakeProcessors)
			{
				try
				{
					Result processResult = processor.Process(pTake);
					if (!processResult.Success)
					{
						Log(pLoger, String.Format("\tError : process {0} failed : {1}", processor.Name, processResult.Message));
						allProcessesSucceeded = false;
						++nbErrors;
						break;
					}
				}
				catch (Exception e)
				{
					Log(pLoger, String.Format("\tError : process {0} failed : {1}", processor.Name, e.Message));
					allProcessesSucceeded = false;
					++nbErrors;
					break;
				}
			}

			if (allProcessesSucceeded)
			{
				string takeDir = Path.GetDirectoryName(pTake.FileName);
				// Exporting all active skeletons to a bvh file.
				for (int i = 0; i < activeSkels.Count(); ++i)
				{
					Node skeleton = activeSkels.ElementAt(i);

					string bvhFilePath = Path.GetFileNameWithoutExtension(pTake.FileName) + "_" + skeleton.Name + "." + pExporter.Extension;
					bvhFilePath = Path.GetFullPath(Path.Combine(takeDir, bvhFilePath));
                    try
					{
						pExporter.SkeletonName = skeleton.Name;
						pExporter.Scale = 1.0f;
						pExporter.StartFrame = pTake.FullFrameRange.Start;
						pExporter.EndFrame = pTake.FullFrameRange.End;
						pExporter.FrameRate = (float)pTake.FrameRate;

						Result exportResult = pExporter.Export(pTake, bvhFilePath, true);
						if (exportResult.Success)
						{
							Log(pLoger, String.Format("\t==> BVH {0}/{1} exported successfully to file {2}", i+1, activeSkels.Count(), bvhFilePath));
						}
						else
						{
							Log(pLoger, String.Format("\tError : cannot export BVH {0}/{1} : {2} => {3}", i+1, activeSkels.Count(), bvhFilePath, exportResult.Message));
							++nbErrors;
						}
					}
					catch (Exception e)
					{
						Log(pLoger, String.Format("\tError : cannot export BVH {0}/{1} : {2} => {3}", i+1, activeSkels.Count(), bvhFilePath, e.Message));
						++nbErrors;
					}
				}
{'fraction_non_alphanumeric': 0.13327457029528428, 'fraction_numerical': 0.005993829881004848, 'mean_word_length': 2.917817679558011, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1072094', 'n_tokens_mistral': 4303, 'n_tokens_neox': 3397, 'n_words': 808}