import math, os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import v4lib as v4

v4.reset()
# Palette: contemporary airport equipment
paint = v4.painted('warm_light_grey_powdercoat', (0.72, 0.74, 0.73), roughness=0.28)
white = v4.painted('ivory_panel', (0.88, 0.88, 0.84), roughness=0.3)
navy = v4.painted('deep_navy_skirt', (0.055, 0.12, 0.20), roughness=0.28)
blue = v4.painted('blue_service_panel', (0.20, 0.34, 0.44), roughness=0.35)
steel = v4.metal('galvanized_steel', (0.63, 0.68, 0.69), roughness=0.3)
darksteel = v4.metal('dark_coated_steel', (0.20, 0.24, 0.26), roughness=0.4)
alum = v4.metal('bright_aluminum', (0.82, 0.84, 0.83), roughness=0.22)
rubber = v4.rubber('tire_rubber', (0.045, 0.052, 0.055))
black = v4.rubber('black_bellows', (0.10, 0.12, 0.13))
glass = v4.glass('smoke_blue_glass', (0.35, 0.52, 0.59), roughness=0.16)
yellow = v4.painted('safety_yellow', (0.98, 0.65, 0.05), roughness=0.32)
red = v4.painted('signal_red', (0.75, 0.08, 0.035), roughness=0.24)
green_led = v4.emissive('green_status_led', (0.15, 0.9, 0.25), strength=2.5)
white_led = v4.emissive('white_worklight', (0.85, 0.9, 1.0), strength=2.0)

# Shared construction helpers
def box(body, name, size, loc, material=paint, bevel=0.025, rot=(0,0,0)):
    if body.get('v4_name') == 'bogie_frame':
        loc=(loc[0]+2.0,loc[1],loc[2])
    elif body.get('v4_name') == 'cab_leveler':
        loc=(loc[0]+2.20,loc[1],loc[2])
    return v4.box(name, size, loc, rot=rot, bevel=bevel, material=material, body=body)
def cyl(body, name, radius, depth, loc, material=steel, rot=(0,0,0), bevel=0):
    if body.get('v4_name') == 'bogie_frame':
        loc=(loc[0]+2.0,loc[1],loc[2])
    elif body.get('v4_name') == 'cab_leveler':
        loc=(loc[0]+2.20,loc[1],loc[2])
    return v4.cylinder(name, radius, depth, loc, rot=rot, material=material, body=body, segments=32)
def beam(body, name, a, b, r, material=steel):
    if body.get('v4_name') == 'bogie_frame':
        a=(a[0]+2.0,a[1],a[2]); b=(b[0]+2.0,b[1],b[2])
    return v4.rod(name, a, b, r, material=material, body=body, segments=16)
