Skip to main content

LEGO rendering tools using Blender

Project description

grid of parts

Lego Rendering Pipeline

Render semi-realistic, individual parts. Primarily used to train machine learning models for detection, part classification and color classification.

This uses LDraw's part models and Blender to produce a render.

A single render typically takes 10-15s on an M1 Pro. There is a draft mode that takes a few seconds.

Parts

In theory this can render any LDraw part. However I've only tested with typical plastic Lego parts. Parts are specified using their LDraw ID. In many cases this will be same as the mold number on the part but not always. Keep in mind there may be multiple molds over time.

Materials

Currently supports plastic, transparent and rubber. Not supported yet: pearlescent, glitter, neon colors, metal and cloth.

grid of materials

Colors

Colors are specified with hex codes in sRGB. Included is a list of named colors from Rebrickable.

grid of colors

Rotation

Parts can be rotated to various angles. The parts are moved up/down to remain touching the ground.

grid of various rotations

Lighting

Lighting Styles: default, bright, hard

grid of lighting styles

Lighting angle around the part

grid of light angles

Camera

Camera height: degrees from 0-90

grid of various camera angles

Camera zoom

grid of various zoom levels

Instruction Look

By default they are rendered with a transparent background. Use PNG format (format=Format.PNG).

grid of various parts in line art style

Setup

  • Install Blender
  • Install ImportLDraw Plugin
  • In your own a folder:
    • Download LDraw parts library
    • unzip complete.zip
    • Download LDraw unofficial parts
    • unzip -o ldrawunf.zip -d ldraw
    • At this point your current folder should look like
      ldraw/
        parts/
        p/
        ...
      
    • In Blender, open the Python Console:
    import subprocess
    import sys
    subprocess.check_call([sys.executable, "-m", "pip", "install", "--user", "lego-rendering"])
    

Alternatively, clone this repo, follow the above steps and run

./setup.sh

Run

Build script:

sys.path.insert(0, site.getusersitepackages())
from lego_rendering import Renderer, RenderOptions, Quality, LightingStyle, Look, Material, RebrickableColors

renderer = Renderer(ldraw_path="./ldraw")

renderer.render_part("6126b", RenderOptions(
    image_filename = "renders/test.jpg",
    part_color = RebrickableColors.Blue.value.best_hex,
    material = Material.PLASTIC,
    lighting_style = LightingStyle.BRIGHT,
    light_angle = 160,
    part_rotation = (0, 0, 270),
    camera_height = 45,
    zoom = 0.1,
    look = Look.NORMAL,
    width = 244,
    height = 244,
))

See lego_renderer/renderer/render_options.py for the full list of options. See docs-*.py to see how the images on this page were genereated.

Run in Blender's Python environment:

./run.sh test.py         # renders to renders/test/png

./run-watch.sh test.py   # run test.py each time a .py file is saved

Contributing

# Setup
uv sync

# Render without publishing a pacakge
./run.sh test-local.py

# Publish a new version
# - bump version in pyproject.toml
uv build
uv publish

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lego_rendering-0.1.5.tar.gz (38.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lego_rendering-0.1.5-py3-none-any.whl (21.6 kB view details)

Uploaded Python 3

File details

Details for the file lego_rendering-0.1.5.tar.gz.

File metadata

  • Download URL: lego_rendering-0.1.5.tar.gz
  • Upload date:
  • Size: 38.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.13

File hashes

Hashes for lego_rendering-0.1.5.tar.gz
Algorithm Hash digest
SHA256 160c32e3815bffd055f3b0ada249a32850e350207a07a6145719ecb69ede7427
MD5 4a6c4ac6a18113623b9ec4babea4e11f
BLAKE2b-256 00b5aa91c27d0296b71ce494735454642566117eaac89719f02f865c9e1ff195

See more details on using hashes here.

File details

Details for the file lego_rendering-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for lego_rendering-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 eb862a6c8a1686b2c9e49df6b2405114dcb5402017f67c27ce5951f2a4ac5971
MD5 ee2176780496b3c44ccd666584a08fa5
BLAKE2b-256 a4fe7aa2f311b125b5b751d4c1d9092a8212b66c755538d015782c5efa3983d4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page