round_collada_numbers.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 '''
4 /*********************************************************************
5  * Software License Agreement (BSD License)
6  *
7  * Copyright (c) 2013, University of Colorado, Boulder
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the Univ of CO, Boulder nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  *
30  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  *********************************************************************/
38 '''
39 # Author: Dave Coleman
40 # Desc: Rounds all the numbers to <decimal places> places
41 
42 from lxml import etree
43 import shlex
44 import sys
45 import io
46 
47 def doRound(values,decimal_places):
48  num_vector = shlex.split(values)
49  new_vector = []
50 
51  for num in num_vector:
52  new_num = round(float(num),decimal_places)
53  print "Old:",num,"New:",new_num
54  new_vector.append(str(new_num))
55 
56  new = " ".join(new_vector)
57  #print 'Original:', values, ' Updated: ', new
58 
59  return new
60 
61 # -----------------------------------------------------------------------------
62 
63 if __name__ == '__main__':
64 
65  # Check input arguments
66  try:
67  input_file = sys.argv[1]
68  output_file = sys.argv[2]
69  decimal_places = int(sys.argv[3])
70  assert( len(sys.argv) < 5 ) # invalid num-arguments
71  except:
72  print '\nUsage: round_collada_numbers.py <input_dae> <output_dae> <decimal places>'
73  print 'Rounds all the numbers to <decimal places> places\n'
74  sys.exit(-1)
75 
76  print '\nCollada Number Rounder'
77  print 'Rounding numbers to', decimal_places, ' decimal places\n'
78 
79  # Read string from file
80  f = open(input_file,'r')
81  xml = f.read();
82 
83  # Parse XML
84  #doc = etree.fromstring(xml)
85  #print(doc.tag)
86  #doc = etree.parse(io.BytesIO(xml))
87  #element=doc.xpath('//ns:asset',namespaces={'ns','http://www.collada.org/2008/03/COLLADASchema'})
88  #print element
89 
90  namespace = 'http://www.collada.org/2008/03/COLLADASchema'
91  dom = etree.parse(io.BytesIO(xml))
92 
93  # find elements of particular name
94  elements=dom.xpath('//ns:translate',namespaces={'ns':namespace})
95  for i in range(len(elements)):
96  elements[i].text = doRound(elements[i].text,decimal_places)
97 
98  # find elements of particular name
99  elements=dom.xpath('//ns:rotate',namespaces={'ns':namespace})
100  for i in range(len(elements)):
101  elements[i].text = doRound(elements[i].text,decimal_places)
102 
103  # find elements of particular name
104  elements=dom.xpath('//ns:min',namespaces={'ns':namespace})
105  for i in range(len(elements)):
106  elements[i].text = doRound(elements[i].text,decimal_places)
107 
108  # find elements of particular name
109  elements=dom.xpath('//ns:max',namespaces={'ns':namespace})
110  for i in range(len(elements)):
111  elements[i].text = doRound(elements[i].text,decimal_places)
112 
113  # find elements of particular name
114  elements=dom.xpath('//ns:float',namespaces={'ns':namespace})
115  for i in range(len(elements)):
116  elements[i].text = doRound(elements[i].text,decimal_places)
117 
118  # save changes
119  f = open(output_file,'w')
120  f.write(etree.tostring(dom))
121  f.close()
122 
123 
def doRound(values, decimal_places)


moveit_kinematics
Author(s): Dave Coleman , Ioan Sucan , Sachin Chitta
autogenerated on Wed Jul 10 2019 04:03:41