round_collada_numbers.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 '''
00004 /*********************************************************************
00005  * Software License Agreement (BSD License)
00006  *
00007  *  Copyright (c) 2013, University of Colorado, Boulder
00008  *  All rights reserved.
00009  *
00010  *  Redistribution and use in source and binary forms, with or without
00011  *  modification, are permitted provided that the following conditions
00012  *  are met:
00013  *
00014  *   * Redistributions of source code must retain the above copyright
00015  *     notice, this list of conditions and the following disclaimer.
00016  *   * Redistributions in binary form must reproduce the above
00017  *     copyright notice, this list of conditions and the following
00018  *     disclaimer in the documentation and/or other materials provided
00019  *     with the distribution.
00020  *   * Neither the name of the Univ of CO, Boulder nor the names of its
00021  *     contributors may be used to endorse or promote products derived
00022  *     from this software without specific prior written permission.
00023  *
00024  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00027  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00028  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00029  *
00030  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00031  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00032  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00033  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00034  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00035  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036  *  POSSIBILITY OF SUCH DAMAGE.
00037  *********************************************************************/
00038 '''
00039 # Author: Dave Coleman
00040 # Desc:   Rounds all the numbers to <decimal places> places
00041 
00042 from lxml import etree
00043 import shlex
00044 import sys
00045 import io
00046 
00047 def doRound(values,decimal_places):
00048     num_vector = shlex.split(values)
00049     new_vector = []
00050 
00051     for num in num_vector:
00052         new_num = round(float(num),decimal_places)
00053         print "Old:",num,"New:",new_num
00054         new_vector.append(str(new_num))
00055 
00056     new = " ".join(new_vector)
00057     #print 'Original:', values, '  Updated: ', new
00058 
00059     return new
00060 
00061 # -----------------------------------------------------------------------------
00062 
00063 if __name__ == '__main__':
00064 
00065     # Check input arguments
00066     try:
00067         input_file = sys.argv[1]
00068         output_file = sys.argv[2]
00069         decimal_places = int(sys.argv[3])
00070         assert( len(sys.argv) < 5 )   # invalid num-arguments
00071     except:
00072         print '\nUsage: round_collada_numbers.py <input_dae> <output_dae> <decimal places>'
00073         print 'Rounds all the numbers to <decimal places> places\n'
00074         sys.exit(-1)
00075 
00076     print '\nCollada Number Rounder'
00077     print 'Rounding numbers to', decimal_places, ' decimal places\n'
00078 
00079     # Read string from file
00080     f = open(input_file,'r')
00081     xml = f.read();
00082 
00083     # Parse XML
00084     #doc = etree.fromstring(xml)
00085     #print(doc.tag)
00086     #doc = etree.parse(io.BytesIO(xml))
00087     #element=doc.xpath('//ns:asset',namespaces={'ns','http://www.collada.org/2008/03/COLLADASchema'})
00088     #print element
00089 
00090     namespace = 'http://www.collada.org/2008/03/COLLADASchema'
00091     dom = etree.parse(io.BytesIO(xml))
00092 
00093     # find elements of particular name
00094     elements=dom.xpath('//ns:translate',namespaces={'ns':namespace})
00095     for i in range(len(elements)):
00096         elements[i].text = doRound(elements[i].text,decimal_places)
00097 
00098     # find elements of particular name
00099     elements=dom.xpath('//ns:rotate',namespaces={'ns':namespace})
00100     for i in range(len(elements)):
00101         elements[i].text = doRound(elements[i].text,decimal_places)
00102 
00103     # find elements of particular name
00104     elements=dom.xpath('//ns:min',namespaces={'ns':namespace})
00105     for i in range(len(elements)):
00106         elements[i].text = doRound(elements[i].text,decimal_places)
00107 
00108     # find elements of particular name
00109     elements=dom.xpath('//ns:max',namespaces={'ns':namespace})
00110     for i in range(len(elements)):
00111         elements[i].text = doRound(elements[i].text,decimal_places)
00112 
00113     # find elements of particular name
00114     elements=dom.xpath('//ns:float',namespaces={'ns':namespace})
00115     for i in range(len(elements)):
00116         elements[i].text = doRound(elements[i].text,decimal_places)
00117 
00118     # save changes
00119     f = open(output_file,'w')
00120     f.write(etree.tostring(dom))
00121     f.close()
00122 
00123 


moveit_kinematics
Author(s): Dave Coleman , Ioan Sucan , Sachin Chitta
autogenerated on Wed Jan 17 2018 03:32:16