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

v4.reset()
# Materials
red = v4.painted('enamel_cherry_red', (0.63, 0.035, 0.055), roughness=0.24, coat=0.72)
red_dark = v4.painted('deep_red_trim', (0.31, 0.018, 0.026), roughness=0.28)
steel = v4.metal('satin_stainless', (0.76, 0.79, 0.81), roughness=0.3, brushed=True)
chrome = v4.metal('polished_pivot_steel', (0.82, 0.84, 0.86), roughness=0.19, brushed=False)
black = v4.rubber('charcoal_grip', (0.045, 0.052, 0.055))
footmat = v4.rubber('rubber_feet', (0.025, 0.028, 0.03))
fruitmat = v4.mat('ripe_cherry', (0.62, 0.018, 0.032), roughness=0.19, coat=0.28)
stemmat = v4.mat('cherry_stem', (0.16, 0.075, 0.025), roughness=0.65)
blackmat = v4.mat('pit_path_shadow', (0.055, 0.018, 0.012), roughness=0.8)

# Dimensions (metres): 150 mm wide x 190 mm deep x ~220 mm tall
cy = -0.015
frame = v4.body('frame')
# Stable, slightly rounded die-cast base and its four rubber contact pads
v4.box('base_casting', (0.092, 0.166, 0.018), (0, 0.004, 0.019), bevel=0.007, material=red, body=frame)
v4.box('base_inset', (0.071, 0.125, 0.004), (0, 0.005, 0.029), bevel=0.003, material=red_dark, body=frame)
for x in (-0.034, 0.034):
    for y in (-0.063, 0.066):
        v4.cylinder('rubber_foot', 0.012, 0.008, (x, y, 0.005), bevel=0.002, material=footmat, body=frame)
# Drafted cast column and two sculpted arms leave the crank link visible between them.
# YZ profiles are extruded across X; bevels soften the cast surfaces.
column_profile=[(0.045,0.028),(0.087,0.028),(0.083,0.112),(0.078,0.145),
                (0.071,0.157),(0.057,0.153),(0.052,0.132)]
v4.extrude('drafted_cast_column', column_profile, 0.074, loc=(-0.037,0,0),
           rot=(math.pi/2,0,math.pi/2), bevel=0.006, material=red, body=frame)
for x in (-0.039,0.039):
    v4.rod('cast_overarm', (x,0.067,0.151), (x,-0.014,0.171), 0.009, material=red, body=frame)
    v4.rod('bushing_support_web', (x,-0.014,0.171), (math.copysign(0.013,x),cy,0.178),
           0.0065, material=red, body=frame)
# Front face branding panel with small die-cast shoulders
v4.box('front_badge', (0.058, 0.006, 0.023), (0, -0.005, 0.029), bevel=0.003, material=red_dark, body=frame)
v4.text('frame_brand', 'CHERRY PIT', 0.008, (0, -0.0082, 0.029), rot=(math.pi/2,0,0),
        material=chrome, body=frame, on=None, extrude_depth=0.00035)

# Fruit cup and its integral mounting collar, centered over the working axis
v4.cylinder('cup_mount_standoff', 0.018, 0.005, (0, cy, 0.0305), bevel=0.0015, material=red_dark, body=frame)
v4.cylinder('cup_mount_boss', 0.025, 0.012, (0, cy, 0.039), bevel=0.002, material=red_dark, body=frame)
# Hollow turned stainless bowl: profile runs from inner floor, over lip, down outer wall.
v4.lathe('fruit_cup', [(0,0.045),(0.014,0.045),(0.020,0.048),(0.025,0.057),(0.029,0.072),
                       (0.031,0.078),(0.034,0.079),(0.034,0.083),(0.029,0.083),
                       (0.026,0.077),(0.022,0.062),(0.017,0.055),(0,0.055)],
         loc=(0,cy,0), material=steel, body=frame)
# Fixed guide sleeve in the bridge, with a machined flange and lower nose
v4.cylinder('guide_upper_flange', 0.017, 0.008, (0,cy,0.178), bevel=0.0015, material=chrome, body=frame)
v4.torus('bushing_rim', 0.013, 0.0012, (0,cy,0.182), material=chrome, body=frame)
v4.cylinder('guide_sleeve', 0.010, 0.076, (0,cy,0.139), bevel=0.001, material=steel, body=frame)
v4.cylinder('guide_lower_lip', 0.013, 0.006, (0,cy,0.099), bevel=0.001, material=chrome, body=frame)
# A small stripper ring controls fruit lift and centers the punch just above the cup.
stripper=v4.cylinder('stripper_plate', 0.021, 0.003, (0,cy,0.0875), bevel=0.001, material=steel, body=frame)
stripper_cut=v4.cylinder('stripper_opening_tool', 0.007, 0.012, (0,cy,0.0875), bevel=0, material=None)
v4.boolean(stripper, stripper_cut)
stripper_standoff=v4.cylinder('stripper_standoff', 0.012, 0.008, (0,cy,0.092), bevel=0.0008, material=chrome, body=frame)
stripper_standoff_cut=v4.cylinder('stripper_standoff_bore_tool', 0.007, 0.012, (0,cy,0.092), bevel=0, material=None)
v4.boolean(stripper_standoff,stripper_standoff_cut)
# Static compression coil is shown around the guide, with safe clearance to the sliding stem.
helix=[]
for i in range(161):
    t=i/160
    a=t*2*math.pi*6.0
    helix.append((0.0125*math.cos(a), cy+0.0125*math.sin(a), 0.104+0.031*t))
