示例#1
0
def Import(fileName):
    log.info("Starting ...")

    log.info("Importing MD3 model: %s", fileName)

    pathName = StripGamePath(StripModel(fileName))
    log.info("Shader path name: %s", pathName)

    modelName = StripExtension(StripPath(fileName))
    log.info("Model name: %s", modelName)

    # read the file in
    file = open(fileName, "rb")
    md3 = md3Object()
    md3.Load(file, log)
    md3.Dump(log)
    file.close()

    scene = Scene.getCurrent()

    for k in range(0, md3.numSurfaces):
        surface = md3.surfaces[k]

        # create a new mesh
        mesh = Mesh.New(surface.name)
        #uv = []
        uvList = []

        # make the verts
        for i in range(0, surface.numVerts):
            mesh.verts.extend([surface.verts[i].xyz])

        # make the faces
        for i in range(0, surface.numTriangles):
            mesh.faces.extend(surface.triangles[i].indexes)

        # use faceUV
        mesh.faceUV = True

        # extend ignores redundant faces
        log.info("Redundant faces for %s: %i", surface.name,
                 surface.numTriangles - len(mesh.faces))

        # make the UV list
        for tex_coord in surface.uv:
            u = tex_coord.u
            v = tex_coord.v
            uv = Mathutils.Vector([u, v])
            uvList.append(uv)

        # import uv
        log.info("uv")
        for i in range(0, len(mesh.faces)):
            uvData = []
            uvData.append(uvList[surface.triangles[i].indexes[0]])
            uvData.append(uvList[surface.triangles[i].indexes[1]])
            uvData.append(uvList[surface.triangles[i].indexes[2]])
            mesh.faces[i].uv = uvData
            # set smooth
            mesh.faces[i].smooth = 1

        # add object
        log.info("addobj")
        meshObject = Object.New('Mesh', surface.name)
        meshObject.link(mesh)
        scene.link(meshObject)

        # animate the verts through keyframe animation
        log.info("anim")
        for i in range(0, surface.numFrames):

            # update the vertices
            for j in range(0, surface.numVerts):
                # i*sufrace.numVerts+j=where in the surface vertex list the vert position for this frame is
                #x = surface.verts[(i * surface.numVerts) + j].xyz[0]
                #y = surface.verts[(i * surface.numVerts) + j].xyz[1]
                #z = surface.verts[(i * surface.numVerts) + j].xyz[2]

                # put the vertex in the right spot
                #mesh.verts[j].co[0] = x
                #mesh.verts[j].co[1] = y
                #mesh.verts[j].co[2] = z
                xyz = Mathutils.Vector(surface.verts[(i * surface.numVerts) +
                                                     j].xyz)
                mesh.verts[j].co = xyz

            meshObject.insertShapeKey()

        #select all and remove doubles
        mesh.sel = 1
        mesh.remDoubles(0.0)

        # create materials for surface
        log.info("shade")
        for i in range(0, surface.numShaders):

            # create new material if necessary
            matName = StripExtension(StripPath(surface.shaders[i].name))
            if matName == "":
                matName = "no_texture"

            try:
                mat = Material.Get(matName)
            except:
                log.info("Creating new material: %s", matName)
                mat = Material.New(matName)

                # create new texture
                texture = Texture.New(matName)
                texture.setType('Image')

                # try .tga by default
                imageName = StripExtension(GAMEDIR +
                                           surface.shaders[i].name) + '.tga'
                try:
                    image = Image.Load(imageName)

                    texture.image = image
                except:
                    try:
                        imageName = StripExtension(imageName) + '.png'
                        image = Image.Load(imageName)

                        texture.image = image
                    except:
                        try:
                            imageName = StripExtension(imageName) + '.jpg'
                            image = Image.Load(imageName)

                            texture.image = image
                        except:
                            log.warning("Unable to load image: %s", imageName)

                # texture to material
                mat.setTexture(0, texture, Texture.TexCo.UV, Texture.MapTo.COL)

            # append material to the mesh's list of materials
            mesh.materials += [mat]
            mesh.update()

    for key in Key.Get():
        key.ipo = Ipo.New('Key', "bleh" + "_ipo")
        index = 1
        for curveName in key.ipo.curveConsts:
            # print curveName
            key.ipo.addCurve(curveName)
            key.ipo[curveName].interpolation = IpoCurve.InterpTypes.CONST
            key.ipo[curveName].addBezier((0, 0))
            key.ipo[curveName].addBezier((index, 1))
            key.ipo[curveName].addBezier((index + 1, 0))
            index += 1
    #for key in Key.Get() :
    #	print key.ipo.curveConsts

    log.info("tags")
    # create tags
    for i in range(0, md3.numTags):
        tag = md3.tags[i]
        # this should be an Empty object
        blenderTag = Object.New("Empty", tag.name)
        # set ipo
        ipo = Ipo.New('Object', tag.name + "_ipo")
        locX = ipo.addCurve('LocX')
        locY = ipo.addCurve('LocY')
        locZ = ipo.addCurve('LocZ')
        rotX = ipo.addCurve('RotX')
        rotY = ipo.addCurve('RotY')
        rotZ = ipo.addCurve('RotZ')
        locX.interpolation = IpoCurve.InterpTypes.LINEAR
        locY.interpolation = IpoCurve.InterpTypes.LINEAR
        locZ.interpolation = IpoCurve.InterpTypes.LINEAR
        rotX.interpolation = IpoCurve.InterpTypes.LINEAR
        rotY.interpolation = IpoCurve.InterpTypes.LINEAR
        rotZ.interpolation = IpoCurve.InterpTypes.LINEAR
        #set ipo for tag
        blenderTag.setIpo(ipo)
        scene.link(blenderTag)
        blenderTag.setLocation(tag.origin)

    # FIXME this imports only the baseframe tags
    for i in range(0, md3.numFrames):

        for j in range(0, md3.numTags):
            tag = md3.tags[i * md3.numTags + j]
            #Blender.Set("curframe", i)
            #tagName = tag.name# + '_' + str(i)
            #blenderTag = Object.New("Empty", tagName);
            #tags.append(blenderTag)
            #scene.link(blenderTag)
            #blenderTag = tags[j]
            blenderTag = Object.Get(tag.name)
            ipo = Ipo.Get(tag.name + "_ipo")
            locX = ipo[Ipo.OB_LOCX]
            locY = ipo[Ipo.OB_LOCY]
            locZ = ipo[Ipo.OB_LOCZ]
            rotX = ipo[Ipo.OB_ROTX]
            rotY = ipo[Ipo.OB_ROTY]
            rotZ = ipo[Ipo.OB_ROTZ]

            # Note: Quake3 uses left-hand geometry
            forward = [tag.axis[0], tag.axis[1], tag.axis[2]]
            left = [tag.axis[3], tag.axis[4], tag.axis[5]]
            up = [tag.axis[6], tag.axis[7], tag.axis[8]]

            transform = MatrixSetupTransform(forward, left, up, tag.origin)
            transform2 = Blender.Mathutils.Matrix(transform[0], transform[1],
                                                  transform[2], transform[3])
            rotation = Blender.Mathutils.Matrix(forward, left, up)
            rot_Euler = rotation.toEuler()
            #rot_Euler.unique()
            #blenderTag.setMatrix(transform2)
            #print "org: ", tag.origin
            locX.addBezier((i + 1, tag.origin[0]))
            locY.addBezier((i + 1, tag.origin[1]))
            locZ.addBezier((i + 1, tag.origin[2]))
            rotX.addBezier((i + 1, DEG2RAD(rot_Euler.x)))
            rotY.addBezier((i + 1, DEG2RAD(rot_Euler.y)))
            rotZ.addBezier((i + 1, DEG2RAD(rot_Euler.z)))
            #blenderTag.setLocation(tag.origin)
            #blenderTag.insertKey(i,"relative")

    # locate the Object containing the mesh at the cursor location
    if md3.numSurfaces:
        cursorPos = Blender.Window.GetCursorPos()
        #meshObject.setLocation(float(cursorPos[0]), float(cursorPos[1]), float(cursorPos[2]))
        meshObject.setLocation(float(cursorPos[0]), float(cursorPos[1]),
                               float(cursorPos[2]))
