"""Detailed articulated front-load washer. Run with Blender's Python."""
import math, os, sys
from mathutils import Vector
import bpy
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import v4lib as v4
v4.reset()

white = v4.painted("porcelain_white_enamel", (0.91,0.92,0.90), roughness=0.24, coat=0.8)
white_dark = v4.plastic("moulded_white", (0.78,0.80,0.79), roughness=0.34)
graphite = v4.plastic("graphite_control", (0.055,0.065,0.07), roughness=0.28)
black = v4.rubber("door_seal", (0.025,0.03,0.03))
bootrubber = v4.rubber("grey_boot_rubber", (0.24,0.25,0.25))
blue_plastic = v4.plastic("inlet_blue", (0.10,0.28,0.68), roughness=0.32)
red_plastic = v4.plastic("inlet_red", (0.72,0.10,0.07), roughness=0.32)
steel = v4.metal("brushed_stainless", (0.72,0.75,0.76), roughness=0.25, brushed=True)
chrome = v4.metal("satin_chrome", (0.76,0.78,0.79), roughness=0.2, brushed=False)
darksteel = v4.metal("darkened_steel", (0.19,0.22,0.23), roughness=0.38, brushed=False)
glass = v4.glass("smoked_door_glass", (0.20,0.29,0.31), roughness=0.11)
display = v4.emissive("amber_display", (0.55,0.16,0.025), strength=1.5)
indicator = v4.emissive("green_indicator", (0.10,0.55,0.25), strength=1.2)
label = v4.mat("warm_white_print", (0.86,0.88,0.87), roughness=0.65)

# Typical 600 x 650 x 850 mm freestanding machine. Front is -Y.
W,D,H=0.600,0.650,0.850
cab=v4.body("cabinet"); door=v4.body("door"); drum=v4.body("drum")
side_t=0.018
v4.box("left_side_skin",(side_t,D,0.812),(-W/2+side_t/2,0.006,0.426),bevel=0.003,material=white,body=cab)
v4.box("right_side_skin",(side_t,D,0.812),(W/2-side_t/2,0.006,0.426),bevel=0.003,material=white,body=cab)
v4.box("top_skin",(W,D,0.018),(0,0.006,0.841),bevel=0.004,material=white,body=cab)
v4.box("rear_service_panel",(W-0.036,0.012,0.79),(0,D/2-0.006,0.425),bevel=0.002,material=white_dark,body=cab)
# Recessed rear service panel with perimeter seam and captive screw heads.
v4.box("rear_service_inset",(0.335,0.004,0.440),(0,0.327,0.435),bevel=0.003,material=white,body=cab)
v4.box("rear_service_recess",(0.292,0.002,0.394),(0,0.330,0.435),bevel=0.002,material=white_dark,body=cab)
for sx in (-1,1):
    for sz in (-1,1):
        v4.cylinder(f"rear_panel_screw_{sx}_{sz}",0.004,0.003,(sx*0.151,0.3305,0.435+sz*0.204),rot=(-math.pi/2,0,0),segments=16,material=chrome,body=cab)
# Hot/cold inlet valve fittings with coloured collars at the upper rear.
for i,(x,mat) in enumerate(((-0.095,blue_plastic),(0.095,red_plastic))):
    v4.cylinder(f"inlet_valve_{i}",0.022,0.022,(x,0.334,0.770),rot=(-math.pi/2,0,0),segments=32,radius_top=0.018,material=mat,body=cab)
# Drain outlet and corrugated drain hose; molded plug and power cord near the top rear.
v4.cylinder("drain_hose_spigot",0.014,0.026,(0.205,0.337,0.155),rot=(-math.pi/2,0,0),material=darksteel,body=cab)
v4.tube("drain_hose",[(0.205,0.349,0.155),(0.205,0.365,0.125),(0.225,0.368,0.105),(0.255,0.368,0.115),(0.270,0.365,0.155),(0.270,0.355,0.205)],0.010,material=black,body=cab,resolution=16)
# One continuous cord mesh tapers into a molded connector at the free end.
cord_a=Vector((0.235,0.329,0.735)); cord_b=Vector((0.224,0.360,0.635))
cord_axis=(cord_b-cord_a).normalized(); cord_u=cord_axis.cross(Vector((0,0,1))).normalized(); cord_v=cord_axis.cross(cord_u).normalized()
cord_verts=[]; cord_faces=[]; cord_n=16
for t,rad in ((0,0.0045),(0.68,0.0045),(0.80,0.006),(0.90,0.010),(1.0,0.010)):
    c=cord_a.lerp(cord_b,t)
    for j in range(cord_n):
        a=2*math.pi*j/cord_n
        p=c+rad*(cord_u*math.cos(a)+cord_v*math.sin(a))
        cord_verts.append(tuple(p))
