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

v4.reset()
# Materials: black grenadilla body, nickel-silver keywork, pads and selective warm trim.
wood = v4.wood("Grenadilla | satin black", light=(0.105,0.092,0.078), dark=(0.025,0.022,0.020), grain_scale=9)
wood_edge = v4.wood("Grenadilla end grain", light=(0.16,0.13,0.105), dark=(0.045,0.038,0.031), grain_scale=13)
silver = v4.metal("Nickel silver", color=(0.82,0.84,0.87), roughness=0.22, brushed=True)
bright = v4.metal("Polished silver", color=(0.96,0.97,0.98), roughness=0.14, brushed=False)
trim = v4.metal("Neck ring alloy", color=(0.76,0.61,0.39), roughness=0.28, brushed=True)
padmat = v4.rubber("Pad leather", (0.16,0.12,0.085))
felt = v4.rubber("Pad felt | charcoal", (0.045,0.040,0.034), roughness=0.92)
black = v4.plastic("Mouthpiece ebonite", (0.025,0.026,0.028), roughness=0.26)
reedmat = v4.wood("Cane reed", light=(0.55,0.34,0.17), dark=(0.30,0.15,0.07), grain_scale=18)
corkmat = v4.rubber("Neck cork", (0.48,0.29,0.13), roughness=0.88)

# Bodies and dimension helpers.
root = v4.body("instrument")
upper = v4.body("upper_key_linkage")
mid = v4.body("middle_key_linkage")
lower = v4.body("lower_key_linkage")
peg = v4.body("floor_peg")

# Main one-piece turned instrument, from top tenon through the lower joint.
v4.lathe("main_bore_body", [(0,0.115),(0.019,0.115),(0.020,0.14),(0.018,0.20),(0.017,0.40),(0.018,0.61),(0.016,0.83),(0.015,1.015),(0,1.015)], material=wood, body=root)
# Lower bore sweeps through a broad 180-degree return toward the keywork side.
# The curved centerline turns the formerly downward bell into an upward-facing bell.
lower_bend = []
for i in range(13):
    a = math.pi*i/12
    lower_bend.append((0.0, -.040+.040*math.cos(a), .115-.040*math.sin(a)))
v4.tube("curved_lower_bore", [(0,0,.14),(0,0,.115)] + lower_bend[1:] + [(0,-.080,.135)], .0185, material=wood, body=root)
# Upward-opening nickel-silver bell, seated over the return-bend outlet.
bell_y = -.080
bell_z = .106
v4.lathe("bell_shell", [(0,0),(.0175,0),(.0185,.004),(.020,.010),(.022,.017),(.025,.025),(.029,.033),(.034,.041),(.039,.048),(.043,.054),(.046,.058),(.047,.061),(.045,.063),(.042,.061),(.040,.057),(.036,.051),(.032,.044),(.027,.036),(.023,.027),(.020,.018),(.016,.008),(0,.008)], loc=(0,bell_y,bell_z), material=silver, body=root)
v4.torus("bell_rim", .0458, .0028, loc=(0,bell_y,bell_z+.061), material=bright, body=root)
v4.torus("bell_throat_ring", .019, .0015, loc=(0,bell_y,bell_z+.006), material=trim, body=root)
v4.torus("bell_socket_bead", .0215, .0011, loc=(0,bell_y,bell_z+.012), material=bright, body=root)
v4.torus("bell_flare_bead", .038, .0009, loc=(0,bell_y,bell_z+.043), material=bright, body=root)
# Dark bore aperture at the base makes the upward-facing bell read as hollow.
v4.cylinder("bell_bore_shadow", .0155, .002, (0,bell_y,bell_z+.011), material=black, body=root, bevel=0)
# Tenon and joint rings, fitting bands around the wood tube.
for z, r, width in [(0.205,.018,0.004),(0.405,.0175,.0035),(0.625,.0175,.004),(0.825,.0165,.003),(1.005,.0155,.004)]:
    v4.torus("tenon_band", r, .0015, loc=(0,0,z), material=silver, body=root)
    v4.torus("band_bead", r-.002, .00065, loc=(0,0,z+width), material=trim if z==1.005 else silver, body=root)
