"""Detailed queen vertical wall bed. Run with Blender: blender -b --python build.py."""
import math
import os
import sys

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

v4.reset()

# Materials: painted maple casework, charcoal bed steel, satin nickel pivots.
paint = v4.mat("warm_ivory_enamel", (0.79, 0.75, 0.67), roughness=0.3, coat=0.25)
paint_edge = v4.wood("painted_maple_edge", light=(0.75, 0.68, 0.56), dark=(0.60, 0.50, 0.39), grain_scale=8)
walnut = v4.wood("smoked_oak_facade", light=(0.39, 0.24, 0.14), dark=(0.20, 0.105, 0.055), grain_scale=4.5, roughness=0.35)
steel = v4.metal("graphite_powdercoat", color=(0.12, 0.14, 0.16), roughness=0.32, brushed=False)
nickel = v4.metal("satin_zinc", color=(0.62, 0.66, 0.68), roughness=0.27)
bronze = v4.metal("dark_bronze", color=(0.30, 0.20, 0.12), roughness=0.26, brushed=False)
rubber = v4.rubber("black_floor_rubber", (0.035, 0.04, 0.04))
fabric = v4.fabric("oatmeal_woven_mattress", (0.68, 0.60, 0.48), weave_scale=210)
seam = v4.fabric("mattress_welt", (0.48, 0.40, 0.31), weave_scale=260)
label = v4.mat("maker_badge", (0.15, 0.17, 0.18), metallic=0.65, roughness=0.28)
interior = v4.mat("warm_matte_interior_laminate", (0.50, 0.455, 0.37), roughness=0.72)
headboard_cloth = v4.fabric("stone_oat_headboard", (0.49, 0.42, 0.34), weave_scale=330)
webbing = v4.fabric("mushroom_retainer_webbing", (0.31, 0.24, 0.17), weave_scale=420)
support_metal = v4.metal("brushed_champagne_supports", color=(0.48, 0.40, 0.29), roughness=0.31)
lamp_lens = v4.mat("warm_reading_lens", (0.96, 0.73, 0.42), roughness=0.38, coat=0.08)
toe_finish = v4.mat("recessed_toe_shadow", (0.23, 0.205, 0.17), roughness=0.74)

# Dimensions in metres, with the cabinet against +Y and the room in -Y.
W, D, H = 1.72, 0.44, 2.42
T = 0.020
PIVOT_Z = 0.28
L = 2.04
BED_W = 1.55
BED_CY = -0.145
FASCIA_Y = -0.255
Y_INSET = 0.22
# Center the stored door face within the case opening; the negative X offset
# compensates for the right-heavy reveal in the reviewed front view.
FRAME_X_OFFSET = -0.020
FRAME_Z_OFFSET = -0.090

cab = v4.body("cabinet")
# Carcase sides and back, butt-jointed panels with eased exposed edges.
for sx, side in ((-1, "left"), (1, "right")):
    v4.box("cabinet_side_" + side, (T, D, H - 0.04), (sx * (W/2-T/2), 0.17, (H-0.04)/2),
           bevel=0.003, material=paint, body=cab)
v4.box("cabinet_back", (W-2*T, T, H-0.04), (0, 0.17 + D/2 - T/2, (H-0.04)/2),
       bevel=0.001, material=interior, body=cab)
v4.box("cabinet_top", (W, D+0.025, 0.045), (0, 0.17, H-0.0225), bevel=0.006, material=paint, body=cab)
v4.box("cabinet_plinth", (W-0.035, 0.12, 0.12), (0, 0.33, 0.06), bevel=0.008, material=paint, body=cab)
# Recessed toe-kick shadow panel links the lower cabinet cheeks.
v4.box("recessed_toe_kick", (1.70, 0.018, 0.028), (0, -0.040, 0.018),
       bevel=0.004, material=toe_finish, body=cab)
for sx in (-1, 1):
    v4.box("toe_kick_end_cap_%s" % sx, (0.014, 0.021, 0.030), (sx*0.795, -0.040, 0.018),
           bevel=0.003, material=paint_edge, body=cab)
# Slim casing around the folded bed aperture.
for sx in (-1, 1):
    v4.box("face_stile_%s" % sx, (0.045, 0.025, 2.11), (sx*0.827, -0.052, 1.185),
           bevel=0.004, material=paint, body=cab)
# Wall fixing cleats and visible fixing screws on the rear carcase.
for z in (0.20, 2.12):
    v4.box("wall_cleat_%.2f" % z, (1.34, 0.035, 0.07), (0, 0.386, z), bevel=0.002, material=steel, body=cab)
    for ai, x in enumerate((-0.55, 0.55)):
        v4.cylinder("wall_anchor_%.2f_%d" % (z, ai), 0.009, 0.008, (x, 0.4075, z),
                    rot=(math.pi/2, 0, 0), segments=24, material=nickel, body=cab)