for k in range(4):
    for j in range(cord_n):
        jj=(j+1)%cord_n
        cord_faces.append((k*cord_n+j,k*cord_n+jj,(k+1)*cord_n+jj,(k+1)*cord_n+j))
cord_mesh=bpy.data.meshes.new("mains_cord_connector_mesh"); cord_mesh.from_pydata(cord_verts,[],cord_faces); cord_mesh.materials.append(graphite); cord_mesh.update()
cord_obj=bpy.data.objects.new("mains_power_cord",cord_mesh); v4._link(cord_obj); v4.part(cord_obj,cab)
for poly in cord_mesh.polygons: poly.use_smooth=True
v4.box("base_pan",(W-0.036,D-0.028,0.028),(0,0.006,0.027),bevel=0.002,material=darksteel,body=cab)
front_y=-D/2+0.012
v4.box("fascia_left_upright",(0.038,0.022,0.565),(-0.281,front_y,0.416),bevel=0.003,material=white,body=cab)
v4.box("fascia_right_upright",(0.038,0.022,0.565),(0.281,front_y,0.416),bevel=0.003,material=white,body=cab)
# Enamelled front fascia closes the cabinet front around a real 360 mm porthole.
door_z=0.421; door_cx=0.0
front_panel=v4.box("front_panel",(0.564,0.015,0.5745),(0,-0.3315,0.40925),bevel=0.0015,material=white,body=cab)
porthole_cut=v4.cylinder("porthole_cutter",0.180,0.060,(0,-0.3315,door_z),rot=(math.pi/2,0,0),segments=96,bevel=0,material=None)
v4.boolean(front_panel,porthole_cut)
latch_cut=v4.box("latch_slot_cutter",(0.027,0.060,0.020),(0.218,-0.3315,door_z),bevel=0,material=None)
v4.boolean(front_panel,latch_cut)
# Molded rubber boot with shallow annular folds, running from the fascia aperture to the drum mouth.
boot_profile=[(-0.324,0.179),(-0.315,0.177),(-0.306,0.181),(-0.297,0.176),
              (-0.288,0.181),(-0.279,0.176),(-0.270,0.181),(-0.261,0.176),
              (-0.252,0.181),(-0.243,0.176),(-0.234,0.181),(-0.225,0.176),
              (-0.216,0.181),(-0.207,0.178)]
boot_verts=[]; boot_faces=[]; boot_n=96
for yb,rb in boot_profile:
    for j in range(boot_n):
        a=2*math.pi*j/boot_n
        boot_verts.append((rb*math.cos(a),yb,door_z+rb*math.sin(a)))
for k in range(len(boot_profile)-1):
    for j in range(boot_n):
        jj=(j+1)%boot_n
        boot_faces.append((k*boot_n+j,k*boot_n+jj,(k+1)*boot_n+jj,(k+1)*boot_n+j))
boot_mesh=bpy.data.meshes.new("rubber_boot_bellows_mesh")
boot_mesh.from_pydata(boot_verts,[],boot_faces); boot_mesh.materials.append(bootrubber); boot_mesh.update()
boot_obj=bpy.data.objects.new("rubber_boot_bellows",boot_mesh); v4._link(boot_obj); v4.part(boot_obj,cab)
for poly in boot_mesh.polygons: poly.use_smooth=True
v4.box("control_fascia",(0.564,0.026,0.143),(0,-0.322,0.768),bevel=0.006,material=white,body=cab)
v4.box("toe_kick",(0.562,0.021,0.086),(0,-0.322,0.079),bevel=0.005,material=graphite,body=cab)
v4.box("toe_kick_lip",(0.55,0.008,0.009),(0,-0.337,0.122),bevel=0.002,material=chrome,body=cab)
for i in range(11):
    v4.box(f"kick_vent_{i:02d}",(0.024,0.0018,0.003),(-0.205+i*0.041,-0.333,0.077),bevel=0.001,material=darksteel,body=cab)
