"""Detailed articulated residential hopper window. Run with Blender background Python."""
import math, os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import v4lib as v4

v4.reset()

# Finishes: painted white extrusions, black EPDM, glazing, and stainless hardware.
white = v4.painted("satin_white_extrusion", (0.88, 0.90, 0.91), roughness=0.27)
white_trim = v4.painted("glazing_bead_white", (0.96, 0.965, 0.96), roughness=0.24)
seal = v4.rubber("black_epdm", color=(0.045, 0.055, 0.058))
steel = v4.metal("brushed_stainless", color=(0.68, 0.72, 0.75), roughness=0.28, brushed=True)
zinc = v4.metal("zinc_fasteners", color=(0.48, 0.52, 0.54), roughness=0.32, brushed=False)
glass = v4.glass("low_e_glass", color=(0.55, 0.74, 0.79), roughness=0.09)
spacer = v4.metal("warm_edge_spacer", color=(0.36, 0.39, 0.40), roughness=0.34, brushed=False)
black = v4.plastic("black_track_liner", color=(0.08, 0.09, 0.09), roughness=0.42)

# Coordinates: X width, Y depth (room/interior is -Y), Z up. Frame rests on Z=0.
W, H = 1.00, 0.70
frame = v4.body("frame")
# Outer frame rails and jambs. Components butt edge to edge with an extruded appearance.
# Jambs span the frame height; head and sill close the opening between them.
v4.box("left_jamb_extrusion", (0.064, 0.115, H), (-0.468, 0, H/2), bevel=0.006, material=white, body=frame)
v4.box("right_jamb_extrusion", (0.064, 0.115, H), (0.468, 0, H/2), bevel=0.006, material=white, body=frame)
v4.box("head_extrusion", (0.872, 0.115, 0.064), (0, 0, 0.668), bevel=0.006, material=white, body=frame)
# Broad sill with a slightly interior-falling nose and underside drip edge.
v4.box("sill_extrusion", (0.872, 0.135, 0.075), (0, -0.006, 0.0375), bevel=0.007, material=white, body=frame)
v4.box("sill_sloped_weather_cap", (0.872, 0.132, 0.008), (0, -0.005, 0.078), rot=(-math.radians(4),0,0), bevel=0.003, material=white_trim, body=frame)
v4.box("sill_interior_nose", (0.90, 0.026, 0.026), (0, -0.083, 0.048), bevel=0.006, material=white_trim, body=frame)
v4.box("sill_drip_underside", (0.88, 0.014, 0.009), (0, -0.040, 0.001), bevel=0.002, material=white, body=frame)
v4.box("sill_exterior_drip_lip", (0.90, 0.014, 0.012), (0, 0.065, 0.018), bevel=0.003, material=white, body=frame)
v4.box("sill_drip_groove", (0.82, 0.003, 0.002), (0, 0.049, 0.009), bevel=0.0006, material=black, body=frame)
# Vertical stainless C track is fixed flat to the room-facing inner cheek of the right jamb.
# The open side faces the sash; the carriage runs vertically and stays within the jamb footprint.
track_x, track_y, track_z = 0.456, -0.060, 0.280
v4.box("stay_track_backplate", (0.040, 0.005, 0.360), (track_x, track_y, track_z), bevel=0.002, material=steel, body=frame)
for sx in (-1, 1):
    v4.box("stay_track_rail", (0.004, 0.012, 0.360), (track_x+sx*0.018, -0.066, track_z), bevel=0.001, material=steel, body=frame)
v4.box("track_dark_channel", (0.032, 0.007, 0.330), (track_x, -0.0655, track_z), bevel=0.0, material=black, body=frame)
# Three countersunk fixings enter the jamb through the track backplate.
for zz in (0.13, 0.28, 0.43):
    v4.cylinder("track_fixing_screw", 0.0032, 0.006, (track_x, -0.061, zz), rot=(math.pi/2, 0, 0), segments=20, material=zinc, body=frame)