# Pair of formed steel pivot plates, washers, pin barrels, and mounting bolts.
for sx, side in ((-1, "L"), (1, "R")):
    x = sx * 0.79
    outline = [(-0.105, -0.09), (0.105, -0.09), (0.105, 0.085), (0.06, 0.14),
               (-0.06, 0.14), (-0.105, 0.085)]
    # Extruded in local XY, turned to present its face across the bed width.
    v4.extrude("pivot_bracket_"+side, outline, 0.012, loc=(x-0.006, 0.108, PIVOT_Z),
               rot=(0, math.pi/2, 0), bevel=0.002, material=steel, body=cab)
    v4.box("pivot_mount_standoff_"+side, (0.044,0.08,0.06), (sx*0.818,0.21,PIVOT_Z),
           bevel=0.003, material=steel, body=cab)
    boss_x = sx*0.8135
    v4.cylinder("pivot_boss_"+side, 0.032, 0.035, (boss_x, 0.095, PIVOT_Z),
                rot=(0, math.pi/2, 0), material=nickel, body=cab)
    v4.cylinder("pivot_pin_cap_"+side, 0.022, 0.009, (sx*0.836, 0.095, PIVOT_Z),
                rot=(0, math.pi/2, 0), material=bronze, body=cab)
    for bi, zoff in enumerate((-0.065, 0.075)):
        v4.cylinder("bracket_bolt_"+side+"_%d" % bi, 0.009, 0.004,
                    (sx*0.798, 0.095, PIVOT_Z+zoff), rot=(0, math.pi/2, 0),
                    segments=6, material=nickel, body=cab)
    # A visible stepped bearing washer and a pair of inset fastener heads.
    v4.cylinder("pivot_washer_outer_"+side, 0.043, 0.008, (sx*0.838, 0.095, PIVOT_Z),
                rot=(0, math.pi/2, 0), material=steel, body=cab)
    for bi, zoff in enumerate((-0.050, 0.055)):
        v4.cylinder("pivot_plate_screw_"+side+"_%d" % bi, 0.006, 0.006,
                    (sx*0.839, 0.095, PIVOT_Z+zoff), rot=(0, math.pi/2, 0),
                    segments=6, material=bronze, body=cab)

# Upholstered inner headboard on the cabinet back, with stitched vertical channels.
v4.box("upholstered_headboard", (1.46, 0.050, 1.50), (0, 0.345, 1.43),
       bevel=0.035, material=headboard_cloth, body=cab)
for i, x in enumerate((-0.57, -0.285, 0.0, 0.285, 0.57)):
    v4.box("headboard_channel_%02d" % i, (0.255, 0.016, 1.40), (x, 0.315, 1.43),
           bevel=0.006, material=headboard_cloth, body=cab)
for xi, x in enumerate((-0.69, -0.46, -0.23, 0.0, 0.23, 0.46, 0.69)):
    for zi, z in enumerate((0.88, 1.25, 1.62, 1.99)):
        v4.sphere("headboard_tuft_%d_%d" % (xi,zi), 0.009,
                  (x, 0.311, z), segments=12, material=paint_edge, body=cab)

# Paired wall-mounted warm reading lamps inside the head-end cabinet cheeks.
for sx, side in ((-1,"L"),(1,"R")):
    v4.cylinder("reading_lamp_mount_"+side, 0.034, 0.012, (sx*0.839, 0.17, 0.58),
                rot=(0, math.pi/2, 0), material=bronze, body=cab)
    v4.rod("reading_lamp_arm_"+side, (sx*0.83, 0.17, 0.58), (sx*0.82, 0.10, 0.58),
           0.009, material=nickel, body=cab)
    v4.cylinder("reading_lamp_shade_"+side, 0.040, 0.040, (sx*0.82, 0.10, 0.58),
                rot=(0, -sx*math.pi/2, 0), radius_top=0.026, material=bronze, body=cab)
    v4.cylinder("reading_lamp_lens_"+side, 0.026, 0.006, (sx*0.80, 0.10, 0.58),
                rot=(0, -sx*math.pi/2, 0), material=lamp_lens, body=cab)

# Moving bed frame and the mattress/facade mounted rigidly to it.
frame = v4.body("bed_frame")
# Side rails and end rails form the perimeter; crossmembers carry the slats.
for sx in (-1, 1):
    rail_inset = 0.012 if sx == -1 else 0.0
    v4.box("frame_long_rail_%s" % sx, (0.044, 0.105, L), (sx*0.75175 + rail_inset, BED_CY, PIVOT_Z+L/2),
           bevel=0.008, material=steel, body=frame)
    for zi, z in enumerate((PIVOT_Z+0.12, PIVOT_Z+L-0.12)):
        v4.box("facade_frame_spacer_%s_%d" % (sx,zi), (0.04,0.045,0.06),
               (sx*0.72,-0.218,z), bevel=0.003, material=steel, body=frame)
