"""Detailed articulated evaporative cooler. Run with Blender: -b --python build.py."""
import math
import os
import sys

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

v4.reset()

# ---- palette -------------------------------------------------------------------------
shell = v4.plastic("warm_ivory_moulded_shell", (0.84, 0.86, 0.82), roughness=0.34, texture=0.09)
shell_light = v4.plastic("soft_white_trim", (0.91, 0.92, 0.89), roughness=0.31, texture=0.07)
trim = v4.plastic("cool_gray_trim", (0.55, 0.60, 0.61), roughness=0.36, texture=0.10)
louvre_mat = v4.plastic("pale_gray_louvre", (0.73, 0.78, 0.78), roughness=0.29, texture=0.08)
duct_mat = v4.plastic("charcoal_air_duct", (0.035, 0.052, 0.057), roughness=0.58, texture=0.12)
rotor_mat = v4.plastic("fan_blade_graphite", (0.17, 0.22, 0.23), roughness=0.34, texture=0.08)
pad_mat = v4.plastic("teal_cellulose_media", (0.14, 0.37, 0.34), roughness=0.91, texture=0.28)
pad_dark = v4.mat("pad_channel_shadow", (0.055, 0.20, 0.19), roughness=0.9)
tank_mat = v4.plastic("blue_gray_reservoir", (0.31, 0.52, 0.56), roughness=0.31, texture=0.1)
tank_light = v4.plastic("reservoir_edge", (0.47, 0.67, 0.69), roughness=0.28, texture=0.08)
water_mat = v4.mat("reservoir_water", (0.12, 0.39, 0.49), roughness=0.2)
steel = v4.metal("satin_zinc_hardware", (0.65, 0.70, 0.70), roughness=0.3, brushed=False)
dark_metal = v4.metal("dark_fan_hardware", (0.19, 0.23, 0.24), roughness=0.34, brushed=False)
rubber = v4.rubber("caster_tire_rubber", (0.025, 0.032, 0.033), roughness=0.82)
hub_mat = v4.plastic("caster_hub_gray", (0.35, 0.39, 0.39), roughness=0.3, texture=0.07)
led = v4.emissive("status_led_aqua", (0.20, 0.85, 0.78), strength=1.8)
ink = v4.mat("control_legend_ink", (0.07, 0.09, 0.09), roughness=0.55)

# ---- envelope and root ----------------------------------------------------------------
W, D = 0.48, 0.36
Z_BOTTOM, Z_TOP = 0.105, 0.79
cab = v4.body("cabinet")

def box(name, size, loc, material=shell, body=cab, bevel=0.002, rot=(0, 0, 0)):
    return v4.box(name, size, loc, rot=rot, bevel=bevel, material=material, body=body)

def cyl(name, radius, depth, loc, material, body=cab, rot=(0, 0, 0), bevel=0.0007, segments=32):
    return v4.cylinder(name, radius, depth, loc, rot=rot, segments=segments, bevel=bevel,
                       material=material, body=body)

# ---- cabinet frame and body shell -------------------------------------------------------
# The broad side and rear openings reveal the three wet pads; the frame surrounds them.
# Corner posts and rails are one moulded body with the top cover and inner chassis.
for sx in (-1, 1):
    for sy in (-1, 1):
        box(f"corner_upright_{sx}_{sy}", (0.028, 0.030, 0.665),
            (sx * 0.226, sy * 0.164, 0.4475), material=shell, bevel=0.008)
    # side-pad top and bottom rails
    box(f"side_pad_top_rail_{sx}", (0.028, 0.322, 0.022),
        (sx * 0.229, 0, 0.731), material=shell_light, bevel=0.006)
    box(f"side_pad_bottom_rail_{sx}", (0.028, 0.322, 0.022),
        (sx * 0.229, 0, 0.309), material=shell, bevel=0.005)
    # Cabinet side sill and upper side trim
    box(f"side_sill_{sx}", (0.032, 0.326, 0.020),
        (sx * 0.224, 0, 0.116), material=shell, bevel=0.005)
    box(f"side_top_moulding_{sx}", (0.034, 0.326, 0.018),
        (sx * 0.224, 0, 0.765), material=shell_light, bevel=0.005)

