Brickheart

Brickheart alternative-history LEGO

: Written by a human with AI assistance.

Goal of the next session

Combine the Rebrickable data with the LDraw parts library to find a selection of sets that:

  • minimises LDraw library file size and the number of parts to model, and
  • keeps good coverage of themes, years and subjects.

Concept

This is a digitally buildable alternative history of LEGO. It is not meant to be complete. LDraw will later show what can actually be built, and the rules get revisited against its catalogue.

Files and tools

  • load_data.py: a combined loader that rebuilds rebrickable.db from scratch on every run, using schema.sql and the Rebrickable .csv.gz files.
  • Enrichment CSVs: rebrickable_themes_by_subject_format_scale.csv and simplified_lego_colors.csv.
  • Colour mapping export: site/data/colour-mapping.json holds the full old-to-modern colour map.
  • Streamlit app: a query() helper plus reference tables (themes, parts, colors_df, sets_all and the subject, format, scale and complexity lookups). A set-selection tab was added this session.

Rules the pipeline applies, in order

  1. Keep version 1 inventories only.
  2. Remove all minifigs and all stickers, before set sizes are recounted.
  3. Recount sets.num_parts from the surviving inventory, excluding spares.
  4. Remove side-project themes (the THEME_IDS list and descendants), database or 1:87 sets, and Modulex, Duplo and Likit colours.
  5. Keep sets of 15 to 399 parts only (MIN_PARTS and MAX_PARTS).
  6. Remove sets containing parts that appear in only one theme. This runs once, after the size filter.
  7. Build the modern colour map:
    • The modern palette is the colours with y2 > 2020, excluding opal and glitter, which gives 74 colours.
    • Retired colours map to the nearest modern colour in Lab space, matching transparency.
    • colors gains modern_color_id, is_modern, base_color and basic_rgb.
  8. Keep the top 100 printed parts (TOP_PRINTED_PARTS), ranked by number of sets.
    • Printed parts are detected through P relationships or a regex on the part number suffix.
    • Printed parts with no plain version compete for the top 100 on equal terms. Losers are removed.
    • Everything is logged in the print_replacements table.
  9. REWRITE_INVENTORY_PARTS (destructive): one pass that, in inventory_parts:
    • swaps printed parts for plain ones and mold variants for the most modern mold (M relationships, ranked on the surviving sets),
    • rolls colours up to the modern palette and drops anything outside it,
    • sums duplicate rows so each (inventory, part, colour, spare) is unique,
    • then recounts set sizes and re-applies the size filter.
  10. DELETE_RETIRED_COLORS: colors is cut to the 74 modern colours.
  11. Remove orphans:
    • elements is trimmed to part and colour pairs actually used.
    • Themes without sets are deleted, and parent_id is cleared where the parent was removed.
  12. Statistics run last (year_stats, part_stats, part_rank, era_stats, part_era_rank):
    • colors.num_parts, num_sets, y1 and y2 are recalculated for the alternative history.
    • part_rank.part_palette and inventories.set_palette are built from base colours.

Last numbers seen

These were before the final sticker-only change:

  • 7,079 sets
  • 3,649 parts
  • 74 colours
  • about 358k inventory_parts rows
  • 3,212 parts in part_rank

7k sets is still too high. Lowering MAX_PARTS is one idea, but the single-theme rule then removes more sets, so the two interact. Upn parts (names starting "Upn": metal trees, signs and sails) deliberately stay until LDraw shows what it can display.

Known quirks

  • norm_part_num strips everything after the leading digits, so it merges part variants in the stats tables. Rebuilding with real part numbers is the better key for LDraw matching.
  • The mold map only looks at the surviving sets, so a newer mold that no remaining set uses can't be picked.
  • Merged rows lose their img_url.
  • Some inventory rows may have no matching elements row after the colour roll-up.
  • "Additional sets buildable" matches on part number only. It ignores colour and quantity.

Selection tool (Streamlit tab)

  • Greedy weighted set cover over chosen coverage dimensions: leaf theme, parent theme, year, subject, format, scale, complexity, and parent theme × year.
  • It minimises number of sets, total pieces or distinct parts, with a "sets per item" depth slider.
  • It sweeps min and max part counts to show the coverage trade-off, and lists extra sets buildable from the chosen parts.
  • distinct parts already uses a marginal cost, counting only parts not yet in the selection. That design is the one to extend for file size.

Plan for the LDraw session

  • Cost is not additive: a part's file size is its .dat plus every subpart (s/) and primitive (p/) it references, recursively. Many parts share files. The cost of adding a part should be the bytes of its dependency files not already included, which is the same marginal approach as the current distinct-parts cost.
  • Part ID mapping: match Rebrickable part numbers to LDraw ids, handling prints and pattern parts, mold variants, and LDraw aliases (the ~Moved to files). Parts with no match need a decision: drop them, or flag them for review.
  • Colour mapping: map the 74 modern colours to LDraw colour codes (via LDConfig.ldr), and check for any colours with no equivalent.
  • Size measure: decide whether size means raw bytes or compressed, and whether to include the high-resolution primitive folders.
  • Pipeline order: the LDraw filter probably belongs before the top-100 printed-part ranking, so that parts without a model never take a place.

Questions to settle at the start

  1. Where is the LDraw library (complete or partial), and does it include unofficial parts?
  2. Is there an existing Rebrickable-to-LDraw ID mapping, or does it need building?
  3. Is the target total library size a budget or just something to minimise?