-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathshorten_bvh.py
executable file
·50 lines (41 loc) · 1.21 KB
/
shorten_bvh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/python3
import argparse
import re
def shortestString(x):
f = float(x)
if abs(f) < 0.00001:
return("0")
else:
return(str(f))
parser = argparse.ArgumentParser(
description='shorten bvh file and change z coordinate of root')
parser.add_argument('-z', default=0.0, type=float, help='change of z coordinate (negative => down)')
parser.add_argument('inputfile', type=str, help='unchanged bvh-file')
parser.add_argument('outputfile', type=str, help='changed bvh-file')
args = parser.parse_args()
with open(args.inputfile) as f:
try:
out= open(args.outputfile, "w")
except:
print ("Cannot write:" + args.outputfile)
exit (20)
#
# handle HIERARCHY
#
for line in f:
out.write(line)
m=re.search("^\s*MOTION", line)
if m:
break
for line in f:
m=re.search("^\s*Frame", line)
if m:
out.write(line)
continue
columns = [shortestString(x) for x in line.split()]
if args.z != 0.0:
columns[2] = "{z:.5f}".format(z=float(columns[2]) + args.z)
newline = ' '.join(columns) + "\n"
out.write(newline)
out.close()
exit(0)