From 3274f9ba4b2f013aaee66a4afd2db8f165f83cd4 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Wed, 16 Jan 2013 12:41:33 +0100 Subject: scripts/osim-enter: Add script to extract component names from .osim --- scripts/osim-extract.py | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100755 scripts/osim-extract.py (limited to 'scripts') diff --git a/scripts/osim-extract.py b/scripts/osim-extract.py new file mode 100755 index 0000000..e9686ec --- /dev/null +++ b/scripts/osim-extract.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# osim-extract.py -- Extract information from .osim file +# +# Copyright (C) 2013 Tobias Klauser +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. + +import os +import sys +import getopt +import xml.etree.ElementTree as ET + +def usage(): + print("""usage: {} [OPTION...] FILE + +options: + -b print only body names + -j print only joint names + -m print only muscle names + -v verbose mode + -h show this help and exit""".format(os.path.basname(sys.argv[0]))) + + +def main(): + try: + opts, args = getopt.getopt(sys.argv[1:], "bjmvh") + except getopt.GetoptError as err: + print(str(err)) + usage() + sys.exit(-1) + + do_print = { 'b': False, 'j': False, 'm': False } + verbose = False + for o, a in opts: + if o == '-b': + do_print['b'] = True; + elif o == '-j': + do_print['j'] = True; + elif o == '-m': + do_print['m'] = True; + elif o == '-v': + verbose = True + elif o == '-h': + usage() + sys.exit(0) + else: + assert False, "unhandled option" + + if len(args) < 1: + print("Error: no .osim model file specified") + usage() + sys.exit(-1) + + if not True in do_print.values(): + for k in do_print.iterkeys(): + do_print[k] = True + + try: + tree = ET.parse(args[0]) + except: + print("Error: failed to parse .osim model file") + sys.exit(-1) + + root = tree.getroot() + if verbose: + print("Found root with version {}".format(root.attrib['Version'])) + model = tree.find('Model') + if verbose: + print("Found model '{}'".format(model.attrib['name'])) + + if do_print['b']: + bodies = [ body.attrib['name'] for body in root.findall('Model/BodySet/objects/Body') ] + if verbose: + print("bodies:") + print(','.join(bodies)) + if do_print['j']: + joints = [ joint.attrib['name'] for joint in root.findall('Model/BodySet/objects/Body/Joint/*') ] + if verbose: + print("joints:") + print(','.join(joints)) + if do_print['m']: + muscles = [ force.attrib['name'] for force in root.findall('Model/ForceSet/objects/Thelen2003Muscle') ] + if verbose: + print("muscles:") + print(','.join(muscles)) + +if __name__ == '__main__': + main() -- cgit v1.2.3-54-g00ecf