"""Full-scale articulated passenger-car sun visor. Run with Blender in background."""
import math
import os
import sys
import bpy

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

v4.reset()

# Materials: typical light headliner trim, soft woven visor cover, and plated steel.
headliner = v4.fabric("warm_grey_headliner", (0.62, 0.59, 0.53), weave_scale=175)
visor_fabric = v4.fabric("visor_woven_fabric", (0.73, 0.70, 0.64), weave_scale=92)
trim = v4.plastic("cool_grey_moulded_trim", (0.40, 0.42, 0.42), roughness=0.34)
dark_trim = v4.plastic("dark_charcoal_trim", (0.055, 0.060, 0.064), roughness=0.38)
rubber = v4.rubber("clip_grip_rubber", (0.045, 0.047, 0.044))
steel = v4.metal("satin_zinc_steel", (0.49, 0.54, 0.58), roughness=0.28, brushed=True)
bright_steel = v4.metal("mirror_bright_bezel", (0.82, 0.83, 0.81), roughness=0.19, brushed=False)
mirror_mat = v4.mat("vanity_mirror", (0.66, 0.69, 0.70), metallic=0.04, roughness=0.24)
ink = v4.mat("embossed_marking", (0.20, 0.19, 0.17), roughness=0.7)
seam_thread = v4.mat("taupe_stitch_thread", (0.48, 0.43, 0.36), roughness=0.82)

# Reference dimensions in metres. The installed assembly sits at headliner height.
rail_x0, rail_x1 = -0.19, 0.195
rail_y, rail_z = 0.035, 0.328
rod_y = 0.060
pivot = (-0.17, rail_y, 0.36)
rod_z = 0.315
visor_hinge = (-0.14, rail_y, rod_z)

mount = v4.body("mount")
# A narrow reinforcement strip joins the compact pivot bracket and outer clip.
# It represents the hidden headliner substrate, not a full-width exposed mounting frame.
v4.box("headliner_reinforcement_strip", (rail_x1 - rail_x0, 0.004, 0.001),
       ((rail_x0 + rail_x1) / 2, rail_y, rail_z), bevel=0.0007, material=headliner, body=mount)
# Circular roof pivot socket, with a clean trim cap and visible fixing screws.
v4.box("pivot_anchor_foot", (0.042, 0.050, 0.014), (pivot[0], rail_y, pivot[2]),
       bevel=0.003, material=trim, body=mount)
v4.rod("anchor_reinforcement_drop", (pivot[0] - 0.016, rail_y, pivot[2] - 0.007),
       (pivot[0] - 0.016, rail_y, rail_z + 0.0005), 0.0025, square=True,
       material=trim, body=mount)
v4.cylinder("pivot_socket", 0.016, 0.018, (pivot[0], rail_y, pivot[2] - 0.009),
            material=dark_trim, body=mount)
v4.cylinder("pivot_socket_cap", 0.011, 0.004, (pivot[0], rail_y, pivot[2] - 0.020),
            material=trim, body=mount)
for dx in (-0.018, 0.018):
    v4.cylinder("anchor_screw_" + str(dx), 0.003, 0.002,
                (pivot[0] + dx, rail_y, pivot[2] + 0.007), rot=(0, 0, 0),
                material=steel, body=mount)
# Compact spring retaining clip at the visor's outboard edge.
clip_x = 0.195
v4.box("clip_back_plate", (0.026, 0.006, 0.030), (clip_x, rail_y - 0.005, 0.313),
       bevel=0.002, material=trim, body=mount)
v4.box("clip_upper_jaw", (0.030, 0.028, 0.004), (clip_x, rail_y - 0.003, 0.330),
       bevel=0.0015, material=trim, body=mount)
v4.box("clip_lower_jaw", (0.030, 0.028, 0.004), (clip_x, rail_y - 0.003, 0.300),
       bevel=0.0015, material=trim, body=mount)
v4.box("clip_lower_grip", (0.030, 0.022, 0.001), (clip_x, rail_y - 0.004, 0.3025),
       bevel=0.0008, material=rubber, body=mount)
v4.box("clip_upper_grip", (0.030, 0.022, 0.001), (clip_x, rail_y - 0.004, 0.3275),
       bevel=0.0008, material=rubber, body=mount)

# Pivoting stamped arm and the transverse steel rod that carries the visor sleeve.
arm = v4.body("swing_arm")
v4.cylinder("arm_pivot_barrel", 0.009, 0.035, pivot, rot=(math.pi / 2, 0, 0),
            material=steel, body=arm)
v4.cylinder("arm_pivot_cover", 0.012, 0.004, (pivot[0], pivot[1] - 0.020, pivot[2]),
            rot=(math.pi / 2, 0, 0), material=trim, body=arm)
# Short formed arm and a dog-leg carry the support rod from the compact roof pivot.
v4.rod("support_arm_neck", (pivot[0], rail_y, pivot[2]), (-0.145, rail_y, pivot[2]),
       0.0028, square=True, material=steel, body=arm)
v4.rod("support_arm_drop", (-0.145, rail_y, pivot[2]), (-0.145, rod_y, rod_z),
       0.0042, square=True, material=steel, body=arm)
v4.rod("visor_support_rod", (-0.17, rod_y, rod_z), (0.205, rod_y, rod_z),
       0.0045, material=steel, body=arm)
v4.cylinder("rod_end_stop", 0.007, 0.008, (0.207, rod_y, rod_z),
            rot=(0, math.pi / 2, 0), material=dark_trim, body=arm)
v4.joint("arm_swing", "revolute", mount, arm, origin=pivot, axis=(0, 0, 1),
         limits=(-math.radians(85), 0))