# Sill weep covers and drainage slots (cover is the visible low cap).
for x in (-0.32, -0.16, 0, 0.16, 0.32):
    v4.box("sill_weep_cover", (0.045, 0.008, 0.003), (x, -0.073, 0.072), bevel=0.0012, material=white_trim, body=frame)
    v4.box("sill_weep_slot", (0.026, 0.002, 0.001), (x, -0.076, 0.072), bevel=0.0004, material=black, body=frame)
# Frame assembly screw heads along jambs and head.
for z in (0.10, 0.32, 0.54):
    for x in (-0.468, 0.468):
        v4.cylinder("frame_screw_cap", 0.003, 0.004, (x, -0.057, z), rot=(math.pi/2, 0, 0), segments=20, material=zinc, body=frame)
# Diagonal seam inlays mark the four mitered frame joints on the front face.
seam = v4.metal("frame_miter_seam", color=(0.52,0.56,0.58), roughness=0.40, brushed=False)
for sx in (-1,1):
    v4.rod("frame_top_miter_line", (sx*0.436,-0.058,0.637), (sx*0.416,-0.058,0.657), 0.0007, material=seam, body=frame)
    v4.rod("frame_bottom_miter_line", (sx*0.436,-0.058,0.075), (sx*0.416,-0.058,0.095), 0.0007, material=seam, body=frame)

# Sash: 880 x 560 mm overall, 45 mm section, centered in the opening.
sash = v4.body("sash")
SX, SZ = 0.868, 0.56
left, right = -SX/2, SX/2
bottom, top = 0.075, 0.635
# Stiles rise full height, rails fit between stiles.
for x, name in ((left+0.0225, "left"), (right-0.0225, "right")):
    v4.box("sash_"+name+"_stile", (0.045, 0.060, SZ), (x, -0.049, bottom+SZ/2), bevel=0.004, material=white, body=sash)
for z, name in ((bottom+0.0225, "bottom"), (top-0.0225, "top")):
    v4.box("sash_"+name+"_rail", (SX-0.09, 0.060, 0.045), (0, -0.049, z), bevel=0.004, material=white, body=sash)
# Glass unit fits within inner sightline. Double-glazed appearance: pane and perimeter spacer line.
gz0, gz1 = bottom+0.045, top-0.045
pane_h, pane_w = gz1-gz0, SX-0.09
v4.box("insulated_glass_unit", (pane_w, 0.010, pane_h), (0, -0.049, (gz0+gz1)/2), bevel=0.0015, material=glass, body=sash)
# Thin spacer visible around the pane on its inner edge, each bar fits between the others.
for x in (-pane_w/2+0.005, pane_w/2-0.005):
    v4.box("glazing_spacer_vertical", (0.004, 0.003, pane_h-0.010), (x, -0.0555, (gz0+gz1)/2), bevel=0.0008, material=spacer, body=sash)
for z in (gz0+0.005, gz1-0.005):
    v4.box("glazing_spacer_horizontal", (pane_w-0.010, 0.003, 0.004), (0, -0.0555, z), bevel=0.0008, material=spacer, body=sash)
# Interior snap beads, segmented to avoid overlaps at corners.
for x in (-0.398, 0.398):
    v4.box("sash_glazing_bead_side", (0.008, 0.012, pane_h-0.018), (x, -0.086, (gz0+gz1)/2), bevel=0.0017, material=white_trim, body=sash)
for z in (gz0, gz1):
    v4.box("sash_glazing_bead_rail", (pane_w-0.018, 0.012, 0.008), (0, -0.086, z), bevel=0.0017, material=white_trim, body=sash)
# Inner sash gasket on perimeter, sits at the back face and compresses onto frame weather seal.
for x in (-0.414, 0.414):
    v4.box("sash_epdm_side", (0.006, 0.008, 0.516), (x, -0.083, 0.355), bevel=0.002, material=seal, body=sash)
for z in (0.094, 0.616):
    v4.box("sash_epdm_rail", (0.828, 0.008, 0.006), (0, -0.083, z), bevel=0.002, material=seal, body=sash)
# Small aluminum corner cleats suggest mechanically crimped miter joints.
for x in (-0.405, 0.405):
    for z in (0.098, 0.612):
        v4.box("sash_corner_crimp", (0.016, 0.002, 0.016), (x, -0.080, z), bevel=0.001, material=steel, body=sash)