def tunnel_shell(body, prefix, x0, x1, width, zfloor, height, wallmat, roofmat, window=True):
    # Open-frame rectangular shell: floor, roof and side panels leave nested stages clear.
    L=x1-x0; xc=(x0+x1)/2; zc=zfloor+height/2; t=0.12
    # The last 2.5 m is an open swivel/receiver bay so the cab can yaw without
    # sweeping its rear wall through tunnel side panels.
    side_x1=min(x1,21.8) if prefix.startswith(('outer_telescoping','inner_telescoping')) else x1
    sideL=side_x1-x0; sidexc=(x0+side_x1)/2
    box(body,prefix+'_floor',(L,width,zfloor*0+0.16),(xc,0,zfloor-0.08),navy,0.035)
    box(body,prefix+'_roof',(L,width,0.18),(xc,0,zfloor+height-0.09),roofmat,0.04)
    for s in (-1,1):
        box(body,prefix+f'_side_{s}',(sideL,0.14,height+0.02),(sidexc,s*(width/2-0.07),zc),wallmat,0.025)
        # broad continuous glazed clerestory bands on both sides, framed by panel rails
        if window:
            # Put the glazing on the exterior face so it reads as a window band.
            box(body,prefix+f'_glazing_{s}',(sideL-0.6,0.035,0.92),(sidexc,s*(width/2+0.018),zc+0.30),glass,0.012)
            for k in range(1,8):
                x=x0+0.34+k*(sideL-0.6)/8
                box(body,prefix+f'_mullion_{s}_{k}',(0.045,0.055,0.96),(x,s*(width/2+0.036),zc+0.30),alum,0.008)
            # Horizontal glazing rails and repeated panel seams break up the long slab.
            for dz in (-0.18,0.78):
                box(body,prefix+f'_window_rail_{s}_{dz}',(sideL-0.46,0.065,0.045),(sidexc,s*(width/2+0.038),zc+dz),alum,0.009)
            for k in range(1,max(2,int(sideL/1.45))):
                x=x0+k*sideL/max(2,int(sideL/1.45))
                box(body,prefix+f'_panel_seam_{s}_{k}',(0.028,0.045,height-0.38),(x,s*(width/2+0.024),zc),steel,0.008)
            for k in range(1,int(sideL/0.85)):
                x=x0+0.42+k*0.85
                for z in (zfloor+0.78,zfloor+2.72):
                    v4.cylinder(f'{prefix}_panel_fastener_{s}_{k}_{z}',0.022,0.035,(x,s*(width/2+0.018),z),rot=(math.pi/2,0,0),material=alum,body=body,segments=12)
        # lower navy apron is a separate color band, inset from the glazing
        box(body,prefix+f'_lower_band_{s}',(sideL-0.12,0.045,0.46),(sidexc,s*(width/2-0.151),zfloor+0.40),navy,0.012)
        # corner uprights and horizontal belt rails
        for xx in (x0+0.08,side_x1-0.08):
            box(body,prefix+f'_end_post_{s}_{xx:.1f}',(0.14,0.15,height-0.16),(xx,s*(width/2-0.06),zc),darksteel,0.02)
        box(body,prefix+f'_belt_{s}',(sideL,0.055,0.065),(sidexc,s*(width/2+0.005),zfloor+1.02),alum,0.012)
    # Roof cap ribs mark each modular panel without cutting through the roof.
    for k in range(1, int(L/1.6)):
        x=x0+k*L/int(L/1.6)
        box(body,prefix+f'_roof_seam_{k}',(0.035,width-0.12,0.025),(x,0,zfloor+height-0.18),steel,0.008)
    # continuous interior handrails and passenger-side skirting
    for s in (-1,1):
        rail_end=x1-3.5 if prefix.startswith(('outer_telescoping','inner_telescoping')) else x1-0.2
        beam(body,prefix+f'handrail_{s}',(x0+0.2,s*(width/2-0.30),zfloor+0.92),(rail_end,s*(width/2-0.30),zfloor+0.92),0.025,alum)
        for xx in (x0+0.8,(x0+rail_end)/2,rail_end-0.8):
            beam(body,prefix+f'rail_stanchion_{s}_{xx:.1f}',(xx,s*(width/2-0.30),zfloor+0.92),(xx,s*(width/2-0.12),zfloor+0.92),0.025,alum)

fixed=v4.body('fixed_tunnel')
# Broad terminal foundation and paired steel support columns.
box(fixed,'terminal_plinth',(3.8,4.4,0.30),(2.0,0,0.15),darksteel,0.07)
for sy in (-1,1):
    box(fixed,f'terminal_column_{sy}',(0.52,0.52,4.72),(3.05,sy*1.22,2.66),paint,0.06)
    box(fixed,f'column_base_collar_{sy}',(0.72,0.72,0.36),(3.05,sy*1.22,0.48),navy,0.06)
    box(fixed,f'column_head_{sy}',(0.82,0.82,0.26),(3.05,sy*1.22,4.91),steel,0.05)
