08-26-2005 07:44
I'll even go as far as, copy code and paste into notepad, save as BVHCleanup.py
CODE
import poser
import re
import string




####################################################################


#--------------------------------------------------------------------------------
# Figure out which OS we're running
#--------------------------------------------------------------------------------
macver = 0
winver = 0
if(os.name == 'mac'):
macver = 1
separator = ':'
elif(os.name == 'dos' or os.name == 'nt'):
winver = 1
separator = '\\'
else:
raise 'unrecognized operating system\n'


startDir = poser.AppLocation()
lastColon = string.rfind(startDir, separator)
if(lastColon == -1):
startDir = None

getOpenFile = poser.DialogFileChooser(type=poser.kDialogFileChooserOpen, message="Please select a bvh file to clean",
startDir=startDir);
if (getOpenFile.Show()):
srcFilePath = getOpenFile.Path()

if (not re.match(".*\.bvh$", srcFilePath)):
poser.DialogSimple.MessageBox( "Selected file was not a bvh file")
raise("Selected file was not a bvh file")





#################################################################################






srcFile = open(srcFilePath, 'r')
lines = srcFile.readlines()
srcFile.close()

isGoalTable = []
removeLineList = []

# run through from first encountered { to matching } and
# return next block's index
def NextBlock(lines, i):
parenLevel = 0
foundFirst = 0
index = i
while(1):
if(index >= len(lines)):
return len(lines)
line = lines[index]
if(re.search('{', line)):
parenLevel = parenLevel + 1
foundFirst = 1
elif(re.search('}', line)):
parenLevel = parenLevel - 1
index = index + 1
if(parenLevel == 0 and foundFirst):
return index



def ParseRootBlock(lines, i):
isGoal = 0
startInd = i
line = lines
if(line[5:].find('GoalCenterOfMass') > -1):
isGoal = 1
isGoalTable.append(isGoal)
nextInd = NextBlock(lines, i+1)
if(isGoal):
for j in range(startInd, nextInd):
removeLineList.append(j)
return nextInd

def ParseMotionBlock(lines, i):
foundFrameTime = 0
index = i
while(1):
if(index >= len(lines)):
return len(lines)
line = lines[index]
index = index + 1
if(re.match('Frame Time:', line)):
#print 'found Frame Time'
foundFrameTime = 1
break
if(not foundFrameTime):
return index
frameCount = 0
while(1):
if(index >= len(lines)):
return len(lines)
line = lines[index]
if(isGoalTable[frameCount % len(isGoalTable)]):
print 'removing line ', index
removeLineList.append(index)

index = index + 1
frameCount = frameCount + 1


lineInd = 0
while(1):
if(lineInd >= len(lines)):
break
line = lines[lineInd]
if(line[:5] == 'ROOT '):
lineInd = ParseRootBlock(lines, lineInd)
continue
elif(line[:6] == 'MOTION'):
lineInd = ParseMotionBlock(lines, lineInd)
continue
lineInd = lineInd + 1


#print isGoalTable
#print removeLineList

dstFilePath = srcFilePath + '.tmp'

dstFile = open(dstFilePath, 'w')
for i in range(len(lines)):
if(i in removeLineList):
pass
else:
dstFile.write(lines)

dstFile.close()

try:
os.remove(srcFilePath) #Remove the old file
except:
raise 'Problems encountered modifying file (permissions issue?).'

os.rename(dstFilePath, srcFilePath) #Rename the newly created file to the name of the original one


print 'BVH clean complete.'