Subagent: Model export and lens shot

agent (cont.)

+10:37
  • Write~/Projects/lowmarch-fable52/tools/whole/patch_lens_flattice.py

    input

    ~/Projects/lowmarch-fable52/tools/whole/patch_lens_flattice.py
    
    """Patch a scratch copy of the built armory page: tiles may take a close-up lens (`lens`, mm on a 36 mm wide frame)
    and Flattice (`flat`, 0..1), drawn by the page's own painted renderer.
    
    Flattice is DillonGoo's add-on done in the vertex shader: every vertex is squashed toward a plane through the head
    bone's head, along the line from there to the camera, by `flat` times a weight that is 1 above the chin and fades to
    0 over 5 cm below it (the add-on's MOD-Flattice group with the cursor at the chin). Normals are left as they were, as
    the Yin rig shades from normals stored before its lattice. Under a lens the outline pass reads perspective depth.
    At flat 1 every layer of the head lands on one plane and a depth buffer can't order them (hair, skin and mouth
    interior fight); 0.85 keeps enough depth, and the outline pass's depth step shrinks with the squash.
    
      python3 patch_lens_flattice.py SITE_DIR
    
    Copied 2026-11-10 from [redacted: private path]/trials/face-shading-2026-10-30/scripts/patch_lens_flattice.py. Changed:
    PATCHES is a list that composes after tools/whole/shoot.py's (tools/whole/lens_shot.py applies both); every fighter on
    the stage is flattened about its own head (cloth displays share the material, so they follow), not only the first;
    the face is found by role, not a VRoid material name; tile keys added: `whole` (default on), `pivot` "chin", `aim`
    {who, bone, dy} (frame centre on a bone), `orbit` (camera yaw about the centre), `height` (camera height, m, in place
    of pitch), `dist` (m, in place of the frame size); the cast shadow's walk reads perspective depth and steps in metres
    at each pixel's own depth; the armory's demo rows are gone (lens_shot.py passes tiles in the URL).
    """
    import sys
    
    PATCHES = [
        # vertex shader: the squash, in view space, after skinning
        ("""  varying vec3 vN; varying vec2 vUv; varying vec3 vView; varying vec3 vObj; varying float vEdge; varying vec4 vPlate;
      void main() {
        ${SKIN_BODY}
        vN = normalize(normalMatrix * objectNormal); vUv = uv; vObj = position; vEdge = edge; vPlate = _plate;
        vec4 mv = modelViewMatrix * vec4(transformed, 1.0); vView = -mv.xyz;""",
         """  varying vec3 vN; varying vec2 vUv; varying vec3 vView; varying vec3 vObj; varying float vEdge; varying vec4 vPlate;
      uniform float uFlattice; uniform float uFlChin; uniform float uFlFall; uniform vec3 uFlPivot;
      void main() {
        ${SKIN_BODY}
        vN = normalize(normalMatrix * objectNormal); vUv = uv; vObj = position; vEdge = edge; vPlate = _plate;
        vec4 mv = modelViewMatrix * vec4(transformed, 1.0);
        if (uFlattice > 0.0) {                                   // Flattice: squash toward the pivot's plane, along pivot-to-camera
          float w = clamp((position.y - (uFlChin - uFlFall)) / uFlFall, 0.0, 1.0);
          vec3 u = normalize(-uFlPivot);
          mv.xyz -= u * dot(mv.xyz - uFlPivot, u) * w * uFlattice;
        }
        vView = -mv.xyz;"""),
        ("""    uMap: { value: map || white }, uAlpha: { value: 0 }, uPass: { value: 0 },""",
         """    uMap: { value: map || white }, uAlpha: { value: 0 }, uPass: { value: 0 }, uCastRef: { value: 0 },
        uFlattice: { value: 0 }, uFlChin: { value: 0 }, uFlFall: { value: 0.05 }, uFlPivot: { value: new THREE.Vector3() },"""),
        # cast shadows: depth read back to metres under a lens, and the walk's 3 mm steps sized at the pixel's own depth
        ("""  float castShadow() {""",
         """  uniform float uCastRef;                                      // a lens: the subject's distance (m); 0 orthographic
      float castZ(float d) { return uCastRef > 0.0 ? uCastNear * uCastFar / (uCastFar - d * (uCastFar - uCastNear))
                                                   : uCastNear + d * (uCastFar - uCastNear); }
      float castShadow() {"""),
        ("float z0 = uCastNear + texture2D(tCastDepth, uv).x * (uCastFar - uCastNear);",
         "float z0 = castZ(texture2D(tCastDepth, uv).x);"),
        ("float zq = uCastNear + texture2D(tCastDepth, q).x * (uCastFar - uCastNear);",
         "float zq = castZ(texture2D(tCastDepth, q).x);"),
        ("vec2 q = uv + dir * dm * uCastPpm * px;",
         "vec2 q = uv + dir * dm * uCastPpm * (uCastRef > 0.0 ? uCastRef / z0 : 1.0) * px;"),
        # outline [redacted: scan false positive] depth under a lens
        ("""  float z(vec2 p) { return uNear + texture2D(tDepth, p).x * (uFar - uNear); }""",
         """  uniform float uPersp;
      float z(vec2 p) { float d = texture2D(tDepth, p).x;
        return uPersp > 0.5 ? uNear * uFar / (uFar - d * (uFar - uNear)) : uNear + d * (uFar - uNear); }"""),
        ("""  uNear: { value: 0.1 }, uFar: { value: 40 },""", """  uNear: { value: 0.1 }, uFar: { value: 40 }, uPersp: { value: 0 },"""),
        # tiles: after the page frames its orthographic view, a lens tile moves in and switches to a perspective projection
        ("""      frameAt(centre, h, tw_, th_, pitch);
          render();""",
         """      frameAt(centre, h, tw_, th_, pitch);
          lensAndFlat(tile, centre, h, tw_, th_, pitch);
          render();"""),
        ("""async function runSheet(kind) {""",
         """function lensAndFlat(tile, centre, h, tw, th, pitch) {
      const pu = paint.mat.uniforms, V3 = THREE.Vector3;
      pu.uPersp.value = 0;
      if (pu.uGap.userData === undefined) pu.uGap.userData = pu.uGap.value;
      pu.uGap.value = pu.uGap.userData * (1 - (tile.flat || 0));   // depth steps shrink with the squash; so does the line test
      let ref = 0;
      if (tile.aim) {                                   // the frame's centre on a fighter's bone
        const c = chars.get(tile.aim.who) || active()[0], b = c.root.getObjectByName(tile.aim.bone || "J_Bip_C_Chest");
        centre = b.getWorldPosition(new V3()); centre.y += tile.aim.dy || 0;
      }
      if (tile.lens) {
        const n = 0.05, f = 60;
        if (tile.dist) h = tile.dist * 36 / tile.lens * th / tw;           // a distance: the frame follows from the lens
        const w = h * tw / th, d = tile.dist || w * tile.lens / 36, o = THREE.MathUtils.degToRad(tile.orbit || 0);
        const a = tile.height != null ? Math.asin(THREE.MathUtils.clamp((tile.height - centre.y) / d, -1, 1)) : THREE.MathUtils.degToRad(pitch);
        camera.position.copy(centre).add(new V3(Math.sin(o) * Math.cos(a), Math.sin(a), Math.cos(o) * Math.cos(a)).multiplyScalar(d));
        camera.up.set(0, 1, 0); camera.lookAt(centre); camera.updateMatrixWorld(true);
        camera.near = n; camera.far = f; camera.top = h / 2; camera.bottom = -h / 2; camera.left = -w / 2; camera.right = w / 2;
        camera.projectionMatrix.makePerspective(-w / 2 * n / d, w / 2 * n / d, h / 2 * n / d, -h / 2 * n / d, n, f);
        camera.projectionMatrixInverse.copy(camera.projectionMatrix).invert();
        pu.uPersp.value = 1; ref = d;
      }
      allMeshes().forEach(o => { const mu = o.userData.mats.painted.uniforms; mu.uCastRef.value = ref; mu.uFlattice.value = 0; });
      const who = tile.flatWho || active().map(c => c.name);
      for (const chr of active().filter(c => who.includes(c.name))) {
        const face = chr.meshes.find(o => o.userData.role === "face");
        face.geometry.computeBoundingBox();
        let pivot = chr.root.getObjectByName("J_Bip_C_Head").getWorldPosition(new V3());
        if (tile.pivot === "chin") {                    // the chin's front: the face's lowest points, the most forward of them
          const P = face.geometry.attributes.position, low = face.geometry.boundingBox.min.y, v = new V3();
          let best = null;
          for (let i = 0; i < P.count; i++) {
            v.fromBufferAttribute(P, i);
            if (v.y < low + 0.006 && (!best || v.z > best.z)) best = v.clone();
          }
          pivot = best.applyMatrix4(face.matrixWorld);
        }
        pivot.applyMatrix4(camera.matrixWorldInverse);
        for (const o of [...chr.meshes, ...chr.cloths.map(cl => cl.display)]) {   // displays share their piece's material
          const mu = o.userData.mats.painted.uniforms;
          mu.uFlattice.value = tile.flat || 0;
          mu.uFlChin.value = tile.whole === false ? face.geometry.boundingBox.min.y + 0.01 : -100;   // whole: the body too
          mu.uFlFall.value = 0.05; mu.uFlPivot.value.copy(pivot);
        }
      }
    }
    async function runSheet(kind) {"""),
    ]
    
    
    def apply(s):
        for old, new in PATCHES:
            if s.count(old) != 1:
                raise SystemExit(f"lens patch target not found once: {old[:70]!r}")
            s = s.replace(old, new)
        return s
    
    
    if __name__ == "__main__":
        path = sys.argv[1] + "/index.html"
        open(path, "w", encoding="utf-8").write(apply(open(path, encoding="utf-8").read()))
        print("patched", path)
    

    result, 1 line

    File created successfully at: ~/Projects/lowmarch-fable52/tools/whole/patch_lens_flattice.py (file state is current in your context — no need to Read it back)
  • Write~/Projects/lowmarch-fable52/tools/whole/lens_shot.py

    input

    ~/Projects/lowmarch-fable52/tools/whole/lens_shot.py
    
    """Shoot one battle frame from a built site (tools/whole/build.py) through a perspective lens, with Flattice, in the
    painted renderer, headless.
    
      python3 tools/whole/lens_shot.py SITE_DIR SPEC.json OUT.png
    
    A scratch copy of the site's page gets tools/whole/shoot.py's patches (tiles from the URL, a clip per fighter, the
    key light, placement, sky), then tools/whole/patch_lens_flattice.py's (lens, Flattice). The site is not changed.
    SPEC: shoot.py's keys (w, h, scale, size, pitch, yaw, centre, clips, place, key, sky, hide), plus:
      lens     focal length, mm on a 36 mm wide frame; 0 or missing: the page's orthographic camera
      flat     Flattice, 0..1 (0.85 is the tested value; 1 makes head layers z-fight)
      whole    true (default): each fighter flattened whole, about its own head bone; false: heads only, from the chin
      pivot    "chin": the squash's plane through the chin's front instead of the head bone
      flatWho  [names]: flatten only these fighters (default all on stage)
      aim      {who, bone, dy}: centre the frame on a fighter's bone (default bone J_Bip_C_Chest), in place of centre
      orbit    the camera's yaw about the centre, degrees (positive moves it toward +x, the soldier's side)
      height   camera height above the ground, m (in place of pitch)
      size     metres of height in frame at the centre's distance; the camera's distance follows from it and the lens
      dist     camera distance, m, in place of size
    """
    import base64
    import io
    import json
    import os
    import re
    import shutil
    import socket
    import subprocess
    import sys
    import tempfile
    import time
    import urllib.parse
    
    from PIL import Image
    
    HERE = os.path.dirname(os.path.abspath(__file__))
    sys.path.insert(0, HERE)
    import shoot  # noqa: E402
    import patch_lens_flattice  # noqa: E402
    
    TILE_KEYS = ("centre", "key", "sky", "hide", "lens", "flat", "whole", "pivot", "flatWho", "aim", "orbit", "height", "dist")
    
    
    def patched_site(site):
        d = tempfile.mkdtemp(prefix="whole_lens_")
        for f in os.listdir(site):
            if f != "index.html":
                os.symlink(os.path.abspath(os.path.join(site, f)), os.path.join(d, f))
        s = open(os.path.join(site, "index.html")).read()
        for old, new in shoot.PATCHES:
            if s.count(old) != 1:
                raise SystemExit(f"shoot patch target not found once: {old[:60]!r}")
            s = s.replace(old, new)
        open(os.path.join(d, "index.html"), "w").write(patch_lens_flattice.apply(s))
        return d
    
    
    def shoot_lens(site, spec, out):
        scale = spec.get("scale", 2)
        tile = dict(clip=spec["clips"], t=0, yaw=spec.get("yaw", 0), pitch=spec.get("pitch", 6), size=spec.get("size", 2.3),
                    label="", **{k: spec[k] for k in TILE_KEYS if k in spec})
        rows = [dict(label="", tw=spec["w"] // scale, th=spec["h"] // scale, tiles=[tile])]
        d = patched_site(site)
        s = socket.socket()
        s.bind(("127.0.0.1", 0))
        port = s.getsockname()[1]
        s.close()
        server = subprocess.Popen([sys.executable, "-m", "http.server", str(port), "--bind", "127.0.0.1", "--directory", d],
                                  stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        try:
            time.sleep(0.5)
            url = f"http://127.0.0.1:{port}/index.html?sheet=duel&scale={scale}&tiles={urllib.parse.quote(json.dumps(rows))}"
            if spec.get("place"):
                url += f"&place={urllib.parse.quote(json.dumps(spec['place']))}"
            r = subprocess.run([os.environ.get("CHROMIUM", "/snap/bin/chromium"), "--headless=new", "--use-angle=swiftshader",
                                "--enable-unsafe-swiftshader", "--ignore-gpu-blocklist", "--window-size=1280,900",
                                "--virtual-time-budget=600000", "--dump-dom", url], capture_output=True, text=True, timeout=900)
            m = re.search(r'id="sheetOut"[^>]*>[redacted: data-URI prefix]([A-Za-z0-9+/=]+)<', r.stdout)
            if not m:
                err = re.search(r'id="sheetOut"[^>]*>([^<]*)<', r.stdout)
                raise SystemExit("FAILED " + (err.group(1)[:400] if err else r.stderr[-800:]))
            im = Image.open(io.BytesIO(base64.b64decode(m.group(1))))
            gap, head = 6, 30                                   # the sheet's margins: crop to the one tile
            im.crop((gap, gap + head, gap + spec["w"], gap + head + spec["h"])).convert("RGB").save(out)
        finally:
            server.terminate()
            shutil.rmtree(d, ignore_errors=True)
    
    
    if __name__ == "__main__":
        if len(sys.argv) != 4:
            raise SystemExit(__doc__)
        t0 = time.time()
        shoot_lens(sys.argv[1], json.load(open(sys.argv[2])), sys.argv[3])
        print(sys.argv[3], f"{time.time() - t0:.0f}s")
    

    result, 1 line

    File created successfully at: ~/Projects/lowmarch-fable52/tools/whole/lens_shot.py (file state is current in your context — no need to Read it back)
  • Bashcd ~/Projects/lowmarch-fable52 && python3 - <<'EOF'

    command

    cd ~/Projects/lowmarch-fable52 && python3 - <<'EOF'
    import json
    f = json.load(open("tools/whole/frame.json"))
    f.pop("centre"); f.pop("size")
    spec = dict(f, lens=40, flat=0.85, whole=True, aim=dict(who="wren", bone="J_Bip_C_Spine", dy=0.05), size=1.35, height=1.25, orbit=24)
    json.dump(spec, open("tools/whole/lens_frame.json", "w"), indent=1)
    EOF
    WHOLE_SITE=review/out/whole-lens/site python3 tools/whole/shoot.py tools/whole/frame.json review/out/whole-lens/ortho_ref.png & 
    python3 tools/whole/lens_shot.py review/out/whole-lens/site tools/whole/lens_frame.json review/out/whole-lens/lens_flat.png; wait

    result, 3 lines

    FAILED 
    review/out/whole-lens/lens_flat.png 14s
    [1]+  Exit 1                  WHOLE_SITE=review/out/whole-lens/site python3 tools/whole/shoot.py tools/whole/frame.json review/out/whole-lens/ortho_ref.png
  • BashWHOLE_SITE=review/out/whole-lens/site python3 tools/whole/shoot.py tools/whole/frame.json review/out/whole-lens/ortho_ref.pngerror

    command

    WHOLE_SITE=review/out/whole-lens/site python3 tools/whole/shoot.py tools/whole/frame.json review/out/whole-lens/ortho_ref.png

    result, error, 2 lines

    Exit code 1
    FAILED
  • Bashpgrep -af chromium | head -5 | cut -c1-200; cd ~/Projects/lowmarch-fable52 && sleep 5; WHOLE_SITE=review/out/whole-lens/site python3 tools/whole/shoot.py tools/whole/frame.json review/out/whole-lens/ortho_ref.pngno result

    command

    pgrep -af chromium | head -5 | cut -c1-200; cd ~/Projects/lowmarch-fable52 && sleep 5; WHOLE_SITE=review/out/whole-lens/site python3 tools/whole/shoot.py tools/whole/frame.json review/out/whole-lens/ortho_ref.png

agent

+11:41

[redacted: process listing]

  • Bashpython3 tools/whole/lens_shot.py review/out/whole-lens/site tools/whole/frame.json review/out/whole-lens/ortho_ref.png

    command

    python3 tools/whole/lens_shot.py review/out/whole-lens/site tools/whole/frame.json review/out/whole-lens/ortho_ref.png

    result, 1 line

    review/out/whole-lens/ortho_ref.png 14s
  • BashWHOLE_SITE=$PWD/review/out/whole-lens/site python3 tools/whole/shoot.py tools/whole/frame.json review/out/whole-lens/ortho_ref_shootpy.png

    command

    WHOLE_SITE=$PWD/review/out/whole-lens/site python3 tools/whole/shoot.py tools/whole/frame.json review/out/whole-lens/ortho_ref_shootpy.png

    result, 1 line

    review/out/whole-lens/ortho_ref_shootpy.png 21s

agent

+12:30

Confirmed: shoot.py needs an absolute WHOLE_SITE. Now the lens shot.

  • Read~/Projects/lowmarch-fable52/review/out/whole-lens/lens_flat.png

    input

    {
      "file_path": "~/Projects/lowmarch-fable52/review/out/whole-lens/lens_flat.png"
    }

    result

    no output

    lens_flat.jpg
    lens_flat.jpg
  • Read~/Projects/lowmarch-fable52/review/out/whole-lens/ortho_ref.png

    input

    {
      "file_path": "~/Projects/lowmarch-fable52/review/out/whole-lens/ortho_ref.png"
    }

    result

    no output

    ortho_ref.jpg
    ortho_ref.jpg
  • Bashpython3 - <<'EOF'

    command

    python3 - <<'EOF'
    p = "tools/whole/patch_lens_flattice.py"
    s = open(p).read()
    old = "centre = b.getWorldPosition(new V3()); centre.y += tile.aim.dy || 0;"
    new = "centre = b.getWorldPosition(new V3()); centre.y += tile.aim.dy || 0; centre.x += tile.aim.dx || 0;"
    assert s.count(old) == 1; s = s.replace(old, new)
    s = s.replace("{who, bone, dy} (frame centre on a bone)", "{who, bone, dx, dy} (frame centre on a bone)")
    open(p, "w").write(s)
    p = "tools/whole/lens_shot.py"
    s = open(p).read()
    s = s.replace("""  aim      {who, bone, dy}: centre the frame on a fighter's bone (default bone J_Bip_C_Chest), in place of centre""",
    """  aim      {who, bone, dx, dy}: centre the frame on a fighter's bone (default J_Bip_C_Chest), moved dx, dy metres
               along the stage's x and y; in place of centre""")
    open(p, "w").write(s)
    import json
    spec = json.load(open("tools/whole/lens_frame.json"))
    spec.update(orbit=10, aim=dict(who="wren", bone="J_Bip_C_Spine", dx=0.45, dy=0.05))
    json.dump(spec, open("tools/whole/lens_frame.json", "w"), indent=1)
    json.dump(dict(spec, flat=0), open("review/out/whole-lens/lens_frame_noflat.json", "w"), indent=1)
    EOF
    python3 tools/whole/lens_shot.py review/out/whole-lens/site tools/whole/lens_frame.json review/out/whole-lens/lens_flat.png && python3 tools/whole/lens_shot.py review/out/whole-lens/site review/out/whole-lens/lens_frame_noflat.json review/out/whole-lens/lens_noflat.png

    result, 2 lines

    review/out/whole-lens/lens_flat.png 12s
    review/out/whole-lens/lens_noflat.png 12s
  • Read~/Projects/lowmarch-fable52/review/out/whole-lens/lens_flat.png

    input

    {
      "file_path": "~/Projects/lowmarch-fable52/review/out/whole-lens/lens_flat.png"
    }

    result

    no output

    lens_flat_2.jpg
    lens_flat_2.jpg
  • Read~/Projects/lowmarch-fable52/review/out/whole-lens/lens_noflat.png

    input

    {
      "file_path": "~/Projects/lowmarch-fable52/review/out/whole-lens/lens_noflat.png"
    }

    result

    no output

    lens_noflat.jpg
    lens_noflat.jpg
  • Bashpython3 -c "

    command

    python3 -c "
    import json; s=json.load(open('tools/whole/lens_frame.json'))
    for k,(o,dx) in enumerate([(0,0),(-20,0)]):
        json.dump(dict(s, orbit=o, aim=dict(s['aim'], dx=dx), w=960, h=540, scale=1), open(f'review/out/whole-lens/t{k}.json','w'))
    " && for k in 0 1; do python3 tools/whole/lens_shot.py review/out/whole-lens/site review/out/whole-lens/t$k.json review/out/whole-lens/t$k.png; done; python3 -c "
    from PIL import Image
    a=[Image.open(f'review/out/whole-lens/t{k}.png') for k in (0,1)]; s=Image.new('RGB',(1920,540)); s.paste(a[0],(0,0)); s.paste(a[1],(960,0)); s.save('[redacted: private path]/scratchpad/orbit.png')"

    result, 2 lines

    review/out/whole-lens/t0.png 8s
    review/out/whole-lens/t1.png 10s
  • Read[redacted: private path]/scratchpad/orbit.png

    input

    {
      "file_path": "[redacted: private path]/scratchpad/orbit.png"
    }

    result

    no output

    orbit.png
    orbit.png
  • Bashrm review/out/whole-lens/t?.json review/out/whole-lens/t?.png && python3 -c "

    command

    rm review/out/whole-lens/t?.json review/out/whole-lens/t?.png && python3 -c "
    import json; s=json.load(open('tools/whole/lens_frame.json'))
    s.update(orbit=-10, size=1.4, aim=dict(who='wren', bone='J_Bip_C_Spine', dx=0.4, dy=0.1))
    json.dump(s, open('tools/whole/lens_frame.json','w'), indent=1)
    json.dump(dict(s, flat=0), open('review/out/whole-lens/lens_frame_noflat.json','w'), indent=1)
    " && date +%s && python3 tools/whole/lens_shot.py review/out/whole-lens/site tools/whole/lens_frame.json review/out/whole-lens/lens_flat.png && python3 tools/whole/lens_shot.py review/out/whole-lens/site review/out/whole-lens/lens_frame_noflat.json review/out/whole-lens/lens_noflat.png && cat tools/whole/lens_frame.json | tr -d '\n ' ; echo

    result, 4 lines

    1794309844
    review/out/whole-lens/lens_flat.png 14s
    review/out/whole-lens/lens_noflat.png 13s
    {"w":1920,"h":1080,"scale":2,"pitch":7,"yaw":0,"place":{"wren":{"pos":[-0.9,0,0],"yaw":62},"mob_soldier":{"pos":[1.3,0,0],"yaw":-70}},"clips":{"wren":["attack",0.4],"mob_soldier":["idle",0.0]},"key":{"wren":[0.85,0.42,0.3]},"sky":["#8fb4d6","#c9dde6","#eef0e2"],"lens":40,"flat":0.85,"whole":true,"aim":{"who":"wren","bone":"J_Bip_C_Spine","dx":0.4,"dy":0.1},"size":1.4,"height":1.25,"orbit":-10}
  • Read~/Projects/lowmarch-fable52/review/out/whole-lens/lens_flat.png

    input

    {
      "file_path": "~/Projects/lowmarch-fable52/review/out/whole-lens/lens_flat.png"
    }

    result

    no output

    lens_flat.png
    lens_flat.png
  • Read~/Projects/lowmarch-fable52/review/out/whole-lens/lens_noflat.png

    input

    {
      "file_path": "~/Projects/lowmarch-fable52/review/out/whole-lens/lens_noflat.png"
    }

    result

    no output

    lens_noflat_2.jpg
    lens_noflat_2.jpg
  • Bashpython3 -c "

    command

    python3 -c "
    from PIL import Image
    a=Image.open('review/out/whole-lens/lens_flat.png').crop((500,40,800,340)).resize((600,600), Image.NEAREST)
    b=Image.open('review/out/whole-lens/lens_noflat.png').crop((500,40,800,340)).resize((600,600), Image.NEAREST)
    s=Image.new('RGB',(1210,600),'white'); s.paste(a,(0,0)); s.paste(b,(610,0)); s.save('[redacted: private path]/scratchpad/heads.png')"

    result, 1 line

    (Bash completed with no output)
  • Read[redacted: private path]/scratchpad/heads.png

    input

    {
      "file_path": "[redacted: private path]/scratchpad/heads.png"
    }

    result

    no output

    heads.png
    heads.png
  • Bashpython3 - <<'EOF'

    command

    python3 - <<'EOF'
    p = "tools/whole/patch_lens_flattice.py"
    s = open(p).read()
    old = "mu.uCastRef.value = ref; mu.uFlattice.value = 0; });"
    new = "mu.uCastRef.value = tile.castOrtho ? 0 : ref; mu.uFlattice.value = 0; });"
    assert s.count(old) == 1; s = s.replace(old, new)
    open(p, "w").write(s)
    p = "tools/whole/lens_shot.py"
    s = open(p).read()
    old = "  dist     camera distance, m, in place of size\n"
    new = old + "  castOrtho  true: the cast shadow walks as if orthographic (the page as built), to compare\n"
    assert s.count(old) == 1; s = s.replace(old, new)
    s = s.replace('"height", "dist")', '"height", "dist", "castOrtho")')
    open(p, "w").write(s)
    import json
    spec = json.load(open("review/out/whole-lens/lens_frame_noflat.json"))
    json.dump(dict(spec, castOrtho=True), open("review/out/whole-lens/lens_frame_castortho.json", "w"))
    EOF
    grep -n castOrtho tools/whole/lens_shot.py tools/whole/patch_lens_flattice.py | head; python3 tools/whole/lens_shot.py review/out/whole-lens/site review/out/whole-lens/lens_frame_castortho.json review/out/whole-lens/lens_noflat_castortho.png && python3 -c "
    from PIL import Image, ImageChops
    a=Image.open('review/out/whole-lens/lens_noflat.png'); b=Image.open('review/out/whole-lens/lens_noflat_castortho.png')
    d=ImageChops.difference(a,b).convert('L'); print('bbox of differences', d.getbbox(), 'pixels >30:', sum(1 for v in d.getdata() if v>30))
    c=Image.open('review/out/whole-lens/ortho_ref.png').crop((440,220,700,480)).resize((520,520), Image.NEAREST)
    x=a.crop((560,140,780,360)).resize((520,520), Image.NEAREST); y=b.crop((560,140,780,360)).resize((520,520), Image.NEAREST)
    s=Image.new('RGB',(1580,520),'white'); s.paste(c,(0,0)); s.paste(x,(530,0)); s.paste(y,(1060,0)); s.save('[redacted: private path]/scratchpad/chin.png')"

    result, 5 lines

    tools/whole/patch_lens_flattice.py:92:  allMeshes().forEach(o => { const mu = o.userData.mats.painted.uniforms; mu.uCastRef.value = tile.castOrtho ? 0 : ref; mu.uFlattice.value = 0; });
    tools/whole/lens_shot.py:20:  castOrtho  true: the cast shadow walks as if orthographic (the page as built), to compare
    tools/whole/lens_shot.py:42:TILE_KEYS = ("centre", "key", "sky", "hide", "lens", "flat", "whole", "pivot", "flatWho", "aim", "orbit", "height", "dist", "castOrtho")
    review/out/whole-lens/lens_noflat_castortho.png 7s
    bbox of differences (614, 251, 656, 265) pixels >30: 197
  • Read[redacted: private path]/scratchpad/chin.png

    input

    {
      "file_path": "[redacted: private path]/scratchpad/chin.png"
    }

    result

    no output

    chin.png
    chin.png
  • Bashpython3 -c "

    command

    python3 -c "
    import json; s=json.load(open('tools/whole/lens_frame.json')); json.dump(dict(s, flat=1.0), open('review/out/whole-lens/lens_frame_flat1.json','w'))" && python3 tools/whole/lens_shot.py review/out/whole-lens/site review/out/whole-lens/lens_frame_flat1.json review/out/whole-lens/lens_flat1.png && python3 -c "
    from PIL import Image
    a=Image.open('review/out/whole-lens/lens_flat1.png').crop((500,40,800,340)).resize((600,600), Image.NEAREST)
    b=Image.open('review/out/whole-lens/lens_flat.png').crop((500,40,800,340)).resize((600,600), Image.NEAREST)
    s=Image.new('RGB',(1210,600),'white'); s.paste(a,(0,0)); s.paste(b,(610,0)); s.save('[redacted: private path]/scratchpad/flat1.png')"

    result, 1 line

    review/out/whole-lens/lens_flat1.png 8s
  • Read[redacted: private path]/scratchpad/flat1.png

    input

    {
      "file_path": "[redacted: private path]/scratchpad/flat1.png"
    }

    result

    no output

    flat1.png
    flat1.png
  • Read~/Projects/lowmarch-fable52/review/out/whole-lens/model_check.png

    input

    {
      "file_path": "~/Projects/lowmarch-fable52/review/out/whole-lens/model_check.png"
    }

    result

    no output

    model_check_2.png
    model_check_2.png
  • Bashrm -f review/out/whole-lens/ortho_ref_shootpy.png && (python3 -m pyflakes tools/whole/export_model.py tools/whole/lens_shot.py tools/whole/patch_lens_flattice.py 2>&1 || python3 -m py_compile tools/whole/export_model.py tools/whole/lens_shot.py tools/whole/patch_lens_flattice.py) | head; grep -n "—" tools/whole/export_model.py tools/whole/lens_shot.py tools/whole/patch_lens_flattice.py; git status --short; ls -la review/out/whole-lens/; date +%s

    command

    rm -f review/out/whole-lens/ortho_ref_shootpy.png && (python3 -m pyflakes tools/whole/export_model.py tools/whole/lens_shot.py tools/whole/patch_lens_flattice.py 2>&1 || python3 -m py_compile tools/whole/export_model.py tools/whole/lens_shot.py tools/whole/patch_lens_flattice.py) | head; grep -n "—" tools/whole/export_model.py tools/whole/lens_shot.py tools/whole/patch_lens_flattice.py; git status --short; ls -la review/out/whole-lens/; date +%s

    result, 26 lines

    /usr/bin/python3: No module named pyflakes
     M tools/whole/build.py
    ?? tools/whole/clips2h.py
    ?? tools/whole/export_model.py
    ?? tools/whole/hairdo.py
    ?? tools/whole/lens_frame.json
    ?? tools/whole/lens_shot.py
    ?? tools/whole/patch_lens_flattice.py
    ?? tools/whole/wren.py
    total 11500
    drwx------ 3 severian severian    4096 Nov 10 19:25 .
    drwx------ 7 severian severian    4096 Nov 10 19:14 ..
    -rw------- 1 severian severian  671752 Nov 10 19:25 lens_flat1.png
    -rw------- 1 severian severian  492573 Nov 10 19:24 lens_flat.png
    -rw------- 1 severian severian     473 Nov 10 19:25 lens_frame_castortho.json
    -rw------- 1 severian severian     456 Nov 10 19:25 lens_frame_flat1.json
    -rw------- 1 severian severian     598 Nov 10 19:24 lens_frame_noflat.json
    -rw------- 1 severian severian  534891 Nov 10 19:25 lens_noflat_castortho.png
    -rw------- 1 severian severian  534602 Nov 10 19:24 lens_noflat.png
    -rw------- 1 severian severian 3828148 Nov 10 19:18 mob_soldier.glb
    -rw------- 1 severian severian  385153 Nov 10 19:18 model_check.png
    -rw------- 1 severian severian  335774 Nov 10 19:18 model_check_soldier.png
    -rw------- 1 severian severian  623647 Nov 10 19:21 ortho_ref.png
    drwx------ 2 severian severian    4096 Nov 10 19:08 site
    -rw------- 1 severian severian 4323156 Nov 10 19:17 wren.glb
    1794309946

agent

+16:50

I've finished both parts: the model export works and loads in three.js and Blender, and the lens shot works. Nothing is committed, and I didn't edit any existing repo file. Along the way I found that shoot.py fails with the relative WHOLE_SITE path from the brief; it needs a one-line fix, described at the end.

Part A: tools/whole/export_model.py SITE_DIR NAME OUT.glb [--check PNG [CLIP [SECONDS]]]

It reads <name>.glb.json and that character's entry in camera.json, so nothing about today's costume is hard-coded. Geometry, skin weights, joints and clips are copied over unchanged. Only the materials are rewritten, by each piece's role:

  • Colours were washed out. export_skinned writes sRGB hex values straight into glTF fields that expect linear colour, and uses palette tones rather than the painted page's colours. Colours now come from the page's paint, converted to linear.
  • Hair and irises had the VRoid sample's colours (navy hair textures, blue irises). Both are recoloured to her auburn hair and amber eyes, keeping the strand and iris detail.
  • Eye highlight disappeared. Its alpha is mostly below 0.5, so the alpha mask cut it all. It is now blended (BLEND) and slightly emissive.
  • Lashes and brows are inked in their flat colour, as cut-outs.
  • Face texture is tinted to the paint's skin colour so it matches the neck.
  • Nose shadow card would show as an orange patch; it is now a faint blend (alpha 0.25) of the skin's shade.
  • Metal: steel and gold are metallic 0.5, roughness 0.4; everything else is metallic 0.
  • Merging: identical materials are merged (61 → 25). All materials stay double-sided.

Project data kept:

  • Per node: group, role and material name, as before.
  • Per material: role, kind, base colour, ramp and bands.
  • The custom _PLATE attribute.
  • On the root node, extras.lowmarch: the character's full scene info (roles, paint, lights, capsules, cloth grids, mouth curve, face marks) and a VRM humanoid map (54 bones).

What a stock renderer gets: her rigged mesh, three clips and plain PBR colours.

What it doesn't get:

  • The painted look: band shading, steel reflection bands, outlines, face cast shadows, brows drawn over the bangs (they sit behind the bangs here), the mouth treatment.
  • Cloth simulation. The skirt follows the leg weights, so in the attack lunge it tears into a wedge (left figure in model_check.png).
  • Expressions (there are no morph targets) and spring bones (this build has none).
  • A clash clip (this build has none).

Counts for wren, read back from the file:

Size4.32 MB
Meshes / primitives61 / 61
Triangles85,506
Materials25 (6 masked, 2 blended, 9 metallic)
Textures / images10 / 10
Joints103, skeleton root Root
Animationsidle 1.80 s, attack 1.50 s, apose 1.00 s
Axes and scalemetres, +Y up, facing +Z, height 1.684 m, feet at y ≈ 0

The rest pose matches the bind matrices to 5.8e-08. Indices, joint indices and weight sums all check out. The rest pose is the site's arms-down reference pose, and the sword points forward along +Z.

Validation:

  • three.js: GLTFLoader 0.160 in headless Chromium, perspective camera, the file's own materials and a plain light. model_check.png shows attack mid-frame, the rest pose and a face close-up. All 61 skinned meshes are present, with no black, white or inside-out pieces and no exploded skinning.
  • One stock-loader catch: posed skinned meshes keep the bounds of their first pose, so three.js culled the eyes in close-ups. The check page turns culling off, as the project page already does; anyone loading the file in three.js needs the same.
  • Blender 5.2: imports in 2 s with no warnings: 1 armature, 103 bones, 3 actions, and lowmarch as a custom property on the armature.
  • The soldier also exports cleanly (mob_soldier.glb, 3.83 MB).

GLB or VRM: GLB. VRM would need the humanoid block (cheap now that the map is in extras), plus a meta block with licence fields and MToon materials, which is the real work. VRM 0.x also expects the model facing −Z.

Licence: the file contains VRoid-derived meshes and textures. The handbook allows baked characters in a game; handing out a model file is a different case, so it's the owner's call before the .glb goes anywhere.

Part B: tools/whole/lens_shot.py SITE_DIR SPEC.json OUT.png

It applies shoot.py's patches first and then the lens patch, to a scratch copy of the page. The spec is shoot.py's format plus these keys, documented in the docstring: lens, flat, whole, pivot, flatWho, aim (who, bone, dx, dy), orbit, height, size or dist, and castOrtho. The spec I used is tools/whole/lens_frame.json: 40 mm lens, camera 1.25 m high, 1.4 m of frame height, aimed at her spine shifted 0.4 m right, orbit −10°, Flattice 0.85, whole figure.

What I changed in the copied patch_lens_flattice.py (the docstring records the source and changes):

  • It is now a list of patches that applies after shoot.py's.
  • Every fighter is flattened about its own head, and the simulated cloth follows.
  • The face is found by role rather than VRoid's material name.
  • I added the aim, orbit, height and distance keys.
  • The face cast shadow now reads perspective depth correctly. Without this the chin shadow on the neck mostly vanishes (lens_noflat_castortho.png shows it gone).
  • The armory demo rows are dropped.

Comparing the two shots (lens_flat.png, lens_noflat.png):

  • What Flattice does: at 0.85 it calms the foreshortening. The near hand and the raised gauntlet shrink, and the face is a little flatter. The look stays the same and the head shows no z-fighting.
  • The soldier's halberd moves a lot: he is flattened about his own head, so his forward-held halberd shifts toward his head on screen and changes the composition. Use flatWho: ["wren"] to leave him alone.
  • At Flattice 1.0 the head is total z-fighting: the back hair shows through the face (lens_flat1.png). 0.85 is the ceiling.
  • The chin shadow narrows with Flattice on, because the jaw-to-neck depth is squashed.
  • Outlines work, but line width is set from the subject's plane, so near and far parts get the same weight.
  • The ground breaks: the 9 × 3.2 m plains strip is built for the orthographic camera at 7°. A chest-height perspective camera sees past its back edge, which shows as a low, slightly tilted fake horizon, and orbiting the camera tilts it more. A perspective scene needs a bigger ground or a backdrop.
  • The breastplate blotches are already in the orthographic frame (ortho_ref.png); the lens didn't cause them.

Wall time

StepTime
Readingabout 4 min
Export2 to 4 s per run (writing the script and iterating: about 4 min)
three.js check7 to 10 s
Blender import2 s
Lens patchabout 2 min to write
Each 1920×1080 shot12 to 14 s (about 6 min of framing iterations in all)

Changes needed in shoot.py (not made)

  1. Relative path bug: with a relative WHOLE_SITE, patched_site() writes broken symlinks into /tmp and the shot fails with an empty "FAILED". The brief's own command hits this. The fix is SITE = os.path.abspath(...).
  2. Optional: if shoot() passed extra spec keys through to the tile, lens_shot.py could reuse it instead of copying the Chromium call.

No change is needed in build.py. Two headless Chromium runs at the same time collided, so run shots one after another.

Files are in ~/Projects/lowmarch-fable52:

  • tools/whole/export_model.py
  • tools/whole/lens_shot.py
  • tools/whole/patch_lens_flattice.py
  • tools/whole/lens_frame.json
  • review/out/whole-lens/wren.glb
  • review/out/whole-lens/model_check.png
  • review/out/whole-lens/lens_flat.png
  • review/out/whole-lens/lens_noflat.png
  • review/out/whole-lens/lens_flat1.png
  • review/out/whole-lens/lens_noflat_castortho.png
  • review/out/whole-lens/ortho_ref.png
  • review/out/whole-lens/mob_soldier.glb
  • review/out/whole-lens/model_check_soldier.png