# terminal fixed connector spans the support portal
# fixed short section bridges terminal tower to rotunda
# tunnel floor elevation 5.15m, crown 8.15m
tunnel_shell(fixed,'terminal_fixed_tunnel',-1.4,6.90,3.42,5.18,3.02,paint,white,True)
# Cross-bracing makes the terminal support portal read as a load-bearing frame.
v4.rod('terminal_portal_x_brace_a',(3.05,-0.96,0.72),(3.05,0.96,4.54),0.055,material=steel,body=fixed,segments=20)
v4.rod('terminal_portal_x_brace_b',(3.05,0.96,0.72),(3.05,-0.96,4.54),0.055,material=steel,body=fixed,segments=20)
for sy in (-1,1):
    for dx in (-0.23,0.23):
        for dy in (-0.23,0.23):
            cyl(fixed,f'column_anchor_bolt_{sy}_{dx}_{dy}',0.035,0.07,(3.05+dx,sy*1.22+dy,0.68),alum)
# landing/boarding interface at terminal building, with accordion weather seal
box(fixed,'terminal_wall_interface',(0.45,3.75,3.50),(-1.48,0,6.72),white,0.045)
box(fixed,'terminal_dark_doorway',(0.10,2.35,2.35),(-1.22,0,6.45),black,0.02)
box(fixed,'terminal_threshold',(0.80,2.55,0.12),(-0.95,0,5.22),alum,0.025)
# Rotunda lower bearing races and a full-height pedestal column.
cyl(fixed,'rotunda_pedestal_column',0.72,5.18,(8.50,0,2.59),navy)
box(fixed,'rotunda_pedestal_base_plate',(1.85,1.85,0.20),(8.50,0,0.10),darksteel,0.06)
for k in range(8):
    a=2*math.pi*k/8
    cyl(fixed,f'rotunda_base_anchor_{k}',0.04,0.08,(8.50+0.69*math.cos(a),0.69*math.sin(a),0.25),alum)
# Turntable slewing race directly under the rotating drum.
cyl(fixed,'rotunda_slewing_ring_base',1.55,0.12,(8.50,0,5.12),darksteel,bevel=0.025)
v4.torus('rotunda_slewing_race',1.67,0.05,(8.50,0,5.18),material=alum,body=fixed)
for k in range(12):
    a=2*math.pi*k/12
    cyl(fixed,f'rotunda_slewing_bolt_{k}',0.035,0.08,(8.50+1.66*math.cos(a),1.66*math.sin(a),5.22),alum)
# Low bearing shelf joins the end of the fixed tunnel to the rotunda pedestal.
box(fixed,'rotunda_bearing_shelf',(2.30,3.10,0.12),(7.65,0,5.12),steel,0.03)

rot=v4.body('rotunda_pivot')
# rotating drum and bridge outer stage
cyl(rot,'rotunda_drum',1.60,3.02,(8.50,0,6.69),paint)
for z in (5.235,8.145):
    v4.torus('rotunda_metal_race_'+str(z),1.655,0.055,(8.50,0,z),material=alum,body=rot)
tunnel_shell(rot,'outer_telescoping_tunnel',10.10,19.10,3.48,5.18,3.38,paint,white,True)
# Reinforced receiver lip at the open end shows the stepped overlap with the inner stage.
for s in (-1,1):
    box(rot,f'outer_receiver_guide_{s}',(1.65,0.10,0.08),(18.15,s*1.62,8.23),darksteel,0.02)
for z in (8.25,):
    box(rot,f'outer_overlap_lip_{z}',(0.14,3.42,0.12),(19.03,0,z),steel,0.025)
# Roof service module with twin screened fans and lift-off covers.
box(rot,'outer_roof_hvac',(1.55,1.18,0.28),(14.2,-0.15,8.34),blue,0.07)
for x in (13.85,14.55):
    cyl(rot,f'outer_roof_hvac_fan_{x}',0.28,0.06,(x,-0.15,8.51),darksteel,bevel=0.018)
    cyl(rot,f'outer_roof_hvac_fan_grille_{x}',0.20,0.025,(x,-0.15,8.55),alum,bevel=0.01)
for s in (-1,1):
    box(rot,f'outer_roof_hvac_louver_{s}',(0.72,0.035,0.16),(14.2,-0.15+s*0.5725,8.35),steel,0.01)
