"""Detailed articulated service biped robot; asset authored entirely through v4lib."""
import math, os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import v4lib as v4
v4.reset()

# Finishes
ivory = v4.painted("ceramic_ivory", (0.78, 0.82, 0.80), roughness=0.27, coat=0.55)
white = v4.plastic("warm_white_polymer", (0.91, 0.92, 0.88), roughness=0.3)
graphite = v4.metal("graphite_anodized", color=(0.12, 0.15, 0.17), roughness=0.3)
steel = v4.metal("brushed_titanium", color=(0.63, 0.69, 0.70), roughness=0.24)
black = v4.rubber("sole_elastomer", (0.035, 0.045, 0.05))
orange = v4.painted("safety_amber", (0.95, 0.36, 0.045), roughness=0.3, coat=0.3)
lens = v4.emissive("ice_blue_status", (0.18, 0.72, 1.0), strength=2.2)

root = v4.body("pelvis_torso")
# Torso shell and pelvis: layered armor on a compact central chassis.
v4.box("pelvis_core", (0.34, 0.27, 0.19), (0, 0.015, 1.025), bevel=0.045, material=graphite, body=root)
# Rear pelvic electronics cover, aligned over the load-bearing core.
v4.box("pelvis_rear_gasket",(0.25,0.008,0.13),(0,0.153,1.025),bevel=0.014,material=graphite,body=root)
v4.box("pelvis_rear_cover",(0.222,0.009,0.105),(0,0.1615,1.025),bevel=0.012,material=ivory,body=root)
v4.box("pelvis_rear_seam",(0.002,0.003,0.07),(0,0.168,1.025),bevel=0.0005,material=graphite,body=root)
for sx in (-1,1):
    for z in (0.985,1.065):
        v4.cylinder(f"pelvis_rear_screw_{sx}_{z}",0.004,0.003,(sx*0.091,0.168,z),rot=(-math.pi/2,0,0),segments=16,material=steel,body=root)
v4.box("pelvis_front_armor", (0.31, 0.035, 0.15), (0, -0.139, 1.035), bevel=0.025, material=ivory, body=root)
# A layered front access hatch breaks up the pelvis armor and exposes its service function.
v4.box("pelvis_hatch_gasket",(0.205,0.008,0.105),(0,-0.1605,1.035),bevel=0.013,material=graphite,body=root)
v4.box("pelvis_hatch",(0.184,0.009,0.084),(0,-0.169,1.035),bevel=0.011,material=steel,body=root)
v4.box("pelvis_hatch_cover",(0.15,0.004,0.058),(0,-0.1755,1.035),bevel=0.008,material=ivory,body=root)
for sx in (-1,1):
    for z in (1.003,1.067):
        v4.cylinder(f"pelvis_hatch_bolt_{sx}_{z}",0.004,0.003,(sx*0.078,-0.178,z),rot=(math.pi/2,0,0),segments=16,bevel=0.0005,material=graphite,body=root)
# Short engraved service seam and paired lower diagnostic ports.
v4.box("pelvis_hatch_seam",(0.002,0.002,0.04),(0,-0.178,1.035),bevel=0.0005,material=graphite,body=root)
for sx in (-1,1):
    v4.cylinder(f"pelvis_diagnostic_port_{sx}",0.008,0.004,(sx*0.095,-0.161,0.985),rot=(math.pi/2,0,0),material=graphite,body=root)
    v4.cylinder(f"pelvis_port_lens_{sx}",0.0035,0.002,(sx*0.095,-0.164,0.985),rot=(math.pi/2,0,0),material=lens,body=root)
v4.box("waist_belt", (0.36, 0.255, 0.055), (0, 0.015, 1.13), bevel=0.018, material=steel, body=root)
v4.box("abdomen_chassis", (0.27, 0.22, 0.19), (0, 0.01, 1.235), bevel=0.035, material=graphite, body=root)
v4.box("chest_shell", (0.43, 0.255, 0.30), (0, 0.005, 1.405), bevel=0.055, material=ivory, body=root)
# Rear service hatch and cooling grille break up the otherwise broad back shell.
v4.box("torso_rear_gasket",(0.33,0.012,0.215),(0,0.14,1.405),bevel=0.018,material=graphite,body=root)
v4.box("torso_rear_door",(0.304,0.012,0.188),(0,0.152,1.405),bevel=0.016,material=steel,body=root)
v4.box("torso_rear_cover",(0.274,0.006,0.158),(0,0.161,1.405),bevel=0.013,material=ivory,body=root)
# Two stepped vent banks and a center service latch on the access door.
for sx in (-1,1):
    v4.box(f"torso_rear_vent_well_{sx}",(0.078,0.005,0.09),(sx*0.078,0.165,1.415),bevel=0.009,material=graphite,body=root)
    for k in range(4):
        v4.box(f"torso_rear_vent_fin_{sx}_{k}",(0.058,0.004,0.006),(sx*0.078,0.169,1.385+k*0.02),bevel=0.002,material=steel,body=root)