# Tone-hole chimneys align exactly with the twelve articulated cup centers.
cup_stations = [
    (.675,.001,.008),(.715,.001,.010),(.775,.001,.008),(.855,.001,.009),
    (.455,-.001,.008),(.500,-.001,.010),(.555,-.001,.008),(.615,-.001,.009),
    (.220,.001,.009),(.270,.001,.011),(.330,.001,.008),(.390,.001,.010)]
for z,x,cup_r in cup_stations:
    chimney_r = cup_r*.64
    v4.cylinder("tone_hole_chimney", chimney_r, .005, (x,-.0185,z), rot=(math.pi/2,0,0), material=wood_edge, body=root, bevel=.00065)
    v4.torus("tone_hole_rim", chimney_r*.90, .0010, loc=(x,-.0210,z), rot=(math.pi/2,0,0), material=silver, body=root)
    v4.cylinder("tone_hole_shadow", chimney_r*.56, .0008, (x,-.0217,z), rot=(math.pi/2,0,0), material=black, body=root, bevel=0)
# Fixed key posts and bearing blocks on both sides, 22 posts total.
post_z = [.17,.22,.29,.36,.43,.50,.57,.64,.71,.78,.85]
for side in (-1,1):
    for z in post_z:
        x = side*.016
        v4.cylinder("key_post_foot", .0042, .006, (x,-.003,z), rot=(math.pi/2,0,0), material=silver, body=root, bevel=.0006)
        v4.cylinder("key_post", .0024, .019, (x,-.014,z), rot=(math.pi/2,0,0), material=silver, body=root, bevel=.0005)
        v4.sphere("post_cap", .003, (x,-.024,z), material=bright, body=root)
# Root-mounted arms carry concentric bushings for the three longitudinal key rods.
def mount_rod_bushings(x, y, z0, z1):
    radial = math.hypot(x, y)
    ux, uy = x/radial, y/radial
    for z in (z0+.014, (z0+z1)*.5, z1-.014):
        v4.torus("key_rod_bushing", .0030, .00115, loc=(x,y,z), material=bright, body=root)
        p0 = (ux*.014, uy*.014, z)
        p1 = (x+ux*.0034, y+uy*.0034, z)
        v4.rod("bushing_mount_arm", p0, p1, .00135, material=silver, body=root)

# Three true rotating long key linkages. Each rod carries arms, shallow domed cups and pads.
def make_linkage(body, x, y, z0, z1, specs, name):
    # Main longitudinal axle turns inside root-mounted bearing rings.
    mount_rod_bushings(x,y,z0,z1)
    v4.rod(name+"_long_rod", (x,y,z0), (x,y,z1), .00165, material=silver, body=body)
    for z in (z0+.014,(z0+z1)*.5,z1-.014):
        v4.torus(name+"_axle_collarette", .0019, .0007, loc=(x,y,z), material=trim, body=body)
    for i,(z,dx,dz,rad) in enumerate(specs):
        # bent offset lever, from the vertical rod to a pad cup on the body face
        tx, ty = x+dx, -.022
        v4.rod(name+"_arm_root", (x,y,z), (x+dx*.62, -.026, z+dz*.35), .00145, material=bright, body=body)
        v4.rod(name+"_arm_sweep", (x+dx*.62,-.026,z+dz*.35), (tx,ty,z+dz), .00125, material=silver, body=body)
        # Cup rim axis points toward the body, broad flat pad closes onto tone-hole.
        v4.cylinder(name+"_key_cup", rad, .0032, (tx,ty,z+dz), rot=(math.pi/2,0,0), material=bright, body=body, bevel=.0007)
        v4.torus(name+"_rolled_cup_rim", rad*.82, .00085, (tx,ty-.0010,z+dz), rot=(math.pi/2,0,0), material=silver, body=body)
        v4.cylinder(name+"_pad_face", rad*.72, .0017, (tx,ty+.0025,z+dz), rot=(math.pi/2,0,0), material=felt, body=body, bevel=.0004)
        v4.torus(name+"_pad_leather_lip", rad*.59, .00045, (tx,ty+.0013,z+dz), rot=(math.pi/2,0,0), material=padmat, body=body)
        # Hinged cup saddle, two rounded knuckles and an exposed adjusting screw.
        v4.cylinder(name+"_cup_pivot", .0020, .012, (tx,ty-.0025,z+dz), rot=(0,math.pi/2,0), material=silver, body=body, bevel=.00035)
        for sign in (-1,1):
            v4.cylinder(name+"_hinge_knuckle", .00235, .0026, (tx+sign*.0065,ty-.0025,z+dz), rot=(0,math.pi/2,0), material=bright, body=body, bevel=.0003)
            v4.sphere(name+"_hinge_pin_head", .0015, (tx+sign*.0082,ty-.0025,z+dz), material=trim, body=body)
        v4.cylinder(name+"_adjuster", .00125, .0014, (tx,ty-.0042,z+dz), rot=(math.pi/2,0,0), material=trim, body=body, bevel=.0002)