# rotunda cover shoulders and operator service hatches
for s in (-1,1):
    box(rot,f'rotunda_cheek_{s}',(1.60,0.14,2.20),(9.30,s*1.67,6.75),blue,0.025)
    box(rot,f'outer_access_hatch_{s}',(0.82,0.035,0.62),(12.35,s*1.59,6.12),white,0.03)
    for j in range(4):
        box(rot,f'outer_hatch_louver_{s}_{j}',(0.54,0.02,0.035),(12.35,s*1.615,6.00+j*0.09),darksteel,0.005)
# roof-mounted travel lights and warning beacon at outer end
for s in (-1,1):
    box(rot,f'rotunda_marker_{s}',(0.24,0.20,0.16),(10.35,s*1.25,8.64),yellow,0.035)
    cyl(rot,f'outer_status_lamp_{s}',0.07,0.10,(16.55,s*1.20,8.61),green_led)

inner=v4.body('telescope_inner')
tunnel_shell(inner,'inner_telescoping_tunnel',11.50,24.50,2.95,5.18,2.98,white,paint,True)
# Rotating collar runs on a low annular bearing race fixed to the inner stage.
v4.torus('inner_cab_swivel_bearing_race',1.22,0.07,(22.30,0,5.245),material=alum,body=inner)
for s in (-1,1):
    # guide rails and roller shoes sit just outside the inner stage shell
    box(inner,f'inner_guide_rail_{s}',(7.50,0.075,0.11),(15.25,s*1.48,5.39),darksteel,0.025)
    box(inner,f'inner_end_cap_{s}',(0.18,0.16,2.65),(21.72,s*1.48,6.61),navy,0.025)
    box(inner,f'inner_hatch_{s}',(1.00,0.035,0.60),(19.30,s*1.405,6.16),blue,0.025)
    for j in range(4):
        box(inner,f'inner_louver_{s}_{j}',(0.72,0.02,0.035),(19.30,s*1.43,5.99+j*0.09),steel,0.004)
# small cable carrier/trough along roof edge
box(inner,'inner_roof_cable_tray',(7.6,0.22,0.14),(20.25,-1.10,8.16),darksteel,0.025)
for x in (16.8,18.0,19.2,20.4,21.6,22.8,24.0):
    box(inner,f'cable_tray_clip_{x}',(0.06,0.26,0.18),(x,-1.10,8.12),yellow,0.012)

bogie=v4.body('bogie_frame')
# Ground-running bogie is a separate branch; its carriage is shifted +2 m to sit under the lift.
box(bogie,'bogie_crossbeam',(2.35,2.35,0.34),(20.75,0,1.47),darksteel,0.06)
box(bogie,'bogie_top_deck',(2.15,1.85,0.18),(20.75,0,1.73),steel,0.045)
for side in (-1,1):
    box(bogie,f'bogie_sideframe_{side}',(2.20,0.20,0.64),(20.75,side*0.84,1.34),navy,0.045)
    box(bogie,f'bogie_reflector_{side}',(1.65,0.035,0.12),(20.75,side*1.075,1.47),yellow,0.02)
for x in (19.62,21.88):
    box(bogie,f'bogie_bumper_{x}',(0.18,2.32,0.28),(x,0,1.34),yellow,0.04)
# Two axle carriers at each tandem station, aligned below the lift mast.
for x in (20.10,21.35):
    for side in (-1,1):
        beam(bogie,f'wheel_carrier_{x}_{side}',(x,side*0.82,1.02),(x,side*0.86,0.66),0.06,steel)
        v4.cylinder(f'wheel_axle_pin_{x}_{side}',0.08,0.10,(x+2.0,side*0.92,0.66),rot=(math.pi/2,0,0),material=steel,body=bogie,segments=24)
# Paired hydraulic barrel bodies sit on a crossbeam above the tandem bogie.
box(bogie,'twin_ram_crossbeam',(1.25,1.40,0.20),(20.75,0,2.18),steel,0.05)
for side in (-1,1):
    cyl(bogie,f'twin_lift_ram_barrel_{side}',0.18,1.58,(20.75,side*0.48,3.04),darksteel,bevel=0.025)
    cyl(bogie,f'twin_lift_ram_gland_{side}',0.22,0.16,(20.75,side*0.48,3.89),alum,bevel=0.018)
    cyl(bogie,f'twin_lift_hose_coupler_{side}',0.045,0.42,(20.75,side*0.66,3.12),yellow,rot=(math.pi/2,0,0))
