Lego CAD

Virtual Lego bricks

tech unix hack cyberpunk

Have you ever tried documenting a Lego model you've invented? I've tried to track how I constructed My Own Creation (or "MOC", in the lingo of Internet brick builders) using graph paper, but getting the angles right can be really hard. Lacking freehand illustration skills, I recently decided to use technology to solve the problem.

CAD for Lego

I have several years working in a virtualized 3D space (even longer in actual 3D space, for what that's worth). I'm comfortable with 3D applications, but all of the ones I've used have been specific to motion graphics and film production. They are, like film itself, generally just for show. How you build something is less important than whether the thing looks good. If you have to "cheat" what's physically possible to ensure that something looks cool, that's OK, because the thing you're building only exists in a virtual space.

Computer-Aided Design (CAD) is different.

CAD software replaced old style drafting, in which specifications are created to demonstrate how something may be built, once or 100 times, in the real world. There's an expectation of precision and realism.

Because Lego fans are legion, there is a prolific community of builders creating Lego models using CAD. The advantages are obvious: you can document what pieces you need, and what steps you must take, to build a model. This isn't a replacement for real Lego bricks, unless you love CAD more than you do Lego, but it's a great augmentation for your hobby.

To build a virtual Lego model, you need two components:

  1. virtual Lego bricks
  2. a CAD application

There are a few ways to satisfy each requirement, but I've found I prefer the open source, modular approach.

Virtual Lego bricks

You can get nearly every Lego piece ever created from the open source LDraw project. LDraw is an open standard for Lego CAD, which includes consistent measurements and relative dimensions, and a simple language for how bricks are oriented. As a part of LDraw's work in defining bricks, the community also provides 3D models of each brick. That means you can download thousands of brick definitions in a relatively small download (42 MB or so).

Installing your bricks

Virtual bricks are a lot like images on a website or fonts on your computer: as long as the application using the files knows where to find them, you can keep them anywhere. On Linux, it's common to put bricks in /usr/share/LDRAW. On Windows, they're usually installed to C:\Users\Public\Documents\LDraw.

The LDRraw download only provides specifications for each brick. Here's what a 1x1 brick looks like in its raw form:

0 ~Brick  1 x  1 without Front Face
0 Name: s\3005s01.dat
0 Author: John Riley [jriley]
0 !LDRAW_ORG Subpart UPDATE 2004-01
0 !LICENSE Redistributable under CCAL version 2.0 : see CAreadme.txt

0 BFC CERTIFY CCW

0 BFC INVERTNEXT
1 16 0 24 0 0 0 6 0 -20 0 -6 0 0 box5.dat
4 16 10 24 -10 6 24 -6 6 24 6 10 24 10
4 16 10 24 10 6 24 6 -6 24 6 -10 24 10
4 16 -10 24 10 -6 24 6 -6 24 -6 -10 24 -10
4 16 -10 24 -10 -6 24 -6 6 24 -6 10 24 -10
1 16 0 24 0 10 0 0 0 -24 0 0 0 10 box4t.dat
1 16 0 0 0 0 0 1 0 1 0 -1 0 0 stud.dat
0

In order to see your creations in a more familiar form, you need a brick viewer.

LDView for Lego renders

The application LDView is a 3D renderer, much like POVray or Blender's Cycles. It's specifically designed to render .ldr files, which are CAD files using LDraw part definitions.

If you're on Linux, then LDview may be available in your software repository, and if not then you can download an installer from the project website. If you're on Mac or Windows, download an installer from the project website.

Viewing a single brick

The easiest way to get started with Lego CAD is to try viewing a single brick.

First, open your favourite text editor. It can be any text editor, but it must be able to save in plain text. Some text editors are too helpful for their own good, and try to save your text in bloated file formats (like .rtf or .docx). There are many good cross-platform text editors out there. For this, I recommend the relatively minimalist Geany editor.

Create a new file called 1brick.ldr, and enter the following text into it:

0 Name: 1brick.ldr
0 Author: Seth Kenlon

0 clr  x y z  a b c  d e f   g h i <file>
1   1  0 0 0  0 0 1  0 1 0  -1 0 0 3001.dat

View your humble creation:

$ LDView 1brick.ldr
1 brick in LDView

You have just created a simple CAD file, defining a single brick (part number 3001), in colour index 1 (blue), located at coordinates 0 on the X-axis, 0 on the Y-axis, and 0 on the Z-axis. The rotation of the brick is controled by a matrix transform, which admittedly isn't exactly trivial math. However, arbitrary rotation is relatively rare in Lego, because most bricks have to fit together accourding to a grid of studs.

You can practice repositioning and rotating a brick in space by adding to your CAD file. A normal brick is 24 LDraw Units (LDU) tall. Therefore, you can "stack" a brick by adjusting its Y-axis position by 24 units. You can rotate a brick by performing a matrix transform. Try this:

0 Name: 1brick.ldr
0 Author: Seth Kenlon

0 clr  x y z  a b c  d e f   g h i  file
1   1  0 0 0  0 0 1  0 1 0  -1 0 0  3001.dat
1   2 0 24 0 -1 0 0  0 1 0   0 0 -1 3001.dat

Take a look at it in LDView, and try clicking and dragging across the window to see it from different angles. Notice that I've changed the colour of the second brick from 1 (blue) to 2 (green).

2 bricks in LDView

You can move the bricks along any axis, of course. The LDraw specification states that a 1x1 brick is 20 LDU in width and 20 LDU in length, so you can adjust your 2-brick design along the X by incrementing it by 20.

0 Name: 1brick.ldr
0 Author: Seth Kenlon

0 clr  x y z  a b c  d e f   g h i  file
1   1  0 0 0  0 0 1  0 1 0  -1 0 0  3001.dat
1   2 0 24 0 -1 0 0  0 1 0   0 0 -1 3001.dat
2 bricks in LDView

Sequence of bricks

The most common use of LDraw is to demonstrate how to build a model. That implies a sequence of steps, which LDraw represents with the STEP meta command.

To introduce a step in your instructions, add this line:

0 STEP

In your simple 2-brick sample file, you can add a step between the two bricks:

0 Name: 1brick.ldr
0 Author: Seth Kenlon

0 clr  x y z  a b c  d e f   g h i  file
1   1  0 0 0  0 0 1  0 1 0  -1 0 0  3001.dat

0 STEP

1   2 0 24 0 -1 0 0  0 1 0   0 0 -1 3001.dat

Your design now has two steps in it: the first displays one brick, and the second displays an additional brick. You can step through your instructions in LDView by clicking the arrows next to the Steps label in the top toolbar.

Step by step

You don't have to make a new step with every brick. As with instructions for official Lego sets, you can group several additional bricks into one step, so long as their placement is obvious enough.

There are other command types in LDraw, such as the ability to draw lines to clarify brick placement, and so on. These are documented in the specification.

Finding a brick part number

I used to keep my Lego collection in fishing tackle boxes so I could find any piece from any set quickly. As my collection grew, however, so did my need for containers, and I suffered a drop in how efficiently I was able to find a brick.

With over 11,000 unique parts, finding digital Lego bricks is equally troublesome. Ever official Lego brick has a part number. For instance, the 2x4 brick used in the example CAD file is 3001. If you know a part number, you can use it in your CAD file, and the brick appears.

The LDraw distribution includes the parts.lst file, which you can grep through in search of a specific brick. However, the syntax isn't always consistent, and keywords (is a brick "curved" or "sloped" or "angled"?) are sometimes difficult to predict.

There are a few online resources, however, that can be useful:

  • lugnet.com is an online Lego user group with a parts reference database constructed from LDraw.
  • bricklink.com is a useful catalague of bricks.
  • rebrickable.com also features an online catalogue of bricks.

Other renderers

Once you've created your masterpiece, LDView can export your model so you can get a high quality render of it. POVray is a popular open source photo-realistic renderer, so you can get very nice images of your work. You can find povray in your Linux distribution's repository, or download it from the povray.org website.

$ povray +I1brick.pov +Q11 +W4196 +H2160 +O1brick-high.png
POVray render

For a renderer dedicated to instructional layout, try the open source LPub3D project, which displays instructions complete with a brick list for each step.

LPub3D

Exploring Lego

Building Lego is fun, and making up your own models is an important part of the creativity Lego inspires. Your own model designs don't have to be ephemeral any more, though. You can preserve them by creating your own instructional booklets.

In addition, playing with digital Lego allows you to create virtual Lego models and scenes using any part available, whether you own the part yet or not. You could use digital Lego to make your own animations, your own photos of complex sets, or even to design your very own bricks.

There are several online communities, and many of them, like brickhub.org, post beautiful renders complete with LDraw files.

Whether you like to build models digitally, or design bricks of your own, or do something entirely unique, the digital Lego scene is a fun and creative place to visit. Get started today!

Solarized GNU by Linux Pictures under the idgaf license.

Previous Post Next Post