v4.tube('return_spring_coil', helix, 0.00115, material=chrome, body=frame)
# Pit exit: a 15 mm through-bore continues from the cup through its mount and base.
def bore_through(obj, label, z0, z1, radius=0.0075):
    cutter=v4.cylinder(label+'_tool', radius, z1-z0, (0,cy,(z0+z1)/2), segments=64, bevel=0, material=None)
    v4.boolean(obj,cutter)
for target,z0,z1 in ((bpy.data.objects['fruit_cup'],0.037,0.060),
                     (bpy.data.objects['cup_mount_boss'],0.030,0.048),
                     (bpy.data.objects['cup_mount_standoff'],0.027,0.034),
                     (bpy.data.objects['base_casting'],0.008,0.030),
                     (bpy.data.objects['base_inset'],0.025,0.033)):
    bore_through(target,'pit_chute',z0,z1)
v4.torus('pit_chute_exit_bezel',0.0082,0.0008,(0,cy,0.010),material=chrome,body=frame)
# Two socket screws on the cup boss and detailed front column rivet pattern
for x in (-0.019,0.019):
    v4.cylinder('cup_mount_screw', 0.0028, 0.002, (x,cy,0.046), bevel=0.0004, material=chrome, body=frame)
for x in (-0.025,0.025):
    for z in (0.082,0.108,0.134):
        v4.cylinder('column_rivet', 0.0033, 0.003, (x,0.049,z), rot=(math.pi/2,0,0), bevel=0.0006, material=chrome, body=frame)
# Enamel maker plate on the drafted column front.
v4.box('column_brand_plate',(0.052,0.004,0.019),(0,0.049,0.061),bevel=0.0025,material=red_dark,body=frame)
v4.text('column_brand','PITTER',0.006,(0,0.0466,0.061),rot=(math.pi/2,0,0),
        material=chrome,body=frame,on=None,extrude_depth=0.00025)

# Cherry sits in the bowl; a punched central bore clears the pin path and shows the dimple.
cherry = v4.body('cherry')
fruit = v4.sphere('cherry_fruit', 0.0175, (0,cy,0.0625), segments=56, material=fruitmat, body=cherry)
# Pin bore: radius 4.2 mm leaves visible fruit shoulders while giving the punch clearance.
cutter = v4.cylinder('temporary_bore', 0.0042, 0.050, (0,cy,0.0625), bevel=0, material=None, body=None)
v4.boolean(fruit, cutter)
# Offset shallow calyx dimple and a bent stem distinguish the stem from the pit path.
dimple=v4.sphere('stem_dimple_tool',0.0032,(0.008,cy,0.079),segments=24,material=None)
v4.boolean(fruit,dimple)
v4.torus('pit_channel_shadow_ring', 0.0047, 0.0003, (0,cy,0.0795), material=blackmat, body=cherry)
v4.rod('fruit_stem', (0.008,cy,0.077), (0.010,cy+0.002,0.091), 0.00125, bevel=0.0002, material=stemmat, body=cherry)
v4.joint('fruit_seated', 'fixed', frame, cherry, origin=(0,cy,0.061), axis=(0,0,1))

# Moving punch: guided stem, top thumb pad, clevis and hardened pitting pin all move together.
plunger = v4.body('plunger')
v4.cylinder('plunger_stem', 0.0048, 0.072, (0,cy,0.134), bevel=0.0007, material=steel, body=plunger)
v4.cylinder('thumb_pad_neck', 0.008, 0.008, (0,cy,0.174), bevel=0.001, material=chrome, body=plunger)
v4.lathe('thumb_pad', [(0,0),(0.008,0),(0.010,0.002),(0.012,0.006),(0.011,0.010),(0.007,0.012),(0,0.012)],
         loc=(0,cy,0.178), material=black, body=plunger)
# Side lug carries the connecting-link pin outside the guide sleeve's swept envelope.
v4.box('plunger_link_lug', (0.034,0.010,0.009), (0.012,cy,0.166), bevel=0.002, material=steel, body=plunger)
for x in (0.015,0.029):
    v4.box('clevis_cheek', (0.0035,0.010,0.020), (x,cy,0.166), bevel=0.0012, material=chrome, body=plunger)
