Breakdown 1 of 4

Python and LDraw data

I scripted a filter that sifts LDraw's 23,000+ parts down to 900+ common LEGO parts.

Overview

Every part comes from LDraw, an open, community-driven recreation of almost every LEGO part, founded by the late James Jessiman (LDraw.org, 2002) and going for over 30 years. Without this community effort the tool would not be possible.

LDraw has its own format for models rather than following an existing convention. A part is a .dat text file of raw transforms and references to a shared library of primitives, which nothing in a games pipeline can read directly.

The LDraw library page for part 3001, Brick 2 x 4, showing the file header with its name, author James Jessiman, CC BY 4.0 licence and update history, a few lines of raw transforms underneath, and a yellow preview render of the brick
Part 3001 and its raw transforms and metadata.

That’s part 3001 (Jessiman, 2004), the 2 x 4 brick, which ended up being my test part at every stage. The output is one JSON part list that everything after it shares: Houdini generates a mesh for every entry and adds the stud positions, and Unreal imports it as the data table behind the part browser.

A section of the generated part list in a code editor, one JSON entry per part with a PartID, a Name such as Brick 2 x 4 or Slope Brick Curved 2 x 1 with Cutout Left, a semicolon-separated Keywords string and an empty Studs array
Collected parts data.

One script

Originally, when I built this out, I made an individual script for each stage. For the release I folded them into a main script and a library with a stage-by-stage walkthrough. The stages run in order, from parsing the parts, through the Houdini cook, to copying the finished list into the Unreal project.

flowchart LR
  A[LDraw library] --> B[Parse and filter]
  B --> C[Part list JSON]
  R[Rebrickable API] --> T[Thumbnails]
  C --> T
  C --> H[Houdini cook]
  H --> U[Unreal data table]
  T --> U

Filtering the library

LDraw has every LEGO part, which is an expansive list with pieces like Duplo and minifig accessories that don’t fit the focus of this tool. Of 23,000+ parts only a small set is really needed to cover most building, and the filter gets down to it in 3 passes:

  • Dead parts - LDraw uses ~, = and _ to state parts that are deprecated or moved to other IDs.
  • Allowlist - Part IDs on the allowlist will be allowed through, even if they contain a keyword.
  • Keyword - Parts whose name or keywords contain any word on the list are dropped.
The three filter passes (Python, 25 lines)
def filter(entry, allowlist, filterlist):
    # Is valid
    if entry:
        name = entry.get("Name", "").lower()
        keywords = entry.get("Keywords", "").lower()
        ID = entry.get("PartID", "").lower()
        
        # Is dead part
        if any(sym.lower() in name for sym in ["~", "=", "_"]):
            return False
        
        # Is allowed bypassing filter
        for al in allowlist:
            if al == ID:
                print(f"{al} == {ID} is in allowlist and bypassed filter.")
                return True
        
        # Check if entry contains any filter word in filterlist
        allowed = True
        for fw in filterlist:
            if fw in name or fw in keywords:
                allowed = False
                break
        return allowed
    return False
A terminal printing the last parts added to the list, including a roadsign and two circuit cubes plates, two lines noting parts that bypassed the filter through the allowlist, and the summary line: Added 945 parts. Did not add 22368 as they did not pass the filter
Stage 1 on the release run.

Thumbnails

The browser needed a picture of every part. Rebrickable (Rebrickable, 2011) hosts renders of LDraw parts by part ID, and the script requests a small render of each one, tries a few other colours if there isn’t one, and moves on if they all fail. Then PIL cleans it up. The background colour is sampled from the corner pixel and masked out, the mask edge is softened and the part is greyscaled. Every thumbnail ends up the same neutral grey, so the UI can tint the tiles to whatever colour you have selected.

The thumbnail clean-up (Python, 25 lines)
def process_image(image_path, tolerance = 12, smooth_radius=1):
    img = Image.open(image_path).convert("RGBA")
    w, h = img.size
    px = img.load()
    bg_colour = px[0, 0]
    
    # Mask anything close to background colour
    mask = Image.new("L", (w, h), 0)
    mask_px = mask.load()
    
    for y in range(h):
        for x in range(w):
            if is_bg(px[x, y], bg_colour, tolerance):
                mask_px[x, y] = 255
            else:
                mask_px[x, y] = 0
    
    # Smooth edges
    if smooth_radius > 0:
        mask = mask.convert("L").filter(ImageFilter.GaussianBlur(radius=smooth_radius))

    # Apply mask to image and save
    grey = ImageOps.grayscale(img)
    result = Image.merge("RGBA", (grey, grey, grey, ImageOps.invert(mask)))
    result.save(image_path, "PNG")

Coverage

For the default parts, these were the final part stats:

Reflections

  • A blunt filter - The keyword pass matches substrings, so a short word catches more than it should. bar removes bar pieces but also Barrel 2 x 2 x 1.667, which is useful for set dressing. Parts like that have to be added to the allowlist one ID at a time.
  • Missing thumbnails - Rebrickable has no render for 225 of the 945 parts, so 1/4 of the library shows a crossed-out placeholder in the browser and those parts are only easy to use if you already know what they are. Rendering the thumbnails myself would have avoided the gaps, but it wasn’t a priority in the timescale.
  • An outdated LDraw2Houdini - Stefan released a more streamlined version of LDraw2Houdini (Müller, 2023) partway through the project. Switching would have meant reworking my network, so the released parts come from the older version 3.0.0.
  • Manual imports - Meshes and thumbnails are still dragged into Unreal by hand. Automating the import would need C++.

Sources

  • Jessiman, J. (2004). File Detail parts/3001.dat. [online] LDraw.org. Available at: https://library.ldraw.org/parts/3436 [Accessed 23 Nov. 2025].

    The 2 x 4 brick, used for testing.

  • LDraw.org (2002). LDraw.org Homepage. [online] Available at: https://www.ldraw.org/ [Accessed 23 Nov. 2025].

    The part library the whole tool is built from.

  • Müller, S. (2023). ldraw2houdini: Import LDraw Files Directly into Houdini. [online] GitHub. Available at: https://github.com/stefanmuller/ldraw2houdini [Accessed 23 Nov. 2025].

    The Houdini processor that uses LDraw.

  • Rebrickable (2011). Rebrickable - Build with LEGO. [online] Available at: https://rebrickable.com/home/ [Accessed 23 Nov. 2025].

    Where part thumbnails come from.