# Sleeve carriage slides over the support rod. Its clevis supports the visor hinge pin.
carriage = v4.body("visor_carriage")
v4.cylinder("sliding_sleeve", 0.0082, 0.030, (-0.14, rod_y, rod_z),
            rot=(0, math.pi / 2, 0), material=trim, body=carriage)
for x in (-0.153, -0.127):
    v4.torus("sleeve_lip_" + str(x), 0.0077, 0.0012, (x, rod_y, rod_z),
             rot=(0, math.pi / 2, 0), material=dark_trim, body=carriage)
v4.box("hinge_clevis", (0.026, 0.036, 0.016), (-0.14, 0.044, rod_z),
       bevel=0.004, material=trim, body=carriage)
v4.cylinder("clevis_pin", 0.0045, 0.030, (-0.14, rail_y, rod_z),
            rot=(0, math.pi / 2, 0), material=steel, body=carriage)
v4.joint("rod_slide", "prismatic", arm, carriage, origin=visor_hinge, axis=(1, 0, 0),
         limits=(0, 0.045))

# Soft rounded visor pad and its attached trim/details. Rest pose is flat under the roof.
visor = v4.body("visor")
panel_center = (0.02, -0.035, rod_z)
visor_core = v4.box("upholstered_visor_core", (0.320, 0.130, 0.024), panel_center, bevel=0.0115,
                    material=visor_fabric, body=visor)
# Apply the broad outer rounding before cutting the mirror well, so the pocket keeps crisp walls.
bpy.context.view_layer.objects.active = visor_core
visor_core.select_set(True)
for mod in list(visor_core.modifiers):
    if mod.type == "BEVEL":
        mod.width = 0.0115
        mod.segments = 8
        bpy.ops.object.modifier_apply(modifier=mod.name)
visor_core.select_set(False)
# A shallow pocket makes the mirror, glass and bezel sit below the upholstered face.
mirror_x, mirror_y = 0.035, -0.040
mirror_w, mirror_h = 0.112, 0.068
pocket_cutter = v4.box("mirror_pocket_cutter", (mirror_w - 0.008, mirror_h - 0.008, 0.004),
                       (mirror_x, mirror_y, rod_z + 0.010), bevel=0.001)
v4.boolean(visor_core, pocket_cutter)
# Low-profile welt strips touch the cloth perimeter and suggest the stitched cover seam.
welt_z = rod_z - 0.01175
for nm, size, loc in (
    ("welt_front", (0.276, 0.0015, 0.0015), (0.020, -0.085, welt_z)),
    ("welt_rear", (0.276, 0.0015, 0.0015), (0.020, 0.015, welt_z)),
    ("welt_inboard", (0.0015, 0.096, 0.0015), (-0.118, -0.035, welt_z)),
    ("welt_outboard", (0.0015, 0.096, 0.0015), (0.158, -0.035, welt_z)),
):
    v4.box(nm, size, loc, bevel=0.0006, material=seam_thread, body=visor)
# Continuous fine stitch line around the vanity face, inset consistently from each edge.
seam_z = rod_z + 0.01225
for nm, size, loc in (
    ("stitch_front", (0.288, 0.0007, 0.0005), (0.020, -0.087, seam_z)),
    ("stitch_rear", (0.288, 0.0007, 0.0005), (0.020, 0.017, seam_z)),
    ("stitch_inboard", (0.0007, 0.104, 0.0005), (-0.124, -0.035, seam_z)),
    ("stitch_outboard", (0.0007, 0.104, 0.0005), (0.164, -0.035, seam_z)),
):
    v4.box(nm, size, loc, bevel=0.00025, material=seam_thread, body=visor)
# End caps wrap the short ends of the padded panel.
for x, side in ((-0.139, "inboard"), (0.179, "outboard")):
    v4.box("visor_" + side + "_endcap", (0.004, 0.112, 0.017), (x, -0.035, rod_z),
           bevel=0.004, material=trim, body=visor)
# Long elastic ticket strap on the cabin-facing underside, away from the vanity mirror.
v4.box("elastic_ticket_strap", (0.008, 0.108, 0.002), (0.095, -0.035, rod_z - 0.013),
       bevel=0.0015, material=dark_trim, body=visor)

# Recessed fitted bezel, mirror glass and a clearly defined part-open sliding cover.
v4.box("mirror_bezel", (mirror_w, mirror_h, 0.0025), (mirror_x, mirror_y, rod_z + 0.00925),
       bevel=0.002, material=bright_steel, body=visor)
mirror_glass = v4.box("mirror_glass", (0.101, 0.057, 0.0012), (mirror_x, mirror_y, rod_z + 0.0119),
       bevel=0.003, material=mirror_mat, body=visor)
v4.box("mirror_cover_slider", (0.030, 0.062, 0.0015), (0.064, mirror_y, rod_z + 0.0114),
       bevel=0.003, material=trim, body=visor)
v4.box("mirror_cover_thumb", (0.008, 0.013, 0.0012), (0.064, mirror_y - 0.022, rod_z + 0.01275),
       bevel=0.0025, material=dark_trim, body=visor)
# Three shallow molded grip ribs make the slider legible without a printed label.
for rib in range(3):
    v4.box("cover_grip_rib_" + str(rib), (0.004, 0.0007, 0.0005),
           (0.064, mirror_y - 0.022 + (rib - 1) * 0.0032, rod_z + 0.0133),
           bevel=0.0002, material=trim, body=visor)
# The visor-side hinge barrel meets the carriage clevis at the inboard upper corner.
v4.cylinder("visor_hinge_barrel", 0.0075, 0.034, (-0.123, rail_y, rod_z),
            rot=(0, math.pi / 2, 0), material=trim, body=visor)
v4.joint("visor_drop", "revolute", carriage, visor, origin=visor_hinge, axis=(1, 0, 0),
         limits=(0, math.radians(100)))

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