"""Detailed articulated wooden spring snap trap. Run with Blender's Python."""
import math
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import v4lib as v4

v4.reset()

# Soft pine board, zinc plated wire and a dark stamped trigger plate.
pine = v4.wood("warm_pine", light=(0.78, 0.59, 0.36), dark=(0.51, 0.32, 0.17), grain_scale=8, roughness=0.58)
endgrain = v4.wood("pine_endgrain", light=(0.72, 0.51, 0.30), dark=(0.44, 0.26, 0.13), grain_scale=13, roughness=0.63)
zinc = v4.metal("zinc_plated_wire", color=(0.73, 0.76, 0.77), roughness=0.31, brushed=False)
steel = v4.metal("spring_steel", color=(0.52, 0.56, 0.58), roughness=0.26, brushed=True)
black = v4.metal("blackened_trigger_steel", color=(0.25, 0.28, 0.29), roughness=0.38, brushed=False)
ink = v4.mat("dark_blue_ink", (0.12, 0.20, 0.22), roughness=0.82)

base = v4.body("base")
bail = v4.body("bail")
hold = v4.body("hold_down_bar")
treadle = v4.body("treadle")

# Board: 105 x 50 x 9 mm, with softly broken pine edges.
L, W, T = 0.105, 0.050, 0.009
v4.box("pine_board", (W, L, T), (0, 0, T/2), bevel=0.0015, material=pine, body=base)
# Subtle visible end grain at the front face; a thin, attached face veneer.
v4.box("front_endgrain", (W-0.004, 0.00035, T-0.002), (0, -L/2+0.00018, T/2), bevel=0.00025, material=endgrain, body=base)

# Four small countersunk fixing nails, flush in the board near its corners.
for ix, x in enumerate((-0.019, 0.019)):
    for iy, y in enumerate((-0.043, 0.006)):
        v4.cylinder(f"board_nail_{ix}_{iy}", 0.0020, 0.0007, (x, y, T+0.00025),
                    segments=20, material=steel, body=base)
        v4.box(f"nail_slot_{ix}_{iy}", (0.0020, 0.00028, 0.00012), (x, y, T+0.00061),
               bevel=0.00008, material=black, body=base)

# Rear staple bridge and two spring anchor legs. All stationary spring hardware is
# carried by the base, with the hinge axis crossing the board in X.
axis_y, axis_z = 0.0120, 0.0200
for side, x in enumerate((-0.020, 0.020)):
    # Small flattened staple feet on the board, uprights, and rounded crown.
    v4.box(f"spring_staple_foot_{side}", (0.006, 0.006, 0.001), (x, axis_y, T+0.0005),
           bevel=0.00045, material=zinc, body=base)
    v4.rod(f"spring_staple_leg_{side}", (x, axis_y, T+0.001), (x, axis_y, axis_z), 0.00115,
           material=zinc, body=base)
    v4.torus(f"spring_hinge_collar_{side}", 0.0018, 0.00055, (x, axis_y, axis_z),
             rot=(0, math.pi/2, 0), material=steel, body=base)
# A compact two-turn torsion helix, with tangential anchor legs seated at both feet.
coil_points = []
for i in range(81):
    u = i / 80.0
    theta = -2.0 * math.pi + u * 4.0 * math.pi
    coil_points.append((-0.0045 + 0.009*u,
                        axis_y + 0.0033*math.cos(theta),
                        axis_z + 0.0033*math.sin(theta)))
v4.tube("torsion_coil", coil_points, 0.0008, material=steel, body=base, resolution=10)
# Transverse hinge axle visibly passes through the coil and both bearing ears.
v4.rod("spring_hinge_axle", (-0.023, axis_y, axis_z), (0.023, axis_y, axis_z), 0.00105,
       material=steel, body=base)
# One torsion leg bears on the board; the opposing leg moves with the bail arm.
v4.tube("spring_ground_leg", [(-0.0045, axis_y, axis_z), (-0.010, axis_y-0.005, 0.015),
                               (-0.016, axis_y-0.009, T+0.001)], 0.00085, material=steel, body=base)

# Front pivot staple for the hold-down lever and trigger pedal.
front_pivot_y = -0.044
for side, x in enumerate((-0.017, 0.017)):
    v4.box(f"trigger_staple_foot_{side}", (0.006, 0.005, 0.001), (x, front_pivot_y, T+0.0005),
           bevel=0.00035, material=zinc, body=base)
    v4.rod(f"trigger_staple_leg_{side}", (x, front_pivot_y, T+0.001), (x, front_pivot_y, 0.016), 0.001,
           material=zinc, body=base)
    v4.torus(f"trigger_pivot_boss_{side}", 0.0022, 0.00075, (x, front_pivot_y, 0.0155),
             rot=(0, math.pi/2, 0), material=steel, body=base)

# Small restrained maker stamp printed on exposed rear board area (up-facing).
v4.text("maker_mark", "VICTOR  /  NO. 1", 0.0044, (0, 0.014, T+0.00008),
        rot=(0, 0, 0), extrude_depth=0.00008, material=ink, body=base)

# Bail: one bent, plated wire U. The cocked striker is raised over the pedal;
# the full half-turn carries it down onto the board at the far end of its stroke.
x_edge = 0.0205
bail_path = [(-x_edge, axis_y, axis_z), (-x_edge, 0.004, 0.022),
             (-x_edge, -0.014, 0.027), (-x_edge, -0.022, 0.030),
             (-0.017, -0.025, 0.031), (0.017, -0.025, 0.031),
             (x_edge, -0.022, 0.030), (x_edge, -0.014, 0.027),
             (x_edge, 0.004, 0.022), (x_edge, axis_y, axis_z)]
