Advanced · optional
Polyhedrons
This one’s a bonus, same as Shapes of Revolution – come back to it any
time. It answers a question that comes up the moment you look at
Polyhedron in the Cheatsheet and think “wait, what’s a face, and how
is that different from a point?”
Why Polyhedron
Every shape you’ve used so far is really a shortcut. Box is a
shortcut for “six flat rectangles, arranged into a closed box.” Even
rotate_extrude() is a shortcut for “lots of flat panels, swept around
in a circle.” Underneath, a 3D shape is always just flat faces
meeting at corners – Polyhedron is the tool that builds that
directly, corner by corner, face by face, instead of going through a
shortcut at all.
It’s also not just a classroom exercise. An exported .stl file –
the kind Download STL makes, the kind a 3D printer actually reads – IS
a Polyhedron, whether it started that way or not: a flat list of
corners and the flat faces connecting them. By the end of this lesson
you’ll know exactly what’s inside one.
Points Are Just Corners
A Polyhedron starts with a list of points – each one just an
(x, y, z) corner in space, same coordinates you already know from
Box. The only new idea is that each point also has a number,
counting from 0, based on where it sits in the list.
Nothing draws yet – a list of points by itself isn’t a shape, any more than four dots on a page are a drawing. That’s the next piece.
Using that same points list, what are the coordinates of point 2?
Faces Are Lists of Point Numbers
A face is a flat panel, and you build one by listing which points
trace its outline – by number, not by coordinates again. Face [0,
1, 2] doesn’t mean “a triangle at the position 0, 1, 2” – it means
“connect point 0, then point 1, then point 2, and fill in the flat
triangle between them.”
One face, one flat triangle – literally a single panel floating in
space, using 3 of the 4 points and completely ignoring point 3. A
Polyhedron can have as many faces as you list, and nothing stops two
faces from sharing the same points – in fact, sharing points along an
edge is exactly how faces fit together into a solid, which is what the
next section builds.
A second face is added: `faces=[(0, 1, 2), (0, 1, 3)]`. What does that second face, `(0, 1, 3)`, do?
Order Around the Face Matters (Even Though You Can’t See It Here)
One more rule about a face’s list: the order you name its points in isn’t arbitrary. Walk them around the outside of the face, in one consistent direction – that’s what tells a face which way is “out” and which way is “in,” exactly the same idea OpenSCAD (and every real CAD tool) uses.
Here’s the honest part: in Studio’s own preview, you will not be able to see a mistake here. Every shape in this tool renders from both sides, and this viewer is specifically built to shade a backwards face as if it were facing you correctly – so a face wound the wrong way around looks completely normal on screen:
Run it. One face on the right copy is wound backwards (0, 3, 1
instead of 0, 1, 3) – and it looks completely identical to the
correct one. That’s not a bug you’re supposed to spot; it’s the actual
point. This viewer being forgiving doesn’t mean winding stopped
mattering – it means this one tool happens to hide the mistake from
you. A .stl file made from those same two shapes still records the
wrong order, and not every tool that reads a .stl is this forgiving.
This is exactly why this Studio’s own drag-and-drop STL importer can sometimes fail to patch a small hole automatically (you’ll meet that feature in the next section): it walks a hole’s boundary by following each edge’s wound direction to figure out how to close it back up, and inconsistent winding is one of the things that can make that automatic repair give up.
A face is wound backwards from its neighbors. What actually happens?
Building a Tetrahedron, All Together
Put it all together: the same 4 points from the start of this lesson, wound consistently, make a complete, solid tetrahedron – the simplest possible solid made of flat faces, 4 points and 4 triangles, nothing left open:
Every one of those 4 faces reuses points from the other faces – that’s not a coincidence, it’s the whole idea. A closed solid is exactly a set of faces where every edge is shared by precisely two faces, one on each side. Try deleting one of the four faces from the list above and running it again: the tetrahedron springs a hole exactly where that face used to be.
From an STL, Automatically
This is also exactly what’s happening when you drag a plain .stl file
– one that doesn’t have this Studio’s own code saved inside it – onto
the viewer. Every corner recorded in that file becomes a point, every
triangle becomes a face, and you get back a real, editable Polyhedron
call built from whatever was in the file. It won’t be neat, hand-written
code like the tetrahedron above (a real model can have hundreds of
points), but it’s built from exactly the same two ideas this lesson just
covered.
If the file has a small gap in its surface – one missing face, say – Studio tries to patch it automatically before handing you the code, by walking the edges around the hole and filling it back in. That’s the “automatic repair” the previous section mentioned: it works by following each edge’s wound direction, so a file with genuinely inconsistent winding is one of the ways that repair can fail and ask you to fix the file another way instead.
Checking Your Work
Exercise: build a square pyramid with Polyhedron
-- 5 points (4 base corners, plus 1 apex above the middle) and at
least 5 faces: a base, plus 4 triangle sides. The base can be one
4-point face, or 2 triangles if you'd rather keep every face a
triangle -- both are real polyhedrons.
Practice
Polyhedron(points, faces, fill=, opacity=100, hole=False): points are
numbered (x, y, z) corners; faces are lists of point numbers, wound
consistently around each face’s outside. It’s the same building block a
.stl file itself is made of.