# Stay arm panel bracket on the right stile, with a pin axis along window width.
panel_pin = (0.434, -0.083, 0.400)
v4.box("stay_panel_bracket", (0.024, 0.010, 0.070), (0.422, -0.084, 0.400), bevel=0.003, material=steel, body=sash)
for z in (0.377, 0.423):
    v4.cylinder("stay_bracket_screw", 0.0032, 0.004, (0.422, -0.089, z), rot=(math.pi/2,0,0), segments=20, material=zinc, body=sash)
# Bottom hinge plates and three knuckles on the actual sill hinge line.
hinge_z, hinge_y = 0.075, -0.052
for x in (-0.365, 0.365):
    v4.box("sash_hinge_leaf", (0.072, 0.008, 0.028), (x, -0.083, 0.090), bevel=0.0015, material=steel, body=sash)
    v4.cylinder("sash_hinge_knuckle", 0.006, 0.038, (x, hinge_y, hinge_z), rot=(0, math.pi/2, 0), segments=24, material=steel, body=sash)
    for dx in (-0.022, 0.022):
        v4.cylinder("hinge_leaf_screw", 0.0027, 0.004, (x+dx, -0.089, 0.090), rot=(math.pi/2,0,0), segments=16, material=zinc, body=sash)
# Hinge leaves seated on the fixed sill; knuckle hardware centered on axis.
for x in (-0.365, 0.365):
    v4.box("frame_hinge_leaf", (0.072, 0.008, 0.027), (x, -0.078, 0.0615), bevel=0.0015, material=steel, body=frame)
    v4.cylinder("frame_hinge_knuckle", 0.006, 0.022, (x, hinge_y, hinge_z), rot=(0, math.pi/2, 0), segments=24, material=steel, body=frame)
# Captive hinge pins visible as small polished end caps.
for x in (-0.389, 0.389):
    v4.cylinder("hinge_pin_cap", 0.004, 0.003, (x, hinge_y, hinge_z), rot=(0, math.pi/2, 0), segments=20, material=zinc, body=sash)

# Stay link occupies side plane; long stainless flat bars reach from sash pivot to shoe pin.
stay_arm = v4.body("stay_arm")
shoe_pin = (0.456, -0.083, 0.200)
# Twin narrow straps centered either side of the jamb's track face; rounded with joint bosses.
for dx in (-0.007, 0.007):
    v4.rod("stay_strap", (panel_pin[0]+dx, panel_pin[1], panel_pin[2]), (shoe_pin[0]+dx, shoe_pin[1], shoe_pin[2]), 0.0045, square=True, material=steel, body=stay_arm)
# Center reinforcing web parallel to the twin straps, linking their midpoints.
mid = tuple((panel_pin[i]+shoe_pin[i])/2 for i in range(3))
v4.box("stay_cross_bridge", (0.018, 0.010, 0.014), mid, bevel=0.003, material=steel, body=stay_arm)
for p in (panel_pin, shoe_pin):
    v4.cylinder("stay_pivot_boss", 0.009, 0.020, p, rot=(0, math.pi/2, 0), segments=28, material=steel, body=stay_arm)
# Friction adjuster screw and locknut are carried on the arm.
v4.cylinder("stay_friction_adjuster", 0.0032, 0.014, (0.4575, -0.083, 0.300), rot=(0,math.pi/2,0), segments=20, material=zinc, body=stay_arm)
v4.cylinder("stay_adjuster_cap", 0.006, 0.004, (0.4665, -0.083, 0.300), rot=(0,math.pi/2,0), segments=24, material=steel, body=stay_arm)
# Shoe carriage body: slim housing straddles the track and retains polymer rollers.
stay_shoe = v4.body("stay_shoe")
v4.box("shoe_carriage", (0.018, 0.022, 0.036), (0.456, -0.079, 0.200), bevel=0.004, material=steel, body=stay_shoe)
v4.box("shoe_liner", (0.010, 0.014, 0.024), (0.456, -0.087, 0.200), bevel=0.002, material=black, body=stay_shoe)
for z in (0.187, 0.213):
    v4.cylinder("shoe_roller", 0.006, 0.012, (0.456, -0.069, z), rot=(math.pi/2,0,0), segments=24, material=black, body=stay_shoe)
