Skip to content

Commit 7ce2082

Browse files
author
Roberto De Ioris
committed
added optimizations
1 parent 2347710 commit 7ce2082

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

bvh.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,36 @@ def frame_joint_channel(self, frame_index, joint, channel, value=None):
149149
return value
150150
return float(self.frames[frame_index][joint_index + channel_index])
151151

152+
def frame_joint_channels(self, frame_index, joint, channels, value=None):
153+
values = []
154+
joint_index = self.get_joint_channels_index(joint)
155+
for channel in channels:
156+
channel_index = self.joint_channels(joint).index(channel)
157+
if channel_index == -1 and value is not None:
158+
values.append(value)
159+
else:
160+
values.append(
161+
float(
162+
self.frames[frame_index][joint_index + channel_index]
163+
)
164+
)
165+
return values
166+
167+
def frames_joint_channels(self, joint, channels, value=None):
168+
all_frames = []
169+
joint_index = self.get_joint_channels_index(joint)
170+
for frame in self.frames:
171+
values = []
172+
for channel in channels:
173+
channel_index = self.joint_channels(joint).index(channel)
174+
if channel_index == -1 and value is not None:
175+
values.append(value)
176+
else:
177+
values.append(
178+
float(frame[joint_index + channel_index]))
179+
all_frames.append(values)
180+
return all_frames
181+
152182
def joint_parent(self, name):
153183
joint = self.get_joint(name)
154184
if joint.parent == self.root:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from distutils.core import setup
44

55
setup(name='bvh',
6-
version='0.2',
6+
version='0.3',
77
description='Python module for parsing BVH mocap files',
88
author='20Tab S.r.l.',
99
author_email='info@20tab.com',

tests/test_bvh.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ def test_joint_parent(self):
148148
mocap = Bvh(f.read())
149149
self.assertEqual(mocap.joint_parent('Chest').name, 'Hips')
150150

151+
def test_frame_joint_multi_channels(self):
152+
with open('tests/test_mocapbank.bvh') as f:
153+
mocap = Bvh(f.read())
154+
rotation = mocap.frame_joint_channels(30, 'Head', ['Xrotation', 'Yrotation', 'Zrotation'])
155+
self.assertEqual(rotation, [1.77, 13.94, -7.42])
156+
157+
def test_frames_multi_channels(self):
158+
with open('tests/test_mocapbank.bvh') as f:
159+
mocap = Bvh(f.read())
160+
rotations = mocap.frames_joint_channels('Head', ['Xrotation', 'Yrotation', 'Zrotation'])
161+
self.assertEqual(len(rotations), mocap.nframes)
162+
151163

152164
if __name__ == '__main__':
153165
unittest.main()

0 commit comments

Comments
 (0)