# Upper side and rear closure bands meet the pad frames and the roof cover.
for sx in (-1, 1):
    box(f"upper_side_closure_{sx}", (0.014, 0.352, 0.032),
        (sx * 0.233, 0, 0.748), material=shell, bevel=0.004)
box("upper_rear_closure", (0.438, 0.016, 0.032), (0, 0.174, 0.748),
    material=shell, bevel=0.004)

# Reinforced pads under the four caster kingpins tie the swivels into the lower sill.
for idx, (sx, sy) in enumerate(((-1, -1), (1, -1), (-1, 1), (1, 1)), 1):
    box(f"caster_mount_plate_{idx}", (0.055, 0.055, 0.006),
        (sx * 0.192, sy * 0.128, 0.103), material=trim, bevel=0.002)

# Rear pad perimeter and front fascia posts.
for sx in (-1, 1):
    box(f"rear_pad_side_rail_{sx}", (0.040, 0.030, 0.422),
        (sx * 0.205, 0.169, 0.520), material=shell, bevel=0.004)
for z, label in ((0.309, "lower"), (0.731, "upper")):
    box(f"rear_pad_{label}_rail", (0.438, 0.026, 0.022),
        (0, 0.170, z), material=shell_light if label == "upper" else shell, bevel=0.004)

# Front body rails around the outlet and reservoir opening.
box("front_left_pillar", (0.026, 0.028, 0.665), (-0.226, -0.169, 0.4475), bevel=0.007)
box("front_right_pillar", (0.026, 0.028, 0.665), (0.226, -0.169, 0.4475), bevel=0.007)
box("front_header", (0.444, 0.026, 0.021), (0, -0.173, 0.777), material=shell_light, bevel=0.005)
box("front_drawer_upper_crossbar", (0.438, 0.025, 0.022), (0, -0.173, 0.246), bevel=0.004)
box("front_base_crossbar", (0.438, 0.026, 0.014), (0, -0.173, 0.098), bevel=0.004)
# Lower moulded skirt closes the reservoir bay from the sides and rear while leaving
# the front drawer opening unobstructed.
for sx in (-1, 1):
    box(f"lower_side_skirt_{sx}", (0.014, 0.328, 0.198),
        (sx * 0.233, 0, 0.204), material=shell, bevel=0.005)
box("lower_rear_skirt", (0.438, 0.016, 0.202), (0, 0.174, 0.199),
    material=shell, bevel=0.005)

# Roof and under-chassis. Tank rides on two real rails along its full travel.
box("top_cover", (0.480, 0.360, 0.026), (0, 0, 0.777), material=shell_light, bevel=0.010)
box("rear_chassis_crossmember", (0.438, 0.030, 0.020), (0, 0.157, 0.107), bevel=0.003)
for sx in (-1, 1):
    box(f"tank_slide_rail_{sx}", (0.018, 0.302, 0.010),
        (sx * 0.169, -0.009, 0.103), material=trim, bevel=0.003)
# A narrow horizontal ledge above the tank leaves its front opening clear.
box("drawer_aperture_lip", (0.424, 0.010, 0.006), (0, -0.187, 0.237), material=trim, bevel=0.002)

