Exemple #1
0
    def __init__(self, filename):
        self.mesh = Mesh()
        self.mesh.metaData['filename'] = filename
        self.volume = self.mesh.newVolume()

        r = ParserCreate()
        r.StartElementHandler = self._StartElementHandler
        r.EndElementHandler = self._EndElementHandler
        r.CharacterDataHandler = self._CharacterDataHandler

        self._base = {}
        self._cur = self._base
        self._idMap = {}
        self._geometryList = []
        self._faceCount = 0
        r.ParseFile(open(filename, "r"))
        
        self.vertexCount = 0
        for instance_visual_scene in self._base['collada'][0]['scene'][0]['instance_visual_scene']:
            for node in self._idMap[instance_visual_scene['_url']]['node']:
                self._ProcessNode1(node)
        self.volume._prepareFaceCount(self._faceCount)
        for instance_visual_scene in self._base['collada'][0]['scene'][0]['instance_visual_scene']:
            for node in self._idMap[instance_visual_scene['_url']]['node']:
                self._ProcessNode2(node)

        scale = float(self._base['collada'][0]['asset'][0]['unit'][0]['_meter']) * 1000
        self.volume.vertexData *= scale
        
        self._base = None
        self._cur = None
        self._idMap = None
        
        self.volume.calculateNormals()
Exemple #2
0
def loadMeshes(filename):
    mesh = Mesh()
    mesh.metaData['filename'] = filename
    volume = mesh.newVolume()

    f = open(filename, "rb")
    if f.read(5).lower() == "solid":
        _loadAscii(volume, f)
        if volume.vertexCount < 3:
            f.seek(5, os.SEEK_SET)
            _loadBinary(volume, f)
    else:
        _loadBinary(volume, f)
    f.close()
    volume.calculateNormals()
    return [mesh]
Exemple #3
0
def loadMeshes(filename):
    mesh = Mesh()
    mesh.metaData["filename"] = filename
    volume = mesh.newVolume()

    vertexList = []
    normalList = []
    faceList = []

    f = open(filename, "r")
    for line in f:
        parts = line.split()
        if len(parts) < 1:
            continue
        if parts[0] == "v":
            vertexList.append([float(parts[1]), -float(parts[3]), float(parts[2])])
        if parts[0] == "vn":
            normalList.append([float(parts[1]), -float(parts[3]), float(parts[2])])
        if parts[0] == "f":
            parts = map(lambda p: p.split("/"), parts)
            for idx in xrange(1, len(parts) - 2):
                data = [int(parts[1][0]), int(parts[idx + 1][0]), int(parts[idx + 2][0])]
                if len(parts[1]) > 2:
                    data += [int(parts[1][2]), int(parts[idx + 1][2]), int(parts[idx + 2][2])]
                faceList.append(data)
    f.close()

    volume._prepareFaceCount(len(faceList))
    for f in faceList:
        i = f[0] - 1
        j = f[1] - 1
        k = f[2] - 1
        if i < 0 or i >= len(vertexList):
            i = 0
        if j < 0 or j >= len(vertexList):
            j = 0
        if k < 0 or k >= len(vertexList):
            k = 0
        volume._addFace(
            vertexList[i][0],
            vertexList[i][1],
            vertexList[i][2],
            vertexList[j][0],
            vertexList[j][1],
            vertexList[j][2],
            vertexList[k][0],
            vertexList[k][1],
            vertexList[k][2],
        )
    volume.calculateNormals()

    if len(normalList) > 0:
        n = 0
        for f in faceList:
            if len(f) > 3:
                i = f[3] - 1
                j = f[4] - 1
                k = f[5] - 1
                if i < 0 or i >= len(normalList):
                    i = 0
                if j < 0 or j >= len(normalList):
                    j = 0
                if k < 0 or k >= len(normalList):
                    k = 0
                volume.vertexData[n][3] = normalList[i][0]
                volume.vertexData[n][4] = normalList[i][1]
                volume.vertexData[n][5] = normalList[i][2]
                volume.vertexData[n + 1][3] = normalList[j][0]
                volume.vertexData[n + 1][4] = normalList[j][1]
                volume.vertexData[n + 1][5] = normalList[j][2]
                volume.vertexData[n + 2][3] = normalList[k][0]
                volume.vertexData[n + 2][4] = normalList[k][1]
                volume.vertexData[n + 2][5] = normalList[k][2]
            n += 3
    return [mesh]
