使用blender创建平铺地图

时间:2010-06-02 12:38:35

标签: python scripting map blender blender-2.49

我正在考虑根据在搅拌机中制作的3D模型创建地图图块,

地图在搅拌机中是16 x 16。

我有4种不同的缩放级别,每个图块为100 x 100像素。最缩小级别的整个地图是4 x 4个拼贴,构成400 x 400的图像。

最放大的级别是256 x 256,显然构建了25600 x 25600的图像

我需要的是一个可以从模型中创建切片的blender脚本。

我以前从未用python编写过,所以我一直在努力调整已有的几个脚本。

到目前为止,我已经提出了一个脚本,但它不能很好地工作。我真的很难让瓷砖无缝排列。我不太关心改变相机的高度,因为我总是可以在6400 x 6400图像上创建相同的缩小图块,并将生成的图像分割成正确的图块。

这是我到目前为止所得到的......

#!BPY

"""
Name: 'Export Map Tiles'
Blender: '242'
Group: 'Export'
Tip: 'Export to Map'
"""

import Blender
from Blender import Scene,sys
from Blender.Scene import Render

def init():
        thumbsize = 200
        CameraHeight = 4.4
        YStart = -8
        YMove = 4
        XStart = -8
        XMove = 4
        ZoomLevel = 1
        Path = "/Images/Map/"
        Blender.drawmap = [thumbsize,CameraHeight,YStart,YMove,XStart,XMove,ZoomLevel,Path]


def show_prefs():
        buttonthumbsize = Blender.Draw.Create(Blender.drawmap[0]);
        buttonCameraHeight = Blender.Draw.Create(Blender.drawmap[1])
        buttonYStart = Blender.Draw.Create(Blender.drawmap[2])
        buttonYMove = Blender.Draw.Create(Blender.drawmap[3])
        buttonXStart = Blender.Draw.Create(Blender.drawmap[4])
        buttonXMove = Blender.Draw.Create(Blender.drawmap[5])
        buttonZoomLevel = Blender.Draw.Create(Blender.drawmap[6])
        buttonPath = Blender.Draw.Create(Blender.drawmap[7])

    block = []
    block.append(("Image Size", buttonthumbsize, 0, 500))
    block.append(("Camera Height", buttonCameraHeight, -0, 10))
    block.append(("Y Start", buttonYStart, -10, 10))
    block.append(("Y Move", buttonYMove, 0, 5))
    block.append(("X Start", buttonXStart,-10, 10))
    block.append(("X Move", buttonXMove, 0, 5))
    block.append(("Zoom Level", buttonZoomLevel, 1, 10))
    block.append(("Export Path", buttonPath,0,200,"The Path to save the tiles"))

    retval = Blender.Draw.PupBlock("Draw Map: Preferences" , block)

    if retval:
        Blender.drawmap[0] = buttonthumbsize.val
        Blender.drawmap[1] = buttonCameraHeight.val
        Blender.drawmap[2] = buttonYStart.val
        Blender.drawmap[3] = buttonYMove.val
        Blender.drawmap[4] = buttonXStart.val
        Blender.drawmap[5] = buttonXMove.val
        Blender.drawmap[6] = buttonZoomLevel.val
        Blender.drawmap[7] = buttonPath.val
        Export()

def Export():
    scn = Scene.GetCurrent()
    context = scn.getRenderingContext()

    def cutStr(str): #cut off path leaving name
        c = str.find("\\")
        while c != -1:
            c = c + 1
            str = str[c:]
            c = str.find("\\")

        str = str[:-6]
        return str

    #variables from gui:
    thumbsize,CameraHeight,YStart,YMove,XStart,XMove,ZoomLevel,Path = Blender.drawmap


    XMove = XMove / ZoomLevel
    YMove = YMove / ZoomLevel
    Camera = Scene.GetCurrent().getCurrentCamera()
    Camera.LocZ = CameraHeight / ZoomLevel
    YStart = YStart + (YMove / 2)
    XStart = XStart + (XMove / 2)

    #Point it straight down
    Camera.RotX = 0
    Camera.RotY = 0
    Camera.RotZ = 0
    TileCount = 4**ZoomLevel
    #Because the first thing we do is move the camera, start it off the map
    Camera.LocY = YStart - YMove
    for i in range(0,TileCount):
        Camera.LocY = Camera.LocY + YMove
        Camera.LocX = XStart - XMove
        for j in range(0,TileCount):
            Camera.LocX = Camera.LocX + XMove
            Render.EnableDispWin()
            context.extensions = True
            context.renderPath = Path

            #setting thumbsize
            context.imageSizeX(thumbsize)
            context.imageSizeY(thumbsize)

            #could be put into a gui.
            context.imageType = Render.PNG
            context.enableOversampling(0)

            #render
            context.render()

            #save image
            ZasString = '%s' %(int(ZoomLevel))
            XasString = '%s' %(int(j+1))
            YasString = '%s' %(int((3-i)+1))
            context.saveRenderedImage("Z" + ZasString + "X" + XasString + "Y" + YasString)

            #close the windows

            Render.CloseRenderWindow()

try:
        type(Blender.drawmap)
except:
        #print 'initialize extern variables'
        init()
show_prefs()    

1 个答案:

答案 0 :(得分:0)

这最终相对简单。

我按比例放大模型,使地图上的1个图块在搅拌机中为1格。

将相机设置为正交。

将相机的比例设置为1表示最高变焦,4表示下一个,16表示下一个,依此类推。

更新了起始坐标并相应地移动了值。