v4.box("torso_rear_latch_base",(0.045,0.006,0.055),(0,0.165,1.405),bevel=0.008,material=graphite,body=root)
v4.box("torso_rear_latch",(0.018,0.006,0.034),(0,0.171,1.405),bevel=0.004,material=orange,body=root)
for sx in (-1,1):
    for z in (1.326,1.484):
        v4.cylinder(f"torso_rear_screw_{sx}_{z}",0.004,0.003,(sx*0.126,0.165,z),rot=(-math.pi/2,0,0),segments=16,material=graphite,body=root)
# Torso inset faceplate and vents are physically layered against the front (-Y).
v4.box("chest_faceplate", (0.235, 0.012, 0.13), (0, -0.126, 1.405), bevel=0.018, material=white, body=root)
# Floating panel seams and captive fasteners sit flush to the chest faceplate.
for sx in (-1,1):
    v4.box(f"chest_seam_v_{sx}",(0.003,0.004,0.086),(sx*0.087,-0.134,1.405),bevel=0.001,material=graphite,body=root)
    for z in (1.357,1.453):
        v4.cylinder(f"chest_fastener_{sx}_{z}",0.004,0.003,(sx*0.099,-0.1335,z),rot=(math.pi/2,0,0),segments=16,bevel=0.0005,material=steel,body=root)
v4.box("chest_display", (0.115, 0.008, 0.032), (0, -0.136, 1.426), bevel=0.006, material=graphite, body=root)
v4.box("display_glass", (0.096, 0.004, 0.014), (0, -0.142, 1.426), bevel=0.003, material=lens, body=root)
# Small lower access panel with a clear split line and corner screws.
v4.box("chest_access_panel",(0.15,0.008,0.046),(0,-0.134,1.365),bevel=0.008,material=ivory,body=root)
v4.box("chest_access_seam",(0.002,0.003,0.026),(0,-0.1395,1.365),bevel=0.0005,material=graphite,body=root)
for sx in (-1,1):
    for z in (1.35,1.38):
        v4.cylinder(f"chest_access_screw_{sx}_{z}",0.003,0.003,(sx*0.064,-0.1395,z),rot=(math.pi/2,0,0),segments=12,bevel=0.0004,material=steel,body=root)
# Shoulder armor wings with dark articulated-looking fixed covers.
for sx, side in [(-1,"L"),(1,"R")]:
    v4.box(f"shoulder_{side}_block", (0.16, 0.23, 0.16), (sx*0.255, 0.005, 1.49), bevel=0.045, material=ivory, body=root)
    v4.cylinder(f"shoulder_{side}_cap", 0.066, 0.018, (sx*0.31, -0.002, 1.49), rot=(0,math.pi/2,0), material=steel, body=root)
    # Angular inset panel gives the shoulder cover a tapered, machined outline.
    v4.extrude(f"shoulder_{side}_vent_plate",[(-0.046,-0.037),(0.046,-0.037),(0.058,-0.02),(0.058,0.027),(0.045,0.043),(-0.045,0.043),(-0.058,0.025),(-0.058,-0.02)],0.012,loc=(sx*0.255,-0.108,1.49),rot=(math.pi/2,0,0),bevel=0.003,material=graphite,body=root)
    for k in range(3):
        v4.box(f"shoulder_{side}_vent_{k}", (0.058, 0.004, 0.006), (sx*0.255, -0.122, 1.468+k*0.021), bevel=0.002, material=steel, body=root)
    for dx in (-0.045,0.045):
        for dz in (-0.03,0.03):
            v4.cylinder(f"shoulder_{side}_screw_{dx}_{dz}",0.0035,0.003,(sx*0.255+dx,-0.1215,1.49+dz),rot=(math.pi/2,0,0),segments=12,material=steel,body=root)
    # Narrow shoulder edge rail follows the softened outside contour.
    v4.box(f"shoulder_{side}_outer_rail",(0.012,0.13,0.018),(sx*0.325,-0.003,1.49),bevel=0.007,material=steel,body=root)