Exemple #4
0
class daeLoader(object):
    """
    COLLADA object loader. This class is a bit of a mess, COLLADA files are complex beasts, and this code has only been tweaked to accept
    the COLLADA files exported from SketchUp.

    Parts of this class can be cleaned up and improved by using more numpy.
    """
    def __init__(self, filename):
        self.mesh = Mesh()
        self.mesh.metaData['filename'] = filename
        self.volume = self.mesh.newVolume()

        r = ParserCreate()
        r.StartElementHandler = self._StartElementHandler
        r.EndElementHandler = self._EndElementHandler
        r.CharacterDataHandler = self._CharacterDataHandler

        self._base = {}
        self._cur = self._base
        self._idMap = {}
        self._geometryList = []
        self._faceCount = 0
        r.ParseFile(open(filename, "r"))
        
        self.vertexCount = 0
        for instance_visual_scene in self._base['collada'][0]['scene'][0]['instance_visual_scene']:
            for node in self._idMap[instance_visual_scene['_url']]['node']:
                self._ProcessNode1(node)
        self.volume._prepareFaceCount(self._faceCount)
        for instance_visual_scene in self._base['collada'][0]['scene'][0]['instance_visual_scene']:
            for node in self._idMap[instance_visual_scene['_url']]['node']:
                self._ProcessNode2(node)

        scale = float(self._base['collada'][0]['asset'][0]['unit'][0]['_meter']) * 1000
        self.volume.vertexData *= scale
        
        self._base = None
        self._cur = None
        self._idMap = None
        
        self.volume.calculateNormals()

    def _ProcessNode1(self, node):
        if 'node' in node:
            for n in node['node']:
                self._ProcessNode1(n)
        if 'instance_geometry' in node:
            for instance_geometry in node['instance_geometry']:
                mesh = self._idMap[instance_geometry['_url']]['mesh'][0]
                if 'triangles' in mesh:
                    for triangles in mesh['triangles']:
                        self._faceCount += int(triangles['_count'])
                elif 'lines' in mesh:
                    pass #Ignore lines
                else:
                    print mesh.keys()
        if 'instance_node' in node:
            for instance_node in node['instance_node']:
                self._ProcessNode1(self._idMap[instance_node['_url']])

    def _ProcessNode2(self, node, matrix = None):
        if 'matrix' in node:
            oldMatrix = matrix
            matrix = map(float, node['matrix'][0]['__data'].split())
            if oldMatrix is not None:
                newMatrix = [0]*16
                newMatrix[0] = oldMatrix[0] * matrix[0] + oldMatrix[1] * matrix[4] + oldMatrix[2] * matrix[8] + oldMatrix[3] * matrix[12]
                newMatrix[1] = oldMatrix[0] * matrix[1] + oldMatrix[1] * matrix[5] + oldMatrix[2] * matrix[9] + oldMatrix[3] * matrix[13]
                newMatrix[2] = oldMatrix[0] * matrix[2] + oldMatrix[1] * matrix[6] + oldMatrix[2] * matrix[10] + oldMatrix[3] * matrix[14]
                newMatrix[3] = oldMatrix[0] * matrix[3] + oldMatrix[1] * matrix[7] + oldMatrix[2] * matrix[11] + oldMatrix[3] * matrix[15]
                newMatrix[4] = oldMatrix[4] * matrix[0] + oldMatrix[5] * matrix[4] + oldMatrix[6] * matrix[8] + oldMatrix[7] * matrix[12]
                newMatrix[5] = oldMatrix[4] * matrix[1] + oldMatrix[5] * matrix[5] + oldMatrix[6] * matrix[9] + oldMatrix[7] * matrix[13]
                newMatrix[6] = oldMatrix[4] * matrix[2] + oldMatrix[5] * matrix[6] + oldMatrix[6] * matrix[10] + oldMatrix[7] * matrix[14]
                newMatrix[7] = oldMatrix[4] * matrix[3] + oldMatrix[5] * matrix[7] + oldMatrix[6] * matrix[11] + oldMatrix[7] * matrix[15]
                newMatrix[8] = oldMatrix[8] * matrix[0] + oldMatrix[9] * matrix[4] + oldMatrix[10] * matrix[8] + oldMatrix[11] * matrix[12]
                newMatrix[9] = oldMatrix[8] * matrix[1] + oldMatrix[9] * matrix[5] + oldMatrix[10] * matrix[9] + oldMatrix[11] * matrix[13]
                newMatrix[10] = oldMatrix[8] * matrix[2] + oldMatrix[9] * matrix[6] + oldMatrix[10] * matrix[10] + oldMatrix[11] * matrix[14]
                newMatrix[11] = oldMatrix[8] * matrix[3] + oldMatrix[9] * matrix[7] + oldMatrix[10] * matrix[11] + oldMatrix[11] * matrix[15]
                newMatrix[12] = oldMatrix[12] * matrix[0] + oldMatrix[13] * matrix[4] + oldMatrix[14] * matrix[8] + oldMatrix[15] * matrix[12]
                newMatrix[13] = oldMatrix[12] * matrix[1] + oldMatrix[13] * matrix[5] + oldMatrix[14] * matrix[9] + oldMatrix[15] * matrix[13]
                newMatrix[14] = oldMatrix[12] * matrix[2] + oldMatrix[13] * matrix[6] + oldMatrix[14] * matrix[10] + oldMatrix[15] * matrix[14]
                newMatrix[15] = oldMatrix[12] * matrix[3] + oldMatrix[13] * matrix[7] + oldMatrix[14] * matrix[11] + oldMatrix[15] * matrix[15]
                matrix = newMatrix
        if 'node' in node:
            for n in node['node']:
                self._ProcessNode2(n, matrix)
        if 'instance_geometry' in node:
            for instance_geometry in node['instance_geometry']:
                mesh = self._idMap[instance_geometry['_url']]['mesh'][0]
                
                if 'triangles' in mesh:
                    for triangles in mesh['triangles']:
                        for input in triangles['input']:
                            if input['_semantic'] == 'VERTEX':
                                vertices = self._idMap[input['_source']]
                        for input in vertices['input']:
                            if input['_semantic'] == 'POSITION':
                                vertices = self._idMap[input['_source']]
                        indexList = map(int, triangles['p'][0]['__data'].split())
                        positionList = map(float, vertices['float_array'][0]['__data'].split())

                        faceCount = int(triangles['_count'])
                        stepSize = len(indexList) / (faceCount * 3)
                        for i in xrange(0, faceCount):
                            idx0 = indexList[((i * 3) + 0) * stepSize]
                            idx1 = indexList[((i * 3) + 1) * stepSize]
                            idx2 = indexList[((i * 3) + 2) * stepSize]
                            x0 = positionList[idx0*3]
                            y0 = positionList[idx0*3+1]
                            z0 = positionList[idx0*3+2]
                            x1 = positionList[idx1*3]
                            y1 = positionList[idx1*3+1]
                            z1 = positionList[idx1*3+2]
                            x2 = positionList[idx2*3]
                            y2 = positionList[idx2*3+1]
                            z2 = positionList[idx2*3+2]
                            if matrix is not None:
                                self.volume._addFace(
                                    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],
                                    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],
                                    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]
                                )
                            else:
                                self.volume._addFace(x0, y0, z0, x1, y1, z1, x2, y2, z2)
        if 'instance_node' in node:
            for instance_node in node['instance_node']:
                self._ProcessNode2(self._idMap[instance_node['_url']], matrix)
    
    def _StartElementHandler(self, name, attributes):
        name = name.lower()
        if not name in self._cur:
            self._cur[name] = []
        new = {'__name': name, '__parent': self._cur}
        self._cur[name].append(new)
        self._cur = new
        for k in attributes.keys():
            self._cur['_' + k] = attributes[k]
        
        if 'id' in attributes:
            self._idMap['#' + attributes['id']] = self._cur
        
    def _EndElementHandler(self, name):
        self._cur = self._cur['__parent']

    def _CharacterDataHandler(self, data):
        if len(data.strip()) < 1:
            return
        if '__data' in self._cur:
            self._cur['__data'] += data
        else:
            self._cur['__data'] = data
    
    def _GetWithKey(self, item, basename, key, value):
        input = basename
        while input in item:
            if item[basename]['_'+key] == value:
                return self._idMap[item[input]['_source']]
            basename += "!"
