Animate the painted girl through the sun salutation

A new renderer draws the Codex-painted girl through Inbe's twelve steps
from a tracked rig: docs/sun-salutation-girl.json holds the poses, timing,
bones, camera and part key points. The upward salute folds as a swan dive,
the half lift steps back through a low lunge, lifted feet point along the
shin, the shirt always covers the leggings, and the layered head lets the
long hair fall behind a fully painted neck. Output is deterministic and
goes under build/.

Also track the first person-rig renderer and the tool that cut the
generated reference video into poses, and move the Ziran toolchain pin to
8bc31e8 so Python builds load LDLIBS libraries and pass C strings.
This commit is contained in:
waozi committed 2026-09-30 01:53:35 -03:00
1 parent 864ea9de6c
commit ab19f67e31
28 files changed
+2266 -1

No files matched your search

+7
View File
@@ -72,3 +72,10 @@ __pycache__/
/pro/
.kotlin/
ziran.local.toml
# Sun salutation reference videos and frames, and check images; the
# renderers write their videos and frames under build/.
design/sun-salutation/*/reference.mp4
design/sun-salutation/*/reference-*.png
design/sun-salutation/generated-video-cut/source.mp4
design/sun-salutation/girl-parts/head-layers-check.png
@@ -0,0 +1,66 @@
# Generated sun salutation video, cut up
`source.mp4` is the generated 10 s clip (360 × 640, 24 fps, woman figure,
light background). It is not tracked; put it in this folder, then
`python3 design/sun-salutation/generated-video-cut/cut.py` writes these under
`build/sun-salutation-cut/`:
- `poses/` — one still per held pose, taken at the lowest-motion frame.
- `clips/` — each continuous movement, hold to hold, silent.
- `stand-ins/` — first-half clips played backwards for the return steps.
- `coverage.png` — the app's twelve steps marked have / near / missing.
The video runs through the classic lunge version of the sequence once and
stops in the return forward fold. It never bends the elbows and never
flattens the back.
## Poses (steps from `locales/en.txt`)
| Step | Pose | Video | Frame |
|---|---|---|---|
| 1 | Mountain pose | have (prayer hands) | 13 |
| 2 | Upward salute | have | 41 |
| 3 | Standing forward fold | have | 72 |
| 4 | Half lift | **missing** — low lunge instead | 96 |
| 5 | Plank | have | 127 |
| 6 | Low plank | **missing** — slides plank → up dog with straight arms | — |
| 7 | Upward-facing dog | near — thighs stay on the floor (cobra-like) | 165 |
| 8 | Downward-facing dog | have | 204 |
| 9 | Half lift | **missing** — walks feet in straight to the fold | — |
| 10 | Standing forward fold | have | 226 |
| 11 | Upward salute | stand-in only (reversed clip 02) | — |
| 12 | Mountain pose | stand-in only (reversed clip 01) | — |
## Transitions
| Move | Video |
|---|---|
| 1 → 2 | `clips/01` |
| 2 → 3 | `clips/02` — turns from front view to side view mid-dive (frames 54–63) |
| 3 → 4 | fold → lunge only (`clips/03`) |
| 4 → 5 | lunge → plank only (`clips/04`) |
| 5 → 6 | **missing** |
| 6 → 7 | **missing**; `clips/05` goes plank → up dog directly |
| 7 → 8 | `clips/07` |
| 8 → 9 | **missing**; `clips/08` walks in to the fold |
| 9 → 10 | **missing** |
| 10 → 11 | `stand-ins/10` (reversed); `clips/10` starts rising but the video ends |
| 11 → 12 | `stand-ins/11` (reversed) |
## To generate next
1. Fold → half lift → fold (steps 3–4 and 9–10). If lunges are kept
instead, steps 4 and 9 need relabeling, and 8 → 9 needs a step forward
into the lunge rather than walking in.
2. Plank → low plank (elbows bent, body hovering) → upward dog, with thighs
off the floor.
3. Down dog → half lift.
4. Fold → rise to upward salute → mountain, if the reversed first half looks
wrong (the front/side turn plays backwards).
Framing notes for any regeneration: in plank, up dog, and down dog the toes
touch the left edge of the 360 px frame (x = 0). The floor line drifts from
y ≈ 611 standing to y ≈ 590 on the floor. The return fold (frame 226) sits
about 50 px further right than the first fold (frame 72), so `clips/08`
followed by `stand-ins/10` jumps. There is no man figure and no dark
background version yet.
@@ -0,0 +1,155 @@
#!/usr/bin/env python3
"""Cut the generated sun salutation video into poses and transitions.
Frame numbers are zero-based at 24 fps. Hold frames were picked where the
frame-to-frame motion is lowest. The generated video is not tracked; put it
here as source.mp4 first, then run:
python3 design/sun-salutation/generated-video-cut/cut.py
Writes poses/*.png, clips/*.mp4, stand-ins/*.mp4 and coverage.png under
build/sun-salutation-cut/.
"""
import subprocess
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont
HERE = Path(__file__).resolve().parent
SOURCE = HERE / "source.mp4"
OUTPUT = HERE.parents[2] / "build/sun-salutation-cut"
FPS = 24
# Held poses found in the video: name -> frame.
HOLDS = {
"mountain-prayer": 13,
"upward-salute": 41,
"forward-fold": 72,
"low-lunge": 96,
"plank": 127,
"upward-dog": 165,
"downward-dog": 204,
"forward-fold-return": 226,
}
# Continuous segments, hold to hold. The last one is cut off by the video end.
CLIPS = [
("01-mountain-to-upward-salute", 13, 41),
("02-upward-salute-to-forward-fold", 41, 72),
("03-forward-fold-to-low-lunge", 72, 96),
("04-low-lunge-to-plank", 96, 127),
("05-plank-to-upward-dog-skips-low-plank", 127, 165),
("07-upward-dog-to-downward-dog", 165, 204),
("08-downward-dog-walk-in-to-forward-fold", 204, 226),
("10-forward-fold-starts-rising-cut-off", 226, 239),
]
# Return-half transitions made by playing the first half backwards.
STAND_INS = [
("10-forward-fold-to-upward-salute-reversed", 41, 72),
("11-upward-salute-to-mountain-reversed", 13, 41),
]
# The app's twelve steps (locales/en.txt) and what the video has for each.
# status: have, near (usable but not the named pose), missing.
STEPS = [
("Mountain pose", "mountain-prayer", "have", "prayer hands"),
("Upward salute", "upward-salute", "have", ""),
("Standing forward fold", "forward-fold", "have", ""),
("Half lift", "low-lunge", "near", "video does a low lunge"),
("Plank", "plank", "have", "toes cut by frame edge"),
("Low plank", None, "missing", "arms never bend"),
("Upward-facing dog", "upward-dog", "near", "thighs on floor, cobra-like"),
("Downward-facing dog", "downward-dog", "have", ""),
("Half lift", None, "missing", "walks feet in to fold"),
("Standing forward fold", "forward-fold-return", "have", ""),
("Upward salute", "upward-salute", "near", "only by reversing step 2"),
("Mountain pose", "mountain-prayer", "near", "only by reversing step 1"),
]
STATUS_COLORS = {
"have": (46, 125, 50),
"near": (191, 120, 0),
"missing": (198, 40, 40),
}
STATUS_TEXT = {"have": "HAVE", "near": "NEAR", "missing": "MISSING"}
def ffmpeg(*args):
subprocess.run(["ffmpeg", "-v", "error", "-y", *args], check=True)
def frame_filter(first, last):
return f"select='between(n,{first},{last})',setpts=N/{FPS}/TB"
def encode(name, first, last, folder, reverse=False):
vf = frame_filter(first, last)
if reverse:
vf += ",reverse"
ffmpeg("-i", str(SOURCE), "-vf", vf, "-an", "-r", str(FPS),
"-c:v", "libx264", "-crf", "16", "-pix_fmt", "yuv420p",
str(folder / f"{name}.mp4"))
def font(size, bold=False):
name = "NotoSans-Bold.ttf" if bold else "NotoSans-Regular.ttf"
return ImageFont.truetype(f"/usr/share/fonts/truetype/noto/{name}", size)
def coverage_sheet(poses):
tile_w, tile_h, label_h = 240, 300, 86
cols, rows = 6, 2
sheet = Image.new("RGB", (cols * tile_w, rows * (tile_h + label_h)),
(250, 249, 246))
draw = ImageDraw.Draw(sheet)
for index, (title, pose, status, note) in enumerate(STEPS):
x = (index % cols) * tile_w
y = (index // cols) * (tile_h + label_h)
color = STATUS_COLORS[status]
if pose:
image = Image.open(poses / f"{pose}.png").convert("RGB")
# The figure lives in the lower 540 px of the 360x640 frame.
image = image.crop((0, 100, 360, 640))
image.thumbnail((tile_w - 12, tile_h - 12))
if status == "near":
image = Image.blend(image, Image.new("RGB", image.size,
(250, 249, 246)), 0.25)
sheet.paste(image, (x + (tile_w - image.width) // 2,
y + (tile_h - image.height) // 2))
else:
draw.line((x + 60, y + 90, x + tile_w - 60, y + tile_h - 90),
fill=color, width=6)
draw.line((x + tile_w - 60, y + 90, x + 60, y + tile_h - 90),
fill=color, width=6)
draw.rectangle((x + 3, y + 3, x + tile_w - 4, y + tile_h + label_h - 4),
outline=color, width=3)
ly = y + tile_h
draw.text((x + 12, ly), f"{index + 1}. {title}", fill=(30, 30, 30),
font=font(17, bold=True))
draw.text((x + 12, ly + 26), STATUS_TEXT[status], fill=color,
font=font(16, bold=True))
if note:
draw.text((x + 12, ly + 50), note, fill=(90, 90, 90), font=font(14))
sheet.save(OUTPUT / "coverage.png")
def main():
poses = OUTPUT / "poses"
clips = OUTPUT / "clips"
stand_ins = OUTPUT / "stand-ins"
for folder in (poses, clips, stand_ins):
folder.mkdir(parents=True, exist_ok=True)
for name, frame in HOLDS.items():
ffmpeg("-i", str(SOURCE), "-vf", f"select='eq(n,{frame})'",
"-frames:v", "1", str(poses / f"{name}.png"))
for name, first, last in CLIPS:
encode(name, first, last, clips)
for name, first, last in STAND_INS:
encode(name, first, last, stand_ins, reverse=True)
coverage_sheet(poses)
if __name__ == "__main__":
main()
Binary file not shown.

After

Width:  |  Height:  |  Size: 971 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 438 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 391 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 420 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 553 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 426 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 796 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 383 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 331 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 383 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 424 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 501 KiB

@@ -0,0 +1,18 @@
{
"master.png": "Refine this master character image for 2D cut-out yoga animation. Preserve exactly her pretty serene adult anime face in STRICT RIGHT-FACING SIDE PROFILE with one blue eye and level gaze, dark sapphire-blue blunt bangs, white fitted long-sleeved sailor-collar top and red front bow, navy full-length leggings and bare feet. Keep feet together and arms hanging straight down. Adjust overall proportions to about SEVEN heads tall, slim graceful adult, and shorten flowing back hair so it ends at MID-BACK, above her waist. Crisp thin dark-blue/brown outlines and soft TWO-TONE cel shading. Full figure entirely visible with generous clear margins on ALL sides including above head and below soles. True transparent alpha background, no floor, cast shadow, labels, pixel grid, dithering, stars, smoke, glow or other elements. ONE full-body master only.",
"head.png": "Use case: stylized-concept. Asset: ONE isolated cut-out body part for 2D skeletal yoga animation. The input master.png is the exact character/style reference, NOT an object to include. Match its face identity, sapphire-blue hair, warm pale skin, white/blue-shadow fabric, red bow, deep navy leggings, thin dark-blue/brown line weight and soft two-tone cel shading exactly. Draw only the requested part, no other body parts, no duplicates, no labels. Strict right-facing side-profile anatomy unless a different hand/foot view is explicitly specified. Keep the same anatomical scale as master (master figure roughly 1590px tall); do NOT enlarge small parts to fill frame. Choose suitable canvas, center the isolated part with generous transparent margins, no clipping. Genuine transparent alpha background, no floor, cast shadows, checkerboard painted into image, stars, glow, smoke, pixel grid or dithering. Clean high-resolution anime, graceful ADULT anatomy. Requested part: Head only, upright STRICT RIGHT-FACING SIDE PROFILE with ONE luminous blue eye, level straight-ahead gaze, pretty serene face matching master. Include blunt bangs, hair on top and back of skull, ear, small nose, pink lips, chin, short neck pointing straight DOWN ending in a clean horizontal cut. Hair at back stops at NAPE/JAW line with soft natural edge; NO long hair. No shoulders or clothing. Skull about 215px tall at master scale.",
"long-hair.png": "Use case: stylized-concept. Asset: ONE isolated cut-out body part for 2D skeletal yoga animation. The input master.png is the exact character/style reference, NOT an object to include. Match its face identity, sapphire-blue hair, warm pale skin, white/blue-shadow fabric, red bow, deep navy leggings, thin dark-blue/brown line weight and soft two-tone cel shading exactly. Draw only the requested part, no other body parts, no duplicates, no labels. Strict right-facing side-profile anatomy unless a different hand/foot view is explicitly specified. Keep the same anatomical scale as master (master figure roughly 1590px tall); do NOT enlarge small parts to fill frame. Choose suitable canvas, center the isolated part with generous transparent margins, no clipping. Genuine transparent alpha background, no floor, cast shadows, checkerboard painted into image, stars, glow, smoke, pixel grid or dithering. Clean high-resolution anime, graceful ADULT anatomy. Requested part: ONE continuous long lock of dark sapphire-blue hair. Hang straight DOWN vertically as continuation beneath back of head. Wide full flat soft top attachment edge, long strand lines and soft blue cel highlights; taper into several softly wavy pointed tips at bottom. Lock about FIVE TIMES TALLER THAN WIDE, approximately 340px high and 68px wide at master scale. No head, skin, clothing or other elements.",
"torso.png": "Use case: stylized-concept. Asset: ONE isolated cut-out body part for 2D skeletal yoga animation. The input master.png is the exact character/style reference, NOT an object to include. Match its face identity, sapphire-blue hair, warm pale skin, white/blue-shadow fabric, red bow, deep navy leggings, thin dark-blue/brown line weight and soft two-tone cel shading exactly. Draw only the requested part, no other body parts, no duplicates, no labels. Strict right-facing side-profile anatomy unless a different hand/foot view is explicitly specified. Keep the same anatomical scale as master (master figure roughly 1590px tall); do NOT enlarge small parts to fill frame. Choose suitable canvas, center the isolated part with generous transparent margins, no clipping. Genuine transparent alpha background, no floor, cast shadows, checkerboard painted into image, stars, glow, smoke, pixel grid or dithering. Clean high-resolution anime, graceful ADULT anatomy. Requested part: ONLY torso upright vertical in fitted white top in STRICT RIGHT-FACING SIDE PROFILE. Neck OPENING at top but NO neck or head; small white sailor collar, red ribbon bow at FRONT on RIGHT side, gentle bust protruding right, slim waist, hem at hips. Rounded clean shoulder silhouette to overlap arm layer, NO ARM or sleeve extension, no hair, hips or legs. Approximately 440px high at master scale.",
"arm.png": "Use case: background-extraction. Edit the attached isolated white long-sleeved arm with bare wrist stump. REMOVE ALL THE EXTERIOR GLOW AND HALO completely. Keep only the opaque body-part illustration and its crisp dark outline. Every pixel outside the physical outline must have alpha ZERO, absolutely no haze, no shadows or glow. Preserve anatomy, colours, shading and orientation exactly. Center with generous transparent margins. This is a production animation layer on TRUE transparent background.",
"leg.png": "Use case: stylized-concept. Asset: ONE isolated cut-out body part for 2D skeletal yoga animation. The input master.png is the exact character/style reference, NOT an object to include. Match its face identity, sapphire-blue hair, warm pale skin, white/blue-shadow fabric, red bow, deep navy leggings, thin dark-blue/brown line weight and soft two-tone cel shading exactly. Draw only the requested part, no other body parts, no duplicates, no labels. Strict right-facing side-profile anatomy unless a different hand/foot view is explicitly specified. Keep the same anatomical scale as master (master figure roughly 1590px tall); do NOT enlarge small parts to fill frame. Choose suitable canvas, center the isolated part with generous transparent margins, no clipping. Genuine transparent alpha background, no floor, cast shadows, checkerboard painted into image, stars, glow, smoke, pixel grid or dithering. Clean high-resolution anime, graceful ADULT anatomy. Requested part: ONE complete straight leg pointing straight DOWN, vertical; rounded hip cap at top, deep navy leggings continuous through knee exactly HALFWAY down to ankle, small bare ankle stump at bottom. Graceful slim adult thigh and calf; calf bulge at BACK on LEFT; front of leg RIGHT. No seam or cut at knee, NO foot or torso, no second leg. Approximately 740px long.",
"hips.png": "Use case: stylized-concept. Asset: ONE isolated cut-out body part for 2D skeletal yoga animation. The input master.png is the exact character/style reference, NOT an object to include. Match its face identity, sapphire-blue hair, warm pale skin, white/blue-shadow fabric, red bow, deep navy leggings, thin dark-blue/brown line weight and soft two-tone cel shading exactly. Draw only the requested part, no other body parts, no duplicates, no labels. Strict right-facing side-profile anatomy unless a different hand/foot view is explicitly specified. Keep the same anatomical scale as master (master figure roughly 1590px tall); do NOT enlarge small parts to fill frame. Choose suitable canvas, center the isolated part with generous transparent margins, no clipping. Genuine transparent alpha background, no floor, cast shadows, checkerboard painted into image, stars, glow, smoke, pixel grid or dithering. Clean high-resolution anime, graceful ADULT anatomy. Requested part: ONLY pelvis/hip region in deep navy leggings, STRICT RIGHT-FACING SIDE PROFILE. Waistband at top, rounded seat bulging at BACK LEFT, front hip RIGHT. Stop at TOPS OF THIGHS with clean bottom overlap edge; short hip piece, NOT a leg. No torso, legs, hair or skin. Approximately 150px high.",
"hand-upright.png": "Use case: background-extraction. Edit the attached isolated upright back-view hand with fingers up and thumb left. REMOVE ALL THE EXTERIOR GLOW AND HALO completely. Keep only the opaque body-part illustration and its crisp dark outline. Every pixel outside the physical outline must have alpha ZERO, absolutely no haze, no shadows or glow. Preserve anatomy, colours, shading and orientation exactly. Center with generous transparent margins. This is a production animation layer on TRUE transparent background.",
"hand-flat.png": "Use case: stylized-concept. Asset: ONE isolated cut-out body part for 2D skeletal yoga animation. The input master.png is the exact character/style reference, NOT an object to include. Match its face identity, sapphire-blue hair, warm pale skin, white/blue-shadow fabric, red bow, deep navy leggings, thin dark-blue/brown line weight and soft two-tone cel shading exactly. Draw only the requested part, no other body parts, no duplicates, no labels. Strict right-facing side-profile anatomy unless a different hand/foot view is explicitly specified. Keep the same anatomical scale as master (master figure roughly 1590px tall); do NOT enlarge small parts to fill frame. Choose suitable canvas, center the isolated part with generous transparent margins, no clipping. Genuine transparent alpha background, no floor, cast shadows, checkerboard painted into image, stars, glow, smoke, pixel grid or dithering. Clean high-resolution anime, graceful ADULT anatomy. Requested part: ONE slender adult hand pressed flat as in yoga floor support, SIDE VIEW from LITTLE-FINGER side. Wrist stump at LEFT end, palm and fingers horizontal, fingertips point RIGHT. Palm underside and fingers lie along one horizontal floor-contact line but NO visible floor. Anatomically correct, four overlapping fingers plus thumb, pretty warm pale skin matching master. No forearm or sleeve. Approximately 130px long.",
"foot-flat.png": "Use case: stylized-concept. Asset: ONE isolated cut-out body part for 2D skeletal yoga animation. The input master.png is the exact character/style reference, NOT an object to include. Match its face identity, sapphire-blue hair, warm pale skin, white/blue-shadow fabric, red bow, deep navy leggings, thin dark-blue/brown line weight and soft two-tone cel shading exactly. Draw only the requested part, no other body parts, no duplicates, no labels. Strict right-facing side-profile anatomy unless a different hand/foot view is explicitly specified. Keep the same anatomical scale as master (master figure roughly 1590px tall); do NOT enlarge small parts to fill frame. Choose suitable canvas, center the isolated part with generous transparent margins, no clipping. Genuine transparent alpha background, no floor, cast shadows, checkerboard painted into image, stars, glow, smoke, pixel grid or dithering. Clean high-resolution anime, graceful ADULT anatomy. Requested part: ONE slim pretty bare adult foot standing FLAT in STRICT SIDE VIEW. Ankle stump at TOP LEFT THIRD, heel LEFT, toes point RIGHT. Flat sole along horizontal floor-contact line but NO visible floor. Anatomically correct overlapping toes. Warm pale skin matching master, no leggings or leg. Approximately 200px heel-to-toe.",
"foot-tucked.png": "Use case: stylized-concept. Asset: ONE isolated cut-out body part for 2D skeletal yoga animation. The input master.png is the exact character/style reference, NOT an object to include. Match its face identity, sapphire-blue hair, warm pale skin, white/blue-shadow fabric, red bow, deep navy leggings, thin dark-blue/brown line weight and soft two-tone cel shading exactly. Draw only the requested part, no other body parts, no duplicates, no labels. Strict right-facing side-profile anatomy unless a different hand/foot view is explicitly specified. Keep the same anatomical scale as master (master figure roughly 1590px tall); do NOT enlarge small parts to fill frame. Choose suitable canvas, center the isolated part with generous transparent margins, no clipping. Genuine transparent alpha background, no floor, cast shadows, checkerboard painted into image, stars, glow, smoke, pixel grid or dithering. Clean high-resolution anime, graceful ADULT anatomy. Requested part: ONE bare adult foot in PLANK toe-tucked pose, side view. Sole faces LEFT, heel at TOP LEFT, foot almost VERTICAL down to forefoot; toes bend forward at metatarsal joints and rest FLAT pointing RIGHT at BOTTOM. Short ankle stump at TOP. Raised heel, L-shaped side silhouette: vertical heel-to-ball foot then horizontal bent toes. Anatomically correct, warm pale skin matching master. No leg, leggings or floor. Approximately 200px tall.",
"foot-instep.png": "Create a single isolated anime cutout of a RIGHT adult bare foot in fully plantar-flexed yoga upward dog pose, matching warm pale skin and thin brown line/cel shading of the reference master. Exact silhouette: TOE TIPS AT FAR LEFT near bottom; the TOENAILS ARE ON THE LOWER EDGE facing DOWN; the SOLE IS ON THE UPPER EDGE facing UP with a concave arch; the rounded HEEL BULGES UPWARD at far RIGHT; from the heel the short ANKLE STUMP extends HORIZONTALLY RIGHT, ending in a VERTICAL CUT. The lower instep/dorsum contour is a nearly straight horizontal contact line from toe tips on left to ankle on right. A foot rotated so its top rests downward and its sole points upward. No upright shin. No standing foot, no toe-tucked pose. ONE foot only. Clean polished anime style, crisp outlines and two-tone shading. Whole part centered with generous clear margins. Transparent background alpha, no floor, no shadow, no text, no extra parts. Reference image is style only.",
"head-base.png": "Use case: identity-preserve. EDIT TARGET: first image head.png. Second image master.png is identity/style reference only. Create head-base.png, the bald skin-only base layer for a registered 2D cut-out animation. Preserve the FULL 1442x1091 canvas and EXACT original head position and scale. DO NOT recenter, zoom or change face. Remove ALL blue hair including bangs, crown hair, back hair and side locks. Reconstruct the smooth bald scalp/skull anatomically under the removed hair, with warm pale skin, subtle two-tone cel shading and thin brown outline. Keep the existing right-facing profile face, blue eye at approximately (940,535), nose tip at (1072,614), lips, blush, ear at (650,590), jaw and expression absolutely identical to the target. Paint the COMPLETE continuous neck, both front throat and back nape under the removed locks; neck points straight down, ends with clean HORIZONTAL cut at y=948, spanning approximately x=578 to x=814. Back neck joins skull behind the ear smoothly. No shoulders, clothing or hair whatsoever. Genuine transparent alpha background everywhere outside the skin silhouette, no checkerboard or shadow. One bald head only. Crisp thin line art, same anime identity and original scale. Keep visible face pixels unchanged.",
"hair-back.png": "Use case: precise-object-edit. EDIT this hair-back.png image. Preserve FULL 1442x1091 canvas, exact original placement and scale, all upper blue hair pixels and colors. ONE CHANGE: replace the curled uneven ends in bottom 120px of the hair mass with a SOFT EVEN attachment edge at y=880. The rear hair mass must end in one gently rounded almost-horizontal lower edge, from approximately x=385 to x=705. Remove individual curled bob tips and hollow loops at its lower corners. Strand lines flow vertically right to this edge, without turning upward or curling back. This edge is intended to overlap the top of a long vertical blue lock, so not a heavy dark border, just a thin soft blue contour. Keep only rear hair, no bangs, no face, no skin, no foreground side lock. All existing upper shape and sapphire blue two-tone cel colors remain unchanged. Transparent alpha everywhere outside hair, no checkerboard or labels. Do not recenter or enlarge.",
"hair-front.png": "Use case: precise-object-edit. Edit FIRST image hair-front.png. SECOND image head-base.png is geometry guide only. Keep everything unchanged except extend the back edge of the blue cap to cover the bald scalp of the second image. The present diagonal bottom edge is much TOO HIGH and leaves bald scalp exposed. Extend the blue cap behind the ear to y=750: left bottom edge should curve from x=410,y=620 through x=480,y=690 to x=600,y=750. This is hair covering the skull, not long hanging hair. Preserve original crown and bangs EXACTLY, same full 1442x1091 canvas and scale/position. Preserve forward side lock ending at y=840. Ear opening must be shaped like the ear in second image: top at y=519, bottom at y=678, left around x=600, right around x=695; reveal the full ear through transparent alpha. Remove any hair crossing the ear itself. All face/ear/scalp/neck guide pixels remain absent, output BLUE HAIR ONLY. No curls or hanging tips behind the ear, a clean curved cap edge over the rear skull. Existing crisp blue strand lines and cel highlights flow down naturally into extended cap. No background or text, true transparent alpha. Do not recenter enlarge or otherwise redesign.",
"long-hair-v2.png": "Use case: precise-object-edit. EDIT TARGET first long-hair.png, second hair-back.png is exact attachment/color reference, third master.png is character style reference. Make long-hair-v2.png: ONE long vertical continuous dark sapphire-blue hair lock, no other parts. True transparent alpha, full 1442x1091 canvas. Lock hangs straight down, about FIVE TIMES TALLER THAN WIDE. Place its flat soft top attachment from x=380 to x=590 at y=45; width about210px, height about1000px, pointed tips end near y=1045. The top must continue the exposed lower edge of second hair-back reference behind neck: same deep sapphire colors and vertical thin strand lines, no bright highlight stripe at top and no dark horizontal seam. Top attachment soft almost-horizontal and full width, then maintain vertical flow for upper half and taper lower half into softly wavy pointed tips. Match original long hair wave rhythm and two-tone blue highlights, crisp thin dark-blue anime line art. One lock only, no face, skin, scalp, neck, clothing, text or shadows. Preserve generous transparent side margins, do not fill entire canvas. All background genuinely transparent. Lock upright, no diagonal lean."
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 645 KiB

@@ -0,0 +1,105 @@
You are generating artwork for a 2D skeletal (cut-out) animation of a sun
salutation in the Inbe app. Use your built-in `image_gen` tool (imagegen
skill, built-in mode). Do not write code to draw images. Do not use the CLI
fallback. Work only in this repository; do not touch git.
## The character (inspiration: attached reference images)
The attached images show the girl to take inspiration from: a gentle,
serene adult young woman drawn in a polished anime style, with very long,
flowing, dark sapphire-blue hair, blunt straight-cut bangs across the
forehead, large luminous blue eyes with fine lashes, a small nose, soft pink
lips, pale warm skin with a faint blush, a calm and pretty expression. She
wears a white top with a red sailor-style ribbon bow at the collar.
For our yoga animation she wears:
- a fitted white long-sleeved top with a small white sailor collar and a
red ribbon bow at the front of the collar, hem at the hips;
- deep navy-blue full-length leggings;
- bare feet.
Art style for every image: clean, high-resolution anime illustration with
crisp thin dark-blue/brown line art and soft two-tone cel shading, the
colour richness of the reference but NO pixel grid, no dithering, no glow,
no stars, no swirling smoke, no background. Adult proportions (about 7 heads
tall), slim and graceful. Same palette, same line weight and same shading in
every image so the pieces fit together.
## Step 1 — master design
Generate `master.png`: ONE full-body image of her standing in a strict
RIGHT-FACING SIDE PROFILE (she looks to the right edge of the image), feet
together, arms hanging relaxed at her sides, long hair falling down her back
to mid-back. Plain transparent background, whole figure visible with margins.
Look at the result. If the face is not pretty and appealing, or it is not a
true right-facing side profile, regenerate until it is. Every later part must
match this master exactly (face, hair colour, outfit colours, line weight).
Attach/view the master when generating the parts so they stay consistent.
## Step 2 — separate parts (transparent background each)
Each part is ONE completely isolated drawing of ONE body part on a truly
transparent background, centred with generous margins, no shadows, no
labels, no floor, no other body parts, same scale and style as the master.
All parts are seen from her right-facing side profile.
1. `head.png` — her head in strict right-facing side profile, looking
straight ahead (level gaze), upright. Include the blunt bangs, the hair on
top and back of the skull, the ear, one large pretty eye, nose, lips,
chin, and a short neck pointing straight down that ends in a clean
horizontal cut. At the back, the hair stops at the nape/jaw line (the
long hair is a separate layer), ending in a soft natural edge. No
shoulders, no clothing.
2. `long-hair.png` — one long lock of her dark sapphire-blue hair hanging
straight DOWN, as if it continues from under the back of the head: wide
and full at the top edge (flat, soft top edge), flowing straight down with
long strand lines and soft highlights, tapering into several softly wavy
pointed tips at the bottom. About five times taller than wide. Vertical.
3. `torso.png` — her torso only, in the white top, right-facing side
profile, upright and vertical: neck opening at the top (no neck, no head),
white sailor collar and the red ribbon bow at the front of the collar,
gentle bust at the front, slim waist, hem at the hips at the bottom.
Shoulder area rounded and clean where an arm layer will overlap. No arms,
no legs.
4. `arm.png` — ONE complete straight arm pointing straight DOWN, vertical:
rounded shoulder cap at the top, white fitted long sleeve through the
elbow (exactly halfway) to a neat white cuff at the wrist, ending in a
small bare wrist stump. No hand, no torso. No seam or cut at the elbow.
5. `leg.png` — ONE complete straight leg pointing straight DOWN, vertical:
rounded hip at the top, navy leggings through the knee (exactly halfway)
to the ankle, ending in a small bare ankle stump. Shapely slim thigh and
calf, calf at the back (left side). No foot, no torso. No seam at the knee.
6. `hips.png` — the pelvis/hip region in navy leggings, right-facing side
profile: waistband at the top, rounded seat at the back (left), front of
the hips at the right, stopping at the tops of the thighs. No torso, no
legs below the top of the thigh.
7. `hand-upright.png` — ONE slender graceful hand seen from the back of the
hand, fingers together pointing straight UP, thumb along the left side,
short wrist stump at the bottom centre. Pretty, anatomically correct,
four fingers plus thumb.
8. `hand-flat.png` — ONE hand pressed flat on the floor seen from the side
(little-finger side): wrist at the LEFT end, palm and fingers lying flat
along a horizontal line, fingertips pointing RIGHT. Anatomically correct.
9. `foot-flat.png` — ONE bare foot standing flat, side view: ankle stump at
the top left third, heel at the LEFT, toes pointing RIGHT, flat sole along
a horizontal line. Pretty, slim, anatomically correct toes.
10. `foot-tucked.png` — ONE bare foot as in a plank: the sole faces LEFT,
heel at the top left, the foot almost vertical, toes bent forward and
resting flat on the floor pointing RIGHT at the bottom. Ankle stump at
the top.
11. `foot-instep.png` — ONE bare foot pointed, as in upward-facing dog: the
top of the foot resting on the floor, sole facing up, toes pointing LEFT,
ankle stump at the RIGHT end.
Check every generated part: isolated, transparent, correct orientation,
consistent with the master. Regenerate any part that fails.
## Saving
Copy each final PNG (keeping its alpha) into
`design/sun-salutation/girl-parts/` with exactly the file names above
(`master.png`, `head.png`, `long-hair.png`, `torso.png`, `arm.png`,
`leg.png`, `hips.png`, `hand-upright.png`, `hand-flat.png`, `foot-flat.png`,
`foot-tucked.png`, `foot-instep.png`). Also write
`design/sun-salutation/girl-parts/prompts.json` mapping each file name to
the exact prompt you used. Finish with a short list of the saved files.
@@ -0,0 +1,61 @@
You are preparing layered head artwork for a 2D cut-out (skeletal) animation
of the girl in `design/sun-salutation/girl-parts/`. Use your built-in
`image_gen` tool (imagegen skill, built-in mode) for the artwork, and Python
with Pillow/NumPy only to check alignment, trim, and shift layers. Do not
touch git. Work only in this repository.
## Problem
`design/sun-salutation/girl-parts/head.png` has her face and her hair painted
together, and the separate `long-hair.png` strand hangs from under it. In the
animation the long hair falls across her neck and throat, and the neck hidden
behind the hair was never painted. We need the head split into clean layers
that are drawn in this order (back to front):
1. `hair-back.png` — all hair that lies BEHIND her head and neck: the back of
the hair mass from the crown down past the nape to about shoulder length,
ending in a soft, even lower edge (the long hanging strand continues from
there). No bangs, no hair in front of the ear or face.
2. `head-base.png` — her head with NO hair at all: face, the smooth skin of
the scalp/skull, ear, jaw, and the COMPLETE neck (front and back of the
neck fully painted) pointing straight down and ending in a clean
horizontal cut. Same face, eye, nose, lips, skin tone, blush and line art
as `head.png`. No shoulders, no clothing.
3. `hair-front.png` — the hair that lies IN FRONT of the skull and face: the
blunt straight bangs, the hair covering the top and side of the head, and
the side lock in front of the ear, ending around the jaw. Transparent
where her face, ear and neck show.
The three layers must register EXACTLY: same canvas size, same scale, same
position. Stacked in the order above they must look like `head.png`
(her identity, right-facing side profile, level gaze, dark sapphire-blue hair,
same anime style, crisp thin line art, two-tone cel shading) — except that
the neck is now complete and the back hair is separate from the face.
Also make `long-hair.png` v2 only if needed: one long lock that continues
seamlessly from the lower edge of `hair-back.png` (same width at the top,
same colours), hanging straight down, tapering into softly wavy pointed tips,
about five times taller than wide, vertical. Save it as `long-hair-v2.png`.
## How
- Attach/view `head.png` and `master.png` as the identity reference.
- Prefer image EDITS of `head.png` (remove the hair to make the base; keep
only the hair to make the hair layers), keeping the full canvas, so the
layers stay registered. Generate with a truly transparent background.
- After generating, use Python to composite hair-back + head-base +
hair-front over a flat background and compare with `head.png`. If a layer
is offset or scaled, fix it by translating/scaling that layer (do not
repaint by hand), or regenerate it. Also check: the base has a complete
neck; the back hair contains no face; the front hair contains no skin.
- Regenerate anything that fails. Keep the face pretty and identical.
## Saving
Save the final PNGs (with alpha, all on the same canvas as each other) into
`design/sun-salutation/girl-parts/`: `head-base.png`, `hair-back.png`,
`hair-front.png`, and `long-hair-v2.png` if you made it. Also save the check
composite as `design/sun-salutation/girl-parts/head-layers-check.png`, and
add each layer's exact prompt to `design/sun-salutation/girl-parts/prompts.json`
(keep the existing entries). Finish with a short list of saved files and the
canvas size.
Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

+65
View File
@@ -0,0 +1,65 @@
{
"about": "Pose timeline, rig, camera and painted-part key points for scripts/render-sun-salutation-girl.py. Positions are rig units (x right, y down, the floor at y = ground); angles are degrees, 0 = right, 90 = down. Part key points are fractions of each part image's trimmed box.",
"output": {"width": 1536, "height": 1024, "fps": 30, "detail": 2.6},
"camera": {"designWidth": 1536, "designHeight": 1024, "centerX": 780, "groundY": 925, "scale": 1.75, "rigX": 630},
"ground": 435,
"bones": {"torso": 110, "upperArm": 64, "forearm": 64, "thigh": 96, "shin": 96, "farOffset": [-5, -1]},
"timing": {"hold": 3, "transition": 3, "breath": 0.15},
"reach": {"leg": 191.5, "arm": 127.5},
"names": [
"Mountain / prayer pose", "Upward salute", "Standing forward fold", "Half lift",
"High plank", "Low plank", "Upward-facing dog", "Downward-facing dog", "Half lift",
"Standing forward fold", "Upward salute", "Mountain / prayer pose"
],
"sequence": ["mountain", "salute", "fold", "lift", "plank", "low", "upDog", "downDog", "lift", "fold", "salute", "mountain"],
"poses": {
"mountain": {"hip": [626, 226], "spine": -90, "gaze": -90, "ankles": [630, 417], "wrists": [691, 128], "upright": 1},
"salute": {"hip": [626, 226], "spine": -90, "gaze": -98, "ankles": [630, 417], "wrists": [626, -11], "upright": 1},
"fold": {"hip": [626, 226], "spine": 60, "gaze": 30, "ankles": [630, 417], "wrists": [662, 425], "palm": 1},
"lift": {"hip": [626, 226], "spine": 1, "gaze": -75, "ankles": [630, 417], "wrists": [643, 332]},
"plank": {"hip": [622, 329], "spine": -19, "gaze": -80, "ankles": [441, 391], "wrists": [727, 421], "foot": 0.8, "palm": 1},
"low": {"hip": [631, 374], "spine": -5, "gaze": -80, "ankles": [441, 391], "wrists": [727, 421], "foot": 0.8, "palm": 1},
"upDog": {"hip": [644, 369], "spine": -44, "gaze": -100, "ankles": [460, 413], "wrists": [727, 421], "palm": 1, "instep": 1},
"downDog": {"hip": [550, 262], "spine": 42, "gaze": 20, "ankles": [420, 404], "wrists": [727, 421], "foot": 0.3, "palm": 1},
"plant": {"hip": [629, 277], "spine": 44, "gaze": -65, "ankles": [630, 417], "wrists": [727, 421], "palm": 1},
"stepBack": {"about": "Low (runner's) lunge: front knee stacked over the front ankle, back foot already in plank.", "hip": [537.5, 304], "spine": 15, "gaze": -65, "ankles": [630, 417], "farAnkle": [441, 391], "wrists": [727, 421], "palm": 1},
"stepForward": {"about": "Low lunge out of down dog: front knee over the front ankle, back foot still where down dog left it.", "hip": [537.5, 304], "spine": 15, "gaze": -65, "ankles": [630, 417], "farAnkle": [420, 404], "wrists": [727, 421], "palm": 1},
"returnCrouch": {"hip": [629, 285], "spine": 36, "gaze": -65, "ankles": [630, 417], "wrists": [727, 421], "palm": 1}
},
"stepping": {
"about": "Transitions that step one foot at a time, as stops through which the move passes; step indices count from 0.",
"3": ["lift", "plant", "stepBack", "plank"],
"7": ["downDog", "stepForward", "returnCrouch", "lift"],
"lift": 34
},
"swanDive": {
"about": "Upward salute to forward fold (step 1) and back up (step 9): straight arms stay in line with the spine, then sweep down to the floor.",
"steps": [1, 9], "lowerFrom": 0.45, "straightReach": 127, "palmFrom": 0.8
},
"feet": {"pointToes": 0.5, "restingRadius": 12},
"head": {"lift": 0.6, "liftMin": -42, "liftMax": 10},
"hair": {"about": "Roots are fractions of the head's reference box, under the lower edge of the back hair.",
"roots": [[0.25, 0.75], [0.23, 0.89]], "width": 1.0, "segment": 13, "count": 11,
"damping": 0.86, "gravity": 5000, "substeps": 8},
"parts": {
"leg": {"file": "leg.png", "root": [0.52, 0.07], "tip": [0.55, 0.93], "width": 0.86,
"fade": 0.955, "top": [0.03, 0.012], "band": 14},
"arm": {"file": "arm.png", "root": [0.50, 0.08], "tip": [0.62, 0.955], "width": 0.78,
"fade": 0.975, "band": 10},
"torso": {"file": "torso.png", "hip": [0.44, 0.97], "shoulder": [0.23, 0.23], "width": 0.8, "neck": [0.62, 0.05],
"slide": 0.2, "slideFrom": [0.1, 0.9]},
"hips": {"file": "hips.png", "pivot": [0.52, 0.66], "height": 53},
"head": {"layers": {"back": "hair-back.png", "base": "head-base.png", "front": "hair-front.png"},
"reference": "head.png", "pivot": [0.66, 0.985], "height": 71.43},
"longHair": {"file": "long-hair-v2.png"},
"handUpright": {"file": "hand-upright.png", "pivot": [0.52, 0.985], "height": 38},
"handFlat": {"file": "hand-flat.png", "pivot": [0.05, 0.32], "width": 46},
"footFlat": {"file": "foot-flat.png", "pivot": [0.21, 0.35], "width": 50, "cut": ["top", 0.27]},
"footTucked": {"file": "foot-tucked.png", "pivot": [0.37, 0.12], "height": 50, "cut": ["top", 0.07]},
"footInstep": {"file": "foot-instep.png", "pivot": [0.9, 0.45], "width": 54, "turn": -11.46, "cut": ["right", 0.93]}
},
"themes": {
"light": {"background": [240, 236, 238], "shadow": [70, 60, 110, 0.18], "far": [30, 20, 50, 0.2]},
"dark": {"background": [22, 24, 40], "shadow": [0, 0, 0, 0.4], "far": [0, 0, 20, 0.35]}
}
}
+51
View File
@@ -0,0 +1,51 @@
# Sun salutation with the painted girl
`scripts/render-sun-salutation-girl.py` animates the painted girl through
Inbe's twelve-step sun salutation:
```sh
python3 scripts/render-sun-salutation-girl.py # video
python3 scripts/render-sun-salutation-girl.py --still 21 # one frame
```
The video goes to `build/sun-salutation-girl/video/light/sun-salutation.mp4`
(1536 × 1024, 30 fps, 69 s, one chapter per step) and stills to
`build/sun-salutation-girl/light/`. It needs Python 3 with pycairo, NumPy and
Pillow, and FFmpeg with libx264. Rendering is offscreen and deterministic: the
same inputs give byte-identical video, and a still matches the same frame of
the video, hair included.
## Inputs
- `docs/sun-salutation-girl.json` holds everything that places her: the
poses and their order, hold and transition times, bone lengths, the
camera, the stepping stops through the low lunge, the swan-dive arms, the
hair strand, the part key points, and the themes. Change poses here.
- `design/sun-salutation/girl-parts/` holds the painted parts, made with
Codex's built-in image generation from the reference girl; `prompts.json`
keeps every prompt and `design/sun-salutation/girl-source/` the briefs.
The head is three layers on one canvas (back hair, face and neck, front
hair) so the long hair falls behind a fully painted neck.
## How a frame is drawn
The timeline interpolates the rig poses and moves the hip so hands and feet
can reach. Arms and legs are two-bone chains; their paintings are warped
along the bent bones. Head, torso, hips, hands and feet move rigidly, and the
hand and foot drawings switch at the moment of contact. The long lock is a
Verlet strand stepped once per video frame from rest at 0 s.
Back to front: far leg and arm (slightly darker), long hair, back hair, hips,
near leg, torso, face and neck, front hair, near arm. The shirt is always
drawn over the leggings; when her hips are above her shoulders it slides
toward her chest and shows the waistband.
## Transitions
- Upward salute to forward fold, and back up, is a swan dive: straight arms
stay in line with the spine, then sweep to the floor.
- Half lift to plank goes through a low lunge: hands planted, the far foot
steps back first with the front knee over the ankle, then the near foot.
Down dog to half lift steps forward the same way.
- A foot in the air points its toes along the shin; each foot keeps its own
drawing while the feet are apart.
+893
View File
@@ -0,0 +1,893 @@
#!/usr/bin/env python3
"""Animate the painted girl through Inbe's twelve-step sun salutation.
Every visible drawing is one of the painted parts in
design/sun-salutation/girl-parts/ (made with Codex's built-in image
generation; prompts in prompts.json). The pose timeline, bone lengths,
camera and part key points live in docs/sun-salutation-girl.json. Arms, legs
and the long hair are warped along their bones; head, torso, hands and feet
move rigidly. Rendering is offscreen with Cairo and deterministic: the same
inputs give the same frames, and a single still matches its video frame.
"""
import argparse
from dataclasses import dataclass, replace
import json
import math
from pathlib import Path
import subprocess
import cairo
import numpy as np
from PIL import Image
ROOT = Path(__file__).resolve().parents[1]
RIG = ROOT / "docs/sun-salutation-girl.json"
PARTS = ROOT / "design/sun-salutation/girl-parts"
BUILD = ROOT / "build/sun-salutation-girl"
# Vector helpers ----------------------------------------------------------
def add(a, b):
return a[0] + b[0], a[1] + b[1]
def sub(a, b):
return a[0] - b[0], a[1] - b[1]
def mul(a, value):
return a[0] * value, a[1] * value
def mix(a, b, amount):
return add(mul(a, 1 - amount), mul(b, amount))
def unit(angle):
return math.cos(angle), math.sin(angle)
def normalize(vector):
return mul(vector, 1 / max(math.hypot(*vector), 1e-9))
def rotate(point, angle):
c, s = math.cos(angle), math.sin(angle)
return point[0] * c - point[1] * s, point[0] * s + point[1] * c
def wrap(angle):
return math.atan2(math.sin(angle), math.cos(angle))
def ease(amount):
amount = min(1.0, max(0.0, amount))
return amount ** 3 * (amount * (amount * 6 - 15) + 10)
def smoothstep(low, high, value):
amount = min(1.0, max(0.0, (value - low) / (high - low)))
return amount * amount * (3 - 2 * amount)
def joint(root, target, first, second, bend):
"""Two-bone inverse kinematics: the middle joint and the reached end."""
delta = sub(target, root)
distance = math.hypot(*delta)
direction = mul(delta, 1 / max(distance, 0.000001))
distance = min(first + second - 0.00001, max(abs(first - second) + 0.00001, distance))
endpoint = add(root, mul(direction, distance))
along = (first ** 2 - second ** 2 + distance ** 2) / (2 * distance)
height = math.sqrt(max(0.0, first ** 2 - along ** 2))
perpendicular = (-direction[1], direction[0])
middle = add(add(root, mul(direction, along)), mul(perpendicular, height * bend))
return middle, endpoint
def spline_points(points, per_segment=6):
"""Dense open Catmull-Rom samples through the points."""
result = []
count = len(points)
for index in range(count - 1):
p0 = points[max(index - 1, 0)]
p1, p2 = points[index], points[index + 1]
p3 = points[min(index + 2, count - 1)]
for step in range(per_segment):
t = step / per_segment
t2, t3 = t * t, t * t * t
result.append(tuple(
0.5 * (2 * p1[axis] + (p2[axis] - p0[axis]) * t
+ (2 * p0[axis] - 5 * p1[axis] + 4 * p2[axis] - p3[axis]) * t2
+ (3 * p1[axis] - p0[axis] - 3 * p2[axis] + p3[axis]) * t3)
for axis in (0, 1)))
result.append(points[-1])
return result
# Poses and timeline ------------------------------------------------------
@dataclass(frozen=True)
class Pose:
hip: tuple
spine: float
gaze: float
near_ankle: tuple
far_ankle: tuple
near_wrist: tuple
far_wrist: tuple
foot: float = 0.0
palm: float = 0.0
instep: float = 0.0
upright: float = 0.0
torso: float = 110.0
@property
def shoulder(self):
return add(self.hip, mul(unit(self.spine), self.torso))
class Timeline:
"""The twelve poses, their holds and transitions, from the rig JSON."""
def __init__(self, rig):
self.rig = rig
self.hold = rig["timing"]["hold"]
self.transition = rig["timing"]["transition"]
self.period = self.hold + self.transition
self.steps = len(rig["sequence"])
self.poses = {name: self.pose(spec) for name, spec in rig["poses"].items()}
self.sequence = [self.poses[name] for name in rig["sequence"]]
self.stepping = {int(step): [self.poses[name] for name in stops]
for step, stops in rig["stepping"].items() if step.isdigit()}
self.duration = self.period * (self.steps - 1) + self.hold
def pose(self, spec):
ankles = tuple(spec["ankles"])
wrists = tuple(spec["wrists"])
return Pose(
hip=tuple(spec["hip"]), spine=math.radians(spec["spine"]), gaze=math.radians(spec["gaze"]),
near_ankle=ankles, far_ankle=tuple(spec.get("farAnkle", ankles)),
near_wrist=wrists, far_wrist=tuple(spec.get("farWrist", wrists)),
foot=spec.get("foot", 0.0), palm=spec.get("palm", 0.0),
instep=spec.get("instep", 0.0), upright=spec.get("upright", 0.0),
torso=self.rig["bones"]["torso"])
def phase(self, time):
"""Step index and linear transition amount, or None while a pose holds."""
step = min(self.steps - 1, int(time / self.period))
local = time - step * self.period
if local < self.hold or step == self.steps - 1:
return step, None
return step, (local - self.hold) / self.transition
def stepping_segment(self, step, amount):
stops = self.stepping[step]
count = len(stops) - 1
segment = min(count - 1, int(amount * count))
return stops[segment], stops[segment + 1], amount * count - segment
def interpolate(self, a, b, amount, stepping=False):
amount = ease(amount)
points = {}
for name in ("hip", "near_ankle", "far_ankle", "near_wrist", "far_wrist"):
point = mix(getattr(a, name), getattr(b, name), amount)
if stepping and "ankle" in name:
travel = abs(getattr(b, name)[0] - getattr(a, name)[0])
lift = min(self.rig["stepping"]["lift"], travel * 0.2)
point = point[0], point[1] - lift * math.sin(math.pi * amount)
points[name] = point
scalars = {name: getattr(a, name) * (1 - amount) + getattr(b, name) * amount
for name in ("spine", "gaze", "foot", "palm", "instep", "upright")}
return Pose(**points, **scalars, torso=a.torso)
def raw(self, time):
step, amount = self.phase(time)
if amount is None:
local = time - step * self.period
breath = math.sin(math.pi * min(local, self.hold) / self.hold) ** 2
pose = self.sequence[step]
return replace(pose, hip=add(pose.hip, (0, -self.rig["timing"]["breath"] * breath)))
if step in self.stepping:
start, end, part = self.stepping_segment(step, amount)
return self.interpolate(start, end, part, stepping=True)
return self.interpolate(self.sequence[step], self.sequence[step + 1], amount)
def sample(self, time):
"""The pose at `time`, with the hip moved so hands and feet can reach."""
p = self.swan_dive(self.raw(time), time)
axis = mul(unit(p.spine), p.torso)
reach = self.rig["reach"]
discs = ((p.near_ankle, reach["leg"]), (p.far_ankle, reach["leg"]),
(sub(p.near_wrist, axis), reach["arm"]), (sub(p.far_wrist, axis), reach["arm"]))
hip = p.hip
for _ in range(40):
for center, radius in discs:
delta = sub(hip, center)
distance = math.hypot(*delta)
if distance > radius:
hip = add(center, mul(delta, radius / distance))
return replace(p, hip=hip)
def swan_dive(self, p, time):
"""Upward salute to forward fold (and back up) as a swan dive.
Moving the wrists in a straight line from overhead to the floor while
the spine hinges folds the elbows up behind the head. Instead the
straight arms stay in line with the spine as she hinges, then sweep
down to the floor beside her feet in the last part of the fold.
"""
dive = self.rig["swanDive"]
step, amount = self.phase(time)
if amount is None or step not in dive["steps"]:
return p
amount = ease(amount)
down = amount if self.sequence[step + 1] is self.poses["fold"] else 1 - amount
fold = self.poses["fold"]
reach_vector = sub(fold.near_wrist, fold.shoulder)
fold_relative = wrap(math.atan2(reach_vector[1], reach_vector[0]) - fold.spine)
lower = smoothstep(dive["lowerFrom"], 1.0, down)
straight = dive["straightReach"]
reach = straight + (math.hypot(*reach_vector) - straight) * lower
wrist = add(p.shoulder, mul(unit(p.spine + fold_relative * lower), reach))
return replace(p, near_wrist=wrist, far_wrist=wrist, upright=0.0,
palm=smoothstep(dive["palmFrom"], 1.0, down))
def resting_foot(self, ankle):
"""The foot angle for where an ankle rests: plank, down dog, or standing."""
radius = self.rig["feet"]["restingRadius"]
for name in ("plank", "downDog"):
pose = self.poses[name]
if math.dist(ankle, pose.near_ankle) < radius:
return pose.foot
return 0.0
def feet(self, p, time):
"""Per-foot (foot angle, airborne amount), keyed by far=True/False.
While stepping back into plank or forward out of down dog the feet are
in different places, and the moving foot is in the air.
"""
shared = {False: (p.foot, 0.0), True: (p.foot, 0.0)}
step, amount = self.phase(time)
if amount is None or step not in self.stepping:
return shared
start, end, part = self.stepping_segment(step, amount)
eased = ease(part)
result = {}
for far, name in ((False, "near_ankle"), (True, "far_ankle")):
begin, finish = getattr(start, name), getattr(end, name)
angle = self.resting_foot(begin) * (1 - eased) + self.resting_foot(finish) * eased
airborne = math.sin(math.pi * eased) if math.dist(begin, finish) > 1 else 0.0
result[far] = (angle, airborne)
return result
class Hair:
"""Verlet strand whose first two points follow the head."""
def __init__(self, spec, ground):
self.segment = spec["segment"]
self.count = spec["count"]
self.damping = spec["damping"]
self.gravity = spec["gravity"]
self.substeps = spec["substeps"]
self.floor = ground - 3
self.points = None
self.previous = None
self.roots = None
def reset(self, roots):
start = roots[1]
self.points = [roots[0], roots[1]] + [
(start[0], start[1] + self.segment * index) for index in range(1, self.count - 1)]
self.points = [(x, min(y, self.floor)) for x, y in self.points]
self.previous = list(self.points)
self.roots = roots
for _ in range(90):
self.substep(roots, 1 / 120)
def step(self, roots, dt, collide):
start = self.roots
for index in range(1, self.substeps + 1):
amount = index / self.substeps
current = tuple(mix(a, b, amount) for a, b in zip(start, roots))
self.substep(current, dt / self.substeps, collide)
self.roots = roots
def substep(self, roots, h, collide=None):
points = self.points
for index in range(2, self.count):
x, y = points[index]
px, py = self.previous[index]
self.previous[index] = (x, y)
points[index] = (x + (x - px) * self.damping,
y + (y - py) * self.damping + self.gravity * h * h)
points[0], points[1] = roots
self.previous[0], self.previous[1] = roots
# Bending limits keep the hair from folding back on itself into a loop.
for _ in range(6):
for index in range(1, self.count - 1):
self.hold(index, index + 1, self.segment, self.segment)
for index in range(1, self.count - 2):
self.hold(index, index + 2, self.segment * 1.85, self.segment * 2)
for index in range(1, self.count - 3):
self.hold(index, index + 3, self.segment * 2.5, self.segment * 3)
for index in range(2, self.count):
x, y = points[index]
points[index] = (x, min(y, self.floor))
if collide:
points[index] = collide(points[index])
def hold(self, first, second, shortest, longest):
points = self.points
delta = sub(points[second], points[first])
distance = max(math.hypot(*delta), 1e-9)
target = min(longest, max(shortest, distance))
if abs(target - distance) < 1e-9:
return
correction = mul(delta, (distance - target) / distance)
if first <= 1:
points[second] = sub(points[second], correction)
else:
points[first] = add(points[first], mul(correction, 0.5))
points[second] = sub(points[second], mul(correction, 0.5))
# Painted parts -----------------------------------------------------------
def trimmed_box(filenames):
"""Union of the non-transparent boxes of images sharing one canvas."""
boxes = []
for filename in filenames:
alpha = np.asarray(Image.open(PARTS / filename).convert("RGBA"))[..., 3]
ys, xs = np.nonzero(alpha > 20)
boxes.append((xs.min(), ys.min(), xs.max() + 1, ys.max() + 1))
return (min(b[0] for b in boxes), min(b[1] for b in boxes),
max(b[2] for b in boxes), max(b[3] for b in boxes))
class Picture:
"""A trimmed part, resampled once to `detail` pixels per rig unit.
`fade` fades out rows below that fraction (a cut stump tucks under a hand
or foot); `top` is (fraction, ramp) where the picture fades in from above;
`cut` is ("top" or "right", fraction) beyond which it is trimmed.
"""
def __init__(self, filename, units_per_pixel, detail, box=None, fade=None, cut=None, top=None):
image = Image.open(PARTS / filename).convert("RGBA")
image = image.crop(box or trimmed_box([filename]))
factor = units_per_pixel * detail
size = (max(2, round(image.width * factor)), max(2, round(image.height * factor)))
image = image.resize(size, Image.LANCZOS)
pixels = np.asarray(image).astype(np.float32)
rows = np.arange(size[1])[:, None] / size[1]
columns = np.arange(size[0])[None, :] / size[0]
if fade is not None:
pixels[..., 3] *= np.clip((fade + 0.012 - rows) / 0.024, 0, 1)
if top is not None:
start, ramp = top
pixels[..., 3] *= np.clip((rows - start) / ramp + 0.5, 0, 1)
if cut is not None:
side, fraction = cut
if side == "top":
pixels[..., 3] *= np.clip((rows - fraction) / 0.03 + 0.5, 0, 1)
else:
pixels[..., 3] *= np.clip((fraction - columns) / 0.02 + 0.5, 0, 1)
premultiplied = pixels.copy()
premultiplied[..., :3] *= pixels[..., 3:4] / 255
bgra = premultiplied[..., [2, 1, 0, 3]].round().astype(np.uint8)
self.width, self.height = size
stride = cairo.ImageSurface.format_stride_for_width(cairo.FORMAT_ARGB32, self.width)
self.buffer = np.zeros((self.height, stride), dtype=np.uint8)
self.buffer[:, :self.width * 4] = bgra.reshape(self.height, -1)
self.surface = cairo.ImageSurface.create_for_data(
self.buffer, cairo.FORMAT_ARGB32, self.width, self.height, stride)
self.alpha = pixels[..., 3]
self.unit = 1 / detail
def point(self, fractions):
return fractions[0] * self.width, fractions[1] * self.height
def affine_triangle(ctx, surface, source, target):
(sx, sy), sb, sc = source
(dx, dy), db, dc = target
u, v = sub(sb, (sx, sy)), sub(sc, (sx, sy))
a, b = sub(db, (dx, dy)), sub(dc, (dx, dy))
determinant = u[0] * v[1] - v[0] * u[1]
if abs(determinant) < 1e-9:
return
xx = (a[0] * v[1] - b[0] * u[1]) / determinant
xy = (-a[0] * v[0] + b[0] * u[0]) / determinant
yx = (a[1] * v[1] - b[1] * u[1]) / determinant
yy = (-a[1] * v[0] + b[1] * u[0]) / determinant
# A collapsed sliver (the inside of a tightly bent knee) has nothing to
# show, and its near-singular transform makes Cairo allocate huge
# intermediate surfaces.
stretch = max(xx * xx + yx * yx, xy * xy + yy * yy)
if abs(xx * yy - xy * yx) < 1e-3 * stretch:
return
ctx.save()
ctx.move_to(*target[0])
ctx.line_to(*target[1])
ctx.line_to(*target[2])
ctx.close_path()
ctx.clip()
ctx.transform(cairo.Matrix(xx, yx, xy, yy, dx - xx * sx - xy * sy, dy - yx * sx - yy * sy))
ctx.set_source_surface(surface)
ctx.get_source().set_filter(cairo.FILTER_BILINEAR)
ctx.paint()
ctx.restore()
def warp_strip(ctx, picture, rows):
"""Map horizontal bands of a picture onto a curved strip.
Each row is (source_y, source_center_x, target_center, target_normal,
across_scale); source columns left of the center go to -normal.
"""
ctx.save()
ctx.set_antialias(cairo.ANTIALIAS_NONE)
for first, second in zip(rows[:-1], rows[1:]):
for left, right in ((0.0, None), (None, float(picture.width))):
corners = []
for row, column in ((first, left), (first, right), (second, right), (second, left)):
source_y, center_x, center, normal, scale = row
x = center_x if column is None else column
corners.append(((x, source_y), add(center, mul(normal, (x - center_x) * scale))))
source = [corner[0] for corner in corners]
target = [corner[1] for corner in corners]
for indices in ((0, 1, 2), (0, 2, 3)):
affine_triangle(ctx, picture.surface, [source[i] for i in indices],
[target[i] for i in indices])
ctx.restore()
def bent_point(start, middle, end, first, band, distance):
"""Point and normal at `distance` along start→middle→end, joint rounded."""
forward = normalize(sub(middle, start))
onward = normalize(sub(end, middle))
if distance <= first - band:
center, tangent = add(start, mul(forward, distance)), forward
elif distance >= first + band:
center, tangent = add(middle, mul(onward, distance - first)), onward
else:
amount = (distance - first + band) / (2 * band)
a, b = sub(middle, mul(forward, band)), add(middle, mul(onward, band))
center = add(add(mul(a, (1 - amount) ** 2), mul(middle, 2 * amount * (1 - amount))), mul(b, amount ** 2))
tangent = normalize(add(mul(sub(middle, a), 1 - amount), mul(sub(b, middle), amount)))
return center, (tangent[1], -tangent[0])
class Limb:
"""An arm or leg painting bent over two bones."""
def __init__(self, spec, lengths, detail):
box = trimmed_box([spec["file"]])
width, height = box[2] - box[0], box[3] - box[1]
root_y, tip_y = spec["root"][1] * height, spec["tip"][1] * height
units = sum(lengths) / (tip_y - root_y)
self.spec, self.lengths = spec, lengths
self.picture = Picture(spec["file"], units, detail, box, spec.get("fade"), top=spec.get("top"))
self.root = self.picture.point(spec["root"])
self.length = self.picture.point(spec["tip"])[1] - self.root[1]
# Smoothed center line of the painting, row by row.
full = Picture(spec["file"], units, detail, box)
centers = []
for y in range(full.height):
xs = np.nonzero(full.alpha[y] > 40)[0]
centers.append((xs.min() + xs.max()) / 2 if len(xs) else full.width / 2)
self.centers = np.convolve(np.pad(centers, 6, mode="edge"), np.ones(13) / 13, mode="valid")
def paint(self, ctx, start, middle, end):
picture = self.picture
first, second = self.lengths
rows = []
for y in np.linspace(0, picture.height - 1, 40):
distance = (y - self.root[1]) / self.length * (first + second)
center, normal = bent_point(start, middle, end, first, self.spec["band"], distance)
rows.append((float(y), float(self.centers[int(y)]), center, normal,
picture.unit * self.spec["width"]))
warp_strip(ctx, picture, rows)
class Rigid:
"""A part that moves rigidly: pivot pinned to a point, turned, scaled.
A part may have `layers` painted on one shared canvas (the head: back
hair, face and neck, front hair). Its key points and size are then
measured on the `reference` image's trimmed box on that canvas.
"""
def __init__(self, spec, detail):
files = list(spec.get("layers", {}).values()) or [spec["file"]]
reference = trimmed_box([spec.get("reference", files[0])])
box = trimmed_box(files + [spec.get("reference", files[0])])
width, height = reference[2] - reference[0], reference[3] - reference[1]
units = spec["height"] / height if "height" in spec else spec["width"] / width
cut = spec.get("cut")
self.layers = {name: Picture(filename, units, detail, box, cut=cut)
for name, filename in spec.get("layers", {}).items()}
self.picture = self.layers.get("base") or Picture(spec["file"], units, detail, box, cut=cut)
# Picture pixels per canvas pixel, and the reference box inside it.
self.scale = self.picture.width / (box[2] - box[0])
self.reference = (reference[0] - box[0], reference[1] - box[1], width, height)
self.pivot = self.canvas_point(spec["pivot"])
self.turn = math.radians(spec.get("turn", 0.0))
def canvas_point(self, fractions):
"""A point given as fractions of the reference box, in picture pixels."""
left, top, width, height = self.reference
return ((left + fractions[0] * width) * self.scale, (top + fractions[1] * height) * self.scale)
def paint(self, ctx, point, angle=0.0, alpha=1.0, layer=None):
if alpha <= 0.01:
return
picture = self.layers[layer] if layer else self.picture
ctx.save()
ctx.translate(*point)
ctx.rotate(angle + self.turn)
ctx.scale(picture.unit, picture.unit)
ctx.translate(-self.pivot[0], -self.pivot[1])
ctx.set_source_surface(picture.surface)
ctx.get_source().set_filter(cairo.FILTER_GOOD)
ctx.paint_with_alpha(alpha)
ctx.restore()
def local(self, fractions):
"""Offset of a reference-box point from the pivot, in unrotated rig units."""
return mul(sub(self.canvas_point(fractions), self.pivot), self.picture.unit)
class Torso:
def __init__(self, spec, length, detail):
box = trimmed_box([spec["file"]])
width, height = box[2] - box[0], box[3] - box[1]
hip = (spec["hip"][0] * width, spec["hip"][1] * height)
shoulder = (spec["shoulder"][0] * width, spec["shoulder"][1] * height)
self.spec = spec
self.picture = Picture(spec["file"], length / math.dist(hip, shoulder), detail, box)
self.hip = self.picture.point(spec["hip"])
shoulder = self.picture.point(spec["shoulder"])
self.axis_angle = math.atan2(shoulder[1] - self.hip[1], shoulder[0] - self.hip[0])
self.neck = self.to_body(self.picture.point(spec["neck"]))
self.back = self.back_contour()
def to_body(self, point):
"""Image pixel to (distance up the spine, forward offset) in rig units."""
offset = sub(point, self.hip)
axis = unit(self.axis_angle)
forward = (-axis[1], axis[0])
up = (offset[0] * axis[0] + offset[1] * axis[1]) * self.picture.unit
ahead = (offset[0] * forward[0] + offset[1] * forward[1]) * self.picture.unit
return up, ahead * self.spec["width"]
def back_contour(self):
ys, xs = np.nonzero(self.picture.alpha > 60)
bins = {}
for x, y in zip(xs[::7], ys[::7]):
up, ahead = self.to_body((x, y))
key = round(up / 4)
bins[key] = min(bins.get(key, 0), ahead)
return sorted((key * 4, depth) for key, depth in bins.items())
def paint(self, ctx, p, length):
ctx.save()
ctx.translate(*p.hip)
ctx.rotate(p.spine)
# The loose top slides toward her chest when her hips are above her
# shoulders, showing the waistband of her leggings.
inverted = smoothstep(*self.spec["slideFrom"], math.sin(p.spine))
ctx.translate(length, 0)
ctx.scale(1 - self.spec["slide"] * inverted, 1)
ctx.translate(-length, 0)
# Squeeze front-to-back around the spine axis.
ctx.scale(1, self.spec["width"])
ctx.rotate(-self.axis_angle)
ctx.scale(self.picture.unit, self.picture.unit)
ctx.translate(-self.hip[0], -self.hip[1])
ctx.set_source_surface(self.picture.surface)
ctx.get_source().set_filter(cairo.FILTER_GOOD)
ctx.paint()
ctx.restore()
# The girl ----------------------------------------------------------------
class Girl:
def __init__(self, rig, theme, width, height, fps):
self.rig = rig
self.timeline = Timeline(rig)
self.theme = rig["themes"][theme]
self.ground = rig["ground"]
self.frame_step = 1 / fps
self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
self.ctx = cairo.Context(self.surface)
self.width, self.height = width, height
parts, bones = rig["parts"], rig["bones"]
detail = rig["output"]["detail"]
self.leg = Limb(parts["leg"], (bones["thigh"], bones["shin"]), detail)
self.arm = Limb(parts["arm"], (bones["upperArm"], bones["forearm"]), detail)
self.torso = Torso(parts["torso"], bones["torso"], detail)
self.head = Rigid(parts["head"], detail)
self.hips = Rigid(parts["hips"], detail)
self.hand_upright = Rigid(parts["handUpright"], detail)
self.hand_flat = Rigid(parts["handFlat"], detail)
self.foot_flat = Rigid(parts["footFlat"], detail)
self.foot_tucked = Rigid(parts["footTucked"], detail)
self.foot_instep = Rigid(parts["footInstep"], detail)
hair = rig["hair"]
hair_box = trimmed_box([parts["longHair"]["file"]])
hair_length = hair["segment"] * (hair["count"] - 1) + 10
self.hair_picture = Picture(parts["longHair"]["file"], hair_length / (hair_box[3] - hair_box[1]),
detail, hair_box)
self.hair = None
self.hair_time = None
self.feet = None
# Placement ---------------------------------------------------------
def body(self, p, point):
"""A (distance up the spine, forward offset) point in rig space."""
forward = (-math.sin(p.spine), math.cos(p.spine))
return add(add(p.hip, mul(unit(p.spine), point[0])), mul(forward, point[1]))
def head_turn(self, p):
head = self.rig["head"]
lift = wrap(p.gaze - p.spine) * head["lift"]
lift = max(math.radians(head["liftMin"]), min(math.radians(head["liftMax"]), lift))
return p.spine + lift + math.pi / 2
def head_point(self, p, fractions):
neck = self.body(p, self.torso.neck)
return add(neck, rotate(self.head.local(fractions), self.head_turn(p)))
def limb_ends(self, p, far, leg):
offset = tuple(self.rig["bones"]["farOffset"]) if far else (0, 0)
bones = self.rig["bones"]
if leg:
root = add(p.hip, offset)
target = add(p.far_ankle if far else p.near_ankle, offset)
return (root, *joint(root, target, bones["thigh"], bones["shin"], -1))
root = add(p.shoulder, offset)
target = add(p.far_wrist if far else p.near_wrist, offset)
return (root, *joint(root, target, bones["upperArm"], bones["forearm"], 1))
# Drawing -----------------------------------------------------------
def tinted(self, draw, far):
"""Draw the far-side limbs slightly darker."""
ctx = self.ctx
if not far:
draw()
return
ctx.push_group()
draw()
red, green, blue, alpha = self.theme["far"]
ctx.set_operator(cairo.OPERATOR_ATOP)
ctx.set_source_rgba(red / 255, green / 255, blue / 255, alpha)
ctx.paint()
ctx.pop_group_to_source()
ctx.paint()
def draw_leg(self, p, far):
hip, knee, ankle = self.limb_ends(p, far, leg=True)
foot, airborne = self.feet[far]
# A lifted foot points its toes along the line of the shin.
shin = math.atan2(ankle[1] - knee[1], ankle[0] - knee[0])
pointed = shin - math.pi / 2 + self.rig["feet"]["pointToes"]
angle = foot + wrap(pointed - foot) * smoothstep(0.0, 0.5, airborne)
def draw():
tucked = (1 - p.instep) * smoothstep(0.55, 0.63, foot) * (1 - smoothstep(0.18, 0.22, airborne))
self.foot_flat.paint(self.ctx, ankle, angle, 1 - p.instep - tucked)
self.foot_tucked.paint(self.ctx, ankle, 0, tucked)
self.foot_instep.paint(self.ctx, ankle, 0, p.instep)
self.leg.paint(self.ctx, hip, knee, ankle)
self.tinted(draw, far)
def draw_arm(self, p, far):
shoulder, elbow, wrist = self.limb_ends(p, far, leg=False)
# Fingers follow the forearm, except upright prayer and salute hands.
hand_angle = wrap(math.atan2(wrist[1] - elbow[1], wrist[0] - elbow[0]) + math.pi / 2)
hand_angle *= 1 - p.upright
def draw():
# The flat hand shows only once the wrist is down at the floor.
flat = smoothstep(0.45, 0.55, p.palm) * (1 - smoothstep(16, 30, self.ground - wrist[1]))
self.hand_upright.paint(self.ctx, wrist, hand_angle, 1 - flat)
if flat > 0.01:
bottom = self.hand_flat.local((0, 1))[1]
self.hand_flat.paint(self.ctx, (wrist[0], self.ground - bottom), 0, flat)
self.arm.paint(self.ctx, shoulder, elbow, wrist)
self.tinted(draw, far)
def update_hair(self, p, time):
"""Advance the hair to `time` exactly as the video does.
The hair starts at rest at 0 s and steps once per video frame with
that frame's pose, so a single still matches the same video frame.
"""
roots = self.rig["hair"]["roots"]
if self.hair_time is None or time < self.hair_time - 1e-9:
start = self.timeline.sample(0.0)
self.hair = Hair(self.rig["hair"], self.ground)
self.hair.reset(tuple(self.head_point(start, fractions) for fractions in roots))
self.hair_time = 0.0
frame = round(self.hair_time / self.frame_step)
target = round(time / self.frame_step)
for index in range(frame + 1, target):
self.advance_hair(self.timeline.sample(index * self.frame_step), index * self.frame_step)
if target > frame:
self.advance_hair(p, time)
def advance_hair(self, p, time):
roots = tuple(self.head_point(p, fractions) for fractions in self.rig["hair"]["roots"])
self.hair.step(roots, time - self.hair_time, self.back_collider(p))
self.hair_time = time
def back_collider(self, p):
"""Keep hair outside her back while she is upright."""
axis = unit(p.spine)
forward = (-math.sin(p.spine), math.cos(p.spine))
strength = 1 - smoothstep(0.2, 0.5, forward[1])
if strength <= 0:
return None
heights = [point[0] for point in self.torso.back]
depths = [point[1] for point in self.torso.back]
def collide(point):
offset = sub(point, p.hip)
height = offset[0] * axis[0] + offset[1] * axis[1]
depth = offset[0] * forward[0] + offset[1] * forward[1]
if not heights[0] <= height <= heights[-1]:
return point
back = float(np.interp(height, heights, depths)) - 1.5
if back < depth < 0:
return add(point, mul(forward, (back - depth) * strength))
return point
return collide
def draw_hair(self):
points = spline_points(self.hair.points, 5)
lengths = [0.0]
for a, b in zip(points[:-1], points[1:]):
lengths.append(lengths[-1] + math.dist(a, b))
total = lengths[-1]
picture = self.hair_picture
rows = []
for y in np.linspace(0, picture.height - 1, 48):
distance = y / (picture.height - 1) * total
index = max(0, min(len(points) - 2, int(np.searchsorted(lengths, distance, side="right")) - 1))
span = max(lengths[index + 1] - lengths[index], 1e-9)
center = mix(points[index], points[index + 1], (distance - lengths[index]) / span)
tangent = normalize(sub(points[index + 1], points[index]))
rows.append((float(y), picture.width / 2, center, (tangent[1], -tangent[0]),
picture.unit * self.rig["hair"]["width"]))
warp_strip(self.ctx, picture, rows)
def ground_shadow(self, p):
ctx = self.ctx
xs = [p.near_ankle[0], p.far_ankle[0], p.near_wrist[0], p.far_wrist[0], p.hip[0]]
left, right = min(xs) - 40, max(xs) + 40
radius = (right - left) / 2
ctx.save()
ctx.translate((left + right) / 2, self.ground)
ctx.scale(1, 9 / radius)
gradient = cairo.RadialGradient(0, 0, 0, 0, 0, radius)
red, green, blue, alpha = self.theme["shadow"]
gradient.add_color_stop_rgba(0, red / 255, green / 255, blue / 255, alpha)
gradient.add_color_stop_rgba(1, red / 255, green / 255, blue / 255, 0)
ctx.set_source(gradient)
ctx.arc(0, 0, radius, 0, math.tau)
ctx.fill()
ctx.restore()
def draw_head(self, p, layer=None):
self.head.paint(self.ctx, self.body(p, self.torso.neck), self.head_turn(p), layer=layer)
def frame(self, time):
ctx = self.ctx
p = self.timeline.sample(time)
self.feet = self.timeline.feet(p, time)
self.update_hair(p, time)
camera = self.rig["camera"]
ctx.save()
ctx.set_source_rgb(*(value / 255 for value in self.theme["background"]))
ctx.paint()
ctx.scale(self.width / camera["designWidth"], self.height / camera["designHeight"])
ctx.translate(camera["centerX"], camera["groundY"])
ctx.scale(camera["scale"], camera["scale"])
ctx.translate(-camera["rigX"], -self.ground)
self.ground_shadow(p)
# Back to front as seen from her near side: far limbs, the long hair
# and the back hair behind her head, neck and back, hips, near leg,
# torso (the shirt covers the leggings), face and neck, the bangs and
# side hair in front of the face, near arm.
self.draw_leg(p, far=True)
self.draw_arm(p, far=True)
# The long lock hangs from under the back hair, which covers its top.
self.draw_hair()
if "back" in self.head.layers:
self.draw_head(p, "back")
# The pelvis in leggings moves with the torso; the shirt covers it
# unless she is upside down.
self.hips.paint(ctx, p.hip, p.spine + math.pi / 2)
self.draw_leg(p, far=False)
self.torso.paint(ctx, p, self.rig["bones"]["torso"])
self.draw_head(p)
if "front" in self.head.layers:
self.draw_head(p, "front")
self.draw_arm(p, far=False)
ctx.restore()
self.surface.flush()
return self.surface.get_data()
def render(rig, theme, output, width, height, fps):
girl = Girl(rig, theme, width, height, fps)
target = output / theme / "sun-salutation.mp4"
target.parent.mkdir(parents=True, exist_ok=True)
BUILD.mkdir(parents=True, exist_ok=True)
timeline = girl.timeline
metadata = BUILD / "chapters.txt"
lines = [";FFMETADATA1", "title=Sun salutation"]
for index, name in enumerate(rig["names"]):
start = index * timeline.period * 1000
end = min(start + timeline.period * 1000, timeline.duration * 1000)
lines.extend(("[CHAPTER]", "TIMEBASE=1/1000", f"START={start:.0f}", f"END={end:.0f}", f"title={name}"))
metadata.write_text("\n".join(lines) + "\n")
encoder = subprocess.Popen([
"ffmpeg", "-hide_banner", "-loglevel", "error", "-nostdin", "-y",
"-f", "rawvideo", "-pixel_format", "bgra", "-video_size", f"{width}x{height}",
"-framerate", str(fps), "-i", "pipe:0", "-f", "ffmetadata", "-i", str(metadata),
"-map", "0:v:0", "-map_metadata", "1", "-map_chapters", "1", "-an",
"-c:v", "libx264", "-crf", "18", "-preset", "fast", "-threads", "3",
"-pix_fmt", "yuv420p", "-movflags", "+faststart", str(target),
], stdin=subprocess.PIPE)
try:
for frame in range(round(timeline.duration * fps)):
encoder.stdin.write(girl.frame(frame / fps))
finally:
encoder.stdin.close()
if encoder.wait() != 0:
raise RuntimeError(f"Encoding failed: {target}")
print(f"Saved {target}", flush=True)
def main():
rig = json.loads(RIG.read_text())
output = rig["output"]
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--theme", choices=("light", "dark", "both"), default="light")
parser.add_argument("--output", type=Path, default=BUILD / "video")
parser.add_argument("--width", type=int, default=output["width"])
parser.add_argument("--height", type=int, default=output["height"])
parser.add_argument("--fps", type=int, default=output["fps"])
parser.add_argument("--still", type=float, action="append", help="render a PNG at this time in seconds")
options = parser.parse_args()
if min(options.width, options.height, options.fps) <= 0 or options.width % 2 or options.height % 2:
parser.error("Dimensions must be positive and even; fps must be positive")
themes = ("light", "dark") if options.theme == "both" else (options.theme,)
for theme in themes:
if options.still:
girl = Girl(rig, theme, options.width, options.height, options.fps)
for still in sorted(options.still):
girl.frame(still)
target = BUILD / theme / f"frame-{still:g}.png"
target.parent.mkdir(parents=True, exist_ok=True)
girl.surface.write_to_png(str(target))
print(target, flush=True)
else:
render(rig, theme, options.output, options.width, options.height, options.fps)
if __name__ == "__main__":
main()
+844
View File
@@ -0,0 +1,844 @@
#!/usr/bin/env python3
"""Draw the reference-video person on the fixed sun salutation rig.
The motion, bone lengths, and pose timeline come from
render-sun-salutation-rig.py. This script replaces the painted cutout
character with the woman from the selected generated reference video:
brown hair, beige crew-neck T-shirt, charcoal leggings, bare feet.
Her face is keyed from one reference frame. Long hair hangs from it as a
simulated strand, and the body is drawn as smooth cel-shaded vector shapes
around the rig bones. Rendering is offscreen with Cairo.
"""
import argparse
import importlib.util
import math
from pathlib import Path
import subprocess
import cairo
import numpy as np
from PIL import Image
from scipy import ndimage
from scipy.interpolate import PchipInterpolator
ROOT = Path(__file__).resolve().parents[1]
SOURCE = ROOT / "design/sun-salutation/person-source"
REFERENCE = SOURCE / "reference.mp4"
HEAD = SOURCE / "head.png"
BUILD = ROOT / "build/sun-salutation-person"
spec = importlib.util.spec_from_file_location("rig", ROOT / "scripts/render-sun-salutation-rig.py")
rig = importlib.util.module_from_spec(spec)
spec.loader.exec_module(rig)
add, sub, mul, unit, joint = rig.add, rig.sub, rig.mul, rig.unit, rig.joint
# Reference frame 108 shows her head in clean profile, facing left.
HEAD_FRAME = 108
HEAD_CROP = (136, 252, 222, 346)
HEAD_NECK_CUT = 343
HEAD_CHIN = 334
HEAD_NECK_COLUMNS = (160, 187)
HEAD_BACKGROUND = (239, 233, 228)
# Rig units per reference pixel: 68 px crown-to-chin becomes 56 units.
HEAD_SCALE = 56 / 68
NECK = (120, 2)
GROUND = 435
THEMES = {
"light": {"background": (240, 234, 229), "shadow": (120, 100, 90, 0.20)},
"dark": {"background": (36, 34, 40), "shadow": (0, 0, 0, 0.35)},
}
SKIN = (242, 208, 188)
SKIN_SHADE = (228, 184, 164)
SKIN_LINE = (156, 108, 96)
SHIRT = (218, 204, 190)
SHIRT_SHADE = (192, 174, 160)
SHIRT_LIGHT = (229, 219, 208)
SHIRT_LINE = (120, 102, 94)
LEGGING = (92, 91, 97)
LEGGING_SHADE = (76, 75, 82)
LEGGING_LIGHT = (103, 102, 109)
LEGGING_FAR = (79, 78, 85)
LEGGING_LINE = (46, 44, 50)
HAIR = (112, 80, 66)
HAIR_SHADE = (88, 60, 50)
HAIR_LIGHT = (142, 106, 88)
HAIR_LINE = (64, 42, 36)
LINE = 1.1
# Width profiles: (distance along the limb, anterior half-width, posterior half-width).
LEG = ((0, 16.5, 17), (12, 16.5, 17.5), (30, 15, 16), (55, 12.5, 13), (80, 10, 10),
(93, 9.2, 8.6), (104, 8.6, 9.4), (118, 8.2, 10.8), (135, 7.6, 10.2),
(155, 6.4, 7.8), (175, 5.3, 5.8), (188, 5.0, 5.2), (192, 5.1, 5.1))
ARM = ((-6, 8.2, 8.2), (0, 8.6, 8.8), (14, 7.8, 8.2), (40, 6.2, 6.6), (58, 5.2, 5.4),
(68, 5.6, 5.9), (80, 6.0, 6.0), (100, 4.8, 4.8), (120, 3.8, 3.8), (128, 3.7, 3.7))
SLEEVE = ((0, 10.4, 10.7), (10, 10.8, 11.2), (20, 10.7, 11.1), (27, 11.0, 11.4))
SLEEVE_CUFF = 21
# Torso outline in (distance up the spine from the hip, forward offset).
TORSO = ((4, -20.5), (14, -17.5), (30, -14.2), (48, -14), (66, -16), (86, -18.5),
(102, -18), (113, -13.5), (119, -7.5), (121.5, 1.5), (119, 9.5), (114, 14),
(106, 18), (98, 22.5), (90, 26.5), (85, 27.6), (79.5, 26.8), (74.5, 24),
(69, 20.5), (57, 18.2), (44, 17.4), (30, 18.4), (16, 20.2), (4, 21.5),
(2.5, 11), (3.5, 0), (3, -10))
TORSO_SHADE = ((4, -20.5), (14, -17.5), (30, -14.2), (48, -14), (66, -16), (86, -18.5),
(102, -18), (111, -14.5), (102, -12.5), (86, -13), (66, -10.5),
(48, -8.8), (30, -9), (14, -11.5), (5, -14))
BUST_SHADE = ((83, 27.4), (79.5, 26.8), (74.5, 24), (69, 20.5), (60, 18.4), (66, 16.4),
(73, 18.5), (78.5, 22))
BUST_LIGHT = ((101, 20.5), (95, 23.8), (89.5, 26), (87, 22.8), (92, 20.2), (98, 18))
TORSO_FOLDS = (((72, 20.5), (57, 16.2), (42, 15.4)), ((40, -12.2), (30, -10.6), (20, -12.4)),
((10, -8), (8.5, 4), (10, 15)))
PELVIS = ((22, -17.5), (8, -19.5), (-4, -20), (-15, -17.8), (-22, -11),
(-20, 4), (-10, 14.5), (4, 18), (22, 18.5))
# Feet, ankle at the origin, facing right. Flat: sole 18 units below.
FLAT_FOOT = ((-4.8, -4), (-5.2, 3), (-7.5, 9), (-9.6, 13.2), (-9, 16.8), (-6, 18),
(0, 17.6), (8, 16.8), (18, 17.6), (26, 18), (33, 18), (37.5, 17.6),
(39.5, 16.2), (38.6, 14.2), (35, 13.3), (28, 12.3), (20, 9.8),
(12, 5.6), (6.5, 1.5), (4.8, -4))
FLAT_FOOT_LINES = (((34.6, 13.5), (35.8, 15.3), (35.4, 17.6)),
((31.2, 13.1), (32.2, 15.4), (31.8, 17.8)))
FLAT_FOOT_SHADE = ((-5, 17.9), (8, 16.8), (22, 17.8), (12, 15.2), (0, 15.6))
# Plank: toes tucked under, sole facing back, floor 44 units below.
TUCKED_FOOT = ((-5, -3), (-9.5, 1.5), (-11.8, 7), (-10.4, 13), (-7.2, 22), (-4.8, 30),
(-3.4, 35.5), (-1.4, 41), (1.5, 43.8), (10, 44), (11.8, 42.6), (10.2, 40.6),
(5, 38.4), (4.2, 30), (5.2, 18), (5.8, 8), (5.2, 0))
TUCKED_FOOT_LINES = (((-1.2, 40.6), (2.5, 39.2), (5, 38.6)),
((6.2, 40.2), (6.8, 42.2), (6.4, 43.9)),
((-9.8, 11.5), (-7.6, 12.6), (-6.4, 15)))
# Upward dog: top of the foot on the floor, sole up, floor 22 units below.
INSTEP_FOOT = ((3.8, -5.6), (-1, -8.6), (-7, -8.4), (-10, -5.5), (-18, -1), (-30, 5.5),
(-40, 11), (-46, 15), (-49.5, 18.6), (-48.5, 21.6), (-43, 22), (-33, 19.6),
(-20, 14.6), (-8, 9.6), (0, 6.8), (3.8, 5.6))
INSTEP_FOOT_LINES = (((-44, 16.2), (-45.4, 19.4), (-44, 21.8)),
((-40.2, 13.8), (-41.6, 17.6), (-40.2, 21)))
INSTEP_FOOT_SHADE = ((-7, -8.4), (-18, -1), (-30, 5.5), (-40, 11), (-38.5, 13.4),
(-28.5, 8.6), (-17.5, 2.6), (-8.5, -3.2))
# Hands, wrist at the origin. Upright: back of the hand, fingers along -y,
# thumb on -x. Flat: little-finger side, palm on a floor 14 units below.
UPRIGHT_HAND = ((3.9, 0), (5.4, -7), (7.4, -14.5), (7.8, -20.5), (7.3, -27.5),
(6.3, -32.6), (5.0, -34.0), (3.7, -33.2), (3.5, -35.6), (2.4, -38.4),
(0.9, -38.1), (0.1, -37.0), (-0.6, -39.7), (-2.2, -40.3), (-3.5, -39.3),
(-4.0, -37.4), (-5.0, -37.6), (-6.5, -36.6), (-7.3, -34.3), (-7.6, -29.5),
(-7.9, -25.0), (-9.2, -24.8), (-11.0, -24.3), (-12.0, -22.2),
(-11.4, -17.5), (-9.6, -11.5), (-6.8, -5.5), (-3.9, 0))
UPRIGHT_HAND_LINES = (((3.6, -33.2), (3.9, -27), (4.1, -21.5)),
((0.1, -37), (0.3, -29), (0.5, -21.5)),
((-4.0, -37.4), (-3.9, -29), (-3.6, -22)),
((-7.9, -25), (-7.6, -20), (-6.6, -15)))
UPRIGHT_HAND_SHADE = ((7.8, -20.5), (7.3, -27.5), (6.3, -32.6), (5.0, -34.0),
(4.4, -27), (4.6, -18), (5.2, -10))
FLAT_HAND = ((-3.9, -1), (-5.0, 5), (-5.0, 10.5), (-2.8, 13.6), (4, 14), (20, 14),
(30, 14), (38.5, 14), (40.4, 13), (39.6, 11.4), (36.8, 10.9), (31, 10.5),
(24, 9.5), (18, 7.8), (12, 4.8), (6.5, 1.5), (3.9, -1))
FLAT_HAND_LINES = (((35.4, 11.1), (34.2, 12.6), (34.8, 14)),
((30.2, 10.6), (29.1, 12.3), (29.7, 14)),
((17, 8.3), (24, 11), (29.2, 12.4)))
# Long hair hanging from under the back of the bob: one full mass with
# clumped tips and a thin loose lock in front. Roots are head-image pixels;
# widths are half-widths per point.
HAIR_LOCKS = (
{"roots": ((30, 42), (22, 66)), "segment": 13, "damping": 0.86, "color": HAIR,
"widths": (5, 10, 15, 16.5, 16.5, 16, 15, 13.5, 12, 10, 8), "clumps": True,
"wave": 2.6, "phase": 0.0},
{"roots": ((38, 48), (32, 74)), "segment": 11, "damping": 0.84, "color": HAIR_LIGHT,
"widths": (4, 5, 6, 6.2, 6, 5.2, 4.2, 3, 1.8), "clumps": False,
"wave": 3.2, "phase": 1.9},
)
HAIR_GRAVITY = 5000
def normalize(vector):
length = math.hypot(*vector)
return mul(vector, 1 / max(length, 1e-9))
def rotate(point, angle):
c, s = math.cos(angle), math.sin(angle)
return point[0] * c - point[1] * s, point[0] * s + point[1] * c
def smoothstep(low, high, value):
amount = min(1.0, max(0.0, (value - low) / (high - low)))
return amount * amount * (3 - 2 * amount)
def spline_points(points, per_segment=6):
"""Dense open Catmull-Rom samples through the points."""
result = []
count = len(points)
for index in range(count - 1):
p0 = points[max(index - 1, 0)]
p1, p2 = points[index], points[index + 1]
p3 = points[min(index + 2, count - 1)]
for step in range(per_segment):
t = step / per_segment
t2, t3 = t * t, t * t * t
result.append(tuple(
0.5 * (2 * p1[axis] + (p2[axis] - p0[axis]) * t
+ (2 * p0[axis] - 5 * p1[axis] + 4 * p2[axis] - p3[axis]) * t2
+ (3 * p1[axis] - p0[axis] - 3 * p2[axis] + p3[axis]) * t3)
for axis in (0, 1)))
result.append(points[-1])
return result
def clockwise(points):
"""Closed shapes share one winding so overlapping sub-paths form a union."""
area = sum(a[0] * b[1] - b[0] * a[1] for a, b in zip(points, points[1:] + points[:1]))
return list(points) if area >= 0 else list(points[::-1])
def smooth_path(ctx, points, closed=True):
"""Catmull-Rom spline through the points, as cubic Beziers."""
if closed:
points = clockwise(points)
count = len(points)
ctx.move_to(*points[0])
last = count if closed else count - 1
for index in range(last):
p0 = points[(index - 1) % count] if closed or index > 0 else points[index]
p1 = points[index]
p2 = points[(index + 1) % count]
p3 = points[(index + 2) % count] if closed or index + 2 < count else p2
c1 = add(p1, mul(sub(p2, p0), 1 / 6))
c2 = sub(p2, mul(sub(p3, p1), 1 / 6))
ctx.curve_to(*c1, *c2, *p2)
if closed:
ctx.close_path()
def polygon_path(ctx, points):
points = clockwise(points)
ctx.move_to(*points[0])
for point in points[1:]:
ctx.line_to(*point)
ctx.close_path()
def rgb(ctx, color, alpha=1.0):
ctx.set_source_rgba(color[0] / 255, color[1] / 255, color[2] / 255, alpha)
def inked(ctx, build, fill, line, width=LINE):
"""Fill a union of sub-paths with only its outer silhouette outlined."""
ctx.new_path()
build()
ctx.set_line_join(cairo.LINE_JOIN_ROUND)
ctx.set_line_width(width * 2)
rgb(ctx, line)
ctx.set_fill_rule(cairo.FILL_RULE_WINDING)
ctx.stroke_preserve()
rgb(ctx, fill)
ctx.fill()
def shaded(ctx, clip, shade, color):
ctx.save()
ctx.new_path()
clip()
ctx.clip()
ctx.new_path()
shade()
rgb(ctx, color)
ctx.fill()
ctx.restore()
def detail_lines(ctx, lines, color, width=0.7, alpha=1.0):
ctx.set_line_cap(cairo.LINE_CAP_ROUND)
ctx.set_line_width(width)
rgb(ctx, color, alpha)
for line in lines:
ctx.new_path()
smooth_path(ctx, line, closed=False)
ctx.stroke()
class Profile:
def __init__(self, rows):
distances = [row[0] for row in rows]
self.first, self.last = distances[0], distances[-1]
self.anterior = PchipInterpolator(distances, [row[1] for row in rows])
self.posterior = PchipInterpolator(distances, [row[2] for row in rows])
LEG_PROFILE = Profile(LEG)
ARM_PROFILE = Profile(ARM)
class Limb:
"""A smooth tapered limb bent over two bones with a rounded joint."""
def __init__(self, root, middle, end, profile, split, band, count=44):
forward = normalize(sub(middle, root))
onward = normalize(sub(end, middle))
start = add(root, mul(forward, profile.first))
first_length = split - profile.first
distances = np.linspace(profile.first, profile.last, count)
self.samples = []
for distance in distances:
local = distance - profile.first
if local <= first_length - band:
center, tangent = add(start, mul(forward, local)), forward
elif local >= first_length + band:
center, tangent = add(middle, mul(onward, local - first_length)), onward
else:
amount = (local - first_length + band) / (2 * band)
a, b = sub(middle, mul(forward, band)), add(middle, mul(onward, band))
center = add(add(mul(a, (1 - amount) ** 2), mul(middle, 2 * amount * (1 - amount))),
mul(b, amount ** 2))
tangent = normalize(add(mul(sub(middle, a), 1 - amount), mul(sub(b, middle), amount)))
normal = (tangent[1], -tangent[0])
self.samples.append((float(distance), center, normal))
self.profile = profile
def edge(self, anterior_fraction, posterior_fraction, low=None, high=None):
front, back = [], []
for distance, center, normal in self.samples:
if low is not None and not low <= distance <= high:
continue
anterior = float(self.profile.anterior(distance))
posterior = float(self.profile.posterior(distance))
front.append(add(center, mul(normal, anterior * anterior_fraction)))
back.append(sub(center, mul(normal, posterior * posterior_fraction)))
return front, back
def build(self, ctx):
front, back = self.edge(1, 1)
for index in range(len(front) - 1):
polygon_path(ctx, (front[index], front[index + 1], back[index + 1], back[index]))
for distance, center, _ in (self.samples[0], self.samples[-1]):
radius = (float(self.profile.anterior(distance)) + float(self.profile.posterior(distance))) / 2
ctx.new_sub_path()
ctx.arc(*center, radius, 0, math.tau)
def posterior_band(self, ctx, fraction, low=None, high=None):
_, inner = self.edge(0, 1 - fraction, low, high)
_, outer = self.edge(0, 1.3, low, high)
polygon_path(ctx, outer + inner[::-1])
def anterior_band(self, ctx, inner_fraction, outer_fraction, low, high):
"""Sheen along the front that tapers to nothing at both ends."""
middle = (inner_fraction + outer_fraction) / 2
inner, outer = [], []
for distance, center, normal in self.samples:
if not low <= distance <= high:
continue
taper = math.sin(math.pi * (distance - low) / (high - low))
anterior = float(self.profile.anterior(distance))
inner.append(add(center, mul(normal, anterior * (middle + (inner_fraction - middle) * taper))))
outer.append(add(center, mul(normal, anterior * (middle + (outer_fraction - middle) * taper))))
if len(inner) > 2:
smooth_path(ctx, outer + inner[::-1])
class Hair:
"""Verlet strand whose first two points follow the head."""
def __init__(self, lock):
self.lock = lock
self.segment = lock["segment"]
self.count = len(lock["widths"])
self.points = None
self.previous = None
self.roots = None
def reset(self, roots):
start = roots[1]
self.points = [roots[0], roots[1]] + [
(start[0], start[1] + self.segment * index) for index in range(1, self.count - 1)]
self.points = [(x, min(y, GROUND - 3)) for x, y in self.points]
self.previous = list(self.points)
self.roots = roots
for _ in range(90):
self.substep(roots, 1 / 120)
def step(self, roots, dt, collide):
if self.points is None:
self.reset(roots)
return
substeps = 8
start = self.roots
for index in range(1, substeps + 1):
amount = index / substeps
current = tuple(add(mul(a, 1 - amount), mul(b, amount)) for a, b in zip(start, roots))
self.substep(current, dt / substeps, collide)
self.roots = roots
def substep(self, roots, h, collide=None):
points = self.points
damping = self.lock["damping"]
for index in range(2, self.count):
x, y = points[index]
px, py = self.previous[index]
self.previous[index] = (x, y)
points[index] = (x + (x - px) * damping, y + (y - py) * damping + HAIR_GRAVITY * h * h)
points[0], points[1] = roots
self.previous[0], self.previous[1] = roots
# Bending limits keep the hair from folding back on itself into a loop.
for _ in range(6):
for index in range(1, self.count - 1):
self.hold(index, index + 1, self.segment, self.segment)
for index in range(1, self.count - 2):
self.hold(index, index + 2, self.segment * 1.85, self.segment * 2)
for index in range(1, self.count - 3):
self.hold(index, index + 3, self.segment * 2.5, self.segment * 3)
for index in range(2, self.count):
x, y = points[index]
points[index] = (x, min(y, GROUND - 3))
if collide:
points[index] = collide(points[index])
def hold(self, first, second, shortest, longest):
points = self.points
delta = sub(points[second], points[first])
distance = max(math.hypot(*delta), 1e-9)
target = min(longest, max(shortest, distance))
if abs(target - distance) < 1e-9:
return
correction = mul(delta, (distance - target) / distance)
if first <= 1:
points[second] = sub(points[second], correction)
else:
points[first] = add(points[first], mul(correction, 0.5))
points[second] = sub(points[second], mul(correction, 0.5))
def outline(self):
raw = spline_points(self.points, 5)
count = len(raw)
widths = np.interp(np.linspace(0, self.count - 1, count), range(self.count), self.lock["widths"])
center, normals = [], []
for index, point in enumerate(raw):
tangent = normalize(sub(raw[min(index + 1, count - 1)], raw[max(index - 1, 0)]))
normal = (tangent[1], -tangent[0])
# A soft wave that grows toward the ends.
amount = index / (count - 1)
wave = self.lock["wave"] * amount * math.sin(amount * 7.5 + self.lock["phase"])
center.append(add(point, mul(normal, wave)))
normals.append(normal)
left = [add(point, mul(normal, width)) for point, normal, width in zip(center, normals, widths)]
right = [sub(point, mul(normal, width)) for point, normal, width in zip(center, normals, widths)]
tip = center[-1]
tangent = normalize(sub(center[-1], center[-3]))
normal = normals[-1]
width = widths[-1]
if self.lock["clumps"]:
# Three clumps of different lengths instead of a blunt end.
notch_left = center[int(count * 0.9)]
notch_right = center[int(count * 0.86)]
end = [add(add(tip, mul(tangent, 7)), mul(normal, width * 0.75)),
add(notch_left, mul(normals[int(count * 0.9)], width * 0.2)),
add(add(tip, mul(tangent, 12)), mul(normal, -width * 0.1)),
sub(notch_right, mul(normals[int(count * 0.86)], width * 0.45)),
add(add(tip, mul(tangent, 3)), mul(normal, -width * 0.85))]
else:
end = [add(tip, mul(tangent, 8))]
return center, widths, left, right, end
def extract_head():
if HEAD.exists():
return
BUILD.mkdir(parents=True, exist_ok=True)
frame = BUILD / "head-frame.png"
subprocess.run([
"ffmpeg", "-hide_banner", "-loglevel", "error", "-y", "-i", str(REFERENCE),
"-vf", f"select='eq(n,{HEAD_FRAME})'", "-frames:v", "1", str(frame),
], check=True)
left, top, right, bottom = HEAD_CROP
pixels = np.asarray(Image.open(frame).convert("RGB")).astype(float)[top:bottom, left:right]
background = np.array(HEAD_BACKGROUND, dtype=float)
distance = np.abs(pixels - background).sum(2)
core = distance > 40
labels, count = ndimage.label(core)
areas = ndimage.sum(core, labels, range(1, count + 1))
core = ndimage.binary_fill_holes(labels == np.argmax(areas) + 1)
# The video's pale fringe on thin hair tips shows as a halo over dark
# hair, so the edge band keeps only dark pixels and a soft outer ring.
edge = core & ~ndimage.binary_erosion(core, iterations=3)
pale = (pixels.mean(2) > 150) & (pixels[..., 0] - pixels[..., 2] < 38)
core = core & ~(edge & pale)
near = ndimage.binary_dilation(core, iterations=1)
alpha = np.where(core, 1.0, np.where(near, 0.35, 0.0))
rows = np.arange(bottom - top)[:, None] + top
columns = np.arange(right - left)[None, :] + left
alpha *= np.clip((HEAD_NECK_CUT - rows) / 2 + 0.5, 0, 1)
# Below the chin keep only the neck and brown hair, not the shirt.
below_chin = rows >= HEAD_CHIN
neck = (columns >= HEAD_NECK_COLUMNS[0]) & (columns <= HEAD_NECK_COLUMNS[1])
brown = (pixels.sum(2) < 480) & (pixels[..., 0] - pixels[..., 2] > 25)
alpha = np.where(below_chin & ~neck & ~brown, 0.0, alpha)
safe = np.maximum(alpha, 1e-3)[..., None]
color = np.clip((pixels - (1 - safe) * background) / safe, 0, 255)
color = np.where(alpha[..., None] > 0.02, color, 0)
image = Image.fromarray(np.dstack([color, alpha * 255]).astype(np.uint8), "RGBA")
image.transpose(Image.FLIP_LEFT_RIGHT).save(HEAD)
class Person:
def __init__(self, theme, width, height):
extract_head()
self.head = cairo.ImageSurface.create_from_png(str(HEAD))
neck_center = (HEAD_NECK_COLUMNS[0] + HEAD_NECK_COLUMNS[1]) / 2 - HEAD_CROP[0]
self.head_pivot = (self.head.get_width() - neck_center, HEAD_NECK_CUT - HEAD_CROP[1] - 2)
self.theme = THEMES[theme]
self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
self.ctx = cairo.Context(self.surface)
self.width, self.height = width, height
self.hair = None
self.hair_time = None
def skin_shape(self, origin, shape, turn, lines, shade, far, alpha):
if alpha <= 0.01:
return
ctx = self.ctx
points = [add(origin, rotate(point, turn)) for point in shape]
ctx.push_group()
inked(ctx, lambda: smooth_path(ctx, points), SKIN_SHADE if far else SKIN, SKIN_LINE)
if shade and not far:
shade_points = [add(origin, rotate(point, turn)) for point in shade]
shaded(ctx, lambda: smooth_path(ctx, points), lambda: smooth_path(ctx, shade_points), SKIN_SHADE)
detail_lines(ctx, [[add(origin, rotate(point, turn)) for point in line] for line in lines],
SKIN_LINE, 0.6, 0.8)
ctx.pop_group_to_source()
ctx.paint_with_alpha(alpha)
def foot(self, ankle, angle, instep, far):
tucked = (1 - instep) * smoothstep(0.45, 0.7, angle)
flat = 1 - instep - tucked
self.skin_shape(ankle, FLAT_FOOT, angle, FLAT_FOOT_LINES, FLAT_FOOT_SHADE, far, flat)
self.skin_shape(ankle, TUCKED_FOOT, 0, TUCKED_FOOT_LINES, None, far, tucked)
self.skin_shape(ankle, INSTEP_FOOT, 0, INSTEP_FOOT_LINES, INSTEP_FOOT_SHADE, far, instep)
def hand(self, wrist, angle, palm, far):
flat = smoothstep(0.35, 0.65, palm)
self.skin_shape(wrist, UPRIGHT_HAND, angle, UPRIGHT_HAND_LINES, UPRIGHT_HAND_SHADE, far, 1 - flat)
self.skin_shape(wrist, FLAT_HAND, 0, FLAT_HAND_LINES, None, far, flat)
def leg(self, p, far=False):
ctx = self.ctx
offset = (-5, -1) if far else (0, 0)
hip = add(p.hip, offset)
target = add(p.far_ankle if far else p.near_ankle, offset)
knee, ankle = joint(hip, target, 96, 96, -1)
self.foot(ankle, p.foot, p.instep, far)
limb = Limb(hip, knee, ankle, LEG_PROFILE, 96, 13)
if far:
inked(ctx, lambda: limb.build(ctx), LEGGING_FAR, LEGGING_LINE)
return
pelvis = self.place(p, PELVIS)
def build():
limb.build(ctx)
ctx.new_sub_path()
smooth_path(ctx, pelvis)
inked(ctx, build, LEGGING, LEGGING_LINE)
shaded(ctx, build, lambda: limb.posterior_band(ctx, 0.42, 26, 192), LEGGING_SHADE)
shaded(ctx, build, lambda: limb.anterior_band(ctx, 0.2, 0.62, 14, 78), LEGGING_LIGHT)
shaded(ctx, build, lambda: limb.anterior_band(ctx, 0.25, 0.6, 108, 160), LEGGING_LIGHT)
def arm(self, p, far=False):
ctx = self.ctx
offset = (-5, -1) if far else (0, 0)
shoulder = add(p.shoulder, offset)
target = add(p.far_wrist if far else p.near_wrist, offset)
elbow, wrist = joint(shoulder, target, 64, 64, 1)
hand_angle = math.atan2(wrist[1] - elbow[1], wrist[0] - elbow[0]) + math.pi / 2
hand_angle *= 1 - p.upright
limb = Limb(shoulder, elbow, wrist, ARM_PROFILE, 64, 9, 36)
inked(ctx, lambda: limb.build(ctx), SKIN_SHADE if far else SKIN, SKIN_LINE)
if not far:
shaded(ctx, lambda: limb.build(ctx), lambda: limb.posterior_band(ctx, 0.38), SKIN_SHADE)
self.hand(wrist, hand_angle, p.palm, far)
self.sleeve(shoulder, elbow, far)
def sleeve(self, shoulder, elbow, far):
ctx = self.ctx
axis = normalize(sub(elbow, shoulder))
normal = (axis[1], -axis[0])
profile = Profile(SLEEVE)
front, back = [], []
for distance in np.linspace(0, SLEEVE[-1][0], 8):
center = add(shoulder, mul(axis, distance))
front.append(add(center, mul(normal, float(profile.anterior(distance)))))
back.append(sub(center, mul(normal, float(profile.posterior(distance)))))
cap = (SLEEVE[0][1] + SLEEVE[0][2]) / 2
angle = math.atan2(axis[1], axis[0])
def build():
ctx.move_to(*back[-1])
for point in back[::-1]:
ctx.line_to(*point)
ctx.arc(*shoulder, cap, angle + math.pi / 2, angle + math.pi * 1.5)
for point in front:
ctx.line_to(*point)
ctx.close_path()
inked(ctx, build, SHIRT_SHADE if far else SHIRT, SHIRT_LINE)
if far:
return
shade = [sub(add(shoulder, mul(axis, d)), mul(normal, w)) for d, w in
((-4, 13), (6, 12), (18, 12.8), (27, 13.5), (27, 6), (16, 7.5), (4, 8))]
shaded(ctx, build, lambda: polygon_path(ctx, shade), SHIRT_SHADE)
cuff = [add(add(shoulder, mul(axis, SLEEVE_CUFF + bow)), mul(normal, side))
for side, bow in ((12.1, 0), (0, 1.2), (-12.5, 0))]
fold = [add(add(shoulder, mul(axis, d)), mul(normal, side))
for d, side in ((6, -5), (13, -3.5), (18, -4.8))]
detail_lines(ctx, [cuff], SHIRT_LINE, 0.8)
detail_lines(ctx, [fold], SHIRT_LINE, 0.6, 0.6)
def place(self, p, points):
axis = unit(p.spine)
forward = (-math.sin(p.spine), math.cos(p.spine))
return [add(add(p.hip, mul(axis, point[0])), mul(forward, point[1])) for point in points]
def sag(self, p, points):
"""A loose shirt hangs away from the belly when her front faces the floor."""
facing_down = max(0.0, math.cos(p.spine))
result = []
for height, depth in points:
if depth > 0:
bell = math.exp(-((height - 42) / 30) ** 2)
depth += 4.5 * facing_down * bell
result.append((height, depth))
return result
def torso(self, p):
ctx = self.ctx
body = self.place(p, self.sag(p, TORSO))
def build():
smooth_path(ctx, body)
inked(ctx, build, SHIRT, SHIRT_LINE)
for region, color in ((TORSO_SHADE, SHIRT_SHADE), (BUST_SHADE, SHIRT_SHADE), (BUST_LIGHT, SHIRT_LIGHT)):
points = self.place(p, region)
shaded(ctx, build, lambda: smooth_path(ctx, points), color)
detail_lines(ctx, [self.place(p, fold) for fold in TORSO_FOLDS], SHIRT_LINE, 0.7, 0.55)
def head_transform(self, p):
neck = self.place(p, (NECK,))[0]
# The head follows part of the rig's gaze relative to the spine.
lift = math.atan2(math.sin(p.gaze - p.spine), math.cos(p.gaze - p.spine))
lift = max(math.radians(-42), min(math.radians(10), lift * 0.6))
return neck, p.spine + lift + math.pi / 2
def head_point(self, p, pixel):
neck, turn = self.head_transform(p)
local = mul(sub(pixel, self.head_pivot), HEAD_SCALE)
return add(neck, rotate(local, turn))
def update_hair(self, p, time):
roots = [tuple(self.head_point(p, pixel) for pixel in lock["roots"]) for lock in HAIR_LOCKS]
if self.hair_time is None or time < self.hair_time:
self.hair = [Hair(lock) for lock in HAIR_LOCKS]
for strand, root in zip(self.hair, roots):
strand.reset(root)
self.hair_time = time
return
collide = self.back_collider(p)
while self.hair_time + 1e-9 < time:
dt = min(1 / 30, time - self.hair_time)
for strand, root in zip(self.hair, roots):
strand.step(root, dt, collide)
self.hair_time += dt
def back_collider(self, p):
"""Keep hair on the outside of her back instead of inside the body.
Only while she is upright: when her front faces the floor, hair
falls past the far side of her shoulders and shows over the body.
"""
axis = unit(p.spine)
forward = (-math.sin(p.spine), math.cos(p.spine))
strength = 1 - smoothstep(0.2, 0.5, forward[1])
if strength <= 0:
return None
contour = sorted([point for point in TORSO + PELVIS if point[1] < 0])
heights = [point[0] for point in contour]
depths = [point[1] for point in contour]
def collide(point):
offset = sub(point, p.hip)
height = offset[0] * axis[0] + offset[1] * axis[1]
depth = offset[0] * forward[0] + offset[1] * forward[1]
if not heights[0] <= height <= heights[-1]:
return point
back = float(np.interp(height, heights, depths)) - 1.5
if back < depth < 0:
return add(point, mul(forward, (back - depth) * strength))
return point
return collide
def long_hair(self):
for strand in self.hair:
self.hair_lock(strand)
def hair_lock(self, strand):
ctx = self.ctx
center, widths, left, right, jagged = strand.outline()
def build():
ctx.move_to(*left[0])
for point in left[1:]:
ctx.line_to(*point)
for point in jagged:
ctx.line_to(*point)
for point in right[::-1]:
ctx.line_to(*point)
ctx.close_path()
inked(ctx, build, strand.lock["color"], HAIR_LINE)
if not strand.lock["clumps"]:
return
band = [add(mul(l, 0.35), mul(c, 0.65)) for l, c in zip(left, center)]
shaded(ctx, build, lambda: polygon_path(ctx, right + band[::-1]), HAIR_SHADE)
count = len(center)
def across(start, end, low, high):
"""A strand drifting from one offset to another across the lock."""
first, last = int(low * count), int(high * count)
strand = []
for index in range(first, last):
amount = (index - first) / max(1, last - first - 1)
offset = start + (end - start) * amount
strand.append(add(mul(left[index], 0.5 + offset / 2), mul(right[index], 0.5 - offset / 2)))
return strand
strands = [across(0.5, 0.25, 0.3, 0.9), across(-0.15, 0.1, 0.45, 0.97)]
detail_lines(ctx, [strand for strand in strands if len(strand) > 2], HAIR_LINE, 0.6, 0.45)
shine = [across(offset, offset + 0.05, 0.24 + stagger, 0.34 + stagger)
for offset, stagger in ((-0.4, 0.02), (0.0, 0.0), (0.35, 0.03))]
detail_lines(ctx, [strand for strand in shine if len(strand) > 1], HAIR_LIGHT, 1.3, 0.6)
def head_and_collar(self, p):
ctx = self.ctx
neck, turn = self.head_transform(p)
ctx.save()
ctx.translate(*neck)
ctx.rotate(turn)
ctx.scale(HEAD_SCALE, HEAD_SCALE)
ctx.translate(-self.head_pivot[0], -self.head_pivot[1])
ctx.set_source_surface(self.head)
ctx.get_source().set_filter(cairo.FILTER_GOOD)
ctx.paint()
ctx.restore()
collar = self.place(p, ((121.8, -2.5), (121.3, 3), (119.5, 8.5), (117, 12)))
ctx.new_path()
smooth_path(ctx, collar, closed=False)
ctx.set_line_cap(cairo.LINE_CAP_ROUND)
ctx.set_line_width(4.4)
rgb(ctx, SHIRT_LINE)
ctx.stroke_preserve()
ctx.set_line_width(2.6)
rgb(ctx, SHIRT)
ctx.stroke()
def ground_shadow(self, p):
ctx = self.ctx
xs = [p.near_ankle[0], p.far_ankle[0], p.near_wrist[0], p.far_wrist[0], p.hip[0]]
left, right = min(xs) - 40, max(xs) + 40
center = ((left + right) / 2, GROUND)
radius = (right - left) / 2
ctx.save()
ctx.translate(*center)
ctx.scale(1, 9 / radius)
gradient = cairo.RadialGradient(0, 0, 0, 0, 0, radius)
red, green, blue, alpha = self.theme["shadow"]
gradient.add_color_stop_rgba(0, red / 255, green / 255, blue / 255, alpha)
gradient.add_color_stop_rgba(1, red / 255, green / 255, blue / 255, 0)
ctx.set_source(gradient)
ctx.arc(0, 0, radius, 0, math.tau)
ctx.fill()
ctx.restore()
def frame(self, time, manifest):
ctx = self.ctx
p = rig.sample(time)
self.update_hair(p, time)
ctx.save()
rgb(ctx, self.theme["background"])
ctx.paint()
ctx.scale(self.width / 1536, self.height / 1024)
ctx.translate(manifest["centerX"], manifest["groundY"])
ctx.scale(manifest["characterScale"], manifest["characterScale"])
ctx.translate(-630, -manifest["rigGround"])
self.ground_shadow(p)
self.leg(p, far=True)
self.arm(p, far=True)
self.leg(p)
self.torso(p)
self.long_hair()
self.head_and_collar(p)
self.arm(p)
ctx.restore()
self.surface.flush()
return self.surface.get_data()
def render(manifest, theme, output, width, height, fps):
person = Person(theme, width, height)
target = output / theme / "sun-salutation.mp4"
target.parent.mkdir(parents=True, exist_ok=True)
BUILD.mkdir(parents=True, exist_ok=True)
duration = manifest["output"]["totalSeconds"]
metadata = BUILD / "chapters.txt"
lines = [";FFMETADATA1", "title=Sun salutation — reference person on the fixed rig"]
for index, name in enumerate(rig.NAMES):
lines.extend(("[CHAPTER]", "TIMEBASE=1/1000", f"START={index * 6000}",
f"END={min(index * 6000 + 6000, int(duration * 1000))}", f"title={name}"))
metadata.write_text("\n".join(lines) + "\n")
encoder = subprocess.Popen([
"ffmpeg", "-hide_banner", "-loglevel", "error", "-nostdin", "-y",
"-f", "rawvideo", "-pixel_format", "bgra", "-video_size", f"{width}x{height}",
"-framerate", str(fps), "-i", "pipe:0", "-f", "ffmetadata", "-i", str(metadata),
"-map", "0:v:0", "-map_metadata", "1", "-map_chapters", "1", "-an",
"-c:v", "libx264", "-crf", "18", "-preset", "fast", "-threads", "3",
"-pix_fmt", "yuv420p", "-movflags", "+faststart", str(target),
], stdin=subprocess.PIPE)
try:
for frame in range(round(duration * fps)):
encoder.stdin.write(person.frame(frame / fps, manifest))
finally:
encoder.stdin.close()
if encoder.wait() != 0:
raise RuntimeError(f"Encoding failed: {target}")
print(f"Saved {target}", flush=True)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--theme", choices=("light", "dark", "both"), default="light")
parser.add_argument("--output", type=Path, default=BUILD / "video")
parser.add_argument("--width", type=int, default=1152)
parser.add_argument("--height", type=int, default=768)
parser.add_argument("--fps", type=int, default=30)
parser.add_argument("--still", type=float, action="append")
options = parser.parse_args()
if min(options.width, options.height, options.fps) <= 0 or options.width % 2 or options.height % 2:
parser.error("Dimensions must be positive and even; fps must be positive")
manifest = rig.json.loads(rig.MANIFEST.read_text())
themes = ("light", "dark") if options.theme == "both" else (options.theme,)
for theme in themes:
if options.still:
person = Person(theme, options.width, options.height)
for still in sorted(options.still):
person.frame(still, manifest)
target = BUILD / theme / f"frame-{still:g}.png"
target.parent.mkdir(parents=True, exist_ok=True)
person.surface.write_to_png(str(target))
print(target, flush=True)
else:
render(manifest, theme, options.output, options.width, options.height, options.fps)
if __name__ == "__main__":
main()
+1 -1
View File
@@ -101,7 +101,7 @@
"name": "Inbe"
},
"toolchain": {
"commit": "d4489a3e09de5769256eca4994382f25208e99d8",
"commit": "8bc31e8e041e2b1df9a60de91c515f7d3fa1ace2",
"ref": "master",
"url": "https://github.com/ziranlang/ziran.git"
}