The front right mover is on DMX 1 and the front left on 45, so M1 and M2 swap addresses in the stage map and test.qxw. Measured on the rig, pan 0 faces forward on the front pair and into the middle on the back pair, back at 85. M1 and M4 turn anticlockwise, so movers get a pan_reverse flag. Tilt 127 now points straight down: the simulator draws a spot under the head, and past 127 the beam flips behind the pan heading. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
287 lines
9.4 KiB
Python
287 lines
9.4 KiB
Python
"""Load a stage map and patch it from a QLC+ workspace."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import re
|
|
import xml.etree.ElementTree as ET
|
|
from dataclasses import dataclass, field
|
|
from pathlib import Path
|
|
|
|
_NUMBER = re.compile(r"#\s*0*(\d+)")
|
|
|
|
|
|
@dataclass
|
|
class FixtureDef:
|
|
name: str
|
|
manufacturer: str
|
|
model: str
|
|
mode: str
|
|
channels: list[str]
|
|
|
|
|
|
@dataclass
|
|
class Fixture:
|
|
id: str
|
|
label: str
|
|
kind: str
|
|
x: float
|
|
y: float
|
|
universe: int | None = None
|
|
address: int | None = None # 0-based, same as the QLC+ workspace file
|
|
channels: int = 0
|
|
mode: str = ""
|
|
name: str = ""
|
|
definition: str = ""
|
|
channel_names: list[str] = field(default_factory=list)
|
|
direction: float = 0.0 # cans: beam heading, degrees clockwise from +Y
|
|
zero: float = 0.0 # movers: heading at pan centre, same degrees
|
|
pan_reverse: bool = False # movers: rising pan turns the head anticlockwise
|
|
|
|
@property
|
|
def dmx(self) -> int | None:
|
|
"""1-based DMX start address."""
|
|
if self.address is None:
|
|
return None
|
|
return self.address + 1
|
|
|
|
def to_json(self) -> dict:
|
|
return {
|
|
"id": self.id,
|
|
"label": self.label,
|
|
"kind": self.kind,
|
|
"x": self.x,
|
|
"y": self.y,
|
|
"universe": self.universe,
|
|
"address": self.address,
|
|
"dmx": self.dmx,
|
|
"channels": self.channels,
|
|
"mode": self.mode,
|
|
"name": self.name or self.label,
|
|
"def": self.definition,
|
|
"channel_names": self.channel_names,
|
|
"direction": self.direction,
|
|
"zero": self.zero,
|
|
"pan_reverse": self.pan_reverse,
|
|
}
|
|
|
|
|
|
@dataclass
|
|
class Stage:
|
|
building_width: float
|
|
origin: str
|
|
fixtures: list[Fixture]
|
|
name: str = "xchc"
|
|
show: str | None = None
|
|
output_ip: str | None = None
|
|
path: Path | None = None
|
|
warnings: list[str] = field(default_factory=list)
|
|
|
|
def to_json(self) -> dict:
|
|
return {
|
|
"name": self.name,
|
|
"building_width": self.building_width,
|
|
"origin": self.origin,
|
|
"show": self.show,
|
|
"output_ip": self.output_ip,
|
|
"fixtures": [fixture.to_json() for fixture in self.fixtures],
|
|
"warnings": self.warnings,
|
|
}
|
|
|
|
|
|
def _local(tag: str) -> str:
|
|
return tag.rsplit("}", 1)[-1]
|
|
|
|
|
|
def _text(element: ET.Element, name: str, default: str = "") -> str:
|
|
for child in element:
|
|
if _local(child.tag) == name and child.text:
|
|
return child.text.strip()
|
|
return default
|
|
|
|
|
|
def _definitions(data: dict) -> dict[str, FixtureDef]:
|
|
found: dict[str, FixtureDef] = {}
|
|
for name, raw in data.get("definitions", {}).items():
|
|
found[str(name)] = FixtureDef(
|
|
name=str(name),
|
|
manufacturer=str(raw.get("manufacturer") or ""),
|
|
model=str(raw.get("model") or ""),
|
|
mode=str(raw.get("mode") or ""),
|
|
channels=[str(channel) for channel in raw.get("channels") or []],
|
|
)
|
|
return found
|
|
|
|
|
|
def load_stage(path: Path) -> Stage:
|
|
data = json.loads(path.read_text())
|
|
definitions = _definitions(data)
|
|
fixtures: list[Fixture] = []
|
|
for key, raw in data.get("fixtures", {}).items():
|
|
kind = raw.get("kind", "can")
|
|
if raw.get("address") is not None:
|
|
address = int(raw["address"])
|
|
elif raw.get("dmx") is not None:
|
|
address = int(raw["dmx"]) - 1
|
|
else:
|
|
address = None
|
|
definition = definitions.get(str(raw.get("def") or ""))
|
|
channel_names = list(definition.channels) if definition else []
|
|
channels = int(raw.get("channels") or 0) or len(channel_names)
|
|
if address is not None and channels == 0:
|
|
channels = 43 if kind == "mover" else 8
|
|
fixtures.append(
|
|
Fixture(
|
|
id=str(key),
|
|
label=str(raw.get("label", key)),
|
|
kind=kind,
|
|
x=float(raw["x"]),
|
|
y=float(raw["y"]),
|
|
universe=None if raw.get("universe") is None else int(raw["universe"]),
|
|
address=None if address is None else int(address),
|
|
channels=channels,
|
|
mode=str(raw.get("mode") or (definition.mode if definition else "")),
|
|
name=str(raw.get("name") or raw.get("label", key)),
|
|
definition=definition.name if definition else str(raw.get("def") or ""),
|
|
channel_names=channel_names,
|
|
direction=float(raw.get("direction") or 0),
|
|
zero=float(raw.get("zero") or 0),
|
|
pan_reverse=bool(raw.get("pan_reverse", False)),
|
|
)
|
|
)
|
|
return Stage(
|
|
building_width=float(data.get("building_width", 7.0)),
|
|
origin=str(data.get("origin", "")),
|
|
fixtures=fixtures,
|
|
name=str(data.get("name") or "xchc"),
|
|
path=Path(path),
|
|
)
|
|
|
|
|
|
def wrap_degrees(degrees: float) -> int:
|
|
"""Nearest degree in -179..180. 0 points toward +Y."""
|
|
return (round(float(degrees)) + 180) % 360 - 180
|
|
|
|
|
|
def set_heading(stage: Stage, fixture_id: str, *, direction: float | None = None, zero: float | None = None) -> Fixture:
|
|
"""Point a can's direction, or a mover's zero, and store it in the stage file."""
|
|
fixture = next((item for item in stage.fixtures if item.id == fixture_id), None)
|
|
if fixture is None:
|
|
raise KeyError(fixture_id)
|
|
if fixture.kind == "can":
|
|
if direction is None:
|
|
raise ValueError("direction required")
|
|
fixture.direction = wrap_degrees(direction)
|
|
_write_number(stage, fixture_id, "direction", fixture.direction)
|
|
else:
|
|
if zero is None:
|
|
raise ValueError("zero required")
|
|
fixture.zero = wrap_degrees(zero)
|
|
_write_number(stage, fixture_id, "zero", fixture.zero)
|
|
return fixture
|
|
|
|
|
|
def _write_number(stage: Stage, fixture_id: str, field_name: str, value: int) -> None:
|
|
if stage.path is None or not stage.path.is_file():
|
|
return
|
|
text = stage.path.read_text()
|
|
marker = f'"{fixture_id}": {{'
|
|
start = text.find(marker)
|
|
if start < 0:
|
|
return
|
|
end = text.find("\n }", start)
|
|
if end < 0:
|
|
return
|
|
block = text[start:end]
|
|
updated, count = re.subn(
|
|
rf'("{re.escape(field_name)}":\s*)-?\d+(?:\.\d+)?',
|
|
rf"\g<1>{value}",
|
|
block,
|
|
count=1,
|
|
)
|
|
if count == 0:
|
|
updated = block.rstrip() + f',\n "{field_name}": {value}'
|
|
stage.path.write_text(text[:start] + updated + text[end:])
|
|
|
|
|
|
def _qxw_fixtures(path: Path) -> tuple[list[dict], str | None]:
|
|
root = ET.parse(path).getroot()
|
|
output_ip = None
|
|
fixtures: list[dict] = []
|
|
for element in root.iter():
|
|
name = _local(element.tag)
|
|
if name == "PluginParameters" and output_ip is None:
|
|
output_ip = element.attrib.get("outputIP") or None
|
|
if name != "Engine":
|
|
continue
|
|
for child in element:
|
|
if _local(child.tag) != "Fixture":
|
|
continue
|
|
fixtures.append(
|
|
{
|
|
"name": _text(child, "Name"),
|
|
"mode": _text(child, "Mode"),
|
|
"model": _text(child, "Model"),
|
|
"manufacturer": _text(child, "Manufacturer"),
|
|
"universe": int(_text(child, "Universe", "0")),
|
|
"address": int(_text(child, "Address", "0")),
|
|
"channels": int(_text(child, "Channels", "0")),
|
|
}
|
|
)
|
|
return fixtures, output_ip
|
|
|
|
|
|
def _stage_id_for(patched: dict, stage_ids: set[str]) -> str | None:
|
|
name = patched["name"]
|
|
if name in stage_ids:
|
|
return name
|
|
match = _NUMBER.search(name)
|
|
number = int(match.group(1)) if match else None
|
|
blob = f"{patched['manufacturer']} {patched['model']} {name}".lower()
|
|
mover = "mover" in blob or "small eye" in blob
|
|
can = "bcc145" in blob or blob.strip().endswith("can")
|
|
if number is None:
|
|
lowered = name.lower()
|
|
if "front left" in lowered and "M1" in stage_ids:
|
|
return "M1"
|
|
if "front right" in lowered and "M2" in stage_ids:
|
|
return "M2"
|
|
if "back left" in lowered and "M3" in stage_ids:
|
|
return "M3"
|
|
if "back right" in lowered and "M4" in stage_ids:
|
|
return "M4"
|
|
return None
|
|
if mover:
|
|
key = f"M{number}"
|
|
if key in stage_ids:
|
|
return key
|
|
if can:
|
|
key = f"#{number}"
|
|
if key in stage_ids:
|
|
return key
|
|
return None
|
|
|
|
|
|
def apply_show(stage: Stage, show_path: Path) -> None:
|
|
"""Fill missing DMX addresses from a QLC+ workspace."""
|
|
patched, output_ip = _qxw_fixtures(show_path)
|
|
stage.show = show_path.name
|
|
stage.output_ip = output_ip
|
|
by_id = {fixture.id: fixture for fixture in stage.fixtures}
|
|
used: set[str] = set()
|
|
for item in patched:
|
|
key = _stage_id_for(item, set(by_id))
|
|
if key is None or key in used:
|
|
continue
|
|
fixture = by_id[key]
|
|
if fixture.address is None:
|
|
fixture.universe = item["universe"]
|
|
fixture.address = item["address"]
|
|
fixture.channels = item["channels"] or fixture.channels
|
|
fixture.mode = item["mode"]
|
|
fixture.name = item["name"]
|
|
used.add(key)
|
|
for fixture in stage.fixtures:
|
|
if fixture.address is None:
|
|
stage.warnings.append(f"{fixture.label} has no DMX address")
|