#
# mocapParser.py
#
# Author: Nathan Huseth
#
# Date: 5/22/2022
#
# input: Text file with motion capture data
# output: Multiple Text files with marker data
#
# Description:
# A program which processes motion capture data, splits frame data,
# formats marker data to a new line and saves a corresponding text file
#
import os
import tkinter
from tkinter import ttk
from tkinter import filedialog
class ProcessMocap():
# Initialize Class
def __init__(self, outputDir = “output”):
self.padx = 10
self.pady = 10
self.outputDir = outputDir
return
# Open files
def openFiles(self):
# Get files as user input
self.files = filedialog.askopenfiles(title=“Source Directory”, initialdir=“.”, filetypes=[(“Text Files”,“*.txt”)])
# Iterate each file
for textFile in self.files:
# Get name of file
name = textFile.name.split(‘/’)
name = name[–1]
name = name[:–4]
print(“\nProcessing: “ + name + “.txt”)
# Define output directory name
if(self.outDirectory.get().strip() != “”):
self.outputDir = self.outDirectory.get()
# Process Data
self.processFile(textFile, name)
# Close file
textFile.close()
print(name + “.txt finished.”)
print(“\nAll files completed and waiting in /” + self.outputDir + ” directory”)
return
# Controls file processing
def processFile(self, data, name):
# Remove header line
data.readline()
# Create output directories
directory = self.createDestination(name)
# Iterate each line
for line in data:
splitData = line.split()
result = “”
# Iterate each value in line
for index, i in enumerate(splitData):
# Handle frame number
if(index != 0):
# Define result data, newline every third value
result += i + ” “
if((index–1)%3 == 2):
result += “\n“
else:
frame = i
# Save output file
self.saveFile(frame, directory, result)
return
# Create GUI
def createGUI(self):
# Setup window
root = tkinter.Tk()
root.geometry(‘380×190’)
root.title(“Parse Mocap Data”)
frame = ttk.Frame(root)
frame.grid(padx=self.padx, pady=self.pady)
# Output directory
self.outDirectory = tkinter.StringVar()
# Create button and instructions
info = ttk.Label(root, text=“Use the button below to select text files containing mocap data.\nEach frame will be exported as a formatted text file with marker\npositions.”)
outLabel = ttk.Label(root, text=“Output directory name.\nDefaults to \”output\” when blank”)
outDir = ttk.Entry(root, textvariable = self.outDirectory)
directory = ttk.Button(root, text=“Select and process files”, command=self.openFiles)
# Layout elements
info.grid(row=1, column=1, columnspan=2, sticky=“w”, pady=0)
outLabel.grid(row=2, column=1, sticky=“W”, pady = self.pady)
outDir.grid(row=2, column=2, sticky=“W”, pady = self.pady)
directory.grid(row=3, column=2, sticky=“w”, pady=self.pady)
root.mainloop()
return
# Save text file
def saveFile(self, frame, directory, data):
name = “out.” + frame + “.txt”
newFile = open(self.outputDir + “/” + directory + “/” + name, “w”)
newFile.write(data)
newFile.close()
return
# Create directories
def createDestination(self, directory):
# Main output directory
if(os.path.isdir(self.outputDir) == False):
os.mkdir(self.outputDir)
# Frame directory
if(os.path.isdir(self.outputDir + “/” + directory)):
return directory
else:
os.mkdir(self.outputDir + “/” + directory)
return directory