Exemple #5
0
def loadMeshes(filename):
    mesh = Mesh()
    mesh.metaData['filename'] = filename
    volume = mesh.newVolume()

    vertexList = []
    normalList = []
    faceList = []

    f = open(filename, "r")
    for line in f:
        parts = line.split()
        if len(parts) < 1:
            continue
        if parts[0] == 'v':
            vertexList.append(
                [float(parts[1]), -float(parts[3]),
                 float(parts[2])])
        if parts[0] == 'vn':
            normalList.append(
                [float(parts[1]), -float(parts[3]),
                 float(parts[2])])
        if parts[0] == 'f':
            parts = map(lambda p: p.split('/'), parts)
            for idx in xrange(1, len(parts) - 2):
                data = [
                    int(parts[1][0]),
                    int(parts[idx + 1][0]),
                    int(parts[idx + 2][0])
                ]
                if len(parts[1]) > 2:
                    data += [
                        int(parts[1][2]),
                        int(parts[idx + 1][2]),
                        int(parts[idx + 2][2])
                    ]
                faceList.append(data)
    f.close()

    volume._prepareFaceCount(len(faceList))
    for f in faceList:
        i = f[0] - 1
        j = f[1] - 1
        k = f[2] - 1
        if i < 0 or i >= len(vertexList):
            i = 0
        if j < 0 or j >= len(vertexList):
            j = 0
        if k < 0 or k >= len(vertexList):
            k = 0
        volume._addFace(vertexList[i][0], vertexList[i][1], vertexList[i][2],
                        vertexList[j][0], vertexList[j][1], vertexList[j][2],
                        vertexList[k][0], vertexList[k][1], vertexList[k][2])
    volume.calculateNormals()

    if len(normalList) > 0:
        n = 0
        for f in faceList:
            if len(f) > 3:
                i = f[3] - 1
                j = f[4] - 1
                k = f[5] - 1
                if i < 0 or i >= len(normalList):
                    i = 0
                if j < 0 or j >= len(normalList):
                    j = 0
                if k < 0 or k >= len(normalList):
                    k = 0
                volume.vertexData[n][3] = normalList[i][0]
                volume.vertexData[n][4] = normalList[i][1]
                volume.vertexData[n][5] = normalList[i][2]
                volume.vertexData[n + 1][3] = normalList[j][0]
                volume.vertexData[n + 1][4] = normalList[j][1]
                volume.vertexData[n + 1][5] = normalList[j][2]
                volume.vertexData[n + 2][3] = normalList[k][0]
                volume.vertexData[n + 2][4] = normalList[k][1]
                volume.vertexData[n + 2][5] = normalList[k][2]
            n += 3
    return [mesh]