make_linkage(upper,.012,-.020,.64,.91,[(.675,-.011,0,.008),(.715,-.011,0,.010),(.775,-.011,0,.008),(.855,-.011,0,.009)],"upper_link")
make_linkage(mid,-.012,-.020,.43,.65,[(.455,.011,0,.008),(.500,.011,0,.010),(.555,.011,0,.008),(.615,.011,0,.009)],"middle_link")
make_linkage(lower,.012,-.020,.20,.45,[(.220,-.011,0,.009),(.270,-.011,0,.011),(.330,-.011,0,.008),(.390,-.011,0,.010)],"lower_link")
# Differentiated touchpieces with swept connecting arms and inset pearl-like faces.
for b,x,z in [(upper,.012,.89),(mid,-.012,.63),(lower,.012,.43)]:
    v4.sphere("thumb_lever_pivot", .0045, (x,-.021,z), material=bright, body=b)
    v4.rod("thumb_lever", (x,-.019,z), (x-.010,-.031,z+.006), .0021, material=silver, body=b)
    v4.rod("touchpiece_stem", (x-.010,-.031,z+.006), (x-.018,-.039,z+.012), .0017, material=bright, body=b)
    v4.cylinder("touch_key", .007,.003, (x-.018,-.040,z+.012), rot=(math.pi/2,0,0), material=bright, body=b, bevel=.0007)
    v4.cylinder("touchpiece_inlay", .0042,.0011,(x-.018,-.0422,z+.012),rot=(math.pi/2,0,0),material=trim,body=b,bevel=.00025)
    v4.torus("touchpiece_bead", .0057,.00055,loc=(x-.018,-.0422,z+.012),rot=(math.pi/2,0,0),material=silver,body=b)
    # A second, smaller spatula sits beside the main touchpiece on each linkage.
    v4.rod("spatula_arm", (x,-.019,z-.018), (x+.012,-.034,z-.024), .00155, material=silver, body=b)
    v4.cylinder("spatula_touchpiece", .0048,.0025,(x+.012,-.036,z-.024),rot=(math.pi/2,0,0),material=bright,body=b,bevel=.00045)
    v4.torus("spatula_edge", .0038,.00045,loc=(x+.012,-.0378,z-.024),rot=(math.pi/2,0,0),material=trim,body=b)

