Skip to main content

pyldraw3

PyPI Lint and Test Coverage Status Ruff Code style: black

A modern Python package for creating and manipulating LDraw format files - the standard for CAD applications that create LEGO models. It is a drop-in replacement for the unmaintained pyldraw library.

Features

  • 🧱 Complete LDraw Support: Full compatibility with the LDraw standard format
  • 🐍 Pythonic API: Import LEGO parts directly as Python modules
  • 📦 Dynamic Library Generation: Automatically generate Python modules from LDraw libraries
  • 📜 Comprehensive Guide: Jump into example or the quick start below, or read a detailed usage guide

Table of Contents

Quick Start

Installation

uv add pyldraw3

Setup

Activate your virtual environment and set up the LDraw library - this will download the LDraw library and create the parts classes:

source .venv/bin/activate
ldraw download --yes
ldraw generate --yes

By default ldraw download fetches the complete LDraw release (~80 MB, everything LDraw publishes). To pin a specific dated release instead - useful for reproducible builds or a smaller download - pass --version, e.g. ldraw download --version 2018-02 --yes. Each downloaded release is cached separately, and ldraw generate builds ldraw.library.* from whichever release is currently configured (see Configuration).

Examples

Check the examples/ directory for sample scripts demonstrating various features:

# Run an example
python examples/figures.py > my_model.ldr

Basic Usage

This package allows users to create LDraw scene descriptions using Pieces which are Parts that have a specific position and orientation. Piece.to_ldraw() and Group.to_ldraw() produce LDraw file content; str(piece) and str(group) delegate to those serializers:

from ldraw.library.colours import Light_Grey
from ldraw.library.parts.bricks import Brick1X2WithClassicSpaceLogoPattern
from ldraw.pieces import Group, Piece
from ldraw.geometry import Vector, Identity

# Create a simple model
model = Group()
Piece(Light_Grey, Vector(-10, -32, -90), Identity(), Brick1X2WithClassicSpaceLogoPattern, model)

with open("my_model.ldr", "w") as ldr_file:
    print(model, file=ldr_file)

ldraw.library.* is generated by ldraw generate and gives you every part as an importable, autocompletable Python name (as used above). If you'd rather look a part up by its catalog description or LDraw code at runtime - for example when the part name isn't known until your program runs - load the parts catalog directly instead:

from pathlib import Path

from ldraw.config import Config
from ldraw.parts import Parts

config = Config.load()
parts = Parts(Path(config.ldraw_library_path) / "ldraw" / "parts.lst")
cowboy_hat = parts.get_entry_by_description("Hat Cowboy").code  # -> "3629"
head = parts.get_entry_by_description("Head with Solid Stud").code  # -> "3626a"
brick1x1 = parts.get_entry_by_description("Brick  1 x  1").code  # -> "3005"

Both cowboy_hat and Brick1X2WithClassicSpaceLogoPattern are just LDraw part code strings, so either style can be passed as the part argument to Piece.

Requirements

  • Python 3.12+

Configuration

ldraw download and ldraw generate write their settings to a YAML config file in an OS-appropriate config directory (via platformdirs). Run ldraw config to see the current values:

$ ldraw config
generated_path: /Users/you/Library/Application Support/pyldraw3/generated
ldraw_library_path: /Users/you/Library/Caches/pyldraw3/2018-02
  • ldraw_library_path - the downloaded LDraw release currently in use (switch releases by re-running ldraw download --version ...)
  • generated_path - where ldraw generate writes the ldraw.library.* package that you import

CLI Reference

usage: ldraw [-h] command ...

Download the LDraw parts library and generate the ldraw.library Python
modules.

positional arguments:
  command
    download  Download and unpack an LDraw parts library release.
    generate  Generate the ldraw.library modules from the downloaded library.
    config    Print the current configuration.
    version   Print the installed pyldraw3 version.

options:
  -h, --help  show this help message and exit
  • ldraw download [--version VERSION] [--yes] - download and unpack an LDraw release (default version: complete)
  • ldraw generate [--yes] [--force] - (re)generate ldraw.library.* from the currently configured release; --force regenerates even if already up to date
  • ldraw config - print the current configuration as YAML
  • ldraw version - print the installed pyldraw3 version

Run ldraw <command> --help for a command's full option list.

Development

This project uses uv for dependency management and packaging.

Setup Development Environment

# Clone the repository
git clone https://github.com/hbmartin/pyldraw3.git
cd pyldraw3

# Install dependencies
uv sync

# Activate virtual environment
source .venv/bin/activate

# Download and set up LDraw library
uv run ldraw download --version 2018-02 --yes
uv run ldraw generate --yes

Development Commands

# Run tests
uv run pytest                 # All tests
uv run pytest --cov=ldraw     # With coverage
uv run pytest --integration   # Integration tests only

# Code formatting and linting
uv run black .               # Format code
uv run ruff check            # Lint code
uv run ruff check --fix      # Fix linting issues

# Build package
uv build

Architecture

Core Components

  • CLI Interface (ldraw/cli.py): Command-line interface with download, generate, config, and version subcommands
  • Dynamic Library Generation (ldraw/generation/): Converts LDraw libraries to Python modules
  • Import System (ldraw/imports.py): Custom meta path hook for dynamic imports

Key Classes

  • Parts - Manages parts catalog and loading
  • Piece - Represents individual LEGO pieces in models
  • Figure - High-level minifigure construction
  • Geometry classes - Matrix operations and 3D mathematics

Contributing

Contributions are welcome! See CONTRIBUTING.md for the fork/branch/PR workflow.

License

This project is licensed under the GNU General Public License v3.0 or later - see the license (COPYING) file for details.

pyldraw, a Python package for creating LDraw format files.
Copyright (C) 2008 David Boddie <david@boddie.org.uk>
Some parts Copyright (C) 2021 Matthieu Berthomé <matthieu@mmea.fr>
Some parts Copyright (C) 2025 Harold Martin <harold.martin@gmail.com>

Trademarks

LDraw is a trademark of the Estate of James Jessiman. LEGO is a registered trademark of the LEGO Group.

Credits

This repository was extracted from the original Mercurial repository and modernized for current Python practices.

Download files

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

Source Distribution

pyldraw3-0.12.0.tar.gz (32.9 kB view details)

Uploaded Source

Built Distribution

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

pyldraw3-0.12.0-py3-none-any.whl (42.5 kB view details)

Uploaded Python 3

File details

Details for the file pyldraw3-0.12.0.tar.gz.

File metadata

  • Download URL: pyldraw3-0.12.0.tar.gz
  • Upload date:
  • Size: 32.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyldraw3-0.12.0.tar.gz
Algorithm Hash digest
SHA256 941878a2538580f66911e4e3185130b9a2ec14f6d01b027775429610affff2f6
MD5 84889b620362c86f35f92589f2c9227a
BLAKE2b-256 7cff176d7a9121add139b1239fd0f420b11a4ce4b7c091017560c0a24e0f617d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyldraw3-0.12.0.tar.gz:

Publisher: publish.yml on hbmartin/pyldraw3

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyldraw3-0.12.0-py3-none-any.whl.

File metadata

  • Download URL: pyldraw3-0.12.0-py3-none-any.whl
  • Upload date:
  • Size: 42.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyldraw3-0.12.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ece365d0e370036c248b893d0621903e44566e01b7a2e243a252e58a74da9da1
MD5 be850a0c99dfb204d3ed9021abf189ec
BLAKE2b-256 ac7aa8423f68fb4f338adcf3be6669e114f6a02fb468cb044a5f354ae9a09a6f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyldraw3-0.12.0-py3-none-any.whl:

Publisher: publish.yml on hbmartin/pyldraw3

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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