# ---- cooling media and intake guards ----------------------------------------------------
# Textured teal cellulose blocks sit against their frame rails. Slim raised channels read
# as the pleated evaporative medium through the protective grille.
for sx in (-1, 1):
    pcx = sx * 0.213
    box(f"wet_pad_side_{'left' if sx < 0 else 'right'}", (0.022, 0.300, 0.400),
        (pcx, 0, 0.520), material=pad_mat, bevel=0.002)
    face_x = sx * 0.2242
    # fine pleat marks on the outer face
    for i in range(17):
        yy = -0.138 + i * 0.01725
        v4.rod(f"pad_side_pleat_{sx}_{i}", (face_x, yy, 0.326), (face_x, yy, 0.714),
               0.00045, material=pad_dark, body=cab, segments=8, bevel=0.0)
    for i in range(12):
        y0 = -0.139 + i * 0.025
        v4.rod(f"pad_side_diagonal_{sx}_{i}", (face_x, y0, 0.335),
               (face_x, min(0.14, y0 + 0.055), 0.705), 0.00035,
               material=pad_dark, body=cab, segments=8, bevel=0.0)
        v4.rod(f"pad_side_diagonal_back_{sx}_{i}", (face_x, y0, 0.705),
               (face_x, min(0.14, y0 + 0.055), 0.335), 0.00035,
               material=pad_dark, body=cab, segments=8, bevel=0.0)
    # six horizontal louvers and five vertical crossbars form the intake safety grille
    gx = sx * 0.238
    for i in range(7):
        z = 0.337 + i * 0.061
        box(f"side_grille_horizontal_{sx}_{i}", (0.004, 0.320, 0.0045),
            (gx, 0, z), material=trim, bevel=0.001)
    for i in range(6):
        y = -0.132 + i * 0.0528
    box(f"side_grille_vertical_{sx}_{i}", (0.004, 0.0035, 0.400),
            (gx, y, 0.520), material=trim, bevel=0.001)

# Rear pad sits behind the fan flow path; the rear grille protects its exposed face.
box("wet_pad_rear", (0.380, 0.022, 0.400), (0, 0.158, 0.520), material=pad_mat, bevel=0.002)
rear_face_y = 0.1692
for i in range(21):
    xx = -0.179 + i * 0.0179
    v4.rod(f"pad_rear_pleat_{i}", (xx, 0.1685, 0.326), (xx, 0.1685, 0.714),
           0.0010, material=pad_dark, body=cab, segments=8, bevel=0.0)
for i in range(8):
    z = 0.348 + i * 0.049
    box(f"rear_intake_bar_{i}", (0.380, 0.006, 0.0065), (0, 0.181, z),
        material=trim, bevel=0.001)
for i in range(9):
    x = -0.17 + i * 0.0425
    box(f"rear_intake_stile_{i}", (0.006, 0.006, 0.400), (x, 0.181, 0.520),
        material=trim, bevel=0.0008)
# Corner service screws and small moulded seam lines.
for sx in (-1, 1):
    for sy in (-1, 1):
        cyl(f"frame_fastener_{sx}_{sy}", 0.0032, 0.002,
            (sx * 0.230, sy * 0.163, 0.737), steel, rot=(0, math.pi / 2, 0), bevel=0.0004, segments=20)

# ---- front air outlet surround and fan chamber -----------------------------------------
# Bezel rails define a generous rectangular louvre opening, with a dark inner duct.
FRAME_Z = 0.520
FRAME_Y = -0.193
box("outlet_frame_left", (0.036, 0.026, 0.486), (-0.222, FRAME_Y, FRAME_Z),
    material=shell_light, bevel=0.008)
box("outlet_frame_right", (0.036, 0.026, 0.486), (0.222, FRAME_Y, FRAME_Z),
    material=shell_light, bevel=0.008)
box("outlet_frame_top", (0.408, 0.026, 0.030), (0, FRAME_Y, 0.748),
    material=shell_light, bevel=0.007)
box("outlet_frame_bottom", (0.408, 0.026, 0.030), (0, FRAME_Y, 0.292),
    material=shell, bevel=0.006)
# Dark annular duct and stationary fan shroud.
# Side baffles frame the fan opening without covering the rotor.
box("inner_duct_left", (0.026, 0.024, 0.418), (-0.197, -0.176, 0.520),
    material=duct_mat, bevel=0.002)
box("inner_duct_right", (0.026, 0.024, 0.418), (0.197, -0.176, 0.520),
    material=duct_mat, bevel=0.002)
FAN_Z, FAN_Y, FAN_R = 0.520, -0.087, 0.139
v4.torus("stationary_fan_shroud", 0.149, 0.006, loc=(0, FAN_Y, FAN_Z), rot=(math.pi / 2, 0, 0),
         material=dark_metal, body=cab)
