simulation-generation/generate_scene.py

96 lines
2.4 KiB
Python

import csv
import math
from pathlib import Path
# -----------------------
# CONFIG
# -----------------------
CSV_PATH = "conveyors.csv"
OUT_TSCN = "generated_conveyors.tscn"
SCALE = 0.0254 # AutoCAD units → Godot units
FIXED_Y = 2.5
BELT_RESOURCE_ID = "3_38ygf" # BeltConveyorAssembly.tscn
# -----------------------
# HELPERS
# -----------------------
def transform_from_points(x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
length = math.hypot(dx, dy) * SCALE
angle = math.atan2(dy, dx)
mid_x = (x1 + x2) / 2 * SCALE
mid_z = (y1 + y2) / 2 * SCALE
return {
"length": length,
"pos": (mid_x, FIXED_Y, mid_z),
"rot_y": -angle
}
def transform3d(rot_y, x, y, z):
c = math.cos(rot_y)
s = math.sin(rot_y)
return (
f"Transform3D({c}, 0, {s}, "
f"0, 1, 0, "
f"{-s}, 0, {c}, "
f"{x}, {y}, {z})"
)
# -----------------------
# READ CSV
# -----------------------
conveyors = []
with open(CSV_PATH, newline="") as f:
reader = csv.DictReader(f)
for row in reader:
conv = transform_from_points(
float(row["start_x"]),
float(row["start_y"]),
float(row["end_x"]),
float(row["end_y"]),
)
conv["name"] = row["conveyor_key"]
conveyors.append(conv)
# -----------------------
# WRITE TSCN
# -----------------------
lines = []
lines.append('[gd_scene load_steps=2 format=3]')
lines.append('')
lines.append(
'[ext_resource type="PackedScene" path="res://parts/assemblies/BeltConveyorAssembly.tscn" id="3_38ygf"]'
)
lines.append('')
lines.append('[node name="GeneratedConveyors" type="Node3D"]')
lines.append('')
for i, c in enumerate(conveyors, start=1):
x, y, z = c["pos"]
t = transform3d(c["rot_y"], x, y, z)
lines.append(
f'[node name="{c["name"]}" parent="." instance=ExtResource("{BELT_RESOURCE_ID}")]'
)
lines.append(f"transform = {t}")
lines.append("right_side_guards_enabled = false")
lines.append("left_side_guards_enabled = false")
lines.append("head_end_leg_enabled = false")
lines.append("tail_end_leg_enabled = false")
lines.append("enable_comms = true")
lines.append(f'speed_tag_name = "{c["name"]}_OIP"')
lines.append(
f"size = Vector3({c['length']:.6f}, 0.5, 1.524)"
)
lines.append("")
Path(OUT_TSCN).write_text("\n".join(lines))
print(f"Generated: {OUT_TSCN}")