# Brief: a mean metal walking mech, as one Blender Python script

You write ONE Python script for Blender 5.2 (headless, `bpy`, `bmesh`, `mathutils`, `numpy` available; no internet, no external files). It builds a heavy, mean-looking metal bipedal walking mech with a looping walk cycle and saves it. Nothing is modeled by hand: your script is the whole model.

## How the script is run
`blender -b --factory-startup -P mech.py -- <out_dir>`
Read `out_dir = sys.argv[sys.argv.index('--') + 1]`. The script must save `<out_dir>/mech.blend` with `bpy.ops.wm.save_as_mainfile(filepath=..., compress=True)` as its last step and exit normally.

## The method (same as the human-led reference build)
1. **Pilot first.** Build a simple human pilot figure, 1.75 to 1.85 m tall if standing, or seated in the cab. Name every pilot mesh `pilot_<something>`. Size the cab around the pilot, the hull around the cab, the legs around the hull. The whole mech should stand 4 to 7 m tall.
2. **Every part is its own named mesh object** (plates, bolts, pistons, sleeves, armor, feet, vents, guns). Name parts `<bodyname>.<part>`, for example `thigh_L.armor_front`. Aim for 120 or more parts. Use real geometry (boxes, cylinders, wedges, rings, extruded plates), not one lumped blob. Mean metal: dark gunmetal, scuffed steel, hazard stripes, rust edge color, heavy shoulders, big feet. Give materials (Principled BSDF: Base Color, Metallic, Roughness).
3. **Rigid bodies are Empties named `B_<name>`** (for example `B_hull`, `B_thigh_L`, `B_shin_L`, `B_foot_L`, `B_cab`, `B_arm_R`). Every mesh part is parented to exactly one body Empty (set `part.parent = body` and keep the part's own location in the body's local frame, no parent inverse needed). Bodies are chained as a skeleton (hull to thigh to shin to foot, hull to arms) by parenting Empties to Empties. Hips, knees, ankles and elbows are real joints: the bodies rotate about the joint position, and the parts either side of a joint must not intersect each other through the whole walk (leave a small gap, 5 to 20 mm, round pin or knuckle parts).
4. **Real hydraulic rams.** At least 4 rams (two per leg is good). A ram is two bodies: `B_ram1_barrel` and `B_ram1_rod` (then ram2, ...). The barrel body is parented to one body (for example the hull), the rod body to another (for example the thigh). Each body's origin is its end pin. Both bodies carry animation so that the ram aims at its other end and its length changes through the walk (use a `TRACK_TO` is NOT allowed since the animation must be keyframed; compute the aim in Python and keyframe rotation). The rod slides inside the barrel sleeve and never leaves it.
5. **A looping walk, keyframed.** 24 fps, 48 frames per loop. `scene.frame_start = 1`, `scene.frame_end = 48`, and the pose at frame 49 must equal the pose at frame 1 (key frames 1 to 49) so the loop is seamless. Feet that touch the ground are planted: a planted foot does not slide on the ground while the hull moves forward. The hull travels forward (+Y) at least 2.5 m per loop (frame 1 to 49). The lowest point of the mech sits on the ground plane z = 0 in the stance and never goes below it. The hull bobs and sways a little, the arms counter-swing the legs, the cab nods slightly. Compute the leg poses with simple two-bone inverse kinematics in Python (planar IK in the leg's sagittal plane), then keyframe the Empties' rotations and locations with `keyframe_insert`.
6. **No collisions.** The audit tests every part against every part of a DIFFERENT body on every frame. Parts of the same body may overlap freely. Parts of different bodies must never intersect (not even touching surfaces), including across joints.
7. Do NOT add a ground, lights or a camera. The harness adds them.

## Blender 5.2 notes
- Use `bpy.data.meshes.new` plus `from_pydata`, or `bmesh`, or `bpy.ops.mesh.primitive_*_add` (works headless). Give every part a closed (watertight) mesh, since the audit uses a closed-mesh containment test.
- `Action.fcurves` no longer exists in Blender 5.x (actions are slotted and layered). Do not touch fcurves. Use `obj.keyframe_insert(data_path="location", frame=f)` and `rotation_euler` only. Set `obj.rotation_mode = 'XYZ'`.
- `mesh.use_auto_smooth` is gone. Use `obj.data.polygons.foreach_set('use_smooth', ...)` or skip smoothing.
- After building, call `bpy.context.view_layer.update()`.
- Keep the poly count sensible (under 400k triangles in total), since the audit runs BVH checks every few frames.
- Put everything in one file, no helper imports other than the standard library, bpy, bmesh, mathutils, math, numpy.

## What you get back each round
The harness runs your script and the audit, then sends you plain text: the script's errors if any, part count, bodies, rams, pilot height, loop seam, travel, foot slide, ground contact, the collision test result (the number of colliding pairs and the worst pairs), and whether the test render worked. You then return the COMPLETE fixed script (not a diff). You may call the tool `check_collisions` to get the full detailed collision list and clearances for the previous build before you write the fix.

## Output format
Reply with a very short plan (3 lines at most), then exactly one fenced ```python code block with the full script. No other code blocks.