# Side brackets tie the shroud to the fixed outlet duct frame, outside the rotor sweep.
for side in (-1, 1):
    v4.rod(f"fan_shroud_side_bracket_{side}",
           (side * 0.149, FAN_Y, FAN_Z), (side * 0.184, -0.176, FAN_Z),
           0.005, material=dark_metal, body=cab, bevel=0.0005)
# Motor support stays behind the rotor disk.
cyl("motor_mount", 0.050, 0.030, (0, -0.041, FAN_Z), dark_metal,
    rot=(math.pi / 2, 0, 0), bevel=0.003)
for i, a in enumerate((math.pi / 2, -math.pi / 2, 0, math.pi)):
    p0 = (0.045 * math.cos(a), -0.050, FAN_Z + 0.045 * math.sin(a))
    p1 = (0.149 * math.cos(a), -0.087, FAN_Z + 0.149 * math.sin(a))
    v4.rod(f"fan_mount_spoke_{i}", p0, p1, 0.0042, material=dark_metal, body=cab, bevel=0.0005)
# A small rear motor cap meets the motor mount and keeps the chamber visually dark.
cyl("fan_rear_diffuser", 0.058, 0.003, (0, -0.026, FAN_Z), duct_mat,
    rot=(math.pi / 2, 0, 0), bevel=0.001)

# ---- fan rotor: seven swept blades on a real continuous axle ----------------------------
fan = v4.body("fan")
for i in range(7):
    a0 = 2 * math.pi * i / 7.0
    radii = (0.030, 0.052, 0.078, 0.104, 0.126, 0.138)
    leading, trailing = [], []
    for r in radii:
        t = (r - radii[0]) / (radii[-1] - radii[0])
        centre_ang = a0 + 0.54 * t
        half = 0.25 - 0.045 * t
        leading.append((r * math.cos(centre_ang - half), r * math.sin(centre_ang - half)))
        trailing.append((r * math.cos(centre_ang + half), r * math.sin(centre_ang + half)))
    outline = leading + list(reversed(trailing))
    v4.extrude(f"fan_swept_blade_{i+1}", outline, 0.008,
               loc=(0, FAN_Y + 0.004, FAN_Z), rot=(math.pi / 2, 0, 0),
               bevel=0.001, material=rotor_mat, body=fan)
cyl("fan_hub", 0.043, 0.036, (0, FAN_Y, FAN_Z), material=dark_metal, body=fan,
    rot=(math.pi / 2, 0, 0), bevel=0.004, segments=48)
cyl("fan_hub_front_cap", 0.025, 0.012, (0, FAN_Y - 0.023, FAN_Z), material=steel, body=fan,
    rot=(math.pi / 2, 0, 0), bevel=0.003, segments=40)
cyl("fan_shaft_stub", 0.010, 0.013, (0, -0.0625, FAN_Z), material=dark_metal, body=fan,
    rot=(math.pi / 2, 0, 0), bevel=0.002, segments=32)
v4.joint("fan_spin", "continuous", cab, fan, origin=(0, FAN_Y, FAN_Z), axis=(0, 1, 0))

# ---- independently hinged horizontal outlet louvers ------------------------------------
LOUVRE_N = 9
LOUVRE_ZS = [0.317 + i * 0.0505 for i in range(LOUVRE_N)]
LOUVRE_Y = -0.202
for i, z in enumerate(LOUVRE_ZS, 1):
    body = v4.body(f"louvre_{i:02d}")
    # A moulded, round-edged blade and end trunnions share the horizontal pivot line.
    v4.box(f"louvre_blade_{i:02d}", (0.366, 0.026, 0.0085), (0, LOUVRE_Y, z),
           bevel=0.0036, material=louvre_mat, body=body)
    for sx in (-1, 1):
        v4.cylinder(f"louvre_trunnion_{i:02d}_{sx}", 0.0032, 0.026,
                    (sx * 0.191, LOUVRE_Y, z), rot=(0, math.pi / 2, 0),
                    segments=20, bevel=0.0004, material=steel, body=body)
        v4.cylinder(f"louvre_end_cap_{i:02d}_{sx}", 0.0047, 0.0018,
                    (sx * 0.204, LOUVRE_Y, z), rot=(0, math.pi / 2, 0),
                    segments=20, bevel=0.0004, material=trim, body=body)
    v4.joint(f"louvre_hinge_{i:02d}", "revolute", cab, body,
             origin=(0, LOUVRE_Y, z), axis=(1, 0, 0),
             limits=(-math.radians(22), math.radians(22)))

