How To Import, Render & Re-Export LEGO® 3D Data from "LDraw" .dat & .ldr Files
For those who are not aware of the fact, there is a huge community of people and WEB sites dedicated to the modeling, rendering and
documentation of LEGO models and how to build them. The following tutorial was written to educate the visitors to the Okino WEB site, and Okino software customers, about the existence of this 3D graphics world of LEGO models, and second, to show how this vast wealth of LEGO 3D parts and model data can be converted into a format most often used by Okino software customers.
As serious as the rest of the Okino WEB site is to our professional customers, we thought we would write this interesting and off beat tutorial for those who still yearn for their earlier days of playing with LEGO. Nonetheless, it is ideally targeted at the existing community of LDraw program users
to show how their models can be rendered and animated in a program other than POV-RAY (which is pretty well the standard used throughout this
By following this fairly simple tutorial you will be able to import 3D LEGO parts and models in the LDraw format and render them in Okino's
ray tracing software, or convert the render-ready scenes to all popular 3D programs such as 3ds max, Maya, Softimage, Lightwave, Cinema-4D, trueSpace and dozens more.
To understand which free programs need to be downloaded to your machine first.
To learn where to find 3D LEGO parts data.
To learn how to view such parts data outside of Okino software (prior to importing).
To learn how to convert the .ldr and .dat files associated with the LDraw program into a format that can be read into Okino software.
To show how to slightly clean up the imported scene before rendering or re-export to other 3D programs.
The following screen shot shows the results of importing the "car.ldr" file (that comes standard in the LDraw program's distribution) into Okino's NuGraf software using the methods outlined in this tutorial. The image shown is a just real-time OpenGL display; the Okino ray tracer can subsequently create a nicer image with full reflections, transparency and glass refraction (see the last image shown in this tutorial).
Click on image for full screen snapshot.
A LEGO car imported into Okino's NuGraf software from the stock example LDraw 'car.ldr' file
If you are completely new to this topic, then this tutorial is going to save you hours and hours of work, plus get you up to speed very fast on all the
First, you need to understand that the LDraw program is at the center of the 3D LEGO parts modeling universe. As described on its WEB site,
"The original LDraw program, as well as the file format used for the tool system were written by James Jessiman who passed away on July 25, 1997. Since then the community of LDraw users have furthered his work. Today many different programs have been built upon his original tool and use the same file format totally interchangeably. There are LDraw editors and renderers for Windows and Linux, Macintosh versions are under development."
The key here is that the LDraw file format "is totally interchangeably". As such, you can visit almost any serious WEB site of a person who has built a LEGO model, or in many cases LEGO Robotics, and find ".ldr, .dat, .mpd" files representing their creations which can be quickly downloaded and viewed.
Tutorial Step # 1: Downloading the Necessary Free Programs and Datasets
First, visit the LDraw WEB Site and navigate to the "Download / Get Started / LDraw Tools Installer (large)" WEB page. Download and execute this "LDraw All In One Installer". From this, you will want to install at least:
The "LDraw Parts Library" (30MB or so uncompressed)
The "LDraw" program and its contents.
The "MLCad" program for editing and viewing the LDraw files.
In general you don't need the other programs, and you don't need the POV-RAY software for this tutorial.
The above process will install in part the "MLCad" program which provides one of the best ways to edit and view the LDraw Parts Library inside a Windows based application (the parts will appear as small rendered icon/images in a list box on the left side of the user interface). Another popular and similar LEGO modeling program is LeoCAD.
Many of the utility programs, such as MLCad, require that the LDraw program be first installed. This is because the LDraw program also includes the all-important and all-mighty "LDraw Parts Library". This is several directories of ".dat" files which are the 3D models of each known LEGO part. New parts are being created and refined all the time. It is recommended that you return to the LDraw WEB Site and update your parts library every now and then.
Second, you need to download and install the "LDraw to Lightwave" free converter program for Windows ("LDraw2lws"). It can be downloaded from the WEB site of Joern-thiemann. We found that this produces the most acceptable conversions from LDraw format to the format Okino customers need (Lightwave will be an ideal file format); it converts the data into 3d model geometry, assigns materials (with color, transparency and reflectivity), and sets up the lights. Most other converters, mainly the common
"L3P" utility, convert from LDraw to POV-Ray format.
As the last installation process, please read the "Appendix: How to Install & Setup 'LDraw2Lightwave.exe'" shown at the end of this document.
Tutorial Step # 2: What are LEGO 3D Files and Where Can You Find Them?
The most convenient place to find some basic 3D LEGO models is in the "LDraw Parts Catalog". This is located in the "parts" and "models" sub-directories of the LDraw program's main directory. The ".dat" files are the basic 3D data files for each LEGO element; the ".ldr" files are similar to a CAD "assembly" file in that the .ldr file simply makes reference to multiple .dat files to "assemble" a complex LEGO model out of basic .dat file elements; and lastly the ".mpd" files are multiple .dat files combined into a single file (designed as a mechanism to allow multiple .dat files to be distributed in email messages or single text files). All three of these file formats can be imported and viewed using the "MLCad" program.
The following, for example, is the contents of the 3D LEGO ls21.dat file. It is ASCII file, which for this file, makes two secondary instance references to the LEGO "primitive" .dat file "4-4cyli.dat" which is implicitly located in the "p" sub-directory of the main LDraw directory.
Tutorial Step # 3: How to View LEGO Parts Data Prior to Importing
The "MLCad" program provides one of the best ways to edit and view the LDraw Parts Library inside a Windows based application (the parts will appear as small rendered icon/images in a list box on the left side of the user interface). Another popular and similar LEGO modeling program is LeoCAD.
You can also download free viewers from the "Download / All Software / Windows" section of the
LDraw WEB Site, such as L3Lab, LDGLite and LDView.
Tutorial Step # 4: How to Convert the .dat, .ldr and .mpr LEGO Parts & Models to Lighwave .lwo Files
By this point of the tutorial you should have all the software installed. Next, let's go and convert the "car.ldr" model (located in the "models" sub-directory of the main LDraw directory) into one or more Lightwave .lwo files:
Execute the "LDraw 2 Lightwave" program
If you wish to have the "LEGO" name appear on each tiny stud then checkmark the "Create LEGO-Signs on Studs".
Press the "Parse File" button
Navigate to the "C:\LDraw\models" directory.
Change the "Files of Type" drop-down combo box to *.*
Select the "car.ldr" file and press the "Open" button.
Wait a minute for the model to be converted into a number of Lightwave ".lwo" models.
Tutorial Step # 5: How to Import the Lightwave .lwo Files Into Okino Software
Now, it's time to import the converted Lightwave .lwo files (1 or more of them) into Okino's NuGraf or PolyTrans programs. NuGraf will provide us the ability to ray trace the imported model. For the sake of this tutorial we will use NuGraf:
Do "File/Import/Lightwave" from the main menu.
Navigate to the directory where you originally loaded in the "car.ldr" file. Now navigate once again to the sub-directory where the new .lwo files were created.
Click and highlight one of the .lwo files, then press "Control-A" so that every .lwo file gets selected.
Press the "Open" button so that all the selected .lwo files get imported.
When the Lightwave importer option dialog box appears press the "Reset" so that all options are sane.
Wait a few seconds or a minute to allow the Lightwave .lwo object file(s) to be imported.
Execute the "Optimize Hierarchy" command from the "Win" menu of the "Selector Window" in the upper right side of the screen. This will remove all the redundant yellow grouping folders which were created because we imported multiple .lwo files.
From the left vertical toolbar, do a "FIT / All Views / Fit to Objects".
Enable shaded mode for the view windows, if you don't have it enabled already.
Tutorial Step # 6: How To Change the Scale of the Imported Data to Be 100 Times Larger
As one last optimization step, the imported data will probably be 100 times too small, since Lightwave uses an implicit "centimeters" units
system. To change the scale in NuGraf:
First, execute the "Add Empty Folder" command from the "Win" menu of the "Selector Window" in the upper right side of the screen. Use the default name shown and press "OK".
This is the slightly tricky part if you've never done this before. Basically you have to highlight all the "Turquoise" 3D models shown in the right-side "Selector Window" and drag & drop them onto the new "Yellow Folder" you just added. The easiest way to do this is first highlight (left click) on the top-most model name (a turquoise icon) shown in the list (the one contained as a child of the "world" green node). Don't highlight the "world" green" node. Now, scroll down to the bottom of the list, press the SHIFT key and at the same time left-click the mouse over the name of the bottom-most object name (the last turquoise icon in the list). Don't highlight or click on the yellow folder. Now, drag & drop the highlighted list of object names on top of the new yellow folder you had just created. Now all the turquoise icon names will be children of the yellow grouping node. Click on the screen snapshot image below to show what the "Selector Window" and its contents should look like after this simple operation is done.
The imported model, at least the one we are importing, will be about 100 times too small. To change the scale, left click the mouse over top of the yellow grouping node folder in the "Selector Window" (the parent grouping node of all the objects). The "Instance Properties" dialog box will pop up. Change the 3 number boxes beside "Scale:" to (100, 100, 100). Press "OK" to exit.
Since the size of the car changed, do a "FIT / All Views / Fit to Objects" from the left vertical toolbar.
And finally, from the "Views" menu do a "Auto-adjust 3D Grid's Width & Height" so that the 3D grid shown in the upper-right camera view resizes
to be slightly wider than the size of the car model.
By this point the final imported and resized model should look like this screen snapshot:
Click on image for full screen snapshot
Tutorial Step # 5: How to Create a Ray Traced Final Image, or Re-Export to Other 3D Programs
At this point you can now use Okino's NuGraf ray tracer to create a final high resolution image, or you can re-export the model to dozens of industry standard 3D file formats via the "File/Export" menu. NuGraf contains all the functionality and all the data conversion capabilities of Okino's PolyTrans sibling product.
Click on image for larger 800x600 rendered image.
The final LDraw LEGO car imported & ray traced with Okino's NuGraf software.
Appendix: How to Install & Setup 'LDraw2Lightwave.exe'
This tutorial depends upon the freely available "LDraw to Lightwave" converter program for Windows ("LDraw2lws"). It can be downloaded from the WEB site of Joern-thiemann. However, it is slightly tricky to install and use just based on the instructions provided with the converter.
Please follow these suggestions to get this program installed properly:
First, make sure you have downloaded and installed the "LDraw" software, especially the LDraw Parts Library. Make note of which directory the software was installed. For this tutorial we will assume it is "C:\LDraw" as the directory.
Download and unzip the LDraw2lws.zip file to a directory on your machine.
As a work-around for a bug in this converter, copy the "lego_sign.lwo" file from the newly created directory to the top-most directory. For example, if you installed the converter to "c:\LDraw2lws" then you will need to copy the "lego_sign.lwo" file to the "C:\" root directory.
Execute the "LDraw 2 Lightwave" program once, then exit it.
This is the tricky part: you need to hand edit the "LDraw2lws.ini" file which is now located in the top-most directory on your hard disk. So, if you installed and ran the "LDraw 2 Lightwave" program on your "C:\" drive, then this .ini file will be located in "C:\LDraw2lws.ini". Assuming that you installed the LDraw program to "C:\LDraw" and the "LDraw 2 Lightwave" program to the "c:\LDraw2lws" directory, all you need to do is edit the "LDraw2lws.ini" file (in the top-most directory) and replace it entirely with the following contents (you need to change the paths appropriately to the locations of the two installed programs):