Play with virtual LEGOs using open source tools | Opensource.com

Play with virtual LEGOs using open source tools

Open source virtual LEGOs allow you to build anything you can imagine.

Open Lego CAD
Image credits : 
Image credits: CC BY-SA 4.0 Klaatu Einzelgänger
x

Subscribe now

Get the highlights in your inbox every week.

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.

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

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

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

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.

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

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

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!

Minecraft and open source?

Interested in a free Minecraft alternative? Here's a quick look at some clones and derivatives out there that you really ought to check out.
plastic game pieces on a board

Celebrate tabletop role-playing games and get free RPG materials at your local game shop on June 15.

About the author

Seth Kenlon
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. He is one of the maintainers of the Slackware-based multimedia production project, http://slackermedia.info