forked from tomochi222/motion-data-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbvh_parser.py
187 lines (172 loc) · 6.66 KB
/
bvh_parser.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# coding: utf-8
# parse bvh(BioVision Hierarchical) file for python 3
import re
##################################################
##################################################
class bvh:
##################################################
def __init__(self, bvh_file = ''):
if bvh_file == '':
print('Please set bvh file path and name')
return 0
self.bvh_file = bvh_file
self.current_token = 0
self.skeleton = {}
self.bone_context = []
self.motion_channels = []
self.motions = []
self.reserved = [ "HIERARCHY", "ROOT", "OFFSET", "CHANNELS", "MOTION" ]
self.channel_names = [ "Xposition", "Yposition", "Zposition", "Zrotation", "Xrotation", "Yrotation" ]
print(type(self.bone_context))
scanner = re.Scanner([
(r"[a-zA-Z_]\w*", self.identifier),
(r"-*[0-9]+(\.[0-9]+)?", self.digit),
(r"}", self.close_brace),
(r"{", self.open_brace),
(r":", None),
(r"\s+", None),
])
bvh_file = open(bvh_file, "r")
bvh = bvh_file.read()
bvh_file.close()
tokens, remainder = scanner.scan(bvh)
self.parse_hierarchy(tokens)
self.current_token = self.current_token + 1
self.parse_motion(tokens)
##################################################
def new_bone(self, parent, name):
bone = { "parent" : parent, "channels" : [], "offsets" : []}
return bone
##################################################
def push_bone_context(self, name):
self.bone_context.append(name)
##################################################
def get_bone_context(self):
return self.bone_context[len(self.bone_context)-1]
##################################################
def pop_bone_context(self):
self.bone_context = self.bone_context[:-1]
return self.bone_context[len(self.bone_context)-1]
##################################################
def identifier(self, scanner, token): return "IDENT", token
##################################################
def operator(self, scanner, token): return "OPERATOR", token
##################################################
def digit(self, scanner, token): return "DIGIT", token
##################################################
def open_brace(self, scanner, token): return "OPEN_BRACE", token
##################################################
def close_brace(self, scanner, token): return "CLOSE_BRACE", token
##################################################
def read_offset(self, bvh, token_index):
if (bvh[token_index] != ("IDENT", "OFFSET")):
return None, None
token_index = token_index + 1
offsets = [ 0.0 ] * 3
for i in range(0,3):
offsets[i] = float(bvh[token_index][1])
token_index = token_index + 1
return offsets, token_index
##################################################
def read_channels(self, bvh, token_index):
if (bvh[token_index] != ("IDENT", "CHANNELS")):
return None, None
token_index = token_index + 1
channel_count = int(bvh[token_index][1])
token_index = token_index+1
channels = [ "" ] * channel_count
for i in range(0, channel_count):
channels[i] = bvh[token_index][1]
token_index = token_index+1
return channels, token_index
##################################################
def parse_joint(self, bvh, token_index):
end_site = False
joint_id = bvh[token_index][1]
token_index = token_index + 1
joint_name = bvh[token_index][1]
token_index = token_index + 1
if (joint_id == "End"): # end site
joint_name = self.get_bone_context() + "_Nub"
end_site = True
joint = self.new_bone(self.get_bone_context(), joint_name)
if bvh[token_index][0] != "OPEN_BRACE":
print("Was expecting brace, got ", bvh[token_index])
return None
token_index = token_index + 1
offsets, token_index = self.read_offset(bvh, token_index)
joint["offsets"] = offsets
if (not(end_site)):
channels, token_index = self.read_channels(bvh, token_index)
joint["channels"] = channels
for channel in channels:
self.motion_channels.append((joint_name, channel))
self.skeleton[joint_name] = joint
while (((bvh[token_index][0] == "IDENT") and (bvh[token_index][1] == "JOINT")) or
((bvh[token_index][0] == "IDENT") and (bvh[token_index][1] == "End"))):
self.push_bone_context(joint_name)
token_index = self.parse_joint(bvh, token_index)
self.pop_bone_context()
if (bvh[token_index][0]) == "CLOSE_BRACE":
return token_index + 1
print("Unexpected token ", bvh[token_index])
##################################################
def parse_hierarchy(self, bvh):
self.current_token = 0
if (bvh[self.current_token] != ("IDENT", "HIERARCHY")):
return None
self.current_token = self.current_token + 1
if (bvh[self.current_token] != ("IDENT", "ROOT")):
return None
self.current_token = self.current_token + 1
if (bvh[self.current_token][0] != "IDENT"):
return None
root_name =bvh[self.current_token][1]
root_bone = self.new_bone(None, root_name)
self.current_token = self.current_token + 1
if bvh[self.current_token][0] != "OPEN_BRACE":
return None
self.current_token = self.current_token + 1
offsets, self.current_token = self.read_offset(bvh, self.current_token)
channels, self.current_token = self.read_channels(bvh, self.current_token)
root_bone["offsets"] = offsets
root_bone["channels"] = channels
self.skeleton[root_name] = root_bone
self.push_bone_context(root_name)
# print("Root ", root_bone)
while(bvh[self.current_token][1] == "JOINT"):
self.current_token = self.parse_joint(bvh, self.current_token)
##################################################
def parse_motion(self, bvh):
if (bvh[self.current_token][0] != "IDENT"):
print("Unexpected text")
return None
if (bvh[self.current_token][1] != "MOTION"):
print("No motion section")
return None
self.current_token = self.current_token + 1
if (bvh[self.current_token][1] != "Frames"):
return None
self.current_token = self.current_token + 1
frame_count = int(bvh[self.current_token][1])
self.current_token = self.current_token + 1
if (bvh[self.current_token][1] != "Frame"):
return None
self.current_token = self.current_token + 1
if (bvh[self.current_token][1] != "Time"):
return None
self.current_token = self.current_token + 1
frame_rate = float(bvh[self.current_token][1])
frame_time = 0.0
self.motions = [ () ] * frame_count
print('Parsing now ...')
for i in range(0, frame_count):
channel_values = []
for channel in self.motion_channels:
channel_values.append((channel[0], channel[1], float(bvh[self.current_token][1])))
self.current_token = self.current_token + 1
self.motions[i] = (frame_time, channel_values)
frame_time = frame_time + frame_rate
print('bvh parsing complete !!!!')
##################################################
##################################################