示例#2
0
def Export(fileName):
    if (fileName.find('.md3', -4) <= 0):
        fileName += '.md3'

    pathName = StripGamePath(StripModel(fileName))

    # ask for a new shader path
    text = Blender.Draw.Create(pathName)
    block = [("", text, 0, MAX_QPATH, "Ex: models/players/human_base/")]
    retval = Blender.Draw.PupBlock("Shader Path :", block)
    #changed
    if retval: pathName = text.val

    #log starts here
    log.start = Blender.sys.time()
    log.info("Starting ...")

    log.info("Exporting MD3 format to: %s", fileName)

    log.info("Shader path name: %s", pathName)

    modelName = StripExtension(StripPath(fileName))
    log.info("Model name: %s", modelName)

    md3 = md3Object()
    md3.ident = MD3_IDENT
    md3.version = MD3_VERSION

    tagList = []

    # get the scene
    scene = Blender.Scene.GetCurrent()
    context = scene.getRenderingContext()

    scene.makeCurrent()
    md3.numFrames = Blender.Get("curframe")
    Blender.Set("curframe", 1)

    # create a bunch of blank frames, they'll be filled in by 'ProcessSurface'
    for i in xrange(1, md3.numFrames + 1):
        frame = md3Frame()
        frame.name = "frame_" + str(i)
        md3.frames.append(frame)

    # export all selected objects
    objlist = Blender.Object.GetSelected()

    # process each object for the export
    for obj in objlist:
        # check if it's a mesh object
        if obj.getType() == "Mesh":
            log.info("Processing surface: %s", obj.name)
            if len(md3.surfaces) == MD3_MAX_SURFACES:
                log.warning(
                    "Hit md3 limit (%i) for number of surfaces, skipping ...",
                    MD3_MAX_SURFACES, obj.getName())
            else:
                ProcessSurface(scene, obj, md3, pathName, modelName)
        elif obj.getType(
        ) == "Empty":  # for tags, we just put em in a list so we can process them all together
            if obj.name[0:4] == "tag_":
                log.info("Processing tag: %s", obj.name)
                tagList.append(obj)
                md3.numTags += 1
        else:
            log.info("Skipping object: %s", obj.name)

    # work out the transforms for the tags for each frame of the export
    for i in xrange(1, md3.numFrames + 1):

        # needed to update IPO's value, but probably not the best way for that...
        scene.makeCurrent()
        Blender.Set("curframe", i)
        Blender.Window.Redraw()
        for tag in tagList:
            t = md3Tag()
            matrix = tag.getMatrix('worldspace')
            t.origin[0] = matrix[3][0]
            t.origin[1] = matrix[3][1]
            t.origin[2] = matrix[3][2]

            t.axis[0] = matrix[0][0]
            t.axis[1] = matrix[0][1]
            t.axis[2] = matrix[0][2]

            t.axis[3] = matrix[1][0]
            t.axis[4] = matrix[1][1]
            t.axis[5] = matrix[1][2]

            t.axis[6] = matrix[2][0]
            t.axis[7] = matrix[2][1]
            t.axis[8] = matrix[2][2]
            t.name = tag.name
            #t.Dump(log)
            md3.tags.append(t)

    # export!
    file = open(fileName, "wb")
    md3.Save(file)
    file.close()
    md3.Dump(log)