v4.tube("formed_striking_bail", bail_path, 0.00135, material=zinc, body=bail, resolution=14)
# Rounded sleeves where the bail legs seat into the spring ears.
for side, x in enumerate((-x_edge, x_edge)):
    v4.cylinder(f"bail_pivot_sleeve_{side}", 0.0019, 0.006, (x, axis_y, axis_z),
                rot=(0, math.pi/2, 0), material=steel, body=bail)
v4.tube("spring_bail_leg", [(0.0045, axis_y, axis_z), (0.010, axis_y-0.005, 0.017),
                             (0.016, axis_y-0.009, axis_z), (x_edge, 0.004, 0.022)], 0.00085,
        material=steel, body=bail)
v4.joint("bail_hinge", "revolute", base, bail, origin=(0, axis_y, axis_z), axis=(1,0,0),
         limits=(-math.radians(180), 0.0))

# Hold-down lever: a low, formed catch wire reaches from its anchored front pivot,
# over the bail striker, and down to a retaining notch on the treadle.
hold_axis_y, hold_axis_z = front_pivot_y, 0.0155
v4.rod("hold_bar_pivot_pin", (-0.019, hold_axis_y, hold_axis_z), (0.019, hold_axis_y, hold_axis_z),
       0.00125, material=steel, body=hold)
v4.tube("hold_down_catch_wire", [(0, hold_axis_y, hold_axis_z),
                                  (0, -0.037, 0.017), (0, -0.031, 0.022),
                                  (0, -0.027, 0.031), (0, -0.024, 0.032),
                                  (0, -0.022, 0.023), (0, -0.027, 0.019)],
        0.00115, material=zinc, body=hold, resolution=14)
# Terminal curl seats in the stamped treadle latch notch.
v4.torus("catch_tip_loop", 0.0017, 0.00065, (0, -0.027, 0.019),
         rot=(math.pi/2, 0, 0), material=steel, body=hold, segments=24, ring_segments=10)
v4.joint("hold_down_pivot", "revolute", base, hold, origin=(0, hold_axis_y, hold_axis_z), axis=(1,0,0),
         limits=(-math.radians(5), math.radians(45)))

# Trigger treadle: a small dark stamped paddle at the front end. Its raised nose
# contacts the catch lever; the dimple is the bait location.
pedal_center_y, pedal_z = -0.0170, 0.0120
v4.box("stamped_treadle_plate", (0.027, 0.024, 0.0015), (0, pedal_center_y, pedal_z),
       bevel=0.0012, material=black, body=treadle)
# Upturned stamped latch at the pedal's rear edge receives the hold-down wire.
v4.box("treadle_latch_tab", (0.0045, 0.0015, 0.006), (0, -0.0275, 0.016),
       bevel=0.0004, material=black, body=treadle)
# shallow raised bait cup and rim; its contact patch remains attached to the plate.
v4.torus("bait_cup_rim", 0.0052, 0.00045, (0, pedal_center_y-0.001, pedal_z+0.0011),
         material=zinc, body=treadle, segments=32, ring_segments=8)
v4.cylinder("bait_well", 0.0038, 0.00025, (0, pedal_center_y-0.001, pedal_z+0.00082),
            material=black, body=treadle)
# Two pressed ribs add stamped-metal read and stiffness.
for x in (-0.008, 0.008):
    v4.rod(f"pedal_rib_{'L' if x<0 else 'R'}", (x, pedal_center_y-0.009, pedal_z+0.0010),
           (x, pedal_center_y+0.008, pedal_z+0.0010), 0.00055, material=steel, body=treadle)
# Hinge pin and side ears are in the moving treadle assembly.
v4.rod("treadle_hinge_pin", (-0.018, front_pivot_y, 0.0155), (0.018, front_pivot_y, 0.0155),
       0.0011, material=steel, body=treadle)
for x in (-0.015, 0.015):
    v4.rod(f"treadle_hinge_ear_{x}", (x, front_pivot_y, 0.0155), (x, -0.039, 0.012),
           0.00085, material=zinc, body=treadle)
# Side rails bridge the hinge ears to the forward-shifted pedal plate.
for x in (-0.014, 0.014):
    v4.rod(f"treadle_bridge_{x}", (x, -0.039, pedal_z), (x, -0.029, pedal_z), 0.00075,
           material=zinc, body=treadle)
# The central release arm is continuous with the pedal and lifts into the catch.
v4.rod("treadle_center_bridge", (0, -0.039, pedal_z), (0, -0.029, pedal_z), 0.00085,
       material=steel, body=treadle)
# A short release nose is placed beneath the catch arm, near its lever tip.
v4.rod("treadle_release_nose", (0, -0.039, 0.012), (0, -0.036, 0.029), 0.0010,
       material=steel, body=treadle)
v4.joint("treadle_pivot", "revolute", base, treadle, origin=(0, front_pivot_y, 0.0155), axis=(1,0,0),
         limits=(-math.radians(5), math.radians(12)))

# Save cocked rest pose; the checker drives the individual revolute joints.
v4.save(os.path.join(os.getcwd(), "asset.blend"))
print("saved asset.blend")
