"""Build a detailed articulated service biped using v4lib."""
import math
import os
import sys

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

v4.reset()

# Materials
ivory = v4.painted("ceramic_ivory", (0.79, 0.83, 0.82), roughness=0.27)
dark = v4.plastic("graphite_polymer", (0.055, 0.075, 0.085), roughness=0.34)
black = v4.rubber("tire_rubber", (0.025, 0.03, 0.032))
steel = v4.metal("satin_titanium", color=(0.48, 0.55, 0.59), roughness=0.28, brushed=True)
bright = v4.metal("machined_aluminum", color=(0.73, 0.78, 0.79), roughness=0.22, brushed=False)
amber = v4.plastic("safety_amber", (0.95, 0.32, 0.045), roughness=0.27)
cyan = v4.emissive("status_cyan", (0.04, 0.62, 0.95), strength=2.0)
screen = v4.glass("display_glass", (0.012, 0.07, 0.09), roughness=0.12)
panel_alloy = v4.metal("panel_alloy", color=(0.32, 0.39, 0.42), roughness=0.31, brushed=True)

# Bodies
pelvis = v4.body("pelvis")
thighs = [v4.body("thigh_L"), v4.body("thigh_R")]
shins = [v4.body("shin_L"), v4.body("shin_R")]
feet = [v4.body("foot_L"), v4.body("foot_R")]

def box(name, size, loc, mat, body, bevel=0.004, rot=(0, 0, 0)):
    return v4.box(name, size, loc, rot=rot, bevel=bevel, material=mat, body=body)

def cyl_x(name, radius, depth, loc, mat, body, bevel=0.0015):
    obj = v4.cylinder(name, radius, depth, loc, rot=(0, math.pi/2, 0), segments=32, material=mat, body=body)
    if bevel:
        # Cylinder helper has no bevel argument; a subtly larger ring supplies a machined rim.
        pass
    return obj

def bolt_x(name, x, y, z, body, material=bright, r=0.005):
    cyl_x(name, r, 0.003, (x, y, z), material, body, 0)

def cyl_y(name, radius, depth, loc, mat, body, bevel=0.0005):
    return v4.cylinder(name, radius, depth, loc, rot=(math.pi/2, 0, 0), segments=24,
                       material=mat, body=body, bevel=bevel)

# Root: lower waist, tapered torso, chest service armor and sensor head.
box("pelvic_core", (0.16, 0.25, 0.235), (0, 0.015, 1.1125), dark, pelvis, 0.035)
box("torso_lower", (0.39, 0.25, 0.27), (0, 0.025, 1.355), dark, pelvis, 0.055)
box("chest_shell", (0.48, 0.29, 0.30), (0, 0.012, 1.575), ivory, pelvis, 0.065)
# Fitted service covers: a dark reveal under each lid reads as a shallow recessed seam.
# The reveal touches the housing face; the lid and fasteners sit on the reveal.
box("torso_service_reveal", (0.235, 0.006, 0.15), (0, -0.103, 1.355), black, pelvis, 0.018)
box("torso_service_lid", (0.218, 0.004, 0.132), (0, -0.108, 1.355), panel_alloy, pelvis, 0.014)
for px in (-0.094, 0.094):
    for pz in (1.306, 1.404):
        cyl_y("torso_lid_fastener", 0.005, 0.004, (px, -0.111, pz), bright, pelvis)
# Pelvis inspection hatch and twin chest-side access panels.
box("pelvis_hatch_reveal", (0.115, 0.006, 0.142), (0, -0.113, 1.112), black, pelvis, 0.012)
box("pelvis_hatch_lid", (0.098, 0.004, 0.124), (0, -0.118, 1.112), steel, pelvis, 0.01)
for px in (-0.038, 0.038):
    for pz in (1.066, 1.158):
        cyl_y("pelvis_hatch_screw", 0.004, 0.003, (px, -0.121, pz), bright, pelvis, 0)
# Rear service architecture: fitted removable covers over the torso electronics and waist bus.
box("rear_chest_reveal", (0.315, 0.006, 0.218), (0, 0.160, 1.585), dark, pelvis, 0.022)
box("rear_chest_cover", (0.294, 0.004, 0.197), (0, 0.165, 1.585), panel_alloy, pelvis, 0.018)
for px in (-0.124, 0.124):
    for pz in (1.506, 1.664):
        cyl_y("rear_chest_fastener", 0.0055, 0.004, (px, 0.168, pz), bright, pelvis)
