Spaces:
Sleeping
Sleeping
Update UnityProject/app.py
Browse files- UnityProject/app.py +122 -110
UnityProject/app.py
CHANGED
|
@@ -1,110 +1,122 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import shutil
|
| 3 |
-
import subprocess
|
| 4 |
-
import json
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
from
|
| 8 |
-
from
|
| 9 |
-
from
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
#
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import subprocess
|
| 4 |
+
import json
|
| 5 |
+
import time
|
| 6 |
+
import requests
|
| 7 |
+
from fastapi import FastAPI, BackgroundTasks, HTTPException, Request
|
| 8 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
+
from pydantic import BaseModel
|
| 10 |
+
from playwright.sync_api import sync_playwright
|
| 11 |
+
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
app.add_middleware(
|
| 15 |
+
CORSMiddleware,
|
| 16 |
+
allow_origins=["*"],
|
| 17 |
+
allow_credentials=True,
|
| 18 |
+
allow_methods=["*"],
|
| 19 |
+
allow_headers=["*"],
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# --- KONFIGURASI ---
|
| 23 |
+
UNITY_PATH = "/opt/unity/Editor/Unity"
|
| 24 |
+
PROJECT_PATH = "/app/UnityProject"
|
| 25 |
+
TEMP_DIR = "/tmp/zepeto_work"
|
| 26 |
+
os.makedirs(TEMP_DIR, exist_ok=True)
|
| 27 |
+
|
| 28 |
+
# --- MODEL DATA (Sesuai dengan kiriman Cloudflare + Cloudinary) ---
|
| 29 |
+
class ConversionRequest(BaseModel):
|
| 30 |
+
file_url: str # URL dari Cloudinary
|
| 31 |
+
filename: str # Nama file (contoh: baju_keren.fbx)
|
| 32 |
+
zepeto_id: str
|
| 33 |
+
zepeto_password: str
|
| 34 |
+
|
| 35 |
+
# --- FUNGSI: LOGIN & UPLOAD KE ZEPETO STUDIO ---
|
| 36 |
+
def upload_to_zepeto(file_path, z_id, z_pw):
|
| 37 |
+
print(f"π [Zepeto] Mencoba login untuk: {z_id}")
|
| 38 |
+
try:
|
| 39 |
+
with sync_playwright() as p:
|
| 40 |
+
browser = p.chromium.launch(headless=True, args=['--no-sandbox'])
|
| 41 |
+
context = browser.new_context(user_agent="Mozilla/5.0...")
|
| 42 |
+
page = context.new_page()
|
| 43 |
+
|
| 44 |
+
# Login Process
|
| 45 |
+
page.goto("https://account.zepeto.me/signin?sm=1")
|
| 46 |
+
page.fill('input[name="userId"]', z_id)
|
| 47 |
+
page.fill('input[type="password"]', z_pw)
|
| 48 |
+
page.click('button[type="submit"]')
|
| 49 |
+
|
| 50 |
+
# Tunggu Token/Cookie
|
| 51 |
+
page.wait_for_url(lambda u: "signin" not in u, timeout=15000)
|
| 52 |
+
cookies = context.cookies()
|
| 53 |
+
jwt_token = next((c['value'] for c in cookies if c['name'] == 'token'), None)
|
| 54 |
+
browser.close()
|
| 55 |
+
|
| 56 |
+
if not jwt_token:
|
| 57 |
+
return False, "Gagal mendapatkan JWT Token"
|
| 58 |
+
|
| 59 |
+
# API Upload Zepeto Studio
|
| 60 |
+
print("π [Zepeto] Mengirim file ke Studio API...")
|
| 61 |
+
upload_url = "https://cf-api-studio.zepeto.me/api/items"
|
| 62 |
+
headers = {"Authorization": f"Bearer {jwt_token}"}
|
| 63 |
+
|
| 64 |
+
with open(file_path, "rb") as f:
|
| 65 |
+
files = {"file": (os.path.basename(file_path), f, "application/octet-stream")}
|
| 66 |
+
res = requests.post(upload_url, headers=headers, files=files)
|
| 67 |
+
|
| 68 |
+
if res.status_code in [200, 201]:
|
| 69 |
+
return True, "Sukses Upload"
|
| 70 |
+
else:
|
| 71 |
+
return False, f"API Error: {res.text}"
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return False, str(e)
|
| 74 |
+
|
| 75 |
+
# --- FUNGSI UTAMA: DOWNLOAD -> UNITY -> UPLOAD ---
|
| 76 |
+
def process_pipeline(req: ConversionRequest):
|
| 77 |
+
local_fbx = os.path.join(PROJECT_PATH, "Assets", req.filename)
|
| 78 |
+
output_filename = req.filename.replace(".fbx", ".zepeto")
|
| 79 |
+
local_zepeto = os.path.join(PROJECT_PATH, "output", output_filename)
|
| 80 |
+
|
| 81 |
+
try:
|
| 82 |
+
# 1. DOWNLOAD DARI CLOUDINARY
|
| 83 |
+
print(f"π₯ [Cloudinary] Downloading: {req.filename}")
|
| 84 |
+
r = requests.get(req.file_url, stream=True)
|
| 85 |
+
with open(local_fbx, 'wb') as f:
|
| 86 |
+
for chunk in r.iter_content(chunk_size=8192):
|
| 87 |
+
f.write(chunk)
|
| 88 |
+
|
| 89 |
+
# 2. JALANKAN UNITY (Konversi & Optimasi Poligon)
|
| 90 |
+
print("βοΈ [Unity] Memulai konversi & optimasi...")
|
| 91 |
+
# Perhatikan: Method C# harus menangani decimation/polygon reduction
|
| 92 |
+
cmd = [
|
| 93 |
+
UNITY_PATH, "-batchmode", "-nographics",
|
| 94 |
+
"-projectPath", PROJECT_PATH,
|
| 95 |
+
"-executeMethod", "TestBuilder.ManualConvert",
|
| 96 |
+
"-inputFile", req.filename,
|
| 97 |
+
"-quit", "-logFile", "-"
|
| 98 |
+
]
|
| 99 |
+
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 100 |
+
|
| 101 |
+
if not os.path.exists(local_zepeto):
|
| 102 |
+
print(f"β [Unity] Gagal. Log: {result.stdout}")
|
| 103 |
+
return
|
| 104 |
+
|
| 105 |
+
# 3. UPLOAD KE ZEPETO STUDIO
|
| 106 |
+
success, msg = upload_to_zepeto(local_zepeto, req.z_id, req.z_pw)
|
| 107 |
+
|
| 108 |
+
if success:
|
| 109 |
+
print(f"β
[FINAL] {req.filename} Berhasil dikirim ke Zepeto!")
|
| 110 |
+
else:
|
| 111 |
+
print(f"β [FINAL] Gagal upload ke Zepeto Studio: {msg}")
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
print(f"β [CRITICAL] Pipeline Error: {e}")
|
| 115 |
+
finally:
|
| 116 |
+
# Cleanup
|
| 117 |
+
if os.path.exists(local_fbx): os.remove(local_fbx)
|
| 118 |
+
|
| 119 |
+
@app.post("/convert")
|
| 120 |
+
async def api_convert(req: ConversionRequest, background_tasks: BackgroundTasks):
|
| 121 |
+
background_tasks.add_task(process_pipeline, req)
|
| 122 |
+
return {"status": "success", "message": "Proses konversi dan upload Zepeto dimulai di background"}
|