if scale > 0:
self.applyMatrix(numpy.matrix([[scale,0,0],[0,scale,0],[0,0,scale]], numpy.float64))
+ #Split splits an object with multiple meshes into different objects, where each object is a part of the original mesh that has
+ # connected faces. This is useful to split up plate STL files.
def split(self, callback):
ret = []
for oriMesh in self._meshList:
ret += oriMesh.split(callback)
return ret
+ #getVertexIndexList returns an array of vertexes, and an integer array for each mesh in this object.
+ # the integer arrays are indexes into the vertex array for each triangle in the model.
+ def getVertexIndexList(self):
+ vertexMap = {}
+ vertexList = []
+ meshList = []
+ for m in self._meshList:
+ verts = m.getTransformedVertexes(True)
+ meshIdxList = []
+ for idx in xrange(0, len(verts)):
+ v = verts[idx]
+ hashNr = int(v[0] * 100) | int(v[1] * 100) << 10 | int(v[2] * 100) << 20
+ vIdx = None
+ if hashNr in vertexMap:
+ for idx2 in vertexMap[hashNr]:
+ if numpy.linalg.norm(v - vertexList[idx2]) < 0.001:
+ vIdx = idx2
+ if vIdx is None:
+ vIdx = len(vertexList)
+ vertexMap[hashNr] = [vIdx]
+ vertexList.append(v)
+ meshIdxList.append(vIdx)
+ meshList.append(numpy.array(meshIdxList, numpy.int32))
+ return numpy.array(vertexList, numpy.float32), meshList
+
class mesh(object):
def __init__(self, obj):
self.vertexes = None
xml.write(' <object id="%d">\n' % (n))
xml.write(' <mesh>\n')
xml.write(' <vertices>\n')
- for m in obj._meshList:
- for v in m.getTransformedVertexes(True):
- xml.write(' <vertex>\n')
- xml.write(' <coordinates>\n')
- xml.write(' <x>%f</x>\n' % (v[0]))
- xml.write(' <y>%f</y>\n' % (v[1]))
- xml.write(' <z>%f</z>\n' % (v[2]))
- xml.write(' </coordinates>\n')
- xml.write(' </vertex>\n')
+ vertexList, meshList = obj.getVertexIndexList()
+ for v in vertexList:
+ xml.write(' <vertex>\n')
+ xml.write(' <coordinates>\n')
+ xml.write(' <x>%f</x>\n' % (v[0]))
+ xml.write(' <y>%f</y>\n' % (v[1]))
+ xml.write(' <z>%f</z>\n' % (v[2]))
+ xml.write(' </coordinates>\n')
+ xml.write(' </vertex>\n')
xml.write(' </vertices>\n')
- idxOffset = 0
- for m in obj._meshList:
+ for m in meshList:
xml.write(' <volume>\n')
- for idx in xrange(0, len(m.vertexes), 3):
+ for idx in xrange(0, len(m), 3):
xml.write(' <triangle>\n')
- xml.write(' <v1>%i</v1>\n' % (idx + idxOffset))
- xml.write(' <v2>%i</v2>\n' % (idx + idxOffset + 1))
- xml.write(' <v3>%i</v3>\n' % (idx + idxOffset + 2))
+ xml.write(' <v1>%i</v1>\n' % (m[idx]))
+ xml.write(' <v2>%i</v2>\n' % (m[idx+1]))
+ xml.write(' <v3>%i</v3>\n' % (m[idx+2]))
xml.write(' </triangle>\n')
xml.write(' </volume>\n')
- idxOffset += len(m.vertexes)
xml.write(' </mesh>\n')
xml.write(' </object>\n')