v4.cylinder("pump_filter_cap",0.026,0.006,(-0.222,-0.338,0.081),rot=(math.pi/2,0,0),material=white_dark,body=cab)
v4.cylinder("pump_filter_grip",0.010,0.008,(-0.222,-0.344,0.081),rot=(math.pi/2,0,0),material=graphite,body=cab)
for sx in (-1,1):
    for sy in (-1,1):
        x,y=sx*0.247,sy*0.267
        v4.cylinder(f"foot_pad_{sx}_{sy}",0.027,0.009,(x,y,0.004),material=black,body=cab)
        v4.lathe(f"adjustable_foot_{sx}_{sy}",[(0,0.009),(0.019,0.009),(0.018,0.021),(0.013,0.034),(0.013,0.046),(0,0.046)],loc=(x,y,0),material=darksteel,body=cab)

door_z=0.421; door_cx=0.0
hinge_x=-0.272

# Control panel and tactile controls.
v4.box("control_pod",(0.246,0.014,0.105),(0.147,-0.340,0.771),bevel=0.006,material=graphite,body=cab)
v4.box("display_bezel",(0.088,0.007,0.041),(0.079,-0.350,0.786),bevel=0.003,material=darksteel,body=cab)
v4.box("display_window",(0.073,0.002,0.027),(0.079,-0.355,0.786),bevel=0.002,material=display,body=cab)
v4.cylinder("selector_base",0.033,0.010,(0.228,-0.350,0.774),rot=(math.pi/2,0,0),material=chrome,body=cab)
v4.cylinder("cycle_selector",0.024,0.025,(0.228,-0.367,0.774),rot=(math.pi/2,0,0),material=white_dark,body=cab)
v4.box("selector_index",(0.003,0.002,0.010),(0.228,-0.382,0.788),bevel=0.001,material=graphite,body=cab)
for i,(x,z) in enumerate(((0.131,0.795),(0.131,0.763),(0.178,0.795),(0.178,0.763))):
    v4.cylinder(f"option_button_{i}",0.008,0.006,(x,-0.352,z),rot=(math.pi/2,0,0),material=chrome,body=cab)
v4.box("detergent_drawer_face",(0.208,0.012,0.066),(-0.171,-0.337,0.782),bevel=0.004,material=white_dark,body=cab)
v4.box("drawer_grip_inset",(0.090,0.006,0.014),(-0.171,-0.3445,0.785),bevel=0.005,material=graphite,body=cab)
v4.box("drawer_pull_ridge",(0.060,0.003,0.003),(-0.171,-0.349,0.785),bevel=0.001,material=chrome,body=cab)
for sx in (-1,1):
    v4.box(f"drawer_split_line_{sx}",(0.0015,0.002,0.050),(-0.171+sx*0.101,-0.344,0.782),bevel=0,material=white,body=cab)
v4.cylinder("start_indicator",0.004,0.003,(0.273,-0.341,0.763),rot=(math.pi/2,0,0),material=indicator,body=cab)
for i,(txt,x,z) in enumerate((("COTTON",0.040,0.807),("ECO",0.197,0.815),("RINSE",0.095,0.735),("SPIN",0.194,0.735),("OFF",0.246,0.811))):
    v4.text(f"program_label_{i}",txt,0.0055,loc=(x,-0.349,z),rot=(math.pi/2,0,0),extrude_depth=0.0004,material=label,body=cab)
    width=max(0.014,len(txt)*0.0035)
    v4.box(f"label_baseline_{i}",(width,0.001,0.001),(x+width/2,-0.348, z-0.001),bevel=0,material=label,body=cab)
# Simple printed status and option icons beneath the display.
for i,x in enumerate((0.128,0.174,0.128,0.174)):
    z=(0.795,0.795,0.763,0.763)[i]
    v4.box(f"button_icon_{i}",(0.006,0.001,0.0015),(x,-0.349,z-0.014),bevel=0,material=label,body=cab)

# Open stainless basket; omitted cells in the tessellated barrel form real perforations.
segments=128; r=0.174; y_front=-0.205; y_back=0.006
rows=29
verts=[]
for iy in range(rows):
    y=y_front+(y_back-y_front)*iy/(rows-1)
    for i in range(segments):
        a=2*math.pi*i/segments
        verts.append((r*math.cos(a),y,door_z+r*math.sin(a)))