# Two compact filtered vent fields, recessed into the rear access cover.
for px in (-0.079, 0.079):
    box("rear_vent_shadow", (0.062, 0.004, 0.055), (px, 0.169, 1.585), dark, pelvis, 0.008)
    for k in range(3):
        box("rear_vent_louver", (0.048, 0.004, 0.004), (px, 0.173, 1.569 + 0.016*k), steel, pelvis, 0.0015)
box("rear_lower_reveal", (0.235, 0.006, 0.158), (0, 0.153, 1.345), black, pelvis, 0.016)
box("rear_lower_cover", (0.218, 0.004, 0.141), (0, 0.158, 1.345), dark, pelvis, 0.014)
for px in (-0.087, 0.087):
    for pz in (1.294, 1.396):
        cyl_y("rear_lower_fastener", 0.0045, 0.003, (px, 0.161, pz), bright, pelvis)
box("rear_waist_reveal", (0.118, 0.006, 0.142), (0, 0.143, 1.112), black, pelvis, 0.012)
box("rear_waist_cover", (0.100, 0.004, 0.124), (0, 0.148, 1.112), panel_alloy, pelvis, 0.01)
for px in (-0.038, 0.038):
    for pz in (1.066, 1.158):
        cyl_y("rear_waist_screw", 0.004, 0.003, (px, 0.151, pz), bright, pelvis, 0)
for s in (-1, 1):
    px = s * 0.157
    box("chest_access_reveal", (0.078, 0.005, 0.075), (px, -0.136, 1.469), dark, pelvis, 0.012)
    box("chest_access_cover", (0.065, 0.004, 0.061), (px, -0.1405, 1.469), panel_alloy, pelvis, 0.009)
    for dx in (-0.024, 0.024):
        for dz in (-0.020, 0.020):
            cyl_y("chest_cover_screw", 0.0035, 0.003, (px+dx, -0.143, 1.469+dz), bright, pelvis, 0)
box("chest_bib", (0.30, 0.025, 0.16), (0, -0.143, 1.585), dark, pelvis, 0.02)
box("display_bezel", (0.19, 0.012, 0.085), (0, -0.161, 1.607), steel, pelvis, 0.012)
box("display", (0.16, 0.006, 0.055), (0, -0.170, 1.607), screen, pelvis, 0.008)
for x in (-0.052, -0.026, 0, 0.026, 0.052):
    box("display_status_pixel", (0.012, 0.003, 0.006), (x, -0.174, 1.61), cyan, pelvis, 0.002)
# Small chest lamps and vent stack.
for x in (-0.17, 0.17):
    cyl_x("chest_lamp_mount", 0.023, 0.018, (x, -0.132, 1.65), steel, pelvis)
    cyl_x("chest_lamp_lens", 0.014, 0.020, (x + (0.011 if x > 0 else -0.011), -0.132, 1.65), amber if x < 0 else cyan, pelvis, 0)
# Head, attached rigidly to torso. Rounded helmet shell and twin optical apertures.
box("neck_pedestal", (0.14, 0.13, 0.065), (0, 0.005, 1.755), steel, pelvis, 0.018)
box("sensor_head", (0.24, 0.20, 0.19), (0, -0.005, 1.865), dark, pelvis, 0.065)
box("head_faceplate", (0.19, 0.035, 0.09), (0, -0.105, 1.87), ivory, pelvis, 0.028)
for x in (-0.045, 0.045):
    cyl_x("optical_sensor_rim", 0.026, 0.016, (x, -0.128, 1.875), steel, pelvis)
    cyl_x("optical_sensor_lens", 0.018, 0.019, (x + (-0.010 if x < 0 else 0.010), -0.128, 1.875), cyan, pelvis, 0)