# ---- pull-out water reservoir and its guide rails ---------------------------------------
tank = v4.body("water_tank")
TANK_Z = 0.170
TANK_FRONT = -0.193
TANK_BACK = 0.077
# Molded tray, open at the top: the floor rests on both cabinet slide rails.
v4.box("tank_floor", (0.380, 0.258, 0.010), (0, -0.057, 0.113),
       bevel=0.004, material=tank_mat, body=tank)
for sx in (-1, 1):
    v4.box(f"tank_side_wall_{sx}", (0.010, 0.258, 0.104),
           (sx * 0.195, -0.057, 0.168), bevel=0.004, material=tank_mat, body=tank)
v4.box("tank_rear_wall", (0.380, 0.010, 0.104), (0, 0.072, 0.168),
       bevel=0.003, material=tank_mat, body=tank)
v4.box("tank_front_fascia", (0.412, 0.014, 0.128), (0, TANK_FRONT, TANK_Z),
       bevel=0.006, material=tank_mat, body=tank)
# Rolled upper lip is made from four rails meeting at the corners.
for sx in (-1, 1):
    v4.box(f"tank_rim_side_{sx}", (0.014, 0.258, 0.008),
           (sx * 0.194, -0.057, 0.221), bevel=0.003, material=tank_light, body=tank)
v4.box("tank_rim_rear", (0.388, 0.014, 0.008), (0, 0.071, 0.221),
       bevel=0.003, material=tank_light, body=tank)
# Visible water in the open well, plus level markings and reinforcing ribs.
v4.box("tank_water_surface", (0.356, 0.220, 0.024), (0, -0.057, 0.130),
       bevel=0.002, material=water_mat, body=tank)
# Front pull grip: dark inset, bar and two moulded shoulders.
v4.box("tank_grip_recess", (0.108, 0.004, 0.030), (0, TANK_FRONT - 0.008, 0.168),
       bevel=0.004, material=duct_mat, body=tank)
v4.box("tank_grip_upper_lip", (0.118, 0.006, 0.004), (0, TANK_FRONT - 0.011, 0.186),
       bevel=0.0015, material=tank_light, body=tank)
for sx in (-1, 1):
    v4.cylinder(f"tank_grip_post_{sx}", 0.0035, 0.012,
                (sx * 0.043, TANK_FRONT - 0.014, 0.168), rot=(math.pi / 2, 0, 0),
                segments=20, bevel=0.0006, material=steel, body=tank)
v4.rod("tank_grip_bar", (-0.043, TANK_FRONT - 0.020, 0.168),
       (0.043, TANK_FRONT - 0.020, 0.168), 0.0032, material=steel, body=tank)
# A narrow blue level window with raised graduations.
v4.box("tank_level_window", (0.012, 0.003, 0.066), (0.157, TANK_FRONT - 0.008, 0.168),
       bevel=0.003, material=duct_mat, body=tank)
v4.box("tank_level_glass", (0.006, 0.002, 0.052), (0.157, TANK_FRONT - 0.0105, 0.168),
       bevel=0.002, material=led, body=tank)
for j in range(4):
    v4.box(f"tank_level_tick_{j}", (0.007, 0.0015, 0.0015),
           (0.157, TANK_FRONT - 0.012, 0.147 + j * 0.014),
           bevel=0.0004, material=shell_light, body=tank)
