"""Detailed articulated brass handbell. Run with Blender's background Python."""
import math
import os
import sys

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

v4.reset()

# Materials
brass = v4.metal("polished_warm_brass", color=(0.78, 0.48, 0.17), roughness=0.22, brushed=False)
bright_brass = v4.metal("bright_brass_edges", color=(0.92, 0.68, 0.31), roughness=0.19, brushed=False)
steel = v4.metal("clapper_stainless", color=(0.56, 0.61, 0.65), roughness=0.28)
dark_steel = v4.metal("spring_dark_steel", color=(0.20, 0.23, 0.25), roughness=0.32)
walnut = v4.wood("dark_walnut_grip", light=(0.27, 0.12, 0.055), dark=(0.075, 0.027, 0.012), grain_scale=7, roughness=0.31, coat=0.42)
black = v4.rubber("grip_inlay", color=(0.025, 0.018, 0.012), roughness=0.62)
engrave = v4.mat("engraving_dark_gold", (0.32, 0.16, 0.045), metallic=0.65, roughness=0.38)

# Bodies
casting = v4.body("casting")
handle = v4.body("handle")
grip = v4.body("grip")
clapper = v4.body("clapper")
spring = v4.body("spring")

# Spun bell shell: closed thick wall profile, open at the lower mouth.
v4.lathe("spun_bell_shell", [
    (0.000, 0.079), (0.012, 0.079), (0.018, 0.076), (0.022, 0.067),
    (0.028, 0.050), (0.037, 0.025), (0.049, 0.0065), (0.054, 0.0030),
    (0.056, 0.0030), (0.056, 0.0012), (0.053, 0.0012), (0.050, 0.0055),
    (0.036, 0.024), (0.027, 0.049), (0.021, 0.067), (0.017, 0.074),
    (0.010, 0.076), (0.000, 0.076),
], loc=(0, 0, 0), material=brass, body=casting)
# Crown boss and turned ornamentation, continuous with the casting.
v4.lathe("crown_pivot_boss", [(0, .073), (.013, .073), (.016, .076), (.016, .082), (.012, .085), (0, .085)], material=bright_brass, body=casting)
v4.torus("crown_bead", .0145, .0014, loc=(0, 0, .078), material=bright_brass, body=casting)
for z, radius in ((.068, .0228), (.055, .0268), (.037, .0328), (.014, .0445)):
    v4.torus("turned_shell_band_" + str(int(z*1000)), radius, .00065, loc=(0, 0, z), material=bright_brass, body=casting)
v4.torus("rolled_mouth_lip", .0543, .0013, loc=(0, 0, .0022), material=bright_brass, body=casting)

# Crown ears straddle the axle. Each carries a brass bushing and a visible screw head.
for side in (-1, 1):
    x = side * .014
    v4.box("pivot_ear_%s" % side, (.007, .012, .016), (x, 0, .077), bevel=.0022, material=brass, body=casting)
    v4.cylinder("pivot_bushing_%s" % side, .0045, .008, (x, 0, .080), rot=(0, math.pi/2, 0), material=steel, body=casting)
    v4.cylinder("pivot_pin_cap_%s" % side, .0032, .0012, (side*.0182, 0, .080), rot=(0, math.pi/2, 0), segments=32, bevel=.0003, material=bright_brass, body=casting)
    # small slot on the outward-facing head
    v4.box("pin_slot_%s" % side, (.00035, .0032, .00055), (side*.01885, 0, .080), bevel=.0001, material=engrave, body=casting)

# Maker's modest engraved cartouche on the forward flare.
v4.text("maker_mark", "BELL CO.", .0044, loc=(0, -.0345, .050), rot=(math.pi/2, 0, 0), extrude_depth=.00012, material=engrave, body=casting)

# Fixed handle shank and ferrule, mounted into the crown boss.
v4.lathe("handle_shank", [(0,.081), (.009,.081), (.009,.086), (.006,.090), (.005,.111), (.008,.114), (.010,.116), (.010,.120), (.008,.122), (0,.122)], material=brass, body=handle)
v4.torus("ferrule_lower_bead", .0094, .001, loc=(0,0,.116), material=bright_brass, body=handle)
v4.cylinder("ferrule_thread_ring", .0082, .002, (0,0,.121), material=bright_brass, body=handle)

# One-piece turned walnut grip with tapered waist, bottom and top brass collars.
v4.lathe("walnut_grip", [(0,.119), (.0078,.119), (.009,.122), (.0095,.127), (.011,.133), (.012,.145), (.011,.157), (.009,.164), (.008,.167), (.006,.169), (0,.169)], material=walnut, body=grip)
v4.lathe("grip_lower_collar", [(0,.119), (.009,.119), (.010,.121), (.010,.124), (.0085,.125), (0,.125)], material=bright_brass, body=grip)
v4.lathe("grip_top_collar", [(0,.164), (.0085,.164), (.0092,.166), (.008,.168), (.006,.169), (0,.169)], material=bright_brass, body=grip)
v4.lathe("handle_finial", [(0,.168), (.006,.168), (.0065,.171), (.005,.174), (0,.176)], material=brass, body=grip)
# Four understated longitudinal flutes on the grip, shallow black inlay strips.
for i in range(4):
    a = i*math.pi/2 + math.pi/4
    r = .0110
    x, y = r*math.cos(a), r*math.sin(a)
    v4.rod("grip_flute_%d"%i, (x,y,.131), (x*.82,y*.82,.159), .00055, material=black, body=grip)

# Clapper: axle through the two ears, slender hanging stem and weighted striking ball.
v4.rod("clapper_axle", (-.0185,0,.080), (.0185,0,.080), .0022, material=steel, body=clapper)
v4.cylinder("axle_center_hub", .0043, .008, (0,0,.080), rot=(0,math.pi/2,0), material=brass, body=clapper)
v4.rod("clapper_stem", (0,0,.078), (0,0,.027), .0020, material=steel, body=clapper)
v4.lathe("clapper_ball", [(0,.008), (.006,.008), (.008,.010), (.009,.015), (.0085,.021), (.006,.025), (.003,.027), (0,.027)], material=brass, body=clapper)
v4.torus("ball_neck_ring", .003, .00055, loc=(0,0,.026), material=bright_brass, body=clapper)

# Captured spring slider around the upper stem. The coil is represented by a true helical tube.
v4.cylinder("spring_slider_sleeve", .0047, .010, (0,0,.060), material=dark_steel, body=spring, bevel=.0007)
for z in (.0548, .0652):
    v4.torus("spring_slider_washer_"+str(int(z*10000)), .0043, .0007, loc=(0,0,z), material=steel, body=spring)
helix = []
for k in range(121):
    t = k/120
    a = 2*math.pi*5*t
    helix.append((.0037*math.cos(a), .0037*math.sin(a), .0555 + .009*t))
v4.tube("five_turn_compression_spring", helix, .00055, material=dark_steel, body=spring, resolution=8)

# Joints. The fixed handle/grip joints form the static handle assembly. The spring slider is
# parented to the clapper so its slide axis follows the clapper as it swings.
v4.joint("handle_mount", "fixed", casting, handle, origin=(0,0,.080), axis=(0,0,1))
v4.joint("grip_mount", "fixed", handle, grip, origin=(0,0,.122), axis=(0,0,1))
v4.joint("clapper_hinge", "revolute", casting, clapper, origin=(0,0,.080), axis=(1,0,0),
         limits=(-math.radians(22), math.radians(22)))
v4.joint("spring_slide", "prismatic", clapper, spring, origin=(0,0,.060), axis=(0,0,1), limits=(0,.004))

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