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/front-left/v1" MANIFEST_PATH = PARTS_DIR / "parts-manifest.json" CORRECTION_DIR = CHARACTER / "parts/front-left/rig-corrections-v1" CORRECTION_MANIFEST_PATH = CORRECTION_DIR / "rig-corrections-manifest.json" MASTER_PATH = CHARACTER / "source/front-left/xinyin-front-left-neutral-master-v2.png" ASSEMBLED_DIR = CHARACTER / "assembled/front-left" ASSEMBLY_PATH = ASSEMBLED_DIR / "xinyin-front-left-neutral-assembly-v4.png" LAYOUT_PATH = ASSEMBLED_DIR / "assembly-layout-v4.json" PREVIEW_PATH = CHARACTER / "previews/xinyin-front-left-assembly-validation-v4.png" FONT_PATH = Path(r"C:\Windows\Fonts\msyh.ttc") CANVAS_SIZE = (500, 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. Target X/Y are root-pivot positions on a 500x760 canvas. LAYOUT = [ item("ponytail-root", 225, 105, 0, .33, "hair-back", "补件根部;含紧凑花冠绑点"), item("ponytail-middle", 197, 170, -48, .33, "hair-back", "补件中段;向根部内压形成遮盖"), item("ponytail-tip", 145, 273, -48, .33, "hair-back", "补件末端;向中段内压形成遮盖"), item("hair-cap", 250, 205, 0, .55, "hair-back", "后脑发帽"), item("coat-panel-01", 150, 358, -12, .74, "cloth-back", "左外后层"), item("coat-panel-02", 176, 356, -8, .70, "cloth-back", "左后层"), item("coat-panel-03", 202, 357, -4, .70, "cloth-back", "中左后层"), item("coat-panel-04", 226, 358, -1, .70, "cloth-back", "中左层"), item("coat-panel-05", 250, 358, 1, .70, "cloth-back", "中右层"), item("coat-panel-06", 274, 357, 4, .70, "cloth-back", "中右后层"), item("coat-panel-07", 300, 356, 8, .70, "cloth-back", "右后层"), item("coat-panel-08", 328, 358, 12, .74, "cloth-back", "右外后层"), item("thigh-a", 216, 382, -2, .62, "legs", "A 腿髋点"), item("shin-boot-a", 210, 563, -4, .50, "legs", "A 腿膝点;脚底基线候选"), item("thigh-b", 282, 382, 3, .62, "legs", "B 腿髋点"), item("shin-boot-b", 290, 563, 4, .50, "legs", "B 腿膝点;脚底基线候选"), item("outer-sleeve-cape-a", 186, 241, -10, .34, "arms-back", "独立外袖披 A"), item("outer-sleeve-cape-b", 314, 241, 14, .31, "arms-back", "独立外袖披 B"), item("upper-arm-a", 186, 241, -10, .30, "arms-back", "补件上臂 A;不含肩甲与袖披"), item("forearm-a", 168, 341, -6, .40, "arms-back", "放松手侧肘到腕"), item("hand-relaxed-a", 160, 421, -5, .28, "arms-back", "放松手"), item("upper-arm-b", 314, 241, 14, .33, "arms-back", "补件上臂 B;不含肩甲与袖披"), item("forearm-b", 337, 332, 154, .38, "arms-back", "持械手侧抬起前臂"), item("torso-upper", 250, 360, 0, .60, "body", "躯干根位于腰线"), item("pelvis-hip-core", 250, 359, 0, .60, "body-overlay", "腰封、骨盆与中性腰饰"), item("shoulder-armor-a", 188, 237, -10, .30, "body-overlay", "固定于躯干的独立肩甲 A"), item("shoulder-armor-b", 312, 237, 14, .30, "body-overlay", "固定于躯干的独立肩甲 B"), item("neck", 250, 220, 0, .50, "body-overlay", "颈部连接"), item("head-face", 250, 215, 0, .52, "head", "头部与面部"), item("hair-front", 250, 114, 0, .57, "head-overlay", "前发与既有双侧发束"), item("hair-ornament-long", 207, 112, -3, .46, "head-overlay", "画面左侧长银饰"), item("hand-fan-grip-b", 370, 262, -6, .28, "weapon", "持扇手;与前臂腕点对齐"), ] EXCLUDED = { "side-lock-a": "hair-front 已包含双侧发束", "side-lock-b": "hair-front 已包含双侧发束", "coat-panel-alt-09": "九选八中的动作替换候选", "front-drape": "pelvis-hip-core 已含中性腰前布与系绳", "red-tassel-a": "中性站姿不使用动作替换流苏", "red-tassel-b": "中性站姿不使用动作替换流苏", "waist-pouch": "pelvis-hip-core 已含腰侧附件;装配后再决定是否替换", "hand-throw-open-b": "投掷动作替换手", "weapon-fan-open": "hand-fan-grip-b 已包含羽扇", "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 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()} correction_manifest = json.loads(CORRECTION_MANIFEST_PATH.read_text(encoding="utf-8")) for record in correction_manifest["parts"]: by_name[record["name"]] = record images[record["name"]] = Image.open(CORRECTION_DIR / record["file"]).convert("RGBA") assembly = Image.new("RGBA", CANVAS_SIZE, (0, 0, 0, 0)) records = [] for entry in LAYOUT: record = by_name[entry["name"]] pivot = record["rootPivot"]["outputPixels"] paste_part(assembly, images[entry["name"]], (pivot["x"], pivot["y"]), (entry["x"], entry["y"]), entry["scale"], entry["rotation"]) records.append({ "part": entry["name"], "file": ( f"assets/images/2d-game-characters/02-xinyin/parts/front-left/rig-corrections-v1/{record['file']}" if entry["name"] in {"upper-arm-a", "upper-arm-b", "ponytail-root", "ponytail-middle", "ponytail-tip"} else f"assets/images/2d-game-characters/02-xinyin/parts/front-left/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() left_bbox = alpha.crop((0, 0, CANVAS_SIZE[0] // 2, CANVAS_SIZE[1])).getbbox() right_bbox = alpha.crop((CANVAS_SIZE[0] // 2, 0, CANVAS_SIZE[0], CANVAS_SIZE[1])).getbbox() left_baseline = left_bbox[3] - 1 if left_bbox else None right_baseline = right_bbox[3] - 1 if right_bbox else None layout_manifest = { "characterId": "02-xinyin", "direction": "front-left", "version": 4, "status": "art-assembly-validated", "canvas": {"width": CANVAS_SIZE[0], "height": CANVAS_SIZE[1], "origin": "top-left", "units": "pixels"}, "sourceManifests": [ "assets/images/2d-game-characters/02-xinyin/parts/front-left/v1/parts-manifest.json", "assets/images/2d-game-characters/02-xinyin/parts/front-left/rig-corrections-v1/rig-corrections-manifest.json" ], "items": records, "excludedParts": EXCLUDED, "validation": { "visibleAlphaBounds": {"x": visible_bbox[0], "y": visible_bbox[1], "width": visible_bbox[2] - visible_bbox[0], "height": visible_bbox[3] - visible_bbox[1]}, "edgeTransparent": True, "leftBaselineY": left_baseline, "rightBaselineY": right_baseline, "baselineDeltaPixels": abs(left_baseline - right_baseline), }, "warnings": [ "This is an art-side assembly candidate, not an engine rig.", "Rig-correction upper arms replace the composite base upper arms; standalone shoulder armor and sleeve capes are restored.", "The three correction ponytail pieces form the current root-middle-tip chain; seam and rotation extremes still require motion validation.", "Pelvis core already contains neutral waist cloth, cords, tassels, and side accessories; standalone waist pieces remain replacements.", ], } LAYOUT_PATH.write_text(json.dumps(layout_manifest, ensure_ascii=False, indent=2), encoding="utf-8") board = Image.new("RGB", (1800, 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), "心音|前左中性站姿装配验证 v4", font=title, fill=(240, 244, 252)) draw.text((44, 78), "高分辨率母版 / 43 件部件中的中性选择 / 根枢轴与层级检查", font=text_font, fill=muted) panel_rects = [(42, 122, 592, 930), (624, 122, 1174, 930), (1206, 122, 1756, 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 冻结的方向母版", font=panel_font, fill=(240, 244, 252)) draw.text((644, 142), "B 部件重建候选", font=panel_font, fill=(240, 244, 252)) draw.text((1226, 142), "C 枢轴与层级", font=panel_font, fill=(240, 244, 252)) master = fit(Image.open(MASTER_PATH).convert("RGBA"), (500, 720), 18) master_bg = checker((500, 720)) master_bg.paste(master, ((500 - 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) board.paste(assembly_bg, (649, 170)) board.paste(assembly_bg, (1231, 170)) overlay = ImageDraw.Draw(board) chains = [ [(1481, 275), (1481, 390), (1481, 530)], [(1417, 411), (1402, 500), (1391, 584)], [(1545, 411), (1567, 500), (1601, 432)], [(1481, 529), (1447, 552), (1441, 733)], [(1481, 529), (1513, 552), (1521, 733)], ] 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) draw.text((1238, 855), "v4 补件验证:", font=text_font, fill=(240, 244, 252)) draw.text((1238, 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) if __name__ == "__main__": main()