for sx in (-1, 1):
    for j in range(3):
        v4.box(f"tank_front_rib_{sx}_{j}", (0.0025, 0.0018, 0.074),
               (sx * (0.115 + j * 0.014), TANK_FRONT - 0.008, 0.166),
               bevel=0.001, material=tank_light, body=tank)
v4.joint("tank_slide", "prismatic", cab, tank, origin=(0, -0.054, TANK_Z),
         axis=(0, -1, 0), limits=(0, 0.14))

# ---- top control deck and three rotary controls -----------------------------------------
panel = box("control_panel_bezel", (0.348, 0.158, 0.010), (0, -0.005, 0.795),
            material=duct_mat, bevel=0.010)
# Thin instrument face panel sits inside the bezel.
control_face = box("control_panel_face", (0.322, 0.133, 0.002), (0, -0.005, 0.801),
                   material=trim, bevel=0.007)

knob_specs = [("speed", -0.103, "SPEED"), ("mode", 0.0, "MODE"), ("timer", 0.103, "TIMER")]
for name, x, label in knob_specs:
    y = -0.018
    # fixed numbered bezel with radial tick marks
    v4.cylinder(f"{name}_dial_base", 0.020, 0.002, (x, y, 0.803),
                material=dark_metal, body=cab, bevel=0.0005, segments=40)
    for k in range(9):
        a = math.radians(-140 + k * 35)
        r = 0.022
        tick = v4.box(f"{name}_scale_tick_{k}", (0.0017, 0.0045, 0.001),
                      (x + r * math.cos(a), y + r * math.sin(a), 0.8025),
                      bevel=0.0003, material=shell_light if k in (0, 4, 8) else ink, body=cab)
        tick.rotation_euler[2] = a - math.pi / 2
    v4.text(f"{name}_legend", label, 0.008,
            loc=(x, 0.038, 0.804), material=ink, body=cab, on=control_face, height=0.0004)
    knob = v4.body(f"knob_{name}")
    v4.lathe(f"{name}_knob_moulding",
             [(0, 0), (0.0105, 0), (0.013, 0.002), (0.014, 0.005),
              (0.0125, 0.008), (0.0115, 0.018), (0.008, 0.021), (0, 0.021)],
             loc=(x, y, 0.804), material=shell_light, body=knob)
    v4.cylinder(f"{name}_knob_top", 0.0085, 0.002,
                (x, y, 0.824), material=trim, body=knob, bevel=0.001, segments=32)
    # Raised pointer mark travels with the knob.
    v4.box(f"{name}_pointer", (0.002, 0.0055, 0.001),
           (x, y - 0.004, 0.8255), material=ink, body=knob, bevel=0.0004)
    v4.joint(f"knob_turn_{name}", "continuous", cab, knob,
             origin=(x, y, 0.804), axis=(0, 0, 1))
# A small illuminated status dot sits between the left and middle legends.
v4.cylinder("status_led_lens", 0.0032, 0.0018, (-0.051, 0.047, 0.803),
            material=led, body=cab, bevel=0.0004, segments=24)

# Front upper product badge, facing the shopper.
badge = box("front_model_badge", (0.236, 0.003, 0.017), (0, -0.2075, 0.748),
            material=trim, bevel=0.002)
v4.text("front_model_badge_text", "EVAPORATIVE COOLER", 0.0105,
        loc=(0, -0.2095, 0.748), rot=(math.pi / 2, 0, 0),
        material=shell_light, body=cab, on=badge, height=0.00045)

# ---- four swivelling caster forks and separately rolling wheels -------------------------
def make_plate_yz(name, xcenter, thickness, outline_yz, body, material):
    """Extrude a 2D fork cheek in the YZ plane along X."""
    verts = []
    for x in (xcenter - thickness / 2, xcenter + thickness / 2):
        verts.extend([(x, y, z) for y, z in outline_yz])
    n = len(outline_yz)
    faces = [tuple(range(n)), tuple(reversed(range(n, 2 * n)))]
    for i in range(n):
        j = (i + 1) % n
        faces.append((i, j, n + j, n + i))
    mesh = bpy.data.meshes.new(name + "_mesh")
    mesh.from_pydata(verts, [], faces)
    mesh.update()
    obj = bpy.data.objects.new(name, mesh)
    bpy.context.scene.collection.objects.link(obj)
    v4._finish(obj, body, material, 0.0012, segments=2, smooth=False)
    return obj

