Hardware Reference
In-Depth Information
CHALLENGE
Now that you have a 3D scanner and a 3D printer, modify your 3D printer pro-
gram to use a FILENAME constant of tree.csv , then run around in the
Minecraft world and keep running print3D.py . You should be able to print
trees all over the place. Try printing some trees up in the sky and under water to
see what happens. How quickly can you build a whole forest of trees this way?
DIGGING INTO THE CODE
In the scan3D.py program, you used a special character in the write() func-
tion that needs to be explained:
f.write("\n")
The \n character (called “backslash n”) is a special non-printable character that
Python allows you to use inside quotes. All it means is “move to the next line.
The n character is used because it stands for “newline. The backslash character
is used to distinguish this letter n from a normal letter n.
In the scan3D.py program, you designed the file format so that there was a
blank line between each layer of the object data to make it easy for people to read
and edit the CSV data file. f.write("\n") just puts in this blank line for you.
Later in the scan3D.py program, there is some interesting code to do with
building up the line variable. Here are the important parts of that code:
for x in range(SIZEX):
line = ""
for z in range(SIZEZ):
blockid = mc.getBlock(originx+x, originy+y,
originz+z)
if line != "": # line is not empty
line = line + ","
line = line + str(blockid)
The parts that are underlined are part of a very common coding pattern that is
regularly used for building comma-separated values. When the x loop starts,
the line variable is set to an empty string. Every time the z loop generates
another value, it first checks if the line is empty, and if it isn't, it adds a comma.
Finally it adds on the blockid of this block. The if statement is necessary to
prevent a comma appearing at the start of the line before the first number,
which would confuse the print3D.py program when it read it back in.
Search WWH ::




Custom Search