Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 import os
00032 import math
00033 import xml.etree.ElementTree as ET
00034 
00035 NS="{http://www.aldebaran-robotics.com/schema/choregraphe/position.xsd}"
00036 
00037 def _makeJointDict(motors, use_radians = True):
00038     pose = {}
00039     for p in motors.findall(NS + "Motor"):
00040         name = p.find(NS + 'name').text
00041         value = float(p.find(NS + 'value').text)
00042         if not use_radians:
00043             value = math.radians(value)
00044 
00045         pose[name] = value
00046 
00047     return pose
00048 
00049 def getpostures(xap_file):
00050     """ Parses a Aldebaran Choregraphe posture library (.xap files)
00051     into a Python dictionary of postures.
00052     """
00053 
00054     if not os.path.exists(xap_file):
00055         raise RuntimeError("The XAP file %s does not exist." % xap_file)
00056 
00057     try:
00058         tree = ET.parse(xap_file)
00059     except ET.ParseError:
00060         raise RuntimeError("The XAP file %s is not a valid XML file." % xap_file)
00061 
00062 
00063     root=tree.getroot()
00064 
00065     postures = {}
00066 
00067 
00068     positions = [p for p in root.iter(NS + 'position')]
00069 
00070     if not positions:
00071         raise RuntimeError("The XAP file %s does not contain any pose." % xap_file)
00072 
00073     for p in positions:
00074         name = p.find(NS + 'name').text
00075         version = p.find(NS + 'version') 
00076         pose = _makeJointDict(p.find(NS + "Motors"), version is not None and version.text=='2')
00077 
00078         postures[name] = pose
00079 
00080     return postures
00081 
00082 if __name__ == "__main__":
00083 
00084     import sys
00085     if len(sys.argv) != 2:
00086         print("Usage: python xapparser.py <file.xap>")
00087         sys.exit(1)
00088 
00089     print(getpostures(sys.argv[1]))
00090