v4.cylinder('plunger_link_pin', 0.0025, 0.024, (0.022,cy,0.166), rot=(0,math.pi/2,0), bevel=0.0004, material=chrome, body=plunger)
# Tapered hardened pin begins directly below the guide stem and enters the fruit at full stroke.
v4.cylinder('pit_pin_shank', 0.0026, 0.044, (0,cy,0.116), bevel=0.0003, material=chrome, body=plunger)
v4.cylinder('pit_pin_tip', 0.0012, 0.006, (0,cy,0.091), radius_top=0.0026, bevel=0.0001, material=chrome, body=plunger)
# Actual slider is the frame closure in the crank-slider loop; rest at zero, 36 mm downward.

# Hinged lever assembly, including cheeks, forged crank, and a soft oval grip.
lever = v4.body('squeeze_handle')
pivot=(0,0.045,0.153)
# Paired cast ears carry the transverse hinge axle from the bridge.
for x in (-0.039,0.039):
    v4.box('frame_hinge_ear', (0.012,0.032,0.027), (x,0.045,0.153), bevel=0.004, material=red, body=frame)
# Flat forged arm runs from the pivot forward to the rounded grip.
v4.rod('lever_crossbar', (0,0.045,0.153), (-0.062,0.045,0.153), 0.008, material=red_dark, body=lever)
v4.rod('lever_arm', (-0.062,0.045,0.153), (-0.062,-0.113,0.178), 0.008, material=red_dark, body=lever)
v4.sphere('lever_pivot_boss', 0.014, pivot, material=red, body=lever)
v4.rod('lever_crank_web', (0.022,0.045,0.153), (0.022,-0.005,0.143), 0.0045, material=red_dark, body=lever)
# grip sleeves the end of the arm, with rounded ends and shallow finger ribs
v4.lathe('soft_grip', [(0,0),(0.009,0),(0.014,0.006),(0.015,0.016),(0.015,0.071),
                       (0.014,0.081),(0.009,0.086),(0,0.086)],
         loc=(-0.062,-0.112,0.178), rot=(math.pi/2,0,0), material=black, body=lever)
# Note: grip's local Z maps to -Y; centered placement aligns it with arm's front portion.
# Slim molded grip rings are collars around grip, on its axis.
for y in (-0.129,-0.145,-0.161,-0.177,-0.193):
    v4.torus('grip_molded_rib', 0.014, 0.0007, (-0.062,y,0.178), rot=(math.pi/2,0,0), material=black, body=lever)
# Visible double-sided hinge washers and domed pin heads.
v4.cylinder('lever_axle', 0.0045, 0.096, pivot, rot=(0,math.pi/2,0), bevel=0.0007, material=chrome, body=lever)
for x in (-0.039,0.039):
    side=math.copysign(1,x)
    v4.torus('hinge_washer',0.0085,0.0012,(side*0.046,0.045,0.153),rot=(0,math.pi/2,0),material=chrome,body=lever)
    v4.cylinder('lever_axle_cap', 0.0065, 0.005, (side*0.049,0.045,0.153), rot=(0,math.pi/2,0), bevel=0.0015, material=steel, body=lever)
    v4.sphere('hinge_pin_crown',0.0048,(side*0.052,0.045,0.153),material=chrome,body=lever)
# Connecting rod pivots at its upper crank pin and lower slider clevis.
link = v4.body('drive_link')
link_top=(0.022,-0.005,0.143)
link_bottom=(0.022,cy,0.166)
v4.rod('link_rod', link_top, link_bottom, 0.0040, material=steel, body=link)
for p in (link_top,link_bottom):
    v4.sphere('link_eye', 0.0060, p, material=chrome, body=link)
# Link transverse pivot pins are integral to link eyes in appearance; separate metal end caps.
for x in (0.0158,0.0282):
    v4.cylinder('link_pin_cap', 0.0028, 0.002, (x,-0.005,0.143), rot=(0,math.pi/2,0), bevel=0.0003, material=steel, body=link)

# Joint tree with the frame as the sole root, plus a real closed crank-slider loop.
v4.joint('plunger_slide','prismatic',frame,plunger,origin=(0,cy,0.11),axis=(0,0,1),limits=(-0.036,0))
v4.joint('link_to_plunger','revolute',plunger,link,origin=link_bottom,axis=(1,0,0),limits=(-0.55,0.55))
v4.joint('handle_hinge','revolute',frame,lever,origin=pivot,axis=(1,0,0),limits=(0,0.55))
v4.joint('crank_pin','revolute',lever,link,origin=link_top,axis=(1,0,0),limits=(-0.55,0.55))

# Studio render bounds and save in true rest pose.
v4.save(os.path.join(os.getcwd(),'asset.blend'))
print('saved asset.blend')
