Play with virtual LEGOs using open source tools

Open source virtual LEGOs allow you to build anything you can imagine.
111 readers like this.
Open Lego CAD
Image credits: CC BY-SA 4.0 Klaatu Einzelgänger

My childhood consisted of about 20% Dungeons & Dragons (D&D) and 80% LEGOs, with a pretty strong crossover of the two. I wasn't allowed to actually play D&D for a variety of reasons, but through some mental acrobatics worthy of a level 15 rogue, I determined that building AD&D characters didn't count as playing, and recreating Dragonlance in LEGO form was a pretty good approximation of the game.

For that reason, one of my favorite genres of LEGOs was the castles, and I spent hours upon hours devising gauntlet-like dungeons for my mini figurines. In order to keep track of my creations, and because I saw friends mapping dungeons at school, I mapped out my Lego creations on graph paper. I also tried to track how my creations were constructed, and using graph paper seemed a logical choice for a medium that was mostly based on rectangular bricks, but the lack of understanding of isometric illustration ultimately confounded me.

Now that I'm older, my love of LEGOs hasn't diminished, and while I'm not overproud of any of my own creations (or "MOC," in the lingo of Internet brick builders), I have lately felt I owe it to myself to learn how to document what I build. Because my freehand illustration skills have never been very good, I decided to use technology to solve the problem.

Easy LEGO

The easiest and quickest way to get started with virtual LEGO bricks is to download an all-in-one package that includes bricks and a building application.

Whether you're on Linux, Windows, or macOS, you can get everything you need with LeoCAD. With LeoCAD, you can build models for fun, take snapshots of your creations, or even break your creation out into steps to create your own instruction booklet.

It's easy to use: Select a color, and drag a brick over into the build space. Bricks "snap" to each other to help you find the right spot, and each brick has handles when you click on it so you can move them around. When you're finished, you can render an image of your model.

It doesn't get any easier than that. One download of a great open source application for endless hours (days? years?) of fun.

However, if you're interested in how it all works, how the bricks are defined, and how models are rendered, then read on!

CAD for LEGOs

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 LEGOs, but it's a great augmentation to your hobby.

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

  • virtual LEGO bricks
  • 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 LDraw 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 favorite 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

one blue brick

You have just created a simple CAD file defining a single brick (part number 3001), in color 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 controlled by a matrix transform, which admittedly isn't exactly trivial math. However, arbitrary rotation is relatively rare with LEGOs, because most bricks have to fit together according to a grid of studs.

Any line starting with a 0 indicates either a comment or metadata. A line starting with 1 indicates the addition of a brick.

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 color of the second brick from 1 (blue) to 2 (green).

one blue and one green brick

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

blue and green bricks linked

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 metacommand.

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.

Steps menu in terminal

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. Every 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.

However, there are a few online resources that can be useful:

  • Lugnet is an online LEGO user group with a parts reference database constructed from LDraw.
  • BrickLink is a useful catalog of bricks.
  • Rebrickable also features an online catalog 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 website.

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

POVray blue and green brick 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.

LPub 3d blue and green brick render

Exploring LEGOs

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

In addition, playing with digital LEGOs 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 LEGOs 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, 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!


This article was originally published in June 2020 and has been updated by the author with updated information.

What to read next
Seth Kenlon
Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time.

6 Comments

THAT IS AMAZING!!! (especially with the current price of Lego hahaha) I will try to do this with my raspberry pi! =)

Fascinating, but the plural for Lego is Lego, there is no s on the end. The Lego Group get quite picky about that.

For the record, I wrote this article using "Lego" not "Legos", but it was changed in copy editing. There seems to be an active debate about who's correct: the Lego Group or the populous. I'm not a linguist or anthropologist, so I'll stay out of that debate.

In reply to by drajt

Don't forget ldraw (ldraw.org)

From their site

LDraw™ is an open standard for LEGO CAD programs that allow the user to create virtual LEGO models and scenes. You can use it to document models you have physically built, create building instructions just like LEGO, render 3D photo realistic images of your virtual models and even make animations. The possibilities are endless. Unlike real LEGO bricks where you are limited by the number of parts and colors, in LDraw nothing is impossible.

Indeed! That's exactly what this article is about: LDraw and LDView, with a brief mention of LPub3D and Povray for rendering. This article steps you through downloading LDraw assets, writing CAD files using those assets, and then rendering the results.

In reply to by Tom Tucker

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.