for k, z in enumerate((PIVOT_Z+0.055, PIVOT_Z+L-0.055)):
    v4.box("frame_end_rail_%d" % k, (1.435, 0.105, 0.055), (0, BED_CY, z), bevel=0.006, material=steel, body=frame)
for i in range(13):
    z = PIVOT_Z + 0.12 + i*0.15
    if z > PIVOT_Z+L-0.12: break
    v4.box("birch_slat_%02d" % i, (1.439, 0.036, 0.018), (0, -0.11, z),
           bevel=0.006, material=paint_edge, body=frame)
# Mattress: softly rounded block, piping, stitched top grid, and a pillow near the head.
mattress = v4.box("queen_mattress", (1.439, 0.18, 1.90), (0, -0.023, PIVOT_Z+L/2),
                  bevel=0.045, material=fabric, body=frame)
# A compact lofted pillow nests against the mattress near the head.
v4.box("head_pillow", (0.90, 0.12, 0.40), (0, 0.092, PIVOT_Z+0.24),
       bevel=0.024, material=fabric, body=frame)
# Two low-profile woven retaining bands lie across the mattress top.
for i, z in enumerate((PIVOT_Z+0.92, PIVOT_Z+1.72)):
    v4.box("mattress_retainer_%d" % i, (1.40, 0.012, 0.046), (0, 0.071, z),
           bevel=0.004, material=webbing, body=frame)
    for sx in (-1, 1):
        v4.box("retainer_buckle_%d_%s" % (i,sx), (0.055, 0.010, 0.060),
               (sx*0.56, 0.078, z), bevel=0.006, material=bronze, body=frame)

# Finished cabinet-facing surface on the outer side of the stored bed: twin Shaker panels.
FACE_X = 0.020
v4.box("facade_backer", (1.60, 0.032, 2.025), (FACE_X, FASCIA_Y, PIVOT_Z+L/2),
       bevel=0.009, material=walnut, body=frame)
# Moving upper filler closes the shallow reveal at the case top in the stored pose.
v4.box("facade_top_valance", (1.60, 0.032, 0.15),
       (FACE_X, FASCIA_Y, PIVOT_Z+L/2+1.0125+0.075),
       bevel=0.006, material=walnut, body=frame)
for sx, side in ((-1,"left"),(1,"right")):
    cx = FACE_X + sx*0.385
    # Slim perimeter bead and recessed central field, all attached to the moving facade.
    for k, (size, loc) in enumerate([
        ((0.035,0.012,1.90),(cx-0.34, FASCIA_Y-0.023, PIVOT_Z+L/2)),
        ((0.035,0.012,1.90),(cx+0.34, FASCIA_Y-0.023, PIVOT_Z+L/2)),
        ((0.68,0.012,0.035),(cx, FASCIA_Y-0.023, PIVOT_Z+0.09)),
        ((0.68,0.012,0.035),(cx, FASCIA_Y-0.023, PIVOT_Z+L-0.09))]):
        v4.box("facade_%s_moulding_%d" % (side,k), size, loc, bevel=0.006, material=walnut, body=frame)
    v4.box("facade_%s_inset" % side, (0.59,0.008,1.70), (cx,FASCIA_Y-0.020,PIVOT_Z+L/2),
           bevel=0.016, material=paint, body=frame)
    # A recessed inner border gives the painted field a crisp, crafted panel edge.
    for k, (size, loc) in enumerate([
        ((0.012,0.006,1.58),(cx-0.275,FASCIA_Y-0.026,PIVOT_Z+L/2)),
        ((0.012,0.006,1.58),(cx+0.275,FASCIA_Y-0.026,PIVOT_Z+L/2)),
        ((0.55,0.006,0.012),(cx,FASCIA_Y-0.026,PIVOT_Z+0.44)),
        ((0.55,0.006,0.012),(cx,FASCIA_Y-0.026,PIVOT_Z+L-0.44))]):
        v4.box("facade_%s_inner_bead_%d" % (side,k), size, loc, bevel=0.003, material=walnut, body=frame)
# Pair of low-profile vertical pulls meet the center stile.
for sx in (-1,1):
    x = FACE_X + sx*0.045
    v4.box("facade_pull_backplate_%s" % sx, (0.055,0.010,0.16), (x,FASCIA_Y-0.031,PIVOT_Z+1.02),
           bevel=0.008, material=bronze, body=frame)
    v4.rod("facade_pull_%s" % sx, (x,FASCIA_Y-0.041,PIVOT_Z+0.985),
           (x,FASCIA_Y-0.041,PIVOT_Z+1.055), 0.006, material=bronze, body=frame)
