예제 #1
0
def offset_and_rotation_to_matrix(offset, rotation):
    """ Return 4x4 transform given offset and (3x3 or quaternion) rotation."""
    if type(rotation).__name__ == 'quaternion':
        rotation = rotation.toMatrix()
    return rotation.resize4x4() * Mathutils.TranslationMatrix(offset)
예제 #2
0
def write_mesh(file, scn, exp_list, matTable, total):
    print "Write Geometric"

    for current_container in exp_list:

        TransTable = {'SizeX': 1, 'SizeY': 1, 'SizeZ': 1}
        nameMe = {'objName': 'obj', 'meName': 'me'}
        sGroups = {}
        hasTable = {
            'hasMat': 0,
            'hasSG': 0,
            'hasUV': 0,
            'hasVC': 0,
            'matRef': 0
        }
        count = {'face': 0, 'vert': 0, 'UVs': 0, 'cVert': 0}

        obj = current_container[0]
        #mat_ref = current_container[1]
        data = obj.getData(0, 1)
        nameMe['objName'] = obj.name
        nameMe['meName'] = data.name

        mats_me = [
            mat for mat in data.materials if mat
        ]  #fix for 2.44, get rid of NoneType Objects in me.materials
        mats_ob = obj.getMaterials(0)
        materials = False

        if mats_me:
            materials = mats_me
        elif mats_ob:
            materials = mats_ob

        if guiTable['MTL'] and materials:
            hasTable['hasMat'] = 1
            hasTable['matRef'] = current_container[1]

        if obj.getParent():
            nameMe['parent'] = obj.getParent().name

        me = Mesh.New()  # Create a new mesh

        if guiTable['MOD']:  # Use modified mesh
            me.getFromObject(
                obj.name, 0)  # Get the object's mesh data, cage 0 = apply mod
        else:
            me.getFromObject(obj.name, 1)

        me.transform(obj.matrix)  # ASE stores transformed mesh data
        if guiTable['RECENTER']:  # Recentre Objects to 0,0,0 feature
            rec_matrix = Mathutils.TranslationMatrix(
                obj.matrix.translationPart().negate())
            me.transform(rec_matrix)

        tempObj = Blender.Object.New('Mesh', 'ASE_export_temp_obj')
        tempObj.setMatrix(obj.matrix)
        tempObj.link(me)

        if guiTable['VG2SG']:
            VGNames = data.getVertGroupNames()
            for vg in VGNames:
                me.addVertGroup(vg)
                gverts = data.getVertsFromGroup(vg, 1)
                gverts_copy = []
                for gv in gverts:
                    gverts_copy.append(gv[0])
                me.assignVertsToGroup(vg, gverts_copy, 1, 1)

        obj = tempObj
        faces = me.faces
        verts = me.verts

        count['vert'] = len(verts)
        total['Verts'] += count['vert']

        if count['vert'] == 0:
            print 'Error: ' + nameMe['meName'] + 'has 0 Verts'
            continue

        vGroups = me.getVertGroupNames()
        if guiTable['VG2SG'] and len(vGroups) > 0:
            for current_VG in vGroups:
                if current_VG.lower().count("smooth."):
                    hasTable['hasSG'] = 1
                    smooth_num = int(current_VG.lower().replace("smooth.", ""))
                    gverts = me.getVertsFromGroup(current_VG)
                    for vi in gverts:
                        if not sGroups.has_key(vi):
                            sGroups[vi] = [smooth_num]
                        else:
                            sGroups[vi].append(smooth_num)

        if guiTable['UV']:
            if me.faceUV == True or me.faceUV == 1:
                hasTable['hasUV'] = 1

        if guiTable['VC']:
            if me.vertexColors:
                hasTable['hasVC'] = 1
            elif hasTable['hasMat']:  # Blender material
                for current_mat in materials:
                    if current_mat.getMode() & Material.Modes['VCOL_PAINT']:
                        hasTable['hasVC'] = 1
                        break

        for current_face in faces:
            if len(current_face.verts) is 3:
                count['face'] += 1
                total['Tris'] += 1
                total['Faces'] += 1
            elif len(current_face.verts) is 4:
                count['face'] += 2
                total['Tris'] += 2
                total['Faces'] += 1

        #Open Geomobject
        file.write("*GEOMOBJECT {\n")
        file.write("%s*NODE_NAME \"%s\"\n" % (Tab, nameMe['objName']))

        if nameMe.has_key('parent'):
            file.write("%s*NODE_PARENT \"%s\"\n" % (Tab, nameMe['parent']))

        idnt = 1
        mesh_matrix(file, idnt, obj, nameMe, TransTable)

        #Open Mesh
        file.write("%s*MESH {\n" % (Tab))

        idnt = 2
        file.write("%s*TIMEVALUE 0\n" % (Tab * idnt))
        file.write("%s*MESH_NUMVERTEX %i\n" % ((Tab * idnt), count['vert']))
        file.write("%s*MESH_NUMFACES %i\n" % ((Tab * idnt), count['face']))

        idnt = 2
        mesh_vertexList(file, idnt, verts, count)
        idnt = 2
        mesh_faceList(file, idnt, me, materials, sGroups, faces, matTable,
                      hasTable, count)

        if hasTable['hasUV'] == 1:
            UVTable = {}

            active_map_channel = me.activeUVLayer
            map_channels = me.getUVLayerNames()

            idnt = 2
            mesh_tVertList(file, idnt, faces, UVTable, count)
            #idnt = 2
            mesh_tFaceList(file, idnt, faces, UVTable, count)
            UVTable = {}

            if len(map_channels) > 1:
                chan_index = 2
                for map_chan in map_channels:
                    if map_chan != active_map_channel:
                        me.activeUVLayer = map_chan

                        idnt = 2
                        file.write("%s*MESH_MAPPINGCHANNEL %i {\n" %
                                   ((Tab * idnt), chan_index))
                        idnt = 3
                        mesh_tVertList(file, idnt, faces, UVTable, count)
                        mesh_tFaceList(file, idnt, faces, UVTable, count)
                        UVTable = {}
                        chan_index += 1
                        idnt = 2
                        file.write("%s}\n" % (Tab * idnt))

            me.activeUVLayer = active_map_channel

        else:
            # dirty fix
            file.write("%s*MESH_NUMTVERTEX %i\n" %
                       ((Tab * idnt), count['UVs']))

        if hasTable['hasVC'] == 1:
            cVertTable = {}

            idnt = 2
            mesh_cVertList(file, idnt, faces, cVertTable, count)
            #idnt = 2
            mesh_cFaceList(file, idnt, faces, cVertTable, count)
        else:
            # dirty fix
            file.write("%s*MESH_NUMCVERTEX %i\n" %
                       ((Tab * idnt), count['cVert']))

        idnt = 2
        mesh_normals(file, idnt, faces, verts, count)

        # Close *MESH
        idnt = 1
        file.write("%s}\n" % (Tab * idnt))

        idnt = 1
        mesh_footer(file, idnt, hasTable)

        # Close *GEOMOBJECT
        file.write("}\n")

        #free some memory
        me.materials = [None]
        me.faces.delete(1, [(f.index) for f in me.faces])
        me.verts.delete(me.verts)
        obj.fakeUser = False
        me.fakeUser = False
        scn.objects.unlink(obj)