1 from __future__ import absolute_import
2 #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
5 from fabmetheus_utilities.geometry.geometry_tools import face
6 from fabmetheus_utilities.geometry.solids import triangle_mesh
7 from fabmetheus_utilities.vector3 import Vector3
9 from xml.parsers.expat import ParserCreate
11 def getCarving(fileName=''):
12 "Get the triangle mesh for the dae file."
13 return daeModel().load(fileName)
15 class daeModel(triangle_mesh.TriangleMesh):
17 triangle_mesh.TriangleMesh.__init__(self)
19 def load(self, filename):
21 r.StartElementHandler = self._StartElementHandler
22 r.EndElementHandler = self._EndElementHandler
23 r.CharacterDataHandler = self._CharacterDataHandler
26 self._cur = self._base
28 self._geometryList = []
29 r.ParseFile(open(filename, "r"))
31 for instance_visual_scene in self._base['collada'][0]['scene'][0]['instance_visual_scene']:
32 for node in self._idMap[instance_visual_scene['_url']]['node']:
33 self._ProcessNode2(node)
41 def _ProcessNode2(self, node, matrix = None):
44 matrix = map(float, node['matrix'][0]['__data'].split())
47 newMatrix[0] = oldMatrix[0] * matrix[0] + oldMatrix[1] * matrix[4] + oldMatrix[2] * matrix[8] + oldMatrix[3] * matrix[12]
48 newMatrix[1] = oldMatrix[0] * matrix[1] + oldMatrix[1] * matrix[5] + oldMatrix[2] * matrix[9] + oldMatrix[3] * matrix[13]
49 newMatrix[2] = oldMatrix[0] * matrix[2] + oldMatrix[1] * matrix[6] + oldMatrix[2] * matrix[10] + oldMatrix[3] * matrix[14]
50 newMatrix[3] = oldMatrix[0] * matrix[3] + oldMatrix[1] * matrix[7] + oldMatrix[2] * matrix[11] + oldMatrix[3] * matrix[15]
51 newMatrix[4] = oldMatrix[4] * matrix[0] + oldMatrix[5] * matrix[4] + oldMatrix[6] * matrix[8] + oldMatrix[7] * matrix[12]
52 newMatrix[5] = oldMatrix[4] * matrix[1] + oldMatrix[5] * matrix[5] + oldMatrix[6] * matrix[9] + oldMatrix[7] * matrix[13]
53 newMatrix[6] = oldMatrix[4] * matrix[2] + oldMatrix[5] * matrix[6] + oldMatrix[6] * matrix[10] + oldMatrix[7] * matrix[14]
54 newMatrix[7] = oldMatrix[4] * matrix[3] + oldMatrix[5] * matrix[7] + oldMatrix[6] * matrix[11] + oldMatrix[7] * matrix[15]
55 newMatrix[8] = oldMatrix[8] * matrix[0] + oldMatrix[9] * matrix[4] + oldMatrix[10] * matrix[8] + oldMatrix[11] * matrix[12]
56 newMatrix[9] = oldMatrix[8] * matrix[1] + oldMatrix[9] * matrix[5] + oldMatrix[10] * matrix[9] + oldMatrix[11] * matrix[13]
57 newMatrix[10] = oldMatrix[8] * matrix[2] + oldMatrix[9] * matrix[6] + oldMatrix[10] * matrix[10] + oldMatrix[11] * matrix[14]
58 newMatrix[11] = oldMatrix[8] * matrix[3] + oldMatrix[9] * matrix[7] + oldMatrix[10] * matrix[11] + oldMatrix[11] * matrix[15]
59 newMatrix[12] = oldMatrix[12] * matrix[0] + oldMatrix[13] * matrix[4] + oldMatrix[14] * matrix[8] + oldMatrix[15] * matrix[12]
60 newMatrix[13] = oldMatrix[12] * matrix[1] + oldMatrix[13] * matrix[5] + oldMatrix[14] * matrix[9] + oldMatrix[15] * matrix[13]
61 newMatrix[14] = oldMatrix[12] * matrix[2] + oldMatrix[13] * matrix[6] + oldMatrix[14] * matrix[10] + oldMatrix[15] * matrix[14]
62 newMatrix[15] = oldMatrix[12] * matrix[3] + oldMatrix[13] * matrix[7] + oldMatrix[14] * matrix[11] + oldMatrix[15] * matrix[15]
65 for n in node['node']:
66 self._ProcessNode2(n, matrix)
67 if 'instance_geometry' in node:
68 for instance_geometry in node['instance_geometry']:
69 mesh = self._idMap[instance_geometry['_url']]['mesh'][0]
71 if 'triangles' in mesh:
72 for triangles in mesh['triangles']:
73 for input in triangles['input']:
74 if input['_semantic'] == 'VERTEX':
75 vertices = self._idMap[input['_source']]
76 for input in vertices['input']:
77 if input['_semantic'] == 'POSITION':
78 vertices = self._idMap[input['_source']]
79 indexList = map(int, triangles['p'][0]['__data'].split())
80 positionList = map(float, vertices['float_array'][0]['__data'].split())
82 startIndex = len(self.vertexes)
83 for idx in xrange(0, len(positionList)/3):
84 x = positionList[idx*3]
85 y = positionList[idx*3+1]
86 z = positionList[idx*3+2]
88 self.vertexes.append(Vector3(x * matrix[0] + y * matrix[1] + z * matrix[2] + matrix[3], x * matrix[4] + y * matrix[5] + z * matrix[6] + matrix[7], x * matrix[8] + y * matrix[9] + z * matrix[10] + matrix[11]))
90 self.vertexes.append(Vector3(x, y, z))
91 stepSize = len(indexList) / (int(triangles['_count']) * 3)
92 for i in xrange(0, int(triangles['_count'])):
93 idx = i * stepSize * 3
95 f.index = len(self.faces)
96 f.vertexIndexes.append(startIndex + indexList[idx])
97 f.vertexIndexes.append(startIndex + indexList[idx+stepSize])
98 f.vertexIndexes.append(startIndex + indexList[idx+stepSize*2])
101 if 'instance_node' in node:
102 for instance_node in node['instance_node']:
103 self._ProcessNode2(self._idMap[instance_node['_url']], matrix)
105 def _StartElementHandler(self, name, attributes):
107 if not name in self._cur:
109 new = {'__name': name, '__parent': self._cur}
110 self._cur[name].append(new)
112 for k in attributes.keys():
113 self._cur['_' + k] = attributes[k]
115 if 'id' in attributes:
116 self._idMap['#' + attributes['id']] = self._cur
118 def _EndElementHandler(self, name):
119 self._cur = self._cur['__parent']
121 def _CharacterDataHandler(self, data):
122 if len(data.strip()) < 1:
124 if '__data' in self._cur:
125 self._cur['__data'] += data
127 self._cur['__data'] = data
129 def _GetWithKey(self, item, basename, key, value):
132 if item[basename]['_'+key] == value:
133 return self._idMap[item[input]['_source']]