# Neck and simple sensor head (head is a fixed torso component, no human features).
v4.cylinder("neck_pedestal", 0.07, 0.055, (0, 0.01, 1.565), material=steel, body=root)
v4.box("sensor_head", (0.23, 0.19, 0.14), (0, 0.012, 1.625), bevel=0.045, material=graphite, body=root)
v4.box("visor_bezel", (0.175, 0.018, 0.052), (0, -0.087, 1.63), bevel=0.018, material=steel, body=root)
v4.box("visor_lens", (0.145, 0.008, 0.027), (0, -0.099, 1.63), bevel=0.012, material=lens, body=root)
for sx in (-1,1):
    v4.cylinder(f"head_sensor_{sx}", 0.013, 0.01, (sx*0.074, -0.105, 1.63), rot=(math.pi/2,0,0), material=orange, body=root)
# Waist hinge bearing housings seated on both sides of pelvis.
for sx,side in [(-1,"L"),(1,"R")]:
    hx=sx*0.17
    v4.cylinder(f"hip_bearing_{side}",0.074,0.055,(hx,0,1.04),rot=(0,math.pi/2,0),material=graphite,body=root)
    v4.cylinder(f"hip_bearing_ring_{side}",0.058,0.012,(hx-sx*0.032,0,1.04),rot=(0,math.pi/2,0),material=steel,body=root)
    # small front-facing hip service panel
    v4.box(f"pelvis_side_panel_{side}",(0.11,0.008,0.09),(hx,-0.16,1.035),bevel=0.012,material=ivory,body=root)