faces=[]
for iy in range(rows-1):
    for i in range(segments):
        # Six staggered rows of 8 mm punched openings, about 38 mm apart.
        if iy in (2,7,12,17,22,27) and i%5==2:
            continue
        j=(i+1)%segments
        faces.append((iy*segments+i,iy*segments+j,(iy+1)*segments+j,(iy+1)*segments+i))
mesh=bpy.data.meshes.new("perforated_drum_shell_mesh"); mesh.from_pydata(verts,[],faces); mesh.materials.append(steel); mesh.update()
obj=bpy.data.objects.new("perforated_drum_shell",mesh); v4._link(obj); v4.part(obj,drum)
for p in mesh.polygons:p.use_smooth=True
v4.cylinder("drum_rear_dish",0.175,0.012,(0,0.003,door_z),rot=(math.pi/2,0,0),material=steel,body=drum)
v4.cylinder("drum_hub",0.045,0.032,(0,0.013,door_z),rot=(math.pi/2,0,0),material=chrome,body=drum)
v4.cylinder("fixed_bearing_shaft",0.015,0.285,(0,0.1705,door_z),rot=(math.pi/2,0,0),material=darksteel,body=cab)
v4.torus("drum_opening_rolled_lip",0.169,0.006,(0,y_front,door_z),rot=(math.pi/2,0,0),material=chrome,body=drum)
# Fine brushed finish stands in for the micro-perforation pattern at this scale.
for i in range(3):
    a=2*math.pi*i/3+math.pi/2; x,z=0.145*math.cos(a),door_z+0.145*math.sin(a)
    v4.box(f"drum_lifter_{i}",(0.045,0.155,0.035),(x,-0.111,z),rot=(0,0,-a),bevel=0.006,material=steel,body=drum)

# Opening door assembly: concentric moulded bezel, dark glass and functional hinges.
door_outer=0.222
v4.torus("door_outer_chrome_ring",door_outer-0.013,0.012,(door_cx,-0.366,door_z),rot=(math.pi/2,0,0),material=chrome,body=door)
v4.torus("door_white_bezel",door_outer-0.042,0.018,(door_cx,-0.355,door_z),rot=(math.pi/2,0,0),material=white_dark,body=door)
v4.torus("door_inner_black_gasket",0.179,0.010,(door_cx,-0.349,door_z),rot=(math.pi/2,0,0),material=black,body=door)
v4.cylinder("door_glass",0.171,0.012,(door_cx,-0.354,door_z),rot=(math.pi/2,0,0),material=glass,body=door)
v4.torus("glass_edge_trim",0.164,0.002,(door_cx,-0.362,door_z),rot=(math.pi/2,0,0),material=darksteel,body=door)
v4.box("handle_recess",(0.014,0.004,0.056),(0.197,-0.350,door_z),bevel=0.006,material=graphite,body=door)
v4.box("handle_inner_grip",(0.004,0.003,0.036),(0.197,-0.353,door_z),bevel=0.002,material=chrome,body=door)
for i,z in enumerate((door_z-0.055,door_z+0.055)):
    v4.box(f"hinge_block_{i}",(0.030,0.015,0.050),(hinge_x-0.021,-0.340,z),bevel=0.004,material=white_dark,body=cab)
    v4.cylinder(f"door_hinge_knuckle_{i}",0.006,0.050,(hinge_x,-0.350,z),material=chrome,body=door)
    v4.rod(f"hinge_arm_{i}",(hinge_x,-0.350,z),(-0.215,-0.356,z),0.006,material=chrome,body=door)
v4.box("door_handle_mount",(0.020,0.010,0.060),(0.185,-0.366,door_z),bevel=0.006,material=white_dark,body=door)
v4.rod("door_pull",(0.186,-0.377,door_z-0.030),(0.186,-0.377,door_z+0.030),0.006,material=chrome,body=door)
v4.box("door_latch_stem",(0.012,0.020,0.012),(0.212,-0.344,door_z),bevel=0.002,material=darksteel,body=door)
v4.box("door_latch_hook",(0.024,0.008,0.012),(0.225,-0.331,door_z),bevel=0.002,material=chrome,body=door)

v4.joint("drum_bearing","continuous",cab,drum,origin=(0,-0.12,door_z),axis=(0,1,0))
v4.joint("door_hinge","revolute",cab,door,origin=(hinge_x,-0.350,door_z),axis=(0,0,1),limits=(-math.radians(110),0))
v4.save(os.path.join(os.getcwd(),"asset.blend"))
print("saved asset.blend")
