Skip to main content

Python library for interfacing with Rocrail model railway control software

Project description

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

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

pyrocrail-1.0.0.tar.gz (46.9 kB view details)

Uploaded Source

Built Distribution

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

pyrocrail-1.0.0-py3-none-any.whl (56.9 kB view details)

Uploaded Python 3

File details

Details for the file pyrocrail-1.0.0.tar.gz.

File metadata

  • Download URL: pyrocrail-1.0.0.tar.gz
  • Upload date:
  • Size: 46.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyrocrail-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c159aca4ba3aac475a91f204781b14face3f1bdcafc2ae0abcd0ad04a16ec1ee
MD5 7f0aa678945d783fb15591340d74c659
BLAKE2b-256 c008c42bf3e15e32e52de3ad2e0d49aa5d594190ad80dcb091a122f7dba1db6c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrocrail-1.0.0.tar.gz:

Publisher: publish.yml on thetrainroom/py-rocrail

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

File details

Details for the file pyrocrail-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyrocrail-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 56.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyrocrail-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fe5090806ec78e9421a5cb2146ada3796748c0825cf35558108f2ac229301c23
MD5 fa0b9307a7f53cd207fc31ff2a5f2306
BLAKE2b-256 08dae4cb599198fa6dc7cbad9aba3feec9819f67c9f3af3014794beadac79864

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrocrail-1.0.0-py3-none-any.whl:

Publisher: publish.yml on thetrainroom/py-rocrail

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 Pingdom Monitoring Sentry Error logging StatusPage Status page