views:

152

answers:

1

I'm looking at creating map tiles based on a 3D model made in blender,

The map is 16 x 16 in blender.

I've got 4 different zoom levels and each tile is 100 x 100 pixels. The entire map at the most zoomed out level is 4 x 4 tiles constructing an image of 400 x 400.

The most zoomed in level is 256 x 256 obviously constructing an image of 25600 x 25600

What I need is a script for blender that can create the tiles from the model.

I've never written in python before so I've been trying to adapt a couple of the scripts which are already there.

So far I've come up with a script, but it doesn't work very well. I'm having real difficulties getting the tiles to line up seamlessly. I'm not too concerned about changing the height of the camera as I can always create the same zoomed out tiles at 6400 x 6400 images and split the resulting images into the correct tiles.

Here is what I've got so far...

#!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()    
A: 

This was relatively simple in the end.

I scaled up the model so that 1 tile on the map was 1 grid in blender.

Set the camera to be orthographic.

Set the scale on the camera to 1 for the highest zoom, 4 for the next one, 16 for the next one and so on.

Updated the start coordinates and move values accordingly.

JamesB