Position and Size
Our First Program
Let’s build a box. Click Run below to run this Python code:
You just ran a Python program that built a real 3D object! Now let’s edit it. In the code above, change some of the numbers and click Run a few times. See how the box changes. If the code stops working, no problem, just click Reset to start over and try again.
The Scene
To understand what’s going on, we first have to talk about the scene. In
this tool, shapes are placed in 3D space instead of drawn on a flat
canvas. We use (x, y, z) coordinates to talk about where something sits:
(0, 0, 0)is the center of the scene, not a corner.- As
xincreases, you head right. - As
yincreases, you head away from you. - As
zincreases, you head up.
If you just came from Lesson 1,
this is a little different: there, y increased as you went down, and
(0, 0) was a corner. Here, up is a real direction, so z takes over that
job, and x/y share the flat ground, centered on zero instead of starting
in a corner.
If you increase a shape's z value, which way does it move?
Building Boxes
Here is the Python code from above:
Box(4, 4, 4)
We use Box to build a rectangular block. In full, Box takes six
values: width, depth, height, x, y, z. The first three are
required, in that order. The last three are optional and default to 0 if
you leave them out. So the code above makes a block 4 wide, 4 deep, 4
tall, centered at x=0, y=0, with its bottom sitting right on the
ground.
That last part is worth slowing down on: Box is centered on x and
y, but it sits on top of z, not centered on it. A box’s bottom is
always at whatever z you give it (0, if you don’t say), and it builds
upward from there.
Question: if you call Box(2, 2, 8, x=1), where does the top of
the box end up? (Hint: which argument is height, and where does height
start counting from?)
Which call makes a box that's 4 wide, 4 deep, 10 tall, sitting on the ground, centered above x=3, y=0?
Checking Your Work
An important part of learning to code is knowing when you’ve actually gotten it right. Below, you’ll see a Check My Work button next to Run. Read the instructions, edit the code, click Run, then click Check My Work to see how you did. If it’s not right yet, keep adjusting and try again.
Exercise: build a box that is 4 wide, 2 deep, 3
tall, centered at x=3 (leave y and
z at their defaults).
Rounding Corners
Every Box so far has had sharp, square corners. You can round them with
fillet=, which takes a size, same units as everything else:
Try changing that 0.8. A small number rounds just the edges a little; a
number close to half the box’s shortest side rounds it almost into a
capsule shape. If you go bigger than that, it gets clamped automatically
– a rounded corner can’t be bigger than the box it’s rounding.
This kind of rounded, curved edge has a real name: a fillet. (A flat, angled cut instead of a curve would be called a chamfer – a different thing.) It’s not just decoration, either: rounded corners and edges are genuinely easier to 3D print cleanly and are less likely to snag or crack than a sharp corner.
What actually happens if you set fillet= to something bigger than half the box's shortest side?
Errors
Python is very picky. Here are some rules to follow:
- Case matters, so
Boxis not the same asbox. - A command must have the right number of values after it. For
Box, those arewidth,depth, andheightat least. These values are called arguments. Arguments must be in the right order, inside parentheses, and separated by commas.
If any of these rules aren’t followed, you have an error, and your code won’t run until you fix it. Try running the broken code below. Read the error message. What does it tell you?
Comments
Did you notice the # signs followed by English text in some of the
code examples in the cheatsheet? Those are called comments. You can add
a comment to any line of your code: anything from the # sign to the
end of that line is a comment, and Python ignores it completely. So why
use them? To make your code easier for you, and for others, to
understand.
Practice
That’s it for the basics! From here, move on to rotating and moving shapes, head to the Studio to build something of your own, or keep the Cheatsheet open while you work.