box("head_crown", (0.13, 0.12, 0.025), (0, 0, 1.96), ivory, pelvis, 0.012)
# Shoulder-like fixed upper silhouette gives a complete robot profile.
for s in (-1, 1):
    box("shoulder_guard", (0.16, 0.22, 0.18), (s * 0.30, 0.02, 1.62), ivory, pelvis, 0.05)
    cyl_x("shoulder_cap", 0.055, 0.026, (s * 0.378, 0.02, 1.62), steel, pelvis)

# Legs: the joint barrels are split across parent and child bodies, exposing real pivot seams.
for i, s in enumerate((-1, 1)):
    side = "L" if s < 0 else "R"
    x = s * 0.17
    thigh, shin, foot = thighs[i], shins[i], feet[i]
    # Hip output clevis and thigh shell, from z=1.05 down to 0.60.
    # Coaxial shaft and annular bearing: the circular contact is invariant as the
    # thigh rotates, while the shaft ties directly into the narrow waist core.
    cyl_x(f"hip_axle_{side}", 0.025, 0.20, (x, 0, 1.05), steel, pelvis)
    v4.torus(f"hip_bearing_ring_{side}", 0.045, 0.020, loc=(x, 0, 1.05),
             rot=(0, math.pi/2, 0), material=panel_alloy, body=thigh)
    v4.torus(f"hip_shaft_retainer_{side}", 0.028, 0.004,
             loc=(x+s*0.097, 0, 1.05), rot=(0, math.pi/2, 0), material=bright, body=pelvis)
    # thigh volume and front armor, anchored clear of the knee pivot.
    box(f"thigh_frame_{side}", (0.18, 0.17, 0.37), (x, 0.01, 0.82), dark, thigh, 0.018)
    box(f"thigh_front_plate_{side}", (0.145, 0.045, 0.29), (x, -0.092, 0.83), ivory, thigh, 0.012)
    box(f"thigh_inset_{side}", (0.075, 0.012, 0.19), (x, -0.119, 0.83), steel, thigh, 0.006)
    for z in (0.76, 0.79, 0.87, 0.90):
        box(f"thigh_groove_{side}", (0.05, 0.008, 0.006), (x, -0.127, z), dark, thigh, 0.002)
    # upper actuator body inset behind armor, one matching piston rod.
    v4.rod(f"thigh_actuator_{side}", (x, 0.10, 0.70), (x, 0.10, 0.93), 0.026, material=steel, body=thigh)
    for z in (0.70, 0.93):
        cyl_x(f"thigh_actuator_eye_{side}", 0.033, 0.14, (x, 0.10, z), dark, thigh)
    # Knee axle belongs to thigh; outer circular pad belongs to moving shin.
    cyl_x(f"knee_hinge_barrel_{side}", 0.070, 0.17, (x, 0, 0.60), steel, thigh)
    cyl_x(f"knee_shaft_{side}", 0.037, 0.19, (x, 0, 0.60), dark, thigh)
    # Shin-mounted annular retainers wrap the thigh barrel at the knee.
    # Their lower arcs land on the shin frame; circular symmetry preserves contact in motion.
    for face in (-1, 1):
        v4.torus(f"knee_retainer_ring_{side}", 0.045, 0.008,
                 loc=(x+face*0.086, 0, 0.595), rot=(0, math.pi/2, 0),
                 material=panel_alloy, body=shin)
    # Lower leg frame from knee to ankle.
    box(f"shin_frame_{side}", (0.17, 0.16, 0.34), (x, 0.018, 0.38), dark, shin, 0.016)
    box(f"shin_front_shell_{side}", (0.14, 0.045, 0.29), (x, -0.075, 0.39), ivory, shin, 0.012)
    box(f"shin_center_guard_{side}", (0.055, 0.012, 0.22), (x, -0.103, 0.39), steel, shin, 0.006)
    # Small contrasting wear indicators break up the repeated calf armor silhouette.
    for dx in (-0.044, 0.044):
        box(f"shin_wear_marker_{side}", (0.018, 0.004, 0.052),
            (x+dx, -0.101, 0.445), amber, shin, 0.0025)
    # Exposed hydraulic damper and rear cable path.
    v4.rod(f"shin_damper_{side}", (x, 0.106, 0.27), (x, 0.106, 0.51), 0.025, material=bright, body=shin)
    for z in (0.27, 0.51):
        cyl_x(f"damper_end_{side}", 0.032, 0.13, (x, 0.106, z), steel, shin)
    v4.tube(f"rear_cable_{side}", [(x+s*0.055,0.12,0.52),(x+s*0.075,0.145,0.47),(x+s*0.075,0.145,0.30),(x+s*0.045,0.13,0.22)], 0.009, material=black, body=shin)
    # Knee output cover at the shell exterior.
    # Ankle joint hardware. Ankle pivot at z=.16.
    cyl_x(f"ankle_axis_{side}", 0.052, 0.15, (x, 0.08, 0.16), steel, shin)
    # Foot body: ankle yoke, sculpted instep, toe and rubber outsole.
    box(f"foot_sole_{side}", (0.22, 0.39, 0.055), (x, -0.055, 0.035), black, foot, 0.018)
    # Open the rear axle pocket in the chassis; paired side rails carry the yoke loads.
    box(f"foot_chassis_{side}", (0.20, 0.255, 0.065), (x, -0.0875, 0.085), dark, foot, 0.018)
    box(f"heel_block_{side}", (0.17, 0.07, 0.05), (x, -0.04, 0.10), steel, foot, 0.010)
    box(f"instep_shell_{side}", (0.17, 0.12, 0.075), (x, -0.15, 0.125), ivory, foot, 0.020)
    box(f"toe_bumper_{side}", (0.18, 0.08, 0.065), (x, -0.205, 0.105), dark, foot, 0.025)
    # A low clevis bridge and side rails join the yoke to the chassis below the axle.
    box(f"ankle_yoke_base_{side}", (0.20, 0.04, 0.016), (x, 0.08, 0.094), steel, foot, 0.004)
    for rail_s in (-1, 1):
        v4.rod(f"ankle_yoke_rail_{side}", (x+rail_s*0.09, 0.025, 0.094),
               (x+rail_s*0.09, 0.08, 0.094), 0.008, material=steel, body=foot)
    # Yoke arms straddle the axle ends with 2 mm clearance and rise from the clevis bridge.
    # All bearing details share the shaft centerline at y=.08 m.
    for dx in (-0.095, 0.095):
        v4.rod(f"ankle_yoke_arm_{side}", (x+dx, 0.08, 0.102), (x+dx, 0.08, 0.22), 0.018, material=steel, body=foot)
        cyl_x(f"ankle_yoke_boss_{side}", 0.034, 0.018, (x+dx, 0.08, 0.16), dark, foot)
        bolt_x(f"ankle_bolt_{side}", x+dx+s*0.012, 0.08, 0.16, foot, bright, 0.009)
    # Outboard ankle retainers and four small socket screws per side.
    for face in (-1, 1):
        cx = x + face * 0.092
        cyl_x(f"ankle_retainer_{side}", 0.029, 0.010, (cx, 0.08, 0.16), panel_alloy, foot)
        cyl_x(f"ankle_retainer_hub_{side}", 0.014, 0.013, (x+face*0.101, 0.08, 0.16), steel, foot)
        bolt_x(f"ankle_retainer_bolt_{side}", x+face*0.110, 0.08, 0.16, foot, bright, 0.0055)
    # Tread blocks directly contact the sole underside; three crosswise segments.
    for j, y in enumerate((-0.18, -0.07, 0.04, 0.14)):
        box(f"tread_{side}_{j}", (0.19, 0.035, 0.012), (x, y, 0.005), black, foot, 0.004)
    # Shell fasteners on visible front faces.
    for z in (0.30, 0.47):
        for dx in (-0.048, 0.048):
            cyl_x(f"shin_screw_{side}", 0.006, 0.004, (x+dx, -0.101, z), bright, shin, 0)

    v4.joint(f"hip_{side}", "revolute", pelvis, thigh, origin=(x, 0, 1.05), axis=(1,0,0),
             limits=(-math.radians(18), math.radians(18)))
    v4.joint(f"knee_{side}", "revolute", thigh, shin, origin=(x, 0, 0.60), axis=(1,0,0),
             limits=(-math.radians(5), math.radians(85)))
    v4.joint(f"ankle_{side}", "revolute", shin, foot, origin=(x, 0.08, 0.16), axis=(1,0,0),
             limits=(-math.radians(25), math.radians(25)))

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