box(bogie,'bogie_drive_motor',(0.92,0.62,0.50),(19.72,-0.66,2.07),navy,0.09)
cyl(bogie,'bogie_motor_end_cap',0.22,0.07,(20.18,-0.66,2.07),alum,rot=(0,math.pi/2,0),bevel=0.015)
for i in range(5):
    box(bogie,f'bogie_motor_vent_{i}',(0.58,0.035,0.035),(19.72,-0.985,1.91+i*0.075),alum,0.008)
# Lower receiver stays on the ground-running bogie while the nested upper sleeve lifts.
box(bogie,'lift_base_socket',(0.95,0.85,0.46),(20.75,0,1.95),darksteel,0.08)
for side in (-1,1):
    cyl(bogie,f'lift_trunnion_{side}',0.18,0.22,(20.75,side*0.51,2.0),steel,rot=(math.pi/2,0,0))
for ix,dx in enumerate((-0.27,0.27)):
    box(bogie,f'mast_lower_guide_x_{ix}',(0.10,0.62,1.20),(20.75+dx,0,2.75),paint,0.0)
for iy,dy in enumerate((-0.27,0.27)):
    box(bogie,f'mast_lower_guide_y_{iy}',(0.44,0.10,1.20),(20.75,dy,2.75),paint,0.0)

# Moving upper sleeve rides vertically from the grounded receiver and pivots under the tunnel.
lift=v4.body('lift_column')
box(lift,'mast_upper_sleeve',(0.44,0.44,1.62),(22.75,0,4.16),paint,0.0)
box(lift,'mast_front_guard',(0.30,0.035,1.42),(22.75,-0.18,4.16),navy,0.018)
cyl(lift,'hydraulic_ram_rod',0.135,1.50,(22.75,0,4.21),alum)
for side in (-1,1):
    cyl(lift,f'twin_lift_ram_rod_{side}',0.095,1.26,(22.75,side*0.48,4.32),alum,bevel=0.012)
    cyl(lift,f'twin_lift_rod_clevis_{side}',0.15,0.12,(22.75,side*0.48,5.01),darksteel,bevel=0.02)
for z in (4.87,): cyl(lift,f'upper_sleeve_collar_{z}',0.235,0.10,(22.75,0,z),darksteel)
box(lift,'tunnel_support_saddle',(2.10,1.35,0.22),(22.75,0,4.99),steel,0.06)
box(lift,'lift_saddle',(0.80,1.20,0.10),(22.75,0,5.05),yellow,0.015)

cab=v4.body('cab_leveler')
# Aircraft-side cab is a separate yawing vestibule at the telescoping-tunnel interface.
# Circular rotunda trim, symmetric about the yaw axis, carries an accordion-like interface.
for z in (5.34,5.62,5.98,6.34,6.70,7.06,7.42,7.78):
    v4.torus(f'cab_rotunda_bellows_rib_{z:.2f}',1.27,0.045,(22.30,0,z),rot=(0,0,0),material=black,body=cab)
for k in range(16):
    ang=2*math.pi*k/16
    x=22.30+1.27*math.cos(ang); y=1.27*math.sin(ang)
    v4.cylinder(f'cab_rotunda_bellows_pleat_{k}',0.035,2.50,(x,y,6.62),material=black,body=cab,segments=12)
for k in range(32):
    ang=2*math.pi*k/32
    x=22.30+1.27*math.cos(ang); y=1.27*math.sin(ang)
    v4.box(f'cab_rotunda_bellows_fabric_{k}',(0.07,0.235,2.50),(x,y,6.62),rot=(0,0,ang),material=black,body=cab)