Exemple #6
0
def loadMeshes(filename):
    try:
        zfile = zipfile.ZipFile(filename)
        xml = zfile.read(zfile.namelist()[0])
        zfile.close()
    except zipfile.BadZipfile:
        f = open(filename, "r")
        xml = f.read()
        f.close()
    amf = ElementTree.fromstring(xml)
    if 'unit' in amf.attrib:
        unit = amf.attrib['unit'].lower()
    else:
        unit = 'millimeter'
    if unit == 'millimeter':
        scale = 1.0
    elif unit == 'meter':
        scale = 1000.0
    elif unit == 'inch':
        scale = 25.4
    elif unit == 'feet':
        scale = 304.8
    elif unit == 'micron':
        scale = 0.001
    else:
        print "Unknown unit in amf: %s" % (unit)
        scale = 1.0

    ret = []
    for amfObj in amf.iter('object'):
        mesh = Mesh()
        mesh.metaData['filename'] = filename
        for amfMesh in amfObj.iter('mesh'):
            vertexList = []
            for vertices in amfMesh.iter('vertices'):
                for vertex in vertices.iter('vertex'):
                    for coordinates in vertex.iter('coordinates'):
                        v = [0.0, 0.0, 0.0]
                        for t in coordinates:
                            if t.tag == 'x':
                                v[0] = float(t.text)
                            elif t.tag == 'y':
                                v[1] = float(t.text)
                            elif t.tag == 'z':
                                v[2] = float(t.text)
                        vertexList.append(v)

            for volume in amfMesh.iter('volume'):
                v = mesh.newVolume()
                count = 0
                for triangle in volume.iter('triangle'):
                    count += 1
                v._prepareFaceCount(count)

                for triangle in volume.iter('triangle'):
                    for t in triangle:
                        if t.tag == 'v1':
                            v1 = vertexList[int(t.text)]
                        elif t.tag == 'v2':
                            v2 = vertexList[int(t.text)]
                        elif t.tag == 'v3':
                            v3 = vertexList[int(t.text)]
                            v._addFace(v1[0], v1[1], v1[2], v2[0], v2[1],
                                       v2[2], v3[0], v3[1], v3[2])
                v.calculateNormals()
        ret.append(mesh)

    return ret
