1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
4 from xml.parsers.expat import ParserCreate
7 from Cura.util import mesh
9 def loadScene(filename):
10 loader = daeLoader(filename)
13 class daeLoader(object):
14 def __init__(self, filename):
15 self.obj = mesh.printableObject(filename)
16 self.mesh = self.obj._addMesh()
19 r.StartElementHandler = self._StartElementHandler
20 r.EndElementHandler = self._EndElementHandler
21 r.CharacterDataHandler = self._CharacterDataHandler
24 self._cur = self._base
26 self._geometryList = []
28 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._ProcessNode1(node)
34 self.mesh._prepareFaceCount(self._faceCount)
35 for instance_visual_scene in self._base['collada'][0]['scene'][0]['instance_visual_scene']:
36 for node in self._idMap[instance_visual_scene['_url']]['node']:
37 self._ProcessNode2(node)
39 scale = float(self._base['collada'][0]['asset'][0]['unit'][0]['_meter']) * 1000
40 self.mesh.vertexes *= scale
46 self.obj._postProcessAfterLoad()
48 def _ProcessNode1(self, node):
50 for n in node['node']:
52 if 'instance_geometry' in node:
53 for instance_geometry in node['instance_geometry']:
54 mesh = self._idMap[instance_geometry['_url']]['mesh'][0]
55 if 'triangles' in mesh:
56 for triangles in mesh['triangles']:
57 self._faceCount += int(triangles['_count'])
62 if 'instance_node' in node:
63 for instance_node in node['instance_node']:
64 self._ProcessNode1(self._idMap[instance_node['_url']])
66 def _ProcessNode2(self, node, matrix = None):
69 matrix = map(float, node['matrix'][0]['__data'].split())
70 if oldMatrix is not None:
72 newMatrix[0] = oldMatrix[0] * matrix[0] + oldMatrix[1] * matrix[4] + oldMatrix[2] * matrix[8] + oldMatrix[3] * matrix[12]
73 newMatrix[1] = oldMatrix[0] * matrix[1] + oldMatrix[1] * matrix[5] + oldMatrix[2] * matrix[9] + oldMatrix[3] * matrix[13]
74 newMatrix[2] = oldMatrix[0] * matrix[2] + oldMatrix[1] * matrix[6] + oldMatrix[2] * matrix[10] + oldMatrix[3] * matrix[14]
75 newMatrix[3] = oldMatrix[0] * matrix[3] + oldMatrix[1] * matrix[7] + oldMatrix[2] * matrix[11] + oldMatrix[3] * matrix[15]
76 newMatrix[4] = oldMatrix[4] * matrix[0] + oldMatrix[5] * matrix[4] + oldMatrix[6] * matrix[8] + oldMatrix[7] * matrix[12]
77 newMatrix[5] = oldMatrix[4] * matrix[1] + oldMatrix[5] * matrix[5] + oldMatrix[6] * matrix[9] + oldMatrix[7] * matrix[13]
78 newMatrix[6] = oldMatrix[4] * matrix[2] + oldMatrix[5] * matrix[6] + oldMatrix[6] * matrix[10] + oldMatrix[7] * matrix[14]
79 newMatrix[7] = oldMatrix[4] * matrix[3] + oldMatrix[5] * matrix[7] + oldMatrix[6] * matrix[11] + oldMatrix[7] * matrix[15]
80 newMatrix[8] = oldMatrix[8] * matrix[0] + oldMatrix[9] * matrix[4] + oldMatrix[10] * matrix[8] + oldMatrix[11] * matrix[12]
81 newMatrix[9] = oldMatrix[8] * matrix[1] + oldMatrix[9] * matrix[5] + oldMatrix[10] * matrix[9] + oldMatrix[11] * matrix[13]
82 newMatrix[10] = oldMatrix[8] * matrix[2] + oldMatrix[9] * matrix[6] + oldMatrix[10] * matrix[10] + oldMatrix[11] * matrix[14]
83 newMatrix[11] = oldMatrix[8] * matrix[3] + oldMatrix[9] * matrix[7] + oldMatrix[10] * matrix[11] + oldMatrix[11] * matrix[15]
84 newMatrix[12] = oldMatrix[12] * matrix[0] + oldMatrix[13] * matrix[4] + oldMatrix[14] * matrix[8] + oldMatrix[15] * matrix[12]
85 newMatrix[13] = oldMatrix[12] * matrix[1] + oldMatrix[13] * matrix[5] + oldMatrix[14] * matrix[9] + oldMatrix[15] * matrix[13]
86 newMatrix[14] = oldMatrix[12] * matrix[2] + oldMatrix[13] * matrix[6] + oldMatrix[14] * matrix[10] + oldMatrix[15] * matrix[14]
87 newMatrix[15] = oldMatrix[12] * matrix[3] + oldMatrix[13] * matrix[7] + oldMatrix[14] * matrix[11] + oldMatrix[15] * matrix[15]
90 for n in node['node']:
91 self._ProcessNode2(n, matrix)
92 if 'instance_geometry' in node:
93 for instance_geometry in node['instance_geometry']:
94 mesh = self._idMap[instance_geometry['_url']]['mesh'][0]
96 if 'triangles' in mesh:
97 for triangles in mesh['triangles']:
98 for input in triangles['input']:
99 if input['_semantic'] == 'VERTEX':
100 vertices = self._idMap[input['_source']]
101 for input in vertices['input']:
102 if input['_semantic'] == 'POSITION':
103 vertices = self._idMap[input['_source']]
104 indexList = map(int, triangles['p'][0]['__data'].split())
105 positionList = map(float, vertices['float_array'][0]['__data'].split())
107 faceCount = int(triangles['_count'])
108 stepSize = len(indexList) / (faceCount * 3)
109 for i in xrange(0, faceCount):
110 idx0 = indexList[((i * 3) + 0) * stepSize]
111 idx1 = indexList[((i * 3) + 1) * stepSize]
112 idx2 = indexList[((i * 3) + 2) * stepSize]
113 x0 = positionList[idx0*3]
114 y0 = positionList[idx0*3+1]
115 z0 = positionList[idx0*3+2]
116 x1 = positionList[idx1*3]
117 y1 = positionList[idx1*3+1]
118 z1 = positionList[idx1*3+2]
119 x2 = positionList[idx2*3]
120 y2 = positionList[idx2*3+1]
121 z2 = positionList[idx2*3+2]
122 if matrix is not None:
124 x0 * matrix[0] + y0 * matrix[1] + z0 * matrix[2] + matrix[3], x0 * matrix[4] + y0 * matrix[5] + z0 * matrix[6] + matrix[7], x0 * matrix[8] + y0 * matrix[9] + z0 * matrix[10] + matrix[11],
125 x1 * matrix[0] + y1 * matrix[1] + z1 * matrix[2] + matrix[3], x1 * matrix[4] + y1 * matrix[5] + z1 * matrix[6] + matrix[7], x1 * matrix[8] + y1 * matrix[9] + z1 * matrix[10] + matrix[11],
126 x2 * matrix[0] + y2 * matrix[1] + z2 * matrix[2] + matrix[3], x2 * matrix[4] + y2 * matrix[5] + z2 * matrix[6] + matrix[7], x2 * matrix[8] + y2 * matrix[9] + z2 * matrix[10] + matrix[11]
129 self.mesh._addFace(x0, y0, z0, x1, y1, z1, x2, y2, z2)
130 if 'instance_node' in node:
131 for instance_node in node['instance_node']:
132 self._ProcessNode2(self._idMap[instance_node['_url']], matrix)
134 def _StartElementHandler(self, name, attributes):
136 if not name in self._cur:
138 new = {'__name': name, '__parent': self._cur}
139 self._cur[name].append(new)
141 for k in attributes.keys():
142 self._cur['_' + k] = attributes[k]
144 if 'id' in attributes:
145 self._idMap['#' + attributes['id']] = self._cur
147 def _EndElementHandler(self, name):
148 self._cur = self._cur['__parent']
150 def _CharacterDataHandler(self, data):
151 if len(data.strip()) < 1:
153 if '__data' in self._cur:
154 self._cur['__data'] += data
156 self._cur['__data'] = data
158 def _GetWithKey(self, item, basename, key, value):
161 if item[basename]['_'+key] == value:
162 return self._idMap[item[input]['_source']]