v4.box("maker_badge", (0.15,0.006,0.035), (FACE_X,FASCIA_Y-0.019,PIVOT_Z+0.15),
       bevel=0.003, material=label, body=frame)
v4.text("maker_wordmark", "NORTHLINE  /  WALL BED", 0.011, loc=(FACE_X,FASCIA_Y-0.023,PIVOT_Z+0.15),
        rot=(math.pi/2,0,0), extrude_depth=0.0004, material=nickel, body=frame)

# Paired folding floor supports, tied together by a transverse stretcher.
legs = v4.body("folding_legs")
LEG_LEN = 0.16
LEG_X = 0.69
LEG_Y = BED_CY
HINGE_Z = PIVOT_Z+L-0.055
# Frame-side hinge ears reach the paired leg knuckles at the outer support width.
for sx, side in ((-1,"L"),(1,"R")):
    ear_end = LEG_X - 0.01
    v4.box("leg_hinge_ear_"+side, (ear_end-0.70,0.02,0.008),
           (sx*(0.70+ear_end)/2,LEG_Y,HINGE_Z), bevel=0.002, material=steel, body=frame)
for sx, side in ((-1,"L"),(1,"R")):
    x = sx*LEG_X
    # Uprights are folded down the facade at rest and become floor posts when deployed.
    v4.rod("support_leg_"+side, (x,LEG_Y,HINGE_Z-LEG_LEN), (x,LEG_Y,HINGE_Z-0.027), 0.022,
           material=support_metal, body=legs, bevel=0.002)
    v4.cylinder("leg_foot_pad_"+side, 0.018, 0.022, (x,LEG_Y,HINGE_Z-LEG_LEN),
                rot=(0,0,0), material=rubber, body=legs)
    v4.cylinder("leg_hinge_knuckle_"+side, 0.027, 0.020, (x,LEG_Y,HINGE_Z),
                rot=(0,math.pi/2,0), material=nickel, body=legs)
cross_y = -0.215
cross_z = HINGE_Z-0.135
v4.rod("leg_cross_stretcher", (-LEG_X,cross_y,cross_z), (LEG_X,cross_y,cross_z),
       0.014, material=support_metal, body=legs)
for sx, side in ((-1,"L"),(1,"R")):
    v4.rod("stretcher_drop_"+side, (sx*LEG_X,LEG_Y,cross_z), (sx*LEG_X,cross_y,cross_z),
           0.010, material=support_metal, body=legs)
    v4.cylinder("leg_adjuster_collar_"+side, 0.026, 0.042,
                (sx*LEG_X, LEG_Y, HINGE_Z-LEG_LEN+0.045),
                material=bronze, body=legs)
# Bright transverse foot rail joins the two posts just under the bed frame.
v4.box("support_foot_rail", (1.38, 0.040, 0.030), (0, LEG_Y, HINGE_Z-0.040),
       bevel=0.008, material=support_metal, body=legs)
# An outboard crossbar and short swept arms remain visible beyond the door skin when deployed.
FOOT_OUT_Y = LEG_Y-0.080
FOOT_OUT_Z = HINGE_Z+0.100
v4.box("deployed_foot_crossbar", (1.38, 0.045, 0.050), (0, FOOT_OUT_Y, FOOT_OUT_Z),
       bevel=0.010, material=support_metal, body=legs)
for sx, side in ((-1,"L"),(1,"R")):
    v4.rod("foot_crossbar_arm_"+side,
           (sx*LEG_X, LEG_Y-0.020, HINGE_Z+0.015),
           (sx*LEG_X, FOOT_OUT_Y, FOOT_OUT_Z),
           0.013, material=support_metal, body=legs)

# Real hinge axes: the main frame rotates out from the cabinet, then the U support folds down.
# Translate the complete moving assembly inward so its stored facade closes flush with the case.
frame.location.y += Y_INSET
frame.location.x += FRAME_X_OFFSET
frame.location.z += FRAME_Z_OFFSET
legs.location.y += Y_INSET
legs.location.x += FRAME_X_OFFSET
legs.location.z += FRAME_Z_OFFSET
v4.joint("bed_pivot", "revolute", cab, frame, origin=(FRAME_X_OFFSET,-0.125+Y_INSET,PIVOT_Z+FRAME_Z_OFFSET), axis=(1,0,0),
         limits=(0, math.radians(90)))
v4.joint("leg_fold", "revolute", frame, legs, origin=(FRAME_X_OFFSET,LEG_Y+Y_INSET,HINGE_Z+FRAME_Z_OFFSET), axis=(1,0,0),
         limits=(-math.radians(90), 0))

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