Exemple #7
0
def loadMeshes(filename):
    try:
        zfile = zipfile.ZipFile(filename)
        xml = zfile.read(zfile.namelist()[0])
        zfile.close()
    except zipfile.BadZipfile:
        f = open(filename, "r")
        xml = f.read()
        f.close()
    amf = ElementTree.fromstring(xml)
    if 'unit' in amf.attrib:
        unit = amf.attrib['unit'].lower()
    else:
        unit = 'millimeter'
    if unit == 'millimeter':
        scale = 1.0
    elif unit == 'meter':
        scale = 1000.0
    elif unit == 'inch':
        scale = 25.4
    elif unit == 'feet':
        scale = 304.8
    elif unit == 'micron':
        scale = 0.001
    else:
        print "Unknown unit in amf: %s" % (unit)
        scale = 1.0

    ret = []
    for amfObj in amf.iter('object'):
        mesh = Mesh()
        mesh.metaData['filename'] = filename
        for amfMesh in amfObj.iter('mesh'):
            vertexList = []
            for vertices in amfMesh.iter('vertices'):
                for vertex in vertices.iter('vertex'):
                    for coordinates in vertex.iter('coordinates'):
                        v = [0.0,0.0,0.0]
                        for t in coordinates:
                            if t.tag == 'x':
                                v[0] = float(t.text)
                            elif t.tag == 'y':
                                v[1] = float(t.text)
                            elif t.tag == 'z':
                                v[2] = float(t.text)
                        vertexList.append(v)

            for volume in amfMesh.iter('volume'):
                v = mesh.newVolume()
                count = 0
                for triangle in volume.iter('triangle'):
                    count += 1
                v._prepareFaceCount(count)

                for triangle in volume.iter('triangle'):
                    for t in triangle:
                        if t.tag == 'v1':
                            v1 = vertexList[int(t.text)]
                        elif t.tag == 'v2':
                            v2 = vertexList[int(t.text)]
                        elif t.tag == 'v3':
                            v3 = vertexList[int(t.text)]
                            v._addFace(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2])
                v.calculateNormals()

                material_id = int(volume.get('materialid', 1))
                if material_id > 1:
                    v.metaData['setting_extruder_nr'] = material_id - 1
        ret.append(mesh)

    return ret