box(cab,'cab_floor',(3.05,3.02,0.18),(23.93,0,5.30),navy,0.045)
# Cap the cab yaw collar at the roofline. It belongs to the cab body so it turns with the swivel.
v4.cylinder('cab_rotunda_roof_disc',1.32,0.10,(22.30,0,7.85),material=white,body=cab,segments=64)
# Short raised deck links the yaw collar to the forward vestibule floor.
v4.box('cab_swivel_connector_deck',(1.80,2.35,0.18),(23.85,0,5.30),material=navy,body=cab,bevel=0.035)
# The main roof begins at the cap's forward edge, avoiding coplanar overlap.
box(cab,'cab_roof',(1.80,2.90,0.16),(24.21,0,7.82),white,0.05)
for side in (-1,1):
    box(cab,f'cab_side_lower_{side}',(2.52,0.15,1.25),(23.83,side*1.39,5.93),paint,0.035)
    box(cab,f'cab_side_window_{side}',(2.28,0.05,1.05),(23.83,side*1.31,7.05),glass,0.018)
    for xx in (22.75,23.85,24.85):
        box(cab,f'cab_window_post_{side}_{xx}',(0.08,0.13,1.16),(xx,side*1.34,7.04),alum,0.012)
    box(cab,f'cab_bumper_side_{side}',(0.18,0.17,1.30),(25.10,side*1.38,5.92),yellow,0.035)
box(cab,'cab_rear_wall',(0.15,2.72,2.38),(22.57,0,6.49),paint,0.03)
box(cab,'cab_rear_glazing',(0.05,2.20,0.95),(22.47,0,7.15),glass,0.01)
# Ribbed accordion canopy at the aircraft face: narrow transverse pleats, rather than flat slabs.
for side in (-1,1):
    for i in range(7):
        x=24.68+i*0.065
        box(cab,f'front_bellows_side_pleat_{side}_{i}',(0.045,0.11,1.86),(x,side*1.38,6.48),black,0.012)
for i in range(7):
    x=24.68+i*0.065
    box(cab,f'front_bellows_roof_pleat_{i}',(0.045,2.55,0.07),(x,0,7.85),black,0.012)
box(cab,'cab_front_bumper',(0.24,2.75,2.10),(25.20,0,6.55),rubber,0.09)
box(cab,'leveling_sill',(0.72,2.60,0.12),(22.82,0,5.34),alum,0.025)
box(cab,'cab_floor_wear_strip',(0.62,2.72,0.025),(22.84,0,5.402),yellow,0.008)
box(cab,'cab_id_panel',(0.12,0.90,0.32),(24.65,-1.48,7.58),blue,0.02)
box(cab,'operator_console',(0.24,0.48,0.40),(22.72,-1.05,6.05),darksteel,0.03)
box(cab,'console_screen',(0.03,0.32,0.18),(22.57,-1.05,6.14),green_led,0.01)
box(cab,'operator_console_glazing',(0.05,0.80,0.52),(23.18,-1.48,6.72),glass,0.02)
box(cab,'cab_service_door',(0.04,0.48,0.72),(24.00,-1.49,6.00),blue,0.02)
for z in (5.77,5.96,6.15,6.34):
    box(cab,f'cab_service_door_louver_{z}',(0.025,0.30,0.035),(24.03,-1.52,z),darksteel,0.007)
for x in (23.82,24.18):
    cyl(cab,f'cab_service_door_fastener_{x}',0.025,0.03,(x,-1.48,6.32),alum,rot=(math.pi/2,0,0))

# Fixed-to-cab maintenance stair, with open treads and matching twin handrails.
stair_x=26.35
for i in range(10):
    sy=-1.72-i*0.45
    sz=5.08-i*0.48
    v4.box(f'cab_service_stair_tread_{i}',(1.02,0.40,0.07),(stair_x,sy,sz),material=steel,body=cab,bevel=0.025)
    v4.box(f'cab_service_stair_nosing_{i}',(1.04,0.045,0.035),(stair_x,sy-0.17,sz+0.035),material=yellow,body=cab,bevel=0.01)
for xx in (stair_x-0.49,stair_x+0.49):
    v4.rod(f'cab_stair_stringer_{xx}',(xx,-1.72,5.045),(xx,-5.77,0.725),0.055,material=darksteel,body=cab,segments=18)
    v4.rod(f'cab_stair_handrail_{xx}',(xx,-1.51,5.82),(xx,-6.08,1.02),0.035,material=alum,body=cab,segments=18)
    for i in range(6):
        sy=-1.9-i*0.72
        sz=4.85-i*0.768
        v4.rod(f'cab_stair_post_{xx}_{i}',(xx,sy,sz),(xx,sy,sz+0.63),0.025,material=steel,body=cab,segments=10)
