"""从 sky-pool.html 的 CHARACTERS 乳体参数生成 5 个角色的乳房几何 JSON。
参数映射:
  baseR  来自 size: 0.24*(0.86+size*0.7)   (与 sky-pool 一致)
  sag    来自 droop (下垂越重越垂)
  squash 上扁下圆桃形度; 高 droop/重乳 → 更泪滴扁平, 低 droop/幼乳 → 更圆润深 dome
  point  乳尖锐度; 圆钟/半球 → 偏圆, 下垂重 → 略收敛
输出: assets/models/breast_<id>.json
"""
import bpy, json, os, subprocess, math, sys

# 与 sky-pool.html CHARACTERS 一一对应
CHARS = [
  {"id":"gxy","name":"宫宵月","size":1.00,"droop":0.72,"spring":0.92,"damp":0.30,"wave":0.85,"nip":0.9,"colorA":"f7ece6"},
  {"id":"jnx","name":"姜凝香","size":0.96,"droop":0.62,"spring":0.78,"damp":0.22,"wave":0.62,"nip":0.72,"colorA":"eef6fb"},
  {"id":"yji","name":"影姬","size":0.86,"droop":0.50,"spring":0.98,"damp":0.18,"wave":0.92,"nip":0.66,"colorA":"f6efe6"},
  {"id":"wz","name":"王姐","size":0.88,"droop":0.88,"spring":0.60,"damp":0.50,"wave":0.55,"nip":0.60,"colorA":"e8c49a"},
  {"id":"fy","name":"方圆","size":0.72,"droop":0.32,"spring":1.00,"damp":0.14,"wave":0.90,"nip":0.80,"colorA":"f8f1ec"},
]

def param_for(c):
    size=c["size"]; droop=c["droop"]
    baseR=0.24*(0.86+size*0.7)
    # 深度(乳长): 重乳更深, 幼乳较浅但饱满
    depth=baseR*(0.9+0.1*size)   # 乳长接近 baseR, 避免"细长棍"
    # 下垂弧量: 只做轻微下垂弧度(乳尖略微往下), 不拖成长串; droop 0.32~0.88 → sag 0.05~0.28
    sag=0.05+ (droop-0.30)/0.6*0.22 if droop>0.30 else 0.05
    # 桃形上扁下圆: 低 droop → 更圆润; 高 droop → 上更扁(泪滴)
    squash=0.10+droop*0.22
    # 乳尖锐度: 圆润 high, 下垂收敛
    point=1.35-droop*0.35
    return {"baseR":round(baseR,4),"depth":round(depth,4),"sag":round(sag,4),
            "squash":round(squash,4),"point":round(point,4)}

BLENDER="blender"
SCRIPT="/home/gordon/Documents/repo/fiction/my-mother-is-a-god-emperor/scripts/sculpt/build_breast.py"
OUTDIR="/home/gordon/Documents/repo/fiction/my-mother-is-a-god-emperor/assets/models"
os.makedirs(OUTDIR, exist_ok=True)

result={}
for c in CHARS:
    p=param_for(c)
    out=os.path.join(OUTDIR, f"breast_{c['id']}.json")
    cmd=[BLENDER,"--background","--python",SCRIPT,"--",
         "-baseR",str(p["baseR"]),"-depth",str(p["depth"]),
         "-sag",str(p["sag"]),"-squash",str(p["squash"]),"-point",str(p["point"]),
         "-out",out]
    proc=subprocess.run(cmd, capture_output=True, text=True)
    tail=[l for l in proc.stdout.splitlines() if "EXPORTED" in l]
    print(c["id"], c["name"], "params", json.dumps(p), "->", tail[-1] if tail else "FAIL "+proc.stderr[-200:])
    result[c["id"]]={"name":c["name"],**p}
# 汇总
with open(os.path.join(OUTDIR,"all_breast_params.json"),"w") as f:
    json.dump(result, f, ensure_ascii=False, indent=2)
print("SUMMARY written")