# Leg modules. Neutral rest is straight; each shell reaches the physical hinge axis.
for sx, side in [(-1,"L"),(1,"R")]:
    hx=sx*0.17
    thigh=v4.body(f"thigh_{side}")
    # structural core, armor front, and outside actuator fairing
    v4.box(f"thigh_{side}_beam",(0.115,0.115,0.37),(hx,0.005,0.825),bevel=0.026,material=graphite,body=thigh)
    v4.box(f"thigh_{side}_armor",(0.148,0.044,0.30),(hx,-0.066,0.825),bevel=0.022,material=ivory,body=thigh)
    v4.box(f"thigh_{side}_badge",(0.075,0.009,0.035),(hx,-0.091,0.79),bevel=0.006,material=orange,body=thigh)
    # Contoured edge rails and four flush screws make the thigh armor serviceable.
    for sx2 in (-1,1):
        v4.box(f"thigh_{side}_edge_rail_{sx2}",(0.008,0.006,0.20),(hx+sx2*0.061,-0.089,0.825),bevel=0.003,material=steel,body=thigh)
        for z in (0.704,0.946):
            v4.cylinder(f"thigh_{side}_screw_{sx2}_{z}",0.0035,0.003,(hx+sx2*0.052,-0.0895,z),rot=(math.pi/2,0,0),segments=12,material=graphite,body=thigh)
    # upper bearing cap and lower knee clevis discs
    v4.cylinder(f"hip_outer_cap_{side}",0.064,0.018,(hx+sx*0.066,0,1.04),rot=(0,math.pi/2,0),material=steel,body=thigh)
    v4.cylinder(f"hip_cap_hub_{side}",0.025,0.021,(hx+sx*0.079,0,1.04),rot=(0,math.pi/2,0),material=graphite,body=thigh)
    v4.cylinder(f"knee_clevis_{side}",0.065,0.035,(hx+sx*0.066,0,0.61),rot=(0,math.pi/2,0),material=steel,body=thigh)
    v4.cylinder(f"knee_bolt_{side}",0.023,0.04,(hx+sx*0.085,0,0.61),rot=(0,math.pi/2,0),material=graphite,body=thigh)
    v4.joint(f"hip_{side}","revolute",root,thigh,origin=(hx,0,1.04),axis=(1,0,0),limits=(-math.radians(45),math.radians(70)))

    shin=v4.body(f"shin_{side}")
    v4.box(f"shin_{side}_beam",(0.105,0.10,0.40),(hx,0.015,0.385),bevel=0.023,material=graphite,body=shin)
    v4.box(f"shin_{side}_guard",(0.142,0.043,0.30),(hx,-0.052,0.395),bevel=0.025,material=ivory,body=shin)
    # Vertical shallow ribs and an inset label on guard.
    v4.box(f"shin_{side}_rib_l",(0.009,0.006,0.20),(hx-0.041,-0.077,0.40),bevel=0.003,material=steel,body=shin)
    v4.box(f"shin_{side}_rib_r",(0.009,0.006,0.20),(hx+0.041,-0.077,0.40),bevel=0.003,material=steel,body=shin)
    # Flush corner screws and a contrasting panel joint on the shin cover.
    for sx2 in (-1,1):
        for z in (0.285,0.505):
            v4.cylinder(f"shin_{side}_guard_screw_{sx2}_{z}",0.0035,0.003,(hx+sx2*0.054,-0.0745,z),rot=(math.pi/2,0,0),segments=12,material=steel,body=shin)
    v4.box(f"shin_{side}_guard_seam",(0.003,0.004,0.026),(hx,-0.0755,0.535),bevel=0.001,material=graphite,body=shin)
    v4.box(f"shin_{side}_ankle_motor",(0.13,0.11,0.12),(hx,0.01,0.19),bevel=0.028,material=graphite,body=shin)
    # Hinge bearings belong to the moving shin, concentric with knee and ankle.
    v4.cylinder(f"knee_outer_cap_{side}",0.050,0.03,(hx-sx*0.066,0,0.61),rot=(0,math.pi/2,0),material=graphite,body=shin)
    v4.cylinder(f"knee_ring_{side}",0.037,0.012,(hx-sx*0.083,0,0.61),rot=(0,math.pi/2,0),material=orange,body=shin)
    v4.cylinder(f"ankle_yoke_{side}",0.052,0.04,(hx,0.005,0.15),rot=(0,math.pi/2,0),material=steel,body=shin)
    v4.cylinder(f"ankle_hub_{side}",0.024,0.046,(hx+sx*0.027,0.005,0.15),rot=(0,math.pi/2,0),material=graphite,body=shin)
    # Split clevis cheeks straddle the narrow heel with 7.5 mm running clearance per side.
    # The fork is fixed to the shin and stays clear as the foot rotates about the axle.
    for sx2 in (-1,1):
        v4.box(f"ankle_yoke_cheek_{side}_{sx2}",(0.03,0.086,0.112),(hx+sx2*0.0815,0.006,0.15),bevel=0.012,material=graphite,body=shin)
        v4.box(f"ankle_yoke_lip_{side}_{sx2}",(0.018,0.064,0.014),(hx+sx2*0.084,0.006,0.202),bevel=0.006,material=steel,body=shin)
    # Protected hydraulic/electrical conduit routed along outside of shin.
    x=hx+sx*0.066
    v4.tube(f"shin_{side}_conduit",[(x,0.045,0.55),(x,0.07,0.48),(x,0.07,0.31),(x,0.045,0.24)],0.009,material=orange,body=shin)
    v4.joint(f"knee_{side}","revolute",thigh,shin,origin=(hx,0,0.61),axis=(1,0,0),limits=(math.radians(-5),math.radians(75)))

    foot=v4.body(f"foot_{side}")
    # Broad forward foot with an armored instep and full-contact rubber tread.
    v4.box(f"foot_{side}_sole",(0.17,0.34,0.034),(hx,-0.105,0.018),bevel=0.018,material=black,body=foot)
    v4.box(f"foot_{side}_heel",(0.13,0.105,0.060),(hx,0.018,0.064),bevel=0.018,material=graphite,body=foot)
    # Exposed axle cover is mounted on the fixed outer yoke cheek; the fork stays
    # clear of the narrower rotating heel and presents a defined bearing face.
    v4.cylinder(f"ankle_axle_cap_{side}",0.044,0.026,(hx+sx*0.107,0.005,0.15),rot=(0,math.pi/2,0),material=steel,body=shin)
    v4.torus(f"ankle_cap_bezel_{side}",0.031,0.003,(hx+sx*0.121,0.005,0.15),rot=(0,math.pi/2,0),material=graphite,body=shin)
    v4.cylinder(f"ankle_cap_lock_{side}",0.016,0.012,(hx+sx*0.125,0.005,0.15),rot=(0,math.pi/2,0),material=orange,body=shin)
    v4.box(f"foot_{side}_instep",(0.13,0.245,0.065),(hx,-0.119,0.079),bevel=0.024,material=ivory,body=foot)
    v4.box(f"foot_{side}_toe_cap",(0.14,0.105,0.035),(hx,-0.232,0.065),bevel=0.015,material=steel,body=foot)
    # Rubber tread lugs set into the sole underside, segmented to remain legible.
    for k in range(4):
        yy=-0.23+k*0.075
        v4.box(f"foot_{side}_tread_{k}",(0.125,0.012,0.006),(hx,yy,0.001),bevel=0.003,material=black,body=foot)
    v4.joint(f"ankle_{side}","revolute",shin,foot,origin=(hx,-0.01,0.15),axis=(1,0,0),limits=(-math.radians(30),math.radians(30)))

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