for idx, (sx, sy) in enumerate(((-1, -1), (1, -1), (-1, 1), (1, 1)), 1):
    px, py, pz = sx * 0.192, sy * 0.128, 0.103
    fork = v4.body(f"caster_fork_{idx}")
    # Swivel kingpin/stem meets the underside face of its reinforced cabinet pad.
    v4.cylinder(f"caster_{idx}_swivel_stem", 0.009, 0.026,
                (px, py, 0.087), material=steel, body=fork, bevel=0.001, segments=32)
    v4.cylinder(f"caster_{idx}_race", 0.016, 0.004,
                (px, py, 0.072), material=dark_metal, body=fork, bevel=0.001, segments=32)
    # Two stamped steel cheeks route around either side of the wheel.
    outline = [
        (py - 0.013, 0.096), (py + 0.010, 0.094), (py + 0.029, 0.078),
        (py + 0.037, 0.057), (py + 0.037, 0.035), (py + 0.020, 0.035),
        (py + 0.014, 0.061), (py - 0.003, 0.078)
    ]
    for side in (-1, 1):
        make_plate_yz(f"caster_{idx}_fork_cheek_{side}", px + side * 0.016,
                      0.004, outline, fork, steel)
        v4.cylinder(f"caster_{idx}_axle_bolt_{side}", 0.006, 0.003,
                    (px + side * 0.0195, py + 0.018, 0.036),
                    rot=(0, math.pi / 2, 0), segments=24, bevel=0.0006,
                    material=dark_metal, body=fork)
    v4.box(f"caster_{idx}_yoke_bridge", (0.034, 0.018, 0.008),
           (px, py + 0.004, 0.082), bevel=0.002, material=steel, body=fork)
    v4.joint(f"caster_swivel_{idx}", "continuous", cab, fork,
             origin=(px, py, 0.100), axis=(0, 0, 1))

    wheel = v4.body(f"caster_wheel_{idx}")
    wy, wz = py + 0.018, 0.036
    v4.cylinder(f"caster_{idx}_tire", 0.034, 0.020, (px, wy, wz),
                rot=(0, math.pi / 2, 0), segments=48, bevel=0.003,
                material=rubber, body=wheel)
    # Circumferential tread blocks give the small rubber wheel a recognisable rolling surface.
    for k in range(24):
        a = 2 * math.pi * k / 24
        lug = v4.box(f"caster_{idx}_tread_{k}", (0.018, 0.007, 0.0022),
                     (px, wy + 0.03455 * math.sin(a), wz + 0.03455 * math.cos(a)),
                     rot=(-a, 0, 0), bevel=0.0006, material=rubber, body=wheel)
    for side in (-1, 1):
        v4.torus(f"caster_{idx}_sidewall_bead_{side}", 0.025, 0.0013,
                 loc=(px + side * 0.0095, wy, wz), rot=(0, math.pi / 2, 0),
                 material=hub_mat, body=wheel, segments=40, ring_segments=10)
        v4.cylinder(f"caster_{idx}_hub_{side}", 0.012, 0.003,
                    (px + side * 0.0115, wy, wz), rot=(0, math.pi / 2, 0),
                    segments=32, bevel=0.001, material=hub_mat, body=wheel)
        v4.cylinder(f"caster_{idx}_hub_cap_{side}", 0.005, 0.0015,
                    (px + side * 0.0135, wy, wz), rot=(0, math.pi / 2, 0),
                    segments=24, bevel=0.0005, material=steel, body=wheel)
    v4.joint(f"wheel_roll_{idx}", "continuous", fork, wheel,
             origin=(px, wy, wz), axis=(1, 0, 0))

# ---- save a clean rest pose --------------------------------------------------------------
v4.save(os.path.join(os.getcwd(), "asset.blend"))
print("saved asset.blend")