示例#3
0
def Import(fileName):
    #log starts here
    log.start = Blender.sys.time()
    log.info("Starting ...")

    log.info("Importing MD3 model: %s", fileName)

    pathName = StripGamePath(StripModel(fileName))
    log.info("Shader path name: %s", pathName)

    modelName = StripExtension(StripPath(fileName))
    log.info("Model name: %s", modelName)

    # read the file in
    file = open(fileName, "rb")
    md3 = md3Object()
    md3.Load(file, log)
    md3.Dump(log)
    file.close()

    scene = Blender.Scene.GetCurrent()

    for k in xrange(md3.numSurfaces):

        surface = md3.surfaces[k]

        # create a new mesh
        mesh = Blender.Mesh.New(surface.name)

        # mesh has vertex uv :/
        mesh.vertexUV = True

        # create the verts
        mesh.verts.extend(
            [surface.verts[i].xyz for i in xrange(surface.numVerts)])

        #set vertex normal and uv
        for i in xrange(len(mesh.verts)):
            mesh.verts[i].no = Blender.Mathutils.Vector(
                surface.verts[i].normal)
            mesh.verts[i].uvco = Blender.Mathutils.Vector(
                surface.uv[i].u, surface.uv[i].v)

        # create the faces
        mesh.faces.extend([
            surface.triangles[i].indexes for i in xrange(surface.numTriangles)
        ])

        # vertex uv to face uv
        mesh.faceUV = True
        for f in mesh.faces:
            f.uv = [v.uvco for v in f.verts]
            f.smooth = 1

        # create materials for surface
        for i in xrange(surface.numShaders):

            # create new material if necessary
            matName = StripExtension(StripPath(surface.shaders[i].name))
            if matName == "":
                matName = surface.name + "_mat"

            try:
                mat = Blender.Material.Get(matName)
            except NameError:
                log.info("Creating new material: %s", matName)
                mat = Blender.Material.New(matName)
                # create new texture
                texture = Blender.Texture.New(matName)
                texture.setType('Image')
                # try .tga by default
                #imageName = StripExtension(GAMEDIR + surface.shaders[i].name) + '.tga'
                imageName = StripExtension(pathName + matName)
                #print imageName
                try:
                    image = Blender.Image.Load(imageName)
                    texture.image = image
                except IOError:
                    try:
                        imageName = StripExtension(imageName) + '.png'
                        image = Blender.Image.Load(imageName)
                        texture.image = image
                    except IOError:
                        try:
                            imageName = StripExtension(imageName) + '.jpg'
                            image = Blender.Image.Load(imageName)
                            texture.image = image
                        except IOError:
                            log.warning("Unable to load image for %s",
                                        surface.name)

                # texture to material
                mat.setTexture(0, texture, Blender.Texture.TexCo.UV,
                               Blender.Texture.MapTo.COL)

            # append material to the mesh's list of materials
            mesh.materials += [mat]
            #mesh.update()

        # add object
        meshObject = Blender.Object.New('Mesh', surface.name)
        meshObject.link(mesh)
        scene.objects.link(meshObject)

        if surface.numFrames > 1:
            # animate the verts through keyframe animation
            for i in xrange(surface.numFrames):

                # update the vertices
                for j in xrange(surface.numVerts):
                    xyz = Blender.Mathutils.Vector(
                        surface.verts[(i * surface.numVerts) + j].xyz)
                    normal = Blender.Mathutils.Vector(
                        surface.verts[(i * surface.numVerts) + j].normal)
                    mesh.verts[j].no = normal
                    mesh.verts[j].co = xyz

                meshObject.insertShapeKey()

            meshKey = mesh.key
            meshKey.ipo = Blender.Ipo.New('Key', surface.name + "_ipo")

            index = 1
            for curveName in meshKey.ipo.curveConsts:
                #print curveName
                meshKey.ipo.addCurve(curveName)
                meshKey.ipo[
                    curveName].interpolation = Blender.IpoCurve.InterpTypes.CONST
                meshKey.ipo[curveName].addBezier((0, 0))
                meshKey.ipo[curveName].addBezier((index, 1))
                meshKey.ipo[curveName].addBezier((index + 1, 0))
                index += 1

        # select all and remove doubles
        #mesh.sel=1
        #mesh.remDoubles(0.0)

    # create tags
    for i in xrange(md3.numTags):
        tag = md3.tags[i]
        # this should be an Empty object
        blenderTag = Blender.Object.New("Empty", tag.name)
        scene.objects.link(blenderTag)
        blenderTag.setLocation(tag.origin)

        if md3.numFrames > 1:
            # set ipo
            ipo = Blender.Ipo.New('Object', tag.name + "_ipo")
            locX = ipo.addCurve('LocX')
            locY = ipo.addCurve('LocY')
            locZ = ipo.addCurve('LocZ')
            rotX = ipo.addCurve('RotX')
            rotY = ipo.addCurve('RotY')
            rotZ = ipo.addCurve('RotZ')
            locX.interpolation = Blender.IpoCurve.InterpTypes.CONST
            locY.interpolation = Blender.IpoCurve.InterpTypes.CONST
            locZ.interpolation = Blender.IpoCurve.InterpTypes.CONST
            rotX.interpolation = Blender.IpoCurve.InterpTypes.CONST
            rotY.interpolation = Blender.IpoCurve.InterpTypes.CONST
            rotZ.interpolation = Blender.IpoCurve.InterpTypes.CONST
            #set ipo for tag
            blenderTag.setIpo(ipo)

            for j in xrange(md3.numFrames):
                tag = md3.tags[j * md3.numTags + i]

                # Note: Quake3 uses left-hand geometry
                forward = [tag.axis[0], tag.axis[1], tag.axis[2]]
                left = [tag.axis[3], tag.axis[4], tag.axis[5]]
                up = [tag.axis[6], tag.axis[7], tag.axis[8]]

                rotation = Blender.Mathutils.Matrix(forward, left, up)
                rot_Euler = rotation.toEuler()

                locX.addBezier((j + 1, tag.origin[0]))
                locY.addBezier((j + 1, tag.origin[1]))
                locZ.addBezier((j + 1, tag.origin[2]))
                #blender: 100 degrees -> 10 units in IPO -> BLARGH
                rotX.addBezier((j + 1, rot_Euler.x / 10))
                rotY.addBezier((j + 1, rot_Euler.y / 10))
                rotZ.addBezier((j + 1, rot_Euler.z / 10))