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
- Object Reference - Complete documentation for all object types with links to official Rocrail docs
- Testing Strategy - Testing approach and guidelines
- Examples - Tutorial examples and usage patterns
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c159aca4ba3aac475a91f204781b14face3f1bdcafc2ae0abcd0ad04a16ec1ee
|
|
| MD5 |
7f0aa678945d783fb15591340d74c659
|
|
| BLAKE2b-256 |
c008c42bf3e15e32e52de3ad2e0d49aa5d594190ad80dcb091a122f7dba1db6c
|
Provenance
The following attestation bundles were made for pyrocrail-1.0.0.tar.gz:
Publisher:
publish.yml on thetrainroom/py-rocrail
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyrocrail-1.0.0.tar.gz -
Subject digest:
c159aca4ba3aac475a91f204781b14face3f1bdcafc2ae0abcd0ad04a16ec1ee - Sigstore transparency entry: 1172332034
- Sigstore integration time:
-
Permalink:
thetrainroom/py-rocrail@823f15d4a3277bd9559500445504ef2fd0cfa50a -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/thetrainroom
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@823f15d4a3277bd9559500445504ef2fd0cfa50a -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe5090806ec78e9421a5cb2146ada3796748c0825cf35558108f2ac229301c23
|
|
| MD5 |
fa0b9307a7f53cd207fc31ff2a5f2306
|
|
| BLAKE2b-256 |
08dae4cb599198fa6dc7cbad9aba3feec9819f67c9f3af3014794beadac79864
|
Provenance
The following attestation bundles were made for pyrocrail-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on thetrainroom/py-rocrail
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyrocrail-1.0.0-py3-none-any.whl -
Subject digest:
fe5090806ec78e9421a5cb2146ada3796748c0825cf35558108f2ac229301c23 - Sigstore transparency entry: 1172332058
- Sigstore integration time:
-
Permalink:
thetrainroom/py-rocrail@823f15d4a3277bd9559500445504ef2fd0cfa50a -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/thetrainroom
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@823f15d4a3277bd9559500445504ef2fd0cfa50a -
Trigger Event:
push
-
Statement type: