from __future__ import annotations import json import math from pathlib import Path from PIL import Image, ImageDraw, ImageFont ROOT = Path(r"C:\milimoe\osm-game") CHARACTER = ROOT / "assets/images/2d-game-characters/02-xinyin" PARTS_DIR = CHARACTER / "parts/down/v1" MANIFEST_PATH = PARTS_DIR / "parts-manifest.json" MASTER_PATH = CHARACTER / "source/down/xinyin-down-neutral-master-v1.png" ASSEMBLED_DIR = CHARACTER / "assembled/down" ASSEMBLY_PATH = ASSEMBLED_DIR / "xinyin-down-neutral-assembly-v1.png" LAYOUT_PATH = ASSEMBLED_DIR / "assembly-layout-v1.json" PREVIEW_PATH = CHARACTER / "previews/xinyin-down-assembly-validation-v1.png" FONT_PATH = Path(r"C:\Windows\Fonts\msyh.ttc") CANVAS_SIZE = (600, 760) def item(name, x, y, rotation, scale, group, note): return { "name": name, "x": x, "y": y, "rotation": rotation, "scale": scale, "group": group, "note": note, } # Draw order is the list order. X/Y are root-pivot positions on a 600x760 canvas. # DOWN side ownership is frozen: A = screen-left fan side, B = screen-right relaxed side. LAYOUT = [ item("ponytail-root", 325, 104, 0, .33, "hair-back", "三段马尾根;位于后脑之后"), item("ponytail-middle", 353, 170, 48, .33, "hair-back", "向画面右侧延伸并与根部内压重叠"), item("ponytail-tip", 405, 273, 48, .33, "hair-back", "末端继续向画面右侧延伸"), item("hair-cap", 300, 205, 0, .55, "hair-back", "后脑发帽;遮盖马尾根接缝"), item("coat-panel-01", 205, 358, -14, .74, "cloth-back", "画面左外后层"), item("coat-panel-02", 232, 356, -10, .71, "cloth-back", "画面左后层"), item("coat-panel-03", 260, 357, -6, .71, "cloth-back", "中左后层"), item("coat-panel-04", 282, 358, -2, .71, "cloth-back", "中左层"), item("coat-panel-05", 318, 358, 2, .71, "cloth-back", "中右层"), item("coat-panel-06", 340, 357, 6, .71, "cloth-back", "中右后层"), item("coat-panel-07", 368, 356, 10, .71, "cloth-back", "画面右后层"), item("coat-panel-08", 395, 358, 14, .74, "cloth-back", "画面右外后层"), item("thigh-a", 266, 382, -2, .62, "legs", "A 腿髋点;画面左"), item("shin-boot-a", 260, 564, -3, .50, "legs", "A 腿膝点;脚底基线候选"), item("thigh-b", 334, 382, 2, .62, "legs", "B 腿髋点;画面右"), item("shin-boot-b", 340, 563, 3, .50, "legs", "B 腿膝点;脚底基线候选"), item("outer-sleeve-cape-a", 226, 241, -10, .34, "arms-back", "持扇手侧独立外袖披"), item("outer-sleeve-cape-b", 374, 241, 10, .34, "arms-back", "放松手侧独立外袖披"), item("upper-arm-a", 226, 241, -10, .33, "arms-back", "A 上臂;肩到肘"), item("forearm-a", 219, 289, -154, .38, "arms-back", "A 前臂;肘点与上臂末端内压重叠"), item("hand-fan-grip-a", 182, 214, -7, .28, "weapon", "A 持扇手;腕点与前臂末端内压重叠"), item("upper-arm-b", 374, 241, 10, .33, "arms-back", "B 上臂;肩到肘"), item("forearm-b", 381, 289, 6, .38, "arms-back", "B 前臂;肘点与上臂末端内压重叠"), item("hand-relaxed-b", 390, 372, 5, .28, "arms-back", "B 放松手;腕点与前臂末端内压重叠"), item("torso-upper", 300, 360, 0, .60, "body", "躯干根位于腰线"), item("pelvis-hip-core", 300, 359, 0, .60, "body-overlay", "腰封、骨盆与中性腰饰"), item("shoulder-armor-a", 228, 237, -10, .30, "body-overlay", "画面左持扇侧肩甲"), item("shoulder-armor-b", 372, 237, 10, .30, "body-overlay", "画面右放松侧肩甲"), item("neck", 300, 220, 0, .50, "body-overlay", "颈部连接"), item("head-face", 300, 215, 0, .52, "head", "严格正面头部与面部"), item("hair-front", 300, 114, 0, .57, "head-overlay", "前发与双侧鬓发"), item("hair-ornament-long", 348, 112, 3, .46, "head-overlay", "母版画面右侧长银花发饰"), ] EXCLUDED = { "coat-panel-alt-09": "九选八中的动作替换候选", "front-drape": "pelvis-hip-core 已含中性腰前布与系绳", "waist-pouch": "pelvis-hip-core 已含画面右侧腰包", "red-tassel-a": "中性站姿不重复叠加动作替换流苏", "red-tassel-b": "中性站姿不重复叠加动作替换流苏", "hand-throw-open-a": "投掷动作替换手", "weapon-fan-open": "hand-fan-grip-a 已包含中性持握羽扇", "weapon-blade-single": "投掷物,不用于中性持握", "weapon-double-feather": "攻击替换物,不用于中性持握", } def transform_about_pivot(image: Image.Image, pivot, scale: float, angle: float): resized = image.resize( (max(1, round(image.width * scale)), max(1, round(image.height * scale))), Image.Resampling.LANCZOS, ) pivot_x, pivot_y = pivot[0] * scale, pivot[1] * scale side = int(math.ceil(math.hypot(resized.width, resized.height) * 2 + 20)) center = side // 2 local = Image.new("RGBA", (side, side), (0, 0, 0, 0)) local.alpha_composite(resized, (round(center - pivot_x), round(center - pivot_y))) if angle: local = local.rotate(angle, resample=Image.Resampling.BICUBIC, center=(center, center)) return local, center def paste_part(canvas, image, pivot, target, scale, angle): transformed, center = transform_about_pivot(image, pivot, scale, angle) canvas.alpha_composite( transformed, (round(target[0] - center), round(target[1] - center)), ) def render_single(image, pivot, entry): result = Image.new("RGBA", CANVAS_SIZE, (0, 0, 0, 0)) paste_part( result, image, pivot, (entry["x"], entry["y"]), entry["scale"], entry["rotation"], ) return result def checker(size, cell=24): result = Image.new("RGB", size, (238, 242, 247)) draw = ImageDraw.Draw(result) for y in range(0, size[1], cell): for x in range(0, size[0], cell): if (x // cell + y // cell) % 2: draw.rectangle((x, y, x + cell - 1, y + cell - 1), fill=(217, 224, 232)) return result def fit(image, size, margin=0): result = image.copy() result.thumbnail( (size[0] - margin * 2, size[1] - margin * 2), Image.Resampling.LANCZOS, ) return result def main(): manifest = json.loads(MANIFEST_PATH.read_text(encoding="utf-8")) by_name = {record["name"]: record for record in manifest["parts"]} images = { name: Image.open(PARTS_DIR / record["file"]).convert("RGBA") for name, record in by_name.items() } layout_names = {entry["name"] for entry in LAYOUT} all_names = set(by_name) if layout_names | set(EXCLUDED) != all_names: missing = sorted(all_names - layout_names - set(EXCLUDED)) unknown = sorted(layout_names | set(EXCLUDED) - all_names) raise ValueError(f"Part accounting mismatch. missing={missing}, unknown={unknown}") assembly = Image.new("RGBA", CANVAS_SIZE, (0, 0, 0, 0)) records = [] entry_by_name = {entry["name"]: entry for entry in LAYOUT} for entry in LAYOUT: record = by_name[entry["name"]] pivot_record = record["rootPivot"]["outputPixels"] pivot = (pivot_record["x"], pivot_record["y"]) paste_part( assembly, images[entry["name"]], pivot, (entry["x"], entry["y"]), entry["scale"], entry["rotation"], ) records.append({ "part": entry["name"], "file": f"assets/images/2d-game-characters/02-xinyin/parts/down/v1/{record['file']}", "targetPivot": {"x": entry["x"], "y": entry["y"]}, "rotationDegrees": entry["rotation"], "scale": entry["scale"], "group": entry["group"], "note": entry["note"], }) ASSEMBLED_DIR.mkdir(parents=True, exist_ok=True) assembly.save(ASSEMBLY_PATH) alpha = assembly.getchannel("A") visible_bbox = alpha.getbbox() edge_clear = not any(( alpha.crop((0, 0, CANVAS_SIZE[0], 1)).getbbox(), alpha.crop((0, CANVAS_SIZE[1] - 1, CANVAS_SIZE[0], CANVAS_SIZE[1])).getbbox(), alpha.crop((0, 0, 1, CANVAS_SIZE[1])).getbbox(), alpha.crop((CANVAS_SIZE[0] - 1, 0, CANVAS_SIZE[0], CANVAS_SIZE[1])).getbbox(), )) baselines = {} for name in ("shin-boot-a", "shin-boot-b"): entry = entry_by_name[name] record = by_name[name] pivot_record = record["rootPivot"]["outputPixels"] single = render_single( images[name], (pivot_record["x"], pivot_record["y"]), entry, ) bbox = single.getchannel("A").getbbox() baselines[name] = bbox[3] - 1 if bbox else None layout_manifest = { "characterId": "02-xinyin", "direction": "down", "version": 1, "status": "art-assembly-validated", "canvas": { "width": CANVAS_SIZE[0], "height": CANVAS_SIZE[1], "origin": "top-left", "units": "pixels", "footOrigin": {"x": CANVAS_SIZE[0] // 2, "y": max(baselines.values())}, }, "sideConvention": manifest["sideConvention"], "sourceManifests": [ "assets/images/2d-game-characters/02-xinyin/parts/down/v1/parts-manifest.json" ], "items": records, "excludedParts": EXCLUDED, "validation": { "usedPartCount": len(records), "excludedPartCount": len(EXCLUDED), "allPartsAccountedFor": len(records) + len(EXCLUDED) == manifest["partCount"], "visibleAlphaBounds": { "x": visible_bbox[0], "y": visible_bbox[1], "width": visible_bbox[2] - visible_bbox[0], "height": visible_bbox[3] - visible_bbox[1], }, "edgeTransparent": edge_clear, "leftBootBaselineY": baselines["shin-boot-a"], "rightBootBaselineY": baselines["shin-boot-b"], "baselineDeltaPixels": abs( baselines["shin-boot-a"] - baselines["shin-boot-b"] ), }, "warnings": [ "This is an art-side assembly candidate, not an engine rig.", "A/B ownership is frozen for DOWN and must not be mirrored.", "The pelvis core already contains neutral waist cloth, cords, tassels, and the side pouch; standalone waist pieces remain action replacements.", "The three-part ponytail chain is validated for the neutral pose; rotation extremes still require motion validation.", ], } LAYOUT_PATH.write_text( json.dumps(layout_manifest, ensure_ascii=False, indent=2), encoding="utf-8", ) board = Image.new("RGB", (1920, 980), (17, 21, 30)) draw = ImageDraw.Draw(board) title = ImageFont.truetype(str(FONT_PATH), 38) panel_font = ImageFont.truetype(str(FONT_PATH), 25) text_font = ImageFont.truetype(str(FONT_PATH), 18) muted = (151, 169, 198) draw.text((42, 24), "心音|DOWN 正面中性站姿装配验证 v1", font=title, fill=(240, 244, 252)) draw.text((44, 78), "冻结母版 / 41 件中的 32 件中性选择 / 正面枢轴、层级与双脚基线", font=text_font, fill=muted) panel_rects = [(42, 122, 622, 930), (654, 122, 1234, 930), (1266, 122, 1878, 930)] for rect in panel_rects: draw.rounded_rectangle(rect, radius=16, fill=(24, 30, 42), outline=(65, 82, 112), width=2) draw.text((62, 142), "A 冻结的 DOWN 母版", font=panel_font, fill=(240, 244, 252)) draw.text((674, 142), "B 32 件部件重建", font=panel_font, fill=(240, 244, 252)) draw.text((1286, 142), "C 枢轴、侧别与基线", font=panel_font, fill=(240, 244, 252)) master = fit(Image.open(MASTER_PATH).convert("RGBA"), (530, 720), 18) master_bg = checker((530, 720)) master_bg.paste(master, ((530 - master.width) // 2, (720 - master.height) // 2), master) board.paste(master_bg, (67, 190)) assembly_bg = checker(CANVAS_SIZE) assembly_bg.paste(assembly, (0, 0), assembly) display = fit(assembly_bg, (540, 740)) board.paste(display, (674 + (540 - display.width) // 2, 180)) board.paste(display, (1302 + (540 - display.width) // 2, 180)) # Skeleton positions in the third panel follow the scaled 600x760 assembly display. ox, oy = 1302, 180 sx = display.width / CANVAS_SIZE[0] sy = display.height / CANVAS_SIZE[1] def pt(x, y): return (round(ox + x * sx), round(oy + y * sy)) overlay = ImageDraw.Draw(board) chains = [ [pt(300, 90), pt(300, 215), pt(300, 360)], [pt(228, 237), pt(219, 289), pt(182, 214)], [pt(372, 237), pt(381, 289), pt(390, 372)], [pt(300, 359), pt(266, 382), pt(260, 563), pt(260, baselines["shin-boot-a"])], [pt(300, 359), pt(334, 382), pt(340, 563), pt(340, baselines["shin-boot-b"])], ] for chain in chains: for start, end in zip(chain, chain[1:]): overlay.line((*start, *end), fill=(69, 211, 255), width=4) for x, y in chain: overlay.ellipse((x - 6, y - 6, x + 6, y + 6), outline=(255, 87, 115), width=3) baseline_y = pt(300, max(baselines.values()))[1] overlay.line((1318, baseline_y, 1826, baseline_y), fill=(255, 204, 75), width=3) draw.text((1286, 853), f"A/B 固定:画面左持扇,画面右放松|脚底差 {abs(baselines['shin-boot-a'] - baselines['shin-boot-b'])} px", font=text_font, fill=(240, 244, 252)) draw.text((1286, 883), "腰部替换件不重复叠加;三段马尾与八片衣摆进入动作极限测试。", font=text_font, fill=muted) PREVIEW_PATH.parent.mkdir(parents=True, exist_ok=True) board.save(PREVIEW_PATH, quality=95) print(ASSEMBLY_PATH) print(LAYOUT_PATH) print(PREVIEW_PATH) print(json.dumps(layout_manifest["validation"], ensure_ascii=False)) if __name__ == "__main__": main()