# Curved, fixed metal crook: socket on upper tenon, elegant S bend, bell-side entry.
# Main bore top is z=1.015; crook rises and curves toward the player's left.
v4.cylinder("neck_socket", .018, .028, (0,0,1.018), material=wood_edge, body=root, bevel=.001)
v4.torus("neck_socket_ring", .017, .0017, loc=(0,0,1.030), material=trim, body=root)
v4.tube("neck_crook", [(0,0,1.025),(0,0,1.050),(-.004,-.001,1.073),(-.013,-.003,1.094),(-.027,-.006,1.110),(-.043,-.010,1.119),(-.057,-.014,1.121)], .0065, material=silver, body=root)
v4.cylinder("crook_mouthpiece_socket", .0073, .020, (-.059,-.014,1.121), rot=(0,math.pi/2,0), material=trim, body=root, bevel=.0006)
# Cork is confined to the neck tenon. The long ebonite mouthpiece projects outward from it.
v4.torus("mouthpiece_cork_seal", .0060,.0010,loc=(-.067,-.014,1.121),rot=(0,math.pi/2,0),material=corkmat,body=root)
v4.lathe("mouthpiece", [(0,0),(.0048,0),(.0055,.007),(.0060,.018),(.0064,.032),(.0073,.043),(.0082,.051),(.0080,.058),(.0065,.066),(.0040,.071),(0,.073)], loc=(-.057,-.014,1.121), rot=(0,-math.pi/2,0), material=black, body=root)
# Tapered cane reed lies on the flat player-facing beak; its tip reaches the mouthpiece tip.
v4.extrude("cane_reed", [(-.073,-.0038),(-.073,.0038),(-.018,.0052),(-.004,.0038),(0,0),(-.004,-.0038),(-.018,-.0052)], .0016, loc=(-.057,-.023,1.121), rot=(math.pi/2,0,0), bevel=.0003, material=reedmat, body=root)
# Fitted two-band ligature clamps the reed heel to the shank.
v4.torus("mouthpiece_ligature", .0061, .0010, loc=(-.073,-.014,1.121), rot=(0,math.pi/2,0), material=silver, body=root)
v4.torus("ligature_rear_band", .0060, .0008, loc=(-.082,-.014,1.121), rot=(0,math.pi/2,0), material=bright, body=root)
v4.cylinder("ligature_screw", .0015, .003, (-.073,-.0208,1.121), rot=(math.pi/2,0,0), material=bright, body=root, bevel=.0003)
# Discreet silver register vent hardware fitted flush to the upper joint.
v4.cylinder("register_button", .0038,.002,(.008,-.016,.875),rot=(math.pi/2,0,0),material=bright,body=root,bevel=.0004)

# Telescoping end pin; outer guide is fixed, inner pin and foot slide on Z.
v4.rod("peg_mount_strut", (0,.015,.285), (0,.047,.285), .0035, material=silver, body=root)
v4.torus("peg_guide_socket", .0052,.0018,loc=(0,.054,.285),material=silver,body=root)
v4.torus("peg_guide_collar", .008,.0012,loc=(0,.054,.272),material=bright,body=root)
v4.rod("peg_lower_mount_strut", (0,.015,.272), (0,.060,.272), .0035, material=silver, body=root)
v4.rod("floor_peg_tube", (0,.054,.285), (0,.054,.012), .0030, material=silver, body=peg)
v4.cylinder("floor_peg_lock_screw", .0026,.006,(.009,.054,.272),rot=(0,math.pi/2,0),material=trim,body=root,bevel=.0004)
v4.lathe("floor_peg_foot", [(0,0),(.004,0),(.0045,.008),(.003,.012),(0,.012)], loc=(0,.054,0), material=padmat, body=peg)

# Joints: each long rod has a physically placed axial bearing; positive peg travel retracts upward.
v4.joint("upper_key_pivot","revolute",root,upper,origin=(.012,-.020,.72),axis=(0,0,1),limits=(-.14,.14))
v4.joint("middle_key_pivot","revolute",root,mid,origin=(-.012,-.020,.52),axis=(0,0,1),limits=(-.14,.14))
v4.joint("lower_key_pivot","revolute",root,lower,origin=(.012,-.020,.30),axis=(0,0,1),limits=(-.14,.14))
v4.joint("floor_peg_slide","prismatic",root,peg,origin=(0,.054,.285),axis=(0,0,1),limits=(0,.063))

# Studio floor meets the deployed floor peg and frames all major silhouette.
v4.studio(floor_z=0.0)
v4.save(os.path.join(os.getcwd(),"asset.blend"))
print("saved asset.blend")