v4.box('cab_stair_landing_plate',(1.12,0.48,0.20),(stair_x,-1.57,5.20),material=steel,body=cab,bevel=0.025)
for xx in (stair_x-0.34,stair_x+0.34):
    v4.rod(f'cab_stair_landing_bracket_{xx}',(xx,-1.49,5.18),(xx,-1.72,5.08),0.075,material=darksteel,body=cab,segments=12)

# Four independently rolling wheels, tandem on two axle stations and both sides.
wheel_bodies=[]
for side,y in [('left',-1.15),('right',1.15)]:
    for i,x in enumerate((22.10,23.35)):
        w=v4.body(f'wheel_{side}_{i+1}')
        wheel_bodies.append((w,side,i,x,y))
        # thick tire axis Y; wheel meshes all live on their own body
        outward=0.05 if y>0 else -0.05
        hub_out=0.005 if y>0 else -0.005
        cyl(w,f'tire_{side}_{i}',0.53,0.44,(x,y+outward,0.66),rubber,rot=(math.pi/2,0,0))
        cyl(w,f'hub_{side}_{i}',0.29,0.37,(x,y+hub_out,0.66),steel,rot=(math.pi/2,0,0))
        cap_y=y+(0.065 if y>0 else -0.065)
        cyl(w,f'hub_cap_{side}_{i}',0.14,0.39,(x,cap_y,0.66),darksteel,rot=(math.pi/2,0,0))
        face_y=y+(-0.23 if y<0 else 0.23)
        for k in range(8):
            ang=2*math.pi*k/8
            cyl(w,f'lug_{side}_{i}_{k}',0.035,0.025,(x+0.205*math.cos(ang),face_y,0.66+0.205*math.sin(ang)),alum,rot=(math.pi/2,0,0),)
        # Sidewall concentric rings are toroidal details around the hub
        v4.torus(f'tire_sidewall_{side}_{i}',0.40,0.025,(x,face_y,0.66),rot=(math.pi/2,0,0),material=darksteel,body=w)

# Joints; all geometry above is authored at the rest position.
v4.joint('rotunda_pitch_yaw','generic',fixed,rot,origin=(8.50,0,5.20),axis=(0,0,1),xdir=(-1,0,0),dofs=[('rotY',(-math.radians(3),math.radians(8))),('rotZ',(-math.radians(35),math.radians(35)))])
v4.joint('telescope_slide','prismatic',rot,inner,origin=(16.45,0,5.18),axis=(1,0,0),limits=(0,7.0))
# Bogie follows the bridge in plan while its chassis and wheel centers stay at ground height.
v4.joint('bogie_ground_track','generic',fixed,bogie,origin=(22.75,0,1.47),axis=(0,0,1),xdir=(1,0,0),dofs=[('transX',(-5.0,8.5)),('transY',(-9.0,9.0)),('rotZ',(-math.radians(40),math.radians(40)))])
# The lift is an actuated tree link; a pitch hinge at the upper saddle closes the support loop to the tunnel.
v4.joint('column_lift','prismatic',bogie,lift,origin=(22.75,0,1.50),axis=(0,0,1),limits=(-1.00,1.50))
v4.joint('lift_top_hinge','revolute',inner,lift,origin=(22.75,0,4.75),axis=(0,1,0),limits=(-math.radians(15),math.radians(15)))
v4.joint('cab_yaw','revolute',inner,cab,origin=(22.30,0,5.20),axis=(0,0,1),limits=(-math.radians(45),math.radians(30)))
for w,side,i,x,y in wheel_bodies:
    v4.joint(f'wheel_roll_{side}_{i+1}','continuous',bogie,w,origin=(x,y,0.66),axis=(0,1,0))

v4.save(os.path.join(os.getcwd(),'asset.blend'))
print('saved asset.blend')
