simplify_collada.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (c) 2019, 2020 Naoki Hiraoka
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # 3. Neither the name of the Kei Okada nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29 
30 from collada import *
31 from urdf_parser_py.urdf import *
32 from tf.transformations import *
33 import copy
34 import os
35 import sys
36 import math
37 import numpy
38 import argparse
39 
40 
41 def search_used_geom_mat(nodes, used_geometries_id, used_materials_id):
42  for node in nodes:
43  if isinstance(node, scene.GeometryNode):
44  used_geometries_id.add(node.geometry.id)
45 
46  # remove empty triangles
47  active_primitives_in_geometry = []
48  for primitive in node.geometry.primitives:
49  if isinstance(primitive, triangleset.TriangleSet):
50  if primitive.ntriangles != 0:
51  active_primitives_in_geometry.append(primitive)
52  else:
53  print("{} is not supported".format(type(primitive)))
54  node.geometry.primitives = active_primitives_in_geometry
55 
56  # remove unsed material refs
57  used_materials_in_geometry = set()
58  for primitive in node.geometry.primitives:
59  used_materials_in_geometry.add(primitive.material)
60  active_materials_in_geometry = []
61  for material in node.materials:
62  if material.symbol in used_materials_in_geometry:
63  active_materials_in_geometry.append(material)
64  node.materials = active_materials_in_geometry
65 
66  for material in node.materials:
67  used_materials_id.add(material.target.id)
68 
69  if isinstance(node, scene.Node):
70  search_used_geom_mat(node.children, used_geometries_id, used_materials_id)
71 
72 
73 def simplify_collada(mesh_):
74  used_geometries_id = set()
75  used_materials_id = set()
76  search_used_geom_mat(mesh_.scene.nodes, used_geometries_id, used_materials_id)
77 
78  # remove unused geometries
79  active_geometries = []
80  for geometry in mesh_.geometries:
81  if geometry.id in used_geometries_id:
82  active_geometries.append(geometry)
83  mesh_.geometries = active_geometries
84 
85  # remove unused materials
86  active_materials = []
87  for material in mesh_.materials:
88  if material.id in used_materials_id:
89  active_materials.append(material)
90  mesh_.materials = active_materials
91 
92  # remove unused effects
93  used_effects_id = set()
94  for material in mesh_.materials:
95  used_effects_id.add(material.effect.id)
96  active_effects = []
97  for effect in mesh_.effects:
98  if effect.id in used_effects_id:
99  active_effects.append(effect)
100  mesh_.effects = active_effects
101 
102  # remove animations
103  mesh_.animations = []
104  # Because library_animations is not modified by Collada.save() function, library_animations needs to be removed manually.
105  node = mesh_.xmlnode.find(tag("library_animations"))
106  if node is not None:
107  mesh_.xmlnode.getroot().remove(node)
108 
109  mesh_.save()
def search_used_geom_mat(nodes, used_geometries_id, used_materials_id)
def simplify_collada(mesh_)


gundam_rx78_description
Author(s): Association GUNDAM GLOBAL CHALLENGE, Kei Okada , Naoki Hiraoka
autogenerated on Wed Sep 2 2020 03:33:36