# Shoe pin, axis through width; lies between carriage and stay outer boss.
v4.cylinder("shoe_pin", 0.0045, 0.030, shoe_pin, rot=(0,math.pi/2,0), segments=24, material=zinc, body=stay_shoe)
v4.cylinder("shoe_pin_cap", 0.007, 0.003, (shoe_pin[0]+0.010, shoe_pin[1], shoe_pin[2]), rot=(0,math.pi/2,0), segments=24, material=steel, body=stay_shoe)

# Cam latch body at upper rail: interior lever rotates in the glazing plane about a Y spindle.
latch = v4.body("latch")
latch_origin = (0.0, -0.084, 0.610)
v4.box("latch_escutcheon", (0.090, 0.008, 0.054), (0, -0.089, 0.610), bevel=0.006, material=white_trim, body=latch)
v4.cylinder("latch_rosette", 0.021, 0.006, (0,-0.094,0.610), rot=(math.pi/2,0,0), segments=40, material=steel, body=latch)
v4.cylinder("latch_hub", 0.010, 0.010, (0,-0.101,0.610), rot=(math.pi/2,0,0), segments=32, material=white, body=latch)
v4.cylinder("latch_spindle", 0.006, 0.025, latch_origin, rot=(math.pi/2,0,0), segments=24, material=steel, body=latch)
# Ergonomic operating lever projects horizontally from the spindle; a molded grip and end cap finish it.
v4.rod("latch_handle", (0, -0.101, 0.610), (0.078, -0.101, 0.610), 0.0075, square=True, material=white, body=latch)
v4.cylinder("latch_grip", 0.010, 0.046, (0.052, -0.102, 0.610), rot=(0,math.pi/2,0), segments=32, material=white, body=latch)
for x in (0.034,0.070):
    v4.torus("latch_grip_groove", 0.0095, 0.0007, (x,-0.102,0.610), rot=(0,math.pi/2,0), material=white_trim, body=latch)
v4.sphere("latch_handle_tip", 0.009, (0.080, -0.101, 0.610), material=white, body=latch)
# Offset cam tongue rotates with lever and meets the frame keeper in closed position.
v4.box("latch_cam_tongue", (0.040, 0.008, 0.008), (0.057, -0.084, 0.604), bevel=0.0015, material=steel, body=latch)
# Keeper return reaches from the frame head around the sash top, outside its interior face.
v4.tube("keeper_return", [(0.105,-0.057,0.642),(0.105,-0.083,0.642),(0.105,-0.083,0.614),(0.073,-0.083,0.614)], 0.003, material=steel, body=frame)
v4.box("latch_keeper", (0.045, 0.008, 0.012), (0.073, -0.086, 0.614), bevel=0.0015, material=steel, body=frame)
for x in (0.061, 0.085):
    v4.cylinder("keeper_screw", 0.0025, 0.004, (x, -0.090, 0.614), rot=(math.pi/2,0,0), segments=16, material=zinc, body=frame)

# Joints: bottom hinge drives opening; the friction stay closes its slider loop; latch is free to turn.
v4.joint("sash_hinge", "revolute", frame, sash, origin=(0, hinge_y, hinge_z), axis=(1,0,0), limits=(0, math.radians(35)))
v4.joint("stay_track_slide", "prismatic", frame, stay_shoe, origin=shoe_pin, axis=(0,0,1), limits=(-0.08,0.22))
v4.joint("stay_shoe_pin", "revolute", stay_shoe, stay_arm, origin=shoe_pin, axis=(1,0,0), limits=(-1.2,1.2))
v4.joint("stay_panel_pin", "revolute", sash, stay_arm, origin=panel_pin, axis=(1,0,0), limits=(-1.8,1.8))
v4.joint("cam_latch", "revolute", sash, latch, origin=latch_origin, axis=(0,1,0), limits=(-0.32,0.32))

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