Hardware Reference
In-Depth Information
4.
Create a constant that will be the name of your data file. This will be easy to
change later on if you want to read different files with different objects in them:
FILENAME = "object1.csv"
5.
Define a
print3D()
function. You will reuse this function later on in the final
project so make sure you name it correctly:
def print3D(filename, originx, originy, originz):
6.
Just as you did with your maze builder program, open the file in read mode and
read in all the lines into a Python list:
f = open(filename, "r")
lines = f.readlines()
7.
The first item in the list is at index 0. This is the first line of the file that holds the
metadata. Split this line into parts by using the
split()
function. Then store
those three numbers into the variables
sizex
,
sizey
and
sizez
. You have to
use the
int()
function here because when you read lines from a file they come
in as strings, but you will need them to be numbers so you can calculate with
them later:
coords = lines[0].split(",")
sizex = int(coords[0])
sizey = int(coords[1])
sizez = int(coords[2])
8.
Set a
lineidx
variable to remember the index of the line in the
lines[]
list
that you are processing. Because your program will be scanning through the
lines
list reading out data for different layers of the 3D object, you can't use
this as the loop control variable:
lineidx = 1
9.
The first
for
loop scans through each of the vertical layers of the data read in
from the file. The first few lines of the file are for the layer that will be built at the
lowest y layer. You can put a
postToChat
here so that the progress of the print-
ing process is visible inside Minecraft:
for y in range(sizey):
mc.postToChat(str(y))
10.
Skip over the blank line between each layer in the file by adding 1 to the
lineidx
variable. Remember that these blank lines are only in the file so that it is easier for
people to read it, but your program has to skip over them.
lineidx = lineidx + 1