```python
import bpy, math, random
from mathutils import Vector
from math import sin, cos, pi, exp

random.seed(27)
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)

# All dimensions and textures are authored here; no external assets are used.
def simple_mat(name, color, roughness=.5, metallic=0):
    m = bpy.data.materials.new(name)
    m.diffuse_color = (*color, 1)
    m.use_nodes = True
    p = m.node_tree.nodes.get('Principled BSDF')
    p.inputs['Base Color'].default_value = (*color, 1)
    p.inputs['Roughness'].default_value = roughness
    p.inputs['Metallic'].default_value = metallic
    return m

def leather(name, dark, light):
    m = simple_mat(name, dark)
    n, l = m.node_tree.nodes, m.node_tree.links
    p = n.get('Principled BSDF')
    tex = n.new('ShaderNodeTexCoord')
    noise = n.new('ShaderNodeTexNoise')
    noise.inputs['Scale'].default_value = 5.8
    noise.inputs['Detail'].default_value = 5
    noise.inputs['Roughness'].default_value = .74
    l.new(tex.outputs['Generated'], noise.inputs['Vector'])
    ramp = n.new('ShaderNodeValToRGB')
    ramp.color_ramp.elements[0].position = .18
    ramp.color_ramp.elements[0].color = (*dark, 1)
    ramp.color_ramp.elements[1].position = .83
    ramp.color_ramp.elements[1].color = (*light, 1)
    l.new(noise.outputs['Fac'], ramp.inputs[0])

    geo = n.new('ShaderNodeNewGeometry')
    sep = n.new('ShaderNodeSeparateXYZ')
    l.new(geo.outputs['Position'], sep.inputs[0])
    # Patches of worn-through finish, concentrated on the toe.
    toe = n.new('ShaderNodeMapRange')
    toe.inputs['From Min'].default_value = -2.34
    toe.inputs['From Max'].default_value = -1.49
    toe.inputs['To Min'].default_value = .8
    toe.inputs['To Max'].default_value = 0
    toe.clamp = True
    l.new(sep.outputs['Y'], toe.inputs[0])
    wearnoise = n.new('ShaderNodeTexNoise')
    wearnoise.inputs['Scale'].default_value = 16
    wearnoise.inputs['Detail'].default_value = 4
    l.new(geo.outputs['Position'], wearnoise.inputs['Vector'])
    wear = n.new('ShaderNodeValToRGB')
    wear.color_ramp.elements[0].position = .48
    wear.color_ramp.elements[0].color = (0, 0, 0, 1)
    wear.color_ramp.elements[1].position = .70
    wear.color_ramp.elements[1].color = (1, 1, 1, 1)
    l.new(wearnoise.outputs['Fac'], wear.inputs[0])
    mult = n.new('ShaderNodeMath'); mult.operation = 'MULTIPLY'
    l.new(toe.outputs[0], mult.inputs[0]); l.new(wear.outputs[0], mult.inputs[1])
    mix = n.new('ShaderNodeMixRGB')
    l.new(mult.outputs[0], mix.inputs[0])
    l.new(ramp.outputs[0], mix.inputs[1])
    mix.inputs[2].default_value = (.285, .145, .066, 1)

    attr = n.new('ShaderNodeAttribute'); attr.attribute_name = 'crease'
    cmix = n.new('ShaderNodeMixRGB')
    l.new(attr.outputs['Fac'], cmix.inputs[0])
    l.new(mix.outputs[0], cmix.inputs[1])
    cmix.inputs[2].default_value = (.037, .017, .009, 1)

    # Broken, faint mineral tide mark just above the welt.
    saltnoise = n.new('ShaderNodeTexNoise')
    saltnoise.inputs['Scale'].default_value = 7
    l.new(geo.outputs['Position'], saltnoise.inputs['Vector'])
    ns = n.new('ShaderNodeMath'); ns.operation = 'MULTIPLY'
    ns.inputs[1].default_value = .105
    l.new(saltnoise.outputs['Fac'], ns.inputs[0])
    add = n.new('ShaderNodeMath'); add.operation = 'ADD'
    l.new(sep.outputs['Z'], add.inputs[0]); l.new(ns.outputs[0], add.inputs[1])
    sub = n.new('ShaderNodeMath'); sub.operation = 'SUBTRACT'
    sub.inputs[1].default_value = .727
    l.new(add.outputs[0], sub.inputs[0])
    ab = n.new('ShaderNodeMath'); ab.operation = 'ABSOLUTE'
    l.new(sub.outputs[0], ab.inputs[0])
    salt = n.new('ShaderNodeMapRange')
    salt.inputs['From Min'].default_value = .004
    salt.inputs['From Max'].default_value = .036
    salt.inputs['To Min'].default_value = .26
    salt.inputs['To Max'].default_value = 0
    l.new(ab.outputs[0], salt.inputs[0])
    smix = n.new('ShaderNodeMixRGB')
    l.new(salt.outputs[0], smix.inputs[0])
    l.new(cmix.outputs[0], smix.inputs[1])
    smix.inputs[2].default_value = (.31, .26, .185, 1)
    l.new(smix.outputs[0], p.inputs['Base Color'])

    rough = n.new('ShaderNodeMapRange')
    rough.inputs['To Min'].default_value = .42
    rough.inputs['To Max'].default_value = .76
    l.new(noise.outputs['Fac'], rough.inputs[0])
    polish = n.new('ShaderNodeMath'); polish.operation = 'MULTIPLY'
    polish.inputs[1].default_value = .20
    l.new(attr.outputs['Fac'], polish.inputs[0])
    rs = n.new('ShaderNodeMath'); rs.operation = 'SUBTRACT'
    l.new(rough.outputs[0], rs.inputs[0]); l.new(polish.outputs[0], rs.inputs[1])
    l.new(rs.outputs[0], p.inputs['Roughness'])

    grain = n.new('ShaderNodeTexNoise')
    grain.inputs['Scale'].default_value = 135
    grain.inputs['Detail'].default_value = 3
    grain.inputs['Roughness'].default_value = .8
    l.new(geo.outputs['Position'], grain.inputs['Vector'])
    cells = n.new('ShaderNodeTexVoronoi')
    cells.feature = 'DISTANCE_TO_EDGE'
    cells.inputs['Scale'].default_value = 100
    l.new(geo.outputs['Position'], cells.inputs['Vector'])
    gm = n.new('ShaderNodeMixRGB'); gm.blend_type = 'MULTIPLY'
    gm.inputs[0].default_value = .45
    l.new(grain.outputs['Fac'], gm.inputs[1]); l.new(cells.outputs['Distance'], gm.inputs[2])
    bump = n.new('ShaderNodeBump')
    bump.inputs['Strength'].default_value = .38
    bump.inputs['Distance'].default_value = .038
    l.new(gm.outputs[0], bump.inputs['Height'])
    pores = n.new('ShaderNodeTexNoise'); pores.inputs['Scale'].default_value = 390
    pores.inputs['Detail'].default_value = 2
    l.new(geo.outputs['Position'], pores.inputs['Vector'])
    bp = n.new('ShaderNodeBump')
    bp.inputs['Strength'].default_value = .22
    bp.inputs['Distance'].default_value = .009
    l.new(pores.outputs['Fac'], bp.inputs['Height'])
    l.new(bump.outputs[0], bp.inputs['Normal'])
    l.new(bp.outputs[0], p.inputs['Normal'])
    p.inputs['Coat Weight'].default_value = .08
    p.inputs['Coat Roughness'].default_value = .48
    return m

uppermat = leather('Oil-tanned full grain / worn walnut', (.049,.021,.009), (.205,.096,.037))
panelmat = leather('Dry quarters / umber leather', (.052,.025,.012), (.173,.081,.032))
edgemat = leather('Burnished facings and collar', (.024,.012,.007), (.094,.043,.017))
tonguemat = leather('Soft folded tongue', (.070,.033,.013), (.225,.112,.045))
lining = simple_mat('Dark suede interior', (.042,.032,.024), .94)
rubber = simple_mat('Carbon rubber, aged', (.026,.027,.023), .83)
weltmat = simple_mat('Compressed leather welt', (.126,.068,.029), .72)
thread = simple_mat('Dirty flax stitching', (.47,.345,.197), .87)
seammat = simple_mat('Dark recessed seams', (.025,.012,.006), .8)
mud = simple_mat('Dry clay lodged in construction', (.19,.145,.092), .97)
scuffmat = simple_mat('Exposed leather fibres', (.255,.139,.064), .91)
lace_mat = simple_mat('Waxed faded ochre cotton lace', (.245,.187,.106), .8)
lace_fibre = simple_mat('Lighter cotton fibres', (.41,.325,.193), .94)
metal = simple_mat('Oxidized brass eyelet bodies', (.175,.13,.070), .43, .78)
rimmetal = simple_mat('Lace-polished brass rims', (.44,.32,.155), .27, .83)
holemat = simple_mat('Eyelet recess', (.012,.009,.006), .93)

# Fine relief on rubber, lining, cotton and compacted mud.
for mat, scale, strength, distance in [
    (rubber,65,.25,.025),(weltmat,95,.25,.017),(lining,115,.45,.019),
    (mud,48,.55,.032),(lace_mat,170,.38,.014)]:
    n,l = mat.node_tree.nodes, mat.node_tree.links
    t=n.new('ShaderNodeTexNoise'); t.inputs['Scale'].default_value=scale
    t.inputs['Detail'].default_value=3
    b=n.new('ShaderNodeBump'); b.inputs['Strength'].default_value=strength
    b.inputs['Distance'].default_value=distance
    l.new(t.outputs['Fac'],b.inputs['Height'])
    l.new(b.outputs[0],n.get('Principled BSDF').inputs['Normal'])

def mesh_obj(name, verts, faces, mat, smooth=True):
    me=bpy.data.meshes.new(name)
    me.from_pydata(verts, [], faces); me.update()
    ob=bpy.data.objects.new(name, me); bpy.context.collection.objects.link(ob)
    if mat: me.materials.append(mat)
    if smooth:
        for p in me.polygons: p.use_smooth=True
    return ob

def tubes(name, paths, radius, mat, sides=8):
    verts=[]; faces=[]
    for path in paths:
        if len(path)<2: continue
        path=[Vector(p) for p in path]
        start=len(verts)
        prev=None
        for i,p in enumerate(path):
            tangent=(path[min(i+1,len(path)-1)]-path[max(i-1,0)]).normalized()
            ref=Vector((0,0,1))
            if abs(tangent.dot(ref))>.94: ref=Vector((0,1,0))
            a=tangent.cross(ref).normalized()
            if prev is not None and a.dot(prev)<0: a=-a
            prev=a
            b=tangent.cross(a).normalized()
            for k in range(sides):
                v=p+radius*(cos(k*2*pi/sides)*a+sin(k*2*pi/sides)*b)
                verts.append(tuple(v))
        for j in range(len(path)-1):
            for k in range(sides):
                a=start+j*sides+k; b=start+j*sides+(k+1)%sides
                faces.append((a,b,b+sides,a+sides))
        faces.append(tuple(start+k for k in reversed(range(sides))))
        end=start+(len(path)-1)*sides
        faces.append(tuple(end+k for k in range(sides)))
    return mesh_obj(name,verts,faces,mat)

def catmull(points, steps=10):
    points=[Vector(x) for x in points]
    out=[]
    for i in range(len(points)-1):
        a=points[max(i-1,0)]; b=points[i]; c=points[i+1]; d=points[min(i+2,len(points)-1)]
        for j in range(steps):
            t=j/steps
            out.append(.5*((2*b)+(-a+c)*t+(2*a-5*b+4*c-d)*t*t+(-a+3*b-3*c+d)*t*t*t))
    out.append(points[-1])
    return out

def stitch_path(points, spacing=.11, length=.058, lift=0):
    pts=[Vector(p) for p in points]
    distances=[0]
    for a,b in zip(pts,pts[1:]): distances.append(distances[-1]+(b-a).length)
    def point_at(t):
        for i in range(len(distances)-1):
            if distances[i+1]>=t:
                f=(t-distances[i])/max(distances[i+1]-distances[i],1e-8)
                return pts[i].lerp(pts[i+1],f)+Vector((0,0,lift))
        return pts[-1]
    out=[]
    d=.03
    while d+length<distances[-1]:
        a=point_at(d); b=point_at(d+length)
        out.append([a,(a+b)*.5+Vector((0,0,.004)),b])
        d+=spacing
    return out

# Horizontal sculpted sections: broad toe becomes an irregular hollow ankle.
profiles=[
(.50,-2.32,1.68,.96),(.68,-2.36,1.69,.995),
(.88,-2.33,1.68,1.005),(1.06,-2.19,1.65,.99),
(1.24,-1.92,1.62,.955),(1.43,-1.50,1.60,.90),
(1.66,-1.02,1.58,.845),(1.93,-.65,1.57,.80),
(2.25,-.43,1.56,.77),(2.60,-.34,1.55,.775),
(2.94,-.33,1.56,.81),(3.28,-.32,1.60,.85)]

def profile(z):
    for i in range(len(profiles)-1):
        a,b=profiles[i:i+2]
        if z<=b[0]:
            t=max(0,(z-a[0])/(b[0]-a[0]))
            # Linear profile sampled densely, with a subsequent smoothing pass.
            return tuple(a[k]*(1-t)+b[k]*t for k in range(1,4))
    return profiles[-1][1:]

def gap(z):
    return .39*max(0,min(1,(z-1.25)/.62)) + .025*max(0,z-2.4)

def crease_value(x,y,z):
    if z<.85 or y>-.72: return 0,0
    envelope=exp(-((x/.90)**6))*exp(-((y+1.42)/.68)**4)
    disp=0; dark=0
    for k,cy in enumerate([-1.79,-1.57,-1.34,-1.115]):
        curve=cy+.095*sin(x*3.2+k*.8)+(.045 if k%2 else -.06)*x
        d=y-curve
        valley=exp(-(d/(.025+.005*k))**2)
        shoulder=exp(-(d/(.069+.006*k))**2)
        disp += -.072*valley+.027*shoulder
        dark=max(dark,valley*.72)
    return disp*envelope,dark*envelope

def surf(theta,z,offset=0):
    front,back,w=profile(z)
    s,c=sin(theta),cos(theta)
    x=w*(1 if s>=0 else -1)*abs(s)**.87
    x*=.91+.09*c
    y=(front+back)*.5-(back-front)*.5*c
    # Toe skew, outward collar sag and heel lean are intentionally unequal.
    x+=-.043*exp(-((z-.9)/.5)**2)*max(0,c)+.075*((z-.5)/2.78)**2
    y+=.034*sin(theta*3+.5)*sin(pi*(z-.5)/2.78)
    side=abs(s)**2
    fold=(.032*sin(13*z+1.4*cos(theta*2))+.019*sin(23*z+theta*3))
    fold*=exp(-((z-1.95)/.75)**2)*side
    x+=fold*s
    y+=fold*.45*(-c)
    dz,cr=crease_value(x,y,z)
    zz=z+dz
    top=max(0,(z-2.70)/.58)
    zz+=top*(-.135*s*s-.078*s+.028*cos(theta*3))
    x+=top*.055*sin(theta*2+.4)
    y+=top*.042*sin(theta*3)
    # Gentle radial surface offset for sewn-on panels.
    normal=Vector((s,-c,.24 if z<1.8 else .03)).normalized()
    return Vector((x,y,zz))+offset*normal

def normal_at(theta,z):
    dt=surf(theta+.002,z)-surf(theta-.002,z)
    dz=surf(theta,z+.002)-surf(theta,z-.002)
    return dt.cross(dz).normalized()

N=192; R=154
verts=[]; faces=[]; creases=[]
for j in range(R):
    z=.505+(3.28-.505)*j/(R-1)
    g=gap(z)
    for i in range(N+1):
        th=g+(2*pi-2*g)*i/N
        p=surf(th,z)
        verts.append(p)
        creases.append(crease_value(p.x,p.y,z)[1])
for j in range(R-1):
    for i in range(N):
        a=j*(N+1)+i
        faces.append((a,a+1,a+N+2,a+N+1))
body=mesh_obj('Collapsed leather upper, open throat and ankle',verts,faces,uppermat)
body.data.materials.append(lining)
at=body.data.attributes.new('crease','FLOAT','POINT')
for d,v in zip(at.data,creases): d.value=v
sm=body.modifiers.new('Softly worked hide','SMOOTH'); sm.factor=.42; sm.iterations=3
sol=body.modifiers.new('Actual leather thickness','SOLIDIFY'); sol.thickness=.055; sol.offset=-1
sol.material_offset=1
sol.material_offset_rim=1

# Dark inner facing following the sagging opening, not a solid capped shaft.
v=[]; f=[]
for j in range(15):
    z=2.04+(3.27-2.04)*j/14
    g=gap(z)
    for i in range(97):
        th=g+(2*pi-2*g)*i/96
        p=surf(th,z,-.073); p.z-=.014
        v.append(p)
for j in range(14):
    for i in range(96):
        a=j*97+i; f.append((a,a+97,a+98,a+1))
mesh_obj('Suede ankle lining',v,f,lining)
# Deep shadowed footbed, far below the rim.
v=[(.05,.59,1.76)]
for i in range(96):
    th=2*pi*i/96
    v.append((.05+.65*sin(th),.59+.82*cos(th),1.76))
mesh_obj('Interior depth',v,[(0,i+1,(i+1)%96+1) for i in range(96)],lining)

# Broad padded collar with a subtly oval rather than perfectly round section.
collar=[]
g=gap(3.28)
for i in range(150):
    th=g+(2*pi-2*g)*i/149
    collar.append(surf(th,3.27,.009))
tubes('Soft rolled leather ankle collar',[collar],.109,edgemat,12)
collar_seam=[surf(g+(2*pi-2*g)*i/180,3.075,.035) for i in range(181)]
tubes('Collar lower seam',[collar_seam],.012,seammat)
tubes('Collar saddle stitching',stitch_path(collar_seam,.108,.060),.008,thread,6)

# Reinforcing eyelet facings laid over the main leather.
all_stitches=[]
for side in (1,-1):
    v=[]; f=[]
    for j in range(54):
        z=1.32+(3.18-1.32)*j/53
        for i in range(9):
            th=side*(gap(z)+.025+.285*i/8)
            v.append(surf(th,z,.022))
    for j in range(53):
        for i in range(8):
            a=j*9+i; f.append((a,a+1,a+10,a+9))
    ob=mesh_obj(('Right' if side==1 else 'Left')+' burnished eyelet facing',v,f,edgemat)
    so=ob.modifiers.new('Facing leather thickness','SOLIDIFY'); so.thickness=.028
    for edge in (.04,.292):
        path=[surf(side*(gap(z)+edge),z,.055) for z in [1.35+i*1.78/100 for i in range(101)]]
        tubes('Facing recessed stitch channel',[path],.011,seammat)
        all_stitches+=stitch_path(path,.092,.05)

# Heel counter patch with a scalloped upper edge and double stitching.
v=[]; f=[]
for j in range(25):
    t=j/24
    for i in range(100):
        th=1.40+(2*pi-2.8)*i/99
        top=1.17+.43*sin((th-1.4)/(2*pi-2.8)*pi)**.8
        z=.565+t*(top-.565)
        v.append(surf(th,z,.026))
for j in range(24):
    for i in range(99):
        a=j*100+i; f.append((a,a+1,a+101,a+100))
ob=mesh_obj('Wrapped reinforced heel counter',v,f,panelmat)
so=ob.modifiers.new('Counter edge thickness','SOLIDIFY'); so.thickness=.028
for dz in (0,-.072):
    path=[]
    for i in range(160):
        th=1.40+(2*pi-2.8)*i/159
        z=1.17+.43*sin((th-1.4)/(2*pi-2.8)*pi)**.8+dz
        path.append(surf(th,z,.061))
    if dz==0: tubes('Heel counter edge',[path],.015,edgemat)
    all_stitches+=stitch_path(path,.108,.059)

# Long curved quarter seams and the rear closing seam.
for side in (-1,1):
    for shift in (0,.047):
        path=[]
        for i in range(110):
            t=i/109; z=.64+2.39*t
            th=side*(1.40-.73*sin(t*pi/2)+shift)
            path.append(surf(th,z,.036))
        if shift==0: tubes('Quarter panel seam',[path],.010,seammat)
        all_stitches+=stitch_path(path,.105,.055)
for shift in (-.043,.043):
    path=[surf(pi+shift,.62+2.40*i/100,.049) for i in range(101)]
    all_stitches+=stitch_path(path,.104,.057)

# Toe-cap seam: rounded reinforced toe, with creases behind rather than painted on.
for dz in (0,-.050):
    path=[]
    for i in range(145):
        th=-1.11+2.22*i/144
        z=.555+.745*max(0,cos(th/1.11*pi/2))**1.12+dz
        path.append(surf(th,max(.52,z),.016))
    if dz==0: tubes('Toe reinforcement seam',[path],.012,edgemat)
    all_stitches+=stitch_path(path,.104,.055)
tubes('Individual lock stitches in upper',all_stitches,.0085,thread,6)

# Tongue, backed by the dark cavity, curled and leaning to one side.
v=[]; f=[]
for j in range(64):
    t=j/63
    z=1.29+2.10*t
    front=profile(min(z,3.28))[0]
    for i in range(27):
        u=-1+2*i/26
        width=.40+.085*t+.025*sin(t*pi)
        x=u*width+.028*sin(t*4)+.032*t*t
        y=front+.09+.075*u*u-.095*exp(-((t-.96)/.12)**2)
        y+=.023*sin(t*25+u*3)*(1-t)
        zz=z+.035*sin(u*3+t*4)-.09*u*t**5
        v.append((x,y,zz))
for j in range(63):
    for i in range(26):
        a=j*27+i; f.append((a,a+1,a+28,a+27))
tongue=mesh_obj('Wrinkled free leather tongue',v,f,tonguemat)
tongue.data.materials.append(lining)
so=tongue.modifiers.new('Tongue thickness','SOLIDIFY'); so.thickness=.060; so.material_offset=1
be=tongue.modifiers.new('Soft tongue edges','BEVEL'); be.width=.025; be.segments=2
tongue_paths=[]
for k in (1,25):
    tongue_paths.append([Vector(v[j*27+k])+Vector((0,-.019,0)) for j in range(3,63)])
tongue_paths.append([Vector(v[62*27+i])+Vector((0,-.02,0)) for i in range(1,26)])
tubes('Tongue edge stitching',sum([stitch_path(p,.11,.06) for p in tongue_paths],[]),.008,thread,6)

def torus(name, loc, normal, major, minor, mat):
    bpy.ops.mesh.primitive_torus_add(major_segments=32,minor_segments=10,
        location=loc,major_radius=major,minor_radius=minor)
    ob=bpy.context.object; ob.name=name
    ob.rotation_euler=Vector((0,0,1)).rotation_difference(Vector(normal)).to_euler()
    ob.data.materials.append(mat)
    for p in ob.data.polygons: p.use_smooth=True
    return ob

def disc(name,loc,normal,radius,depth,mat):
    bpy.ops.mesh.primitive_cylinder_add(vertices=32,radius=radius,depth=depth,location=loc)
    ob=bpy.context.object; ob.name=name
    ob.rotation_euler=Vector((0,0,1)).rotation_difference(Vector(normal)).to_euler()
    ob.data.materials.append(mat)
    be=ob.modifiers.new('Rounded edge','BEVEL'); be.width=.007; be.segments=2
    return ob

eye={}
for row,z in enumerate([1.48,1.72,1.965,2.215,2.465,2.715,2.975]):
    for side in (-1,1):
        th=side*(gap(z)+.158)
        normal=normal_at(th,z)
        p=surf(th,z,.052)
        disc('Dark bore through leather',p+normal*.003,normal,.068,.016,holemat)
        torus('Antique brass grommet %s %02d'%(side,row+1),p+normal*.018,normal,.077,.021,metal)
        torus('Bright rubbed lip',p+normal*.035,normal,.064,.009,rimmetal)
        eye[(side,row)]=p+normal*.060

# Genuine round laces. The final two crossings hang away from the tongue.
lace_paths=[]
a=eye[(-1,0)]; b=eye[(1,0)]
lace_paths.append(catmull([a,a.lerp(b,.33)+Vector((0,-.08,0)),a.lerp(b,.67)+Vector((0,-.08,0)),b],12))
for row in range(6):
    for side in (-1,1):
        a=eye[(side,row)]
        b=eye[(-side,row+1)]
        loosen=.115 if row<4 else (.25 if row==4 else .37)
        drop=0 if row<4 else (.075 if row==4 else .135)
        controls=[a,
            a.lerp(b,.23)+Vector((0,-loosen*.78,-drop*.7)),
            a.lerp(b,.54)+Vector((0,-loosen-(.026 if side==1 else 0),-drop)),
            a.lerp(b,.81)+Vector((0,-loosen*.72,-drop*.5)),b]
        lace_paths.append(catmull(controls,12))
# Free ends, visibly different lengths, and only one retained aglet.
a=eye[(1,6)]
end1=catmull([a,(.73,-.66,2.93),(.99,-.80,2.63),(1.06,-.91,2.16),
             (.97,-1.02,1.76),(.84,-1.16,1.49)],12)
a=eye[(-1,6)]
end2=catmull([a,(-.29,-.79,2.95),(.02,-.93,2.68),(.19,-1.00,2.29),
             (.31,-1.17,1.91),(.47,-1.39,1.62)],12)
lace_paths.extend([end1,end2])
tubes('Round crisscross cotton laces with slack upper courses',lace_paths,.030,lace_mat,10)

# Sparse twisted fibre highlights on the lace surfaces.
fibrepaths=[]
for path in lace_paths:
    for strand in range(2):
        out=[]
        for i,p in enumerate(path):
            p=Vector(p)
            tangent=(Vector(path[min(i+1,len(path)-1)])-Vector(path[max(0,i-1)])).normalized()
            aa=tangent.cross(Vector((0,1,0))).normalized()
            bb=tangent.cross(aa).normalized()
            angle=i*.94+strand*pi
            out.append(p+.0305*(aa*cos(angle)+bb*sin(angle)))
        fibrepaths.append(out)
tubes('Cotton lay on round laces',fibrepaths,.0025,lace_fibre,5)

tip=Vector(end1[-1]); direction=(tip-Vector(end1[-5])).normalized()
agletmat=simple_mat('Dented surviving lace aglet',(.105,.083,.051),.46,.55)
tubes('One surviving crimped aglet',[[tip-direction*.075,tip+direction*.045]],.033,agletmat,10)
frays=[]
for base,direction,count in [(Vector(end2[-1]),(Vector(end2[-1])-Vector(end2[-5])).normalized(),17),
                            (tip+direction*.045,direction,5)]:
    for i in range(count):
        jitter=Vector((random.uniform(-.025,.025),random.uniform(-.02,.02),random.uniform(-.01,.01)))
        length=random.uniform(.06,.155)
        start=base+jitter*.6
        finish=base+direction*length+jitter*2.2
        frays.append(catmull([start,base+direction*length*.48+jitter*1.3,finish],5))
tubes('Frayed cotton ends / missing left aglet',frays,.0037,lace_fibre,5)

# A stitched, hollow heel-pull loop, made from a bent leather strip.
loop_center=catmull([(0,1.645,2.81),(0,1.81,3.14),(0,1.88,3.50),
                    (0,1.76,3.65),(0,1.62,3.50),(0,1.59,3.04),(0,1.625,2.81)],12)
v=[]; f=[]
for i,p in enumerate(loop_center):
    p=Vector(p)
    for x in (-.172,.172):
        v.append(p+Vector((x+.065,0,0)))
for i in range(len(loop_center)-1): f.append((2*i,2*i+1,2*i+3,2*i+2))
pull=mesh_obj('Bent stitched heel pull loop',v,f,edgemat)
so=pull.modifiers.new('Pull strap thickness','SOLIDIFY'); so.thickness=.050
be=pull.modifiers.new('Rounded strap edges','BEVEL'); be.width=.018; be.segments=2
pullst=[]
for x in (-.130,.130):
    path=[Vector(p)+Vector((x+.065,.029,0)) for p in loop_center]
    pullst+=stitch_path(path,.090,.048)
tubes('Pull loop twin stitch rows',pullst,.008,thread,6)
for z in (2.86,2.94):
    pts=[(.065-.125,1.687,z),(.065+.125,1.687,z)]
    tubes('Pull loop bar tack',stitch_path(pts,.030,.021),.009,thread,6)

# Sole stack, with a rolled outer heel rather than a perfect extrusion.
def footprint(th,scale=1):
    s,c=sin(th),cos(th)
    x=1.035*(1 if s>=0 else -1)*abs(s)**.87*(.91+.09*c)-.025*max(0,c)
    y=-.33-2.08*c
    return Vector((x*scale,y*scale,0))

def roll(x,y):
    return .055*max(0,x)**2*max(0,min(1,(y-.65)/.9))

def sole_layer(name,rings,mat):
    v=[]; f=[]; count=192
    for z,scale in rings:
        for i in range(count):
            p=footprint(2*pi*i/count,scale)
            p.z=z+roll(p.x,p.y)
            # Subtle irregularity of compressed stacked leather.
            p.z+=.005*sin(i*.43+z*22)
            v.append(p)
    for j in range(len(rings)-1):
        for i in range(count):
            a=j*count+i; b=j*count+(i+1)%count
            f.append((a,b,b+count,a+count))
    f.append(tuple(reversed(range(count))))
    f.append(tuple((len(rings)-1)*count+i for i in range(count)))
    return mesh_obj(name,v,f,mat)

sole_layer('Rubber outsole with eased edge',[(.175,1.005),(.20,1.038),(.31,1.047),(.348,1.027)],rubber)
sole_layer('Leather midsole',[(.341,1.02),(.375,1.037),(.421,1.027)],weltmat)
sole_layer('Raised stitched storm welt',[(.418,1.027),(.469,1.03),(.505,1.003),(.519,.962)],weltmat)
for z,sc,rad,mat in [(.356,1.035,.013,seammat),(.411,1.026,.012,seammat),(.494,.967,.017,edgemat)]:
    path=[]
    for i in range(241):
        p=footprint(2*pi*i/240,sc); p.z=z+roll(p.x,p.y)
        path.append(p)
    tubes('Continuous welt and sole seam',[path],rad,mat)

weltst=[]
for i in range(136):
    th=2*pi*(i+.2)/136
    points=[]
    for k in range(4):
        a=th+(k/3-.5)*.033
        p=footprint(a,1.002+(k/3-.5)*.018)
        p.z=.506+roll(p.x,p.y)+.009*sin(k*pi/3)
        points.append(p)
    weltst.append(points)
tubes('136 individual welt lock stitches',weltst,.0105,thread,7)

def block(name,poly,zbottom,ztop,mat,bevel=.035):
    v=[]
    for z in (zbottom,ztop):
        for x,y in poly:
            v.append((x,y,z+roll(x,y)))
    n=len(poly)
    f=[tuple(reversed(range(n))),tuple(range(n,2*n))]
    for i in range(n): f.append((i,(i+1)%n,(i+1)%n+n,i+n))
    ob=mesh_obj(name,v,f,mat,False)
    if bevel:
        be=ob.modifiers.new('Worn rounded tread corners','BEVEL')
        be.width=bevel; be.segments=2
        ob.modifiers.new('Weighted tread normals','WEIGHTED_NORMAL')
    return ob

heelpoly=[(-.67,.59),(.70,.59),(.79,.87),(.78,1.38),(.57,1.71),
          (-.50,1.73),(-.76,1.43),(-.80,.91)]
block('Raised solid rubber heel block',heelpoly,.075,.255,rubber,.055)
# Distinct heel lugs and channels all the way across the bottom.
for j in range(3):
    y=.69+j*.32
    for side in (-1,1):
        x0=.065*side; x1=(.69 if j<2 else .60)*side
        poly=[(x0,y),(x1,y+.015),(x1,y+.225),(x0,y+.235)]
        if side<0: poly.reverse()
        block('Heel traction block',poly,.016,.118,rubber,.028)

# Forefoot V-cleats, isolated by deep true geometry channels.
for j in range(7):
    y=-2.20+j*.355
    w=.58 if j==0 else (.86 if j<5 else .84)
    for side in (-1,1):
        poly=[(side*.075,y+.05),(side*w,y-.10),(side*(w+.045),y+.12),
              (side*.19,y+.27),(side*.075,y+.23)]
        if side<0: poly.reverse()
        block('Chevron forefoot tread %02d'%j,poly,.027,.215,rubber,.032)
# Small recessed arch ribs, leaving the heel unmistakably raised.
for y in (.28,.43):
    block('Recessed arch traction rib',[(-.62,y),(.64,y),(.64,y+.075),(-.62,y+.075)],.137,.205,rubber,.022)

# Edge sipes cut visually as dark grooves between shallow projecting shoulder lugs.
for side in (-1,1):
    for j in range(8):
        y=-1.92+j*.39
        if y>.60: continue
        width=.97 if y<-.1 else .85
        poly=[(side*(width-.10),y),(side*(width+.055),y+.015),
              (side*(width+.05),y+.205),(side*(width-.10),y+.20)]
        if side<0: poly.reverse()
        block('Outsole shoulder lug',poly,.12,.29,rubber,.019)

# Physically lodged dirt, not loose objects on the sweep.
def dirt_blob(loc,scale):
    bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=1,radius=1,location=loc)
    ob=bpy.context.object; ob.name='Compacted dry mud in seam'
    for ve in ob.data.vertices: ve.co*=random.uniform(.76,1.20)
    ob.scale=scale
    ob.data.materials.append(mud)
    return ob

for i in range(94):
    th=random.uniform(0,2*pi)
    if random.random()<.25 and cos(th)>.4: continue
    p=footprint(th,random.uniform(1.004,1.032))
    p.z=random.choice([.419,.442,.475])+roll(p.x,p.y)
    size=random.uniform(.014,.041)
    dirt_blob(p,(size,random.uniform(.025,.065),size*.42))
for i in range(42):
    side=random.choice([-1,1]); y=random.uniform(-2.02,1.58)
    x=side*(.91 if y<.20 else .71)+random.uniform(-.025,.025)
    dirt_blob((x,y,.155+roll(x,y)),(random.uniform(.014,.035),random.uniform(.025,.065),random.uniform(.012,.036)))

# Small scars with uneven broken fibre edges across the toe and outer side.
scars=[]
for i in range(53):
    th=random.uniform(-.78,.97)
    z=random.uniform(.76,1.24)
    length=random.uniform(.018,.075)
    path=[]
    for j in range(5):
        t=j/4
        p=surf(th+(t-.5)*length,z+(t-.5)*random.uniform(-.045,.025),.009)
        path.append(p)
    scars.append(path)
tubes('Small abraded toe scratches',scars,.0028,scuffmat,5)
# Few deeper, dark scratches on the outer quarter.
scars=[]
for i in range(9):
    th=random.uniform(.9,1.8); z=random.uniform(.89,1.75)
    scars.append([surf(th-.025,z-.018,.034),surf(th,z,.036),surf(th+.07,z+.025,.033)])
tubes('Old nicks in quarter leather',scars,.004,seammat,5)

# Neutral seamless product stage: only the boot and its contact shadow.
groundmat=simple_mat('Warm neutral studio sweep',(.285,.277,.258),.86)
bpy.ops.mesh.primitive_plane_add(size=200,location=(0,0,-.018))
ground=bpy.context.object; ground.name='Seamless neutral sweep'; ground.data.materials.append(groundmat)

world=bpy.data.worlds.new('Neutral studio ambient')
world.use_nodes=True
world.node_tree.nodes['Background'].inputs[0].default_value=(.32,.35,.40,1)
world.node_tree.nodes['Background'].inputs[1].default_value=.28
bpy.context.scene.world=world

def area(name,loc,power,color,size,target,size_y=None):
    data=bpy.data.lights.new(name,'AREA'); data.energy=power; data.color=color
    if size_y:
        data.shape='RECTANGLE'; data.size=size; data.size_y=size_y
    else: data.shape='DISK'; data.size=size
    ob=bpy.data.objects.new(name,data); bpy.context.collection.objects.link(ob)
    ob.location=loc
    ob.rotation_euler=(Vector(target)-ob.location).to_track_quat('-Z','Y').to_euler()
    return ob

area('Large raking softbox',(-3.8,-4.6,6.6),820,(1.0,.89,.76),4.3,(0,-.45,1.35),3.1)
area('Cool soft edge separation',(3.4,3.5,5.7),1050,(.79,.87,1.0),3.3,(0,.4,1.8),4.4)
area('Low frontal fill',(4.5,-4.0,3.4),230,(1.0,.95,.86),3.8,(0,-.4,1.4))

bpy.ops.object.camera_add(location=(6.15,-8.8,4.65))
camera=bpy.context.object; camera.name='Hero three-quarter product camera'
target=Vector((0,-.24,1.69))
camera.rotation_euler=(target-camera.location).to_track_quat('-Z','Y').to_euler()
camera.data.type='ORTHO'; camera.data.ortho_scale=6.25
camera.data.lens=58
bpy.context.scene.camera=camera

scene=bpy.context.scene
scene.render.engine='CYCLES'
scene.cycles.samples=64
scene.cycles.use_denoising=True
scene.render.resolution_x=1536
scene.render.resolution_y=1536
scene.render.resolution_percentage=100
scene.render.image_settings.file_format='PNG'
scene.render.film_transparent=False
scene.view_settings.view_transform='AgX'
scene.view_settings.look='AgX - Medium High Contrast'
scene.view_settings.exposure=.25
scene.render.filepath='/mnt/data/boot_hero.png'
# Set a useful uncluttered inspection viewport as well.
for screen in bpy.data.screens:
    for a in screen.areas:
        if a.type=='VIEW_3D':
            a.spaces.active.region_3d.view_distance=7
            a.spaces.active.region_3d.view_location=target

bpy.ops.wm.save_as_mainfile(filepath='/mnt/data/output.blend')
```