Skip to main content

PyRocrail

Python library for controlling Rocrail model railway systems.

PyRocrail provides a Pythonic interface to the Rocrail server, allowing you to control locomotives, switches, signals, routes, and other layout objects programmatically. Replace XML scripting with Python for better automation and control.

Features

  • 17 Object Types: Full support for locomotives, blocks, switches, signals, routes, and more
  • Real-time Control: Direct TCP/IP communication with Rocrail server
  • Event-Driven: Action system for time-based and event-based automation
  • Type-Safe: Complete type annotations for IDE support and error detection
  • Verified Commands: All commands verified against official Rocrail documentation

Documentation

Quick Start

from pyrocrail import PyRocrail, Action, Trigger
import time

# Connect to Rocrail using context manager (automatic cleanup)
with PyRocrail("localhost", 8051) as pr:
    # Control a locomotive
    loco = pr.model.get_lc("BR01")
    loco.set_speed(50)
    loco.set_direction(True)
    loco.go()

    # Set a switch
    switch = pr.model.get_sw("SW01")
    switch.turnout()

    # Activate a route
    route = pr.model.get_st("RT01")
    route.set()

    # Time-based action (run at 12:30)
    def morning_announcement(model):
        print("Morning operations starting")

    pr.add(Action(
        script=morning_announcement,
        trigger_type=Trigger.TIME,
        trigger="12:30"
    ))

    # Event-based action (when sensor activates)
    def on_train_detected(model):
        loco = model.get_lc("BR01")
        loco.set_speed(25)  # Slow down

    pr.add(Action(
        script=on_train_detected,
        trigger_type=Trigger.EVENT,
        trigger="FB01",
        condition="obj.state == True"
    ))

    # Keep running
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        pass
# Automatically cleaned up when exiting 'with' block

Installation

# Clone the repository
git clone https://github.com/thetrainroom/py-rocrail.git
cd py-rocrail

# Install with Poetry
poetry install

# Or install with pip
pip install -e .

Supported Objects

See OBJECTS.md for complete documentation.

Object Description Rocrail Docs
Locomotive Train engine control lc-gen-en
Block Track section management block-gen-en
Switch Turnout/point control switch-gen-en
Signal Signal aspect control signal-gen-en
Route Path routing route-gen-en
Feedback Sensor detection sensor-gen-en
Output Accessory control output-gen-en
Car Rolling stock car-gen-en
Operator Train compositions ("Trains" in GUI) operator-gen-en
Schedule Timetables schedule-gen-en
Stage Staging yards stage-details-en
Text Information displays text-gen-en
Booster Power districts booster-gen-en
Variable Global variables variable-gen-en
Weather Atmospheric effects weather-gen-en

Plus: Tour, Location, and more...

Development

# Run linter
ruff check $(git ls-files '*.py')

# Run type checker
pyright src/pyrocrail

# Format code
black --line-length 180 src/

References

License

See LICENSE file for details

Metadata

Release files for pyrocrail 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyrocrail 1.0.0
File Size Uploaded Provenance Actions
pyrocrail-1.0.0.tar.gz 46.9 kB With provenance Details for pyrocrail-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyrocrail 1.0.0
File Interpreter ABI Platform Actions
pyrocrail-1.0.0-py3-none-any.whl Python 3 none any Details for pyrocrail-1.0.0-py3-none-any.whl

Total release size: 103.8 kB

Release files / pyrocrail-1.0.0.tar.gz

Download URL pyrocrail-1.0.0.tar.gz
Size 46.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c159aca4ba3aac475a91f204781b14face3f1bdcafc2ae0abcd0ad04a16ec1ee
BLAKE2b-256 checksum
How to use checksums
c008c42bf3e15e32e52de3ad2e0d49aa5d594190ad80dcb091a122f7dba1db6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.

Transparency log

Release files / pyrocrail-1.0.0-py3-none-any.whl

Download URL pyrocrail-1.0.0-py3-none-any.whl
Size 56.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fe5090806ec78e9421a5cb2146ada3796748c0825cf35558108f2ac229301c23
BLAKE2b-256 checksum
How to use checksums
08dae4cb599198fa6dc7cbad9aba3feec9819f67c9f3af3014794beadac79864
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.

Transparency log

Security for pyrocrail 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pyrocrail-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page