diff --git a/VFXReviewConnector.jsx b/VFXReviewConnector.jsx index 2f83860..a5be521 100644 --- a/VFXReviewConnector.jsx +++ b/VFXReviewConnector.jsx @@ -2527,6 +2527,13 @@ var SETTINGS_KEY = "cfg"; status === "VALIDATING" || status === "GENERATING_PREVIEW"; } + // Mirrors REGENERATABLE_STATUSES on the server: every state where the EXRs + // are known good, so only the preview needs rebuilding. + function isRegeneratableExportStatus(status) { + return status === "READY_FOR_QC" || status === "QC_FAILED" || + status === "PREVIEW_FAILED"; + } + function formatEtaShort(seconds) { var mins; var secs; @@ -2858,6 +2865,57 @@ var SETTINGS_KEY = "cfg"; } } + // Rebuild the delivered MOV/MP4 after a slate/burn-in mistake: same version, + // same EXRs, freshly built slate. Slate text (description, scope, note) + // comes from the shot record, so it is fixed on the web app first — except + // the panel's Sub. Note field, which still wins as a per-export override. + function regenerateLatestPreview() { + var shotCode = getStatusShotCode(); + var data; + var exportInfo; + var body = {}; + var note; + var response; + + if (!shotCode) { + setPipelineStatus("Select a comp or shot first"); + return; + } + data = getLatestExportData(shotCode); + exportInfo = data ? data.export : null; + if (!exportInfo) { + setPipelineStatus(shotCode + " — no export to regenerate"); + return; + } + if (!isRegeneratableExportStatus(String(exportInfo.status))) { + setPipelineStatus(formatExportStatus(shotCode, exportInfo) + + " — preview can only be regenerated once the render is done"); + return; + } + if (!confirm("Rebuild the preview for " + shotCode + " " + exportInfo.versionString + "?\n\n" + + "The rendered EXRs are reused and the version number stays the same.\n" + + "The existing review version's media is replaced.")) { + return; + } + + if (uiState.submissionNoteField) { + note = trimText(uiState.submissionNoteField.text); + if (note) { + body.submissionNote = note; + } + } + + setPipelineStatus(shotCode + " " + exportInfo.versionString + " — regenerating preview..."); + response = postJSON(CONFIG.baseUrl + "/api/ext/exports/" + exportInfo.id + "/regenerate-preview", body); + if (response.status === 200) { + setPipelineStatus(shotCode + " " + exportInfo.versionString + " — preview queued"); + startExportPolling(shotCode); + } else { + setPipelineStatus("Regenerate failed: " + + ((response.data && response.data.error) ? response.data.error : "HTTP " + response.status)); + } + } + function buildUI(thisObjRef) { var win = (thisObjRef instanceof Panel) ? thisObjRef : new Window("palette", "VFXReview Connector", undefined, { resizeable: true }); var bannerFile; @@ -2869,9 +2927,11 @@ var SETTINGS_KEY = "cfg"; var pipelinePanel; var pipelineRow1; var pipelineRow2; + var pipelineRow3; var queueExportButton; var refreshExportButton; var retryExportButton; + var regeneratePreviewButton; var buttonsPanel; var btnRow1; var btnRow2; @@ -3006,6 +3066,16 @@ var SETTINGS_KEY = "cfg"; refreshExportButton = pipelineRow2.add("button", undefined, "Refresh Status"); retryExportButton = pipelineRow2.add("button", undefined, "Retry Export"); + pipelineRow3 = pipelinePanel.add("group"); + pipelineRow3.orientation = "row"; + pipelineRow3.alignChildren = ["fill", "center"]; + pipelineRow3.spacing = 6; + regeneratePreviewButton = pipelineRow3.add("button", undefined, "Regenerate Previews"); + regeneratePreviewButton.helpTip = + "Rebuild the MOV/MP4 slate and burn-ins for the delivered version.\n" + + "Reuses the rendered EXRs and keeps the version number. Slate text is\n" + + "re-read from the shot, so fix it on the web app's Slate tab first."; + buttonsPanel = scrollContent.add("panel", undefined, "Actions"); buttonsPanel.orientation = "column"; buttonsPanel.alignChildren = ["fill", "top"]; @@ -3122,6 +3192,7 @@ var SETTINGS_KEY = "cfg"; queueExportButton.onClick = queueExport; refreshExportButton.onClick = refreshExportStatus; retryExportButton.onClick = retryLatestExport; + regeneratePreviewButton.onClick = regenerateLatestPreview; refreshButton.onClick = refreshSelection; fixNamesButton.onClick = fixCompNames; overlayButton.onClick = addOverlay; diff --git a/app/api/ext/exports/[exportId]/regenerate-preview/route.ts b/app/api/ext/exports/[exportId]/regenerate-preview/route.ts new file mode 100644 index 0000000..94f8d78 --- /dev/null +++ b/app/api/ext/exports/[exportId]/regenerate-preview/route.ts @@ -0,0 +1,49 @@ +import { NextRequest, NextResponse } from "next/server"; +import { isExtAuthorized } from "@/lib/render-pipeline/ext-auth"; +import { toErrorResponse } from "@/lib/render-pipeline/errors"; +import { regeneratePreview } from "@/lib/render-pipeline/preview"; + +// ── POST /api/ext/exports/{exportId}/regenerate-preview (E15b) ─────────────── +// +// Rebuild the preview MOV/MP4 for an export that already delivered one, after a +// slate or burn-in mistake was caught. Reuses the validated EXRs and keeps the +// version number; slate text edited on the web app's Slate tab is picked up. +// +// Body (all optional): +// vfxScope per-export override; omit to re-read the shot's slate field +// submissionNote same +// note audit note for the ExportEvent + +export async function POST( + req: NextRequest, + { params }: { params: Promise<{ exportId: string }> } +) { + if (!isExtAuthorized(req)) { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } + const { exportId } = await params; + + let body: { + vfxScope?: string | null; + submissionNote?: string | null; + note?: string; + } = {}; + try { + const parsed = await req.json(); + if (parsed && typeof parsed === "object") body = parsed; + } catch { + // empty body is fine — everything falls back to the shot's slate fields + } + + try { + const result = await regeneratePreview(exportId, { + ...(body.vfxScope !== undefined ? { vfxScope: body.vfxScope } : {}), + ...(body.submissionNote !== undefined ? { submissionNote: body.submissionNote } : {}), + note: typeof body.note === "string" ? body.note : "Regenerate preview from AE panel", + }); + return NextResponse.json(result); + } catch (err) { + const { body: errBody, status } = toErrorResponse(err); + return NextResponse.json(errBody, { status }); + } +} diff --git a/lib/render-pipeline/preview.ts b/lib/render-pipeline/preview.ts index bdbddf4..0069354 100644 --- a/lib/render-pipeline/preview.ts +++ b/lib/render-pipeline/preview.ts @@ -1,4 +1,4 @@ -import { Prisma } from "@prisma/client"; +import { ExportStatus, Prisma } from "@prisma/client"; import { db } from "@/lib/db"; import { PipelineError } from "./errors"; import { applyTransition } from "./transitions"; @@ -68,7 +68,14 @@ type ExportForPreview = { export async function buildPreviewManifest(exportRow: ExportForPreview): Promise { const shot = await db.shot.findUnique({ where: { id: exportRow.shotId }, - select: { shotCode: true, episode: true, scene: true, description: true, notes: true }, + select: { + shotCode: true, + episode: true, + scene: true, + description: true, + notes: true, + slateDescription: true, + }, }); if (!shot) throw new PipelineError(404, "Shot not found for export"); @@ -98,7 +105,10 @@ export async function buildPreviewManifest(exportRow: ExportForPreview): Promise slate: { versionName: base, date: slateDate(), - description: shot.description, + // The slate's own description is authored on the Shot's Slate tab and + // falls back to the general shot description — the same precedence the + // tab shows as its placeholder. + description: shot.slateDescription ?? shot.description, notes: shot.notes, shotCode: shot.shotCode, episode: shot.episode, @@ -197,45 +207,67 @@ export async function finalizePreview(jobId: string, machineId: string, body: Fi }); let versionId: string | null = null; + let versionReplaced = false; if (artifacts.previewMovKey) { - // Version numbering stays monotonic for the task (existing review flows - // rely on the newest version having the highest number) while matching - // the export version whenever it is ahead. - const taskMax = exportRow.taskId - ? await tx.version.aggregate({ - where: { taskId: exportRow.taskId }, - _max: { versionNumber: true }, - }) - : { _max: { versionNumber: null as number | null } }; - const versionNumber = Math.max((taskMax._max.versionNumber ?? 0) + 1, exportRow.versionNumber); + const mediaFields = { + fileUrl: `/api/files/${artifacts.previewMovKey}`, + fileName: media.fileName ?? `${exportRow.versionString}.mp4`, + mimeType: "video/mp4", + thumbnailUrl: artifacts.thumbnailKey ? `/api/files/${artifacts.thumbnailKey}` : undefined, + fps: media.fps ?? exportRow.fps, + frameCount: media.frameCount ?? exportRow.frameEnd - exportRow.frameStart + 1, + width: media.width ?? exportRow.width, + height: media.height ?? exportRow.height, + }; + + // Regenerating a preview (T11b) re-delivers the *same* export version, so + // it replaces the media on the Version already registered rather than + // stacking a second row: the version number stays put and any notes or + // annotations reviewers left on it survive the reslate. + const existing = exportRow.versionId + ? await tx.version.findUnique({ where: { id: exportRow.versionId }, select: { id: true } }) + : null; + + if (existing) { + await tx.version.update({ + where: { id: existing.id }, + data: { ...mediaFields, isLatest: true }, + }); + versionId = existing.id; + versionReplaced = true; + } else { + // Version numbering stays monotonic for the task (existing review flows + // rely on the newest version having the highest number) while matching + // the export version whenever it is ahead. + const taskMax = exportRow.taskId + ? await tx.version.aggregate({ + where: { taskId: exportRow.taskId }, + _max: { versionNumber: true }, + }) + : { _max: { versionNumber: null as number | null } }; + const versionNumber = Math.max((taskMax._max.versionNumber ?? 0) + 1, exportRow.versionNumber); + + const version = await tx.version.create({ + data: { + ...mediaFields, + versionNumber, + shotId: exportRow.shotId, + taskId: exportRow.taskId, + artistId: exportRow.submittedById, + notes: `Pipeline render ${exportRow.versionString} (internal only)`, + isLatest: true, + isClientVisible: false, // never shared to the client portal (§10.0) + }, + }); + versionId = version.id; + } if (exportRow.taskId) { await tx.version.updateMany({ - where: { taskId: exportRow.taskId }, + where: { taskId: exportRow.taskId, id: { not: versionId } }, data: { isLatest: false }, }); } - - const version = await tx.version.create({ - data: { - versionNumber, - shotId: exportRow.shotId, - taskId: exportRow.taskId, - artistId: exportRow.submittedById, - fileUrl: `/api/files/${artifacts.previewMovKey}`, - fileName: media.fileName ?? `${exportRow.versionString}.mp4`, - mimeType: "video/mp4", - thumbnailUrl: artifacts.thumbnailKey ? `/api/files/${artifacts.thumbnailKey}` : undefined, - fps: media.fps ?? exportRow.fps, - frameCount: media.frameCount ?? exportRow.frameEnd - exportRow.frameStart + 1, - width: media.width ?? exportRow.width, - height: media.height ?? exportRow.height, - notes: `Pipeline render ${exportRow.versionString} (internal only)`, - isLatest: true, - isClientVisible: false, // never shared to the client portal (§10.0) - }, - }); - versionId = version.id; } // The task title carries the version it is showing (house convention: @@ -270,7 +302,11 @@ export async function finalizePreview(jobId: string, machineId: string, body: Fi id: machineId, note: `Preview complete in ${body.renderStats?.previewSeconds ?? "?"}s` + - (versionId ? " — review version created" : " — no preview media reported") + + (versionId + ? versionReplaced + ? " — review version media replaced" + : " — review version created" + : " — no preview media reported") + (taskRenamed ? ` — task renamed to ${taskTitle}` : ""), }, { @@ -284,7 +320,7 @@ export async function finalizePreview(jobId: string, machineId: string, body: Fi return { export: { id: updated.id, status: updated.status }, - version: versionId ? { id: versionId } : null, + version: versionId ? { id: versionId, replaced: versionReplaced } : null, task: exportRow.taskId ? { id: exportRow.taskId, title: taskTitle, renamed: taskRenamed } : null, alreadyApplied: false, }; @@ -331,3 +367,90 @@ export async function retryPreview(exportId: string, actorId?: string) { }; }); } + +/** Statuses a preview can be regenerated from — every state where the EXRs are known good. */ +const REGENERATABLE_STATUSES: ExportStatus[] = ["READY_FOR_QC", "QC_FAILED", "PREVIEW_FAILED"]; + +export interface RegeneratePreviewInput { + /** Per-export override; omitted → re-read from the shot's slate fields. */ + vfxScope?: string | null; + submissionNote?: string | null; + actorId?: string; + note?: string; +} + +/** + * T11b — rebuild the preview for an export that already delivered one, after a + * slate or burn-in mistake was spotted in QC. + * + * Re-runs the *whole* preview stage rather than patching anything: the manifest + * is rebuilt from scratch, so the shot code, episode/scene, frame count, slate + * date and burn-ins are all re-derived from current data exactly as they are on + * a first-time preview. The validated EXRs are reused and the version number is + * untouched — this re-delivers the same version, it does not make a new one. + * + * Slate text authored on the web app's Slate tab (description, VFX scope, + * submission note) is picked up here: `vfxScope` and `submissionNote` are + * frozen onto the Export at submission time, so regenerating refreshes that + * snapshot from the shot unless the caller passes an explicit override. + */ +export async function regeneratePreview(exportId: string, input: RegeneratePreviewInput = {}) { + const maxAttempts = await getConfigNumber("render.maxAttempts"); + + return db.$transaction(async (tx) => { + const exportRow = await tx.export.findUnique({ where: { id: exportId } }); + if (!exportRow) throw new PipelineError(404, "Export not found"); + if (!REGENERATABLE_STATUSES.includes(exportRow.status)) { + throw new PipelineError( + 409, + `Export is ${exportRow.status} — a preview can only be regenerated from ${REGENERATABLE_STATUSES.join(", ")}` + ); + } + + const shot = await tx.shot.findUniqueOrThrow({ + where: { id: exportRow.shotId }, + select: { slateVfxScope: true, slateSubmissionNote: true }, + }); + // Same precedence as createExport: explicit override → shot's slate field. + const vfxScope = input.vfxScope !== undefined ? input.vfxScope : (shot.slateVfxScope ?? null); + const submissionNote = + input.submissionNote !== undefined ? input.submissionNote : (shot.slateSubmissionNote ?? null); + + const refreshed = { ...exportRow, vfxScope, submissionNote }; + const manifest = await buildPreviewManifest(refreshed); + + const last = await tx.renderJob.findFirst({ + where: { exportId, type: "PREVIEW_ONLY" }, + orderBy: { attempt: "desc" }, + }); + + const job = await tx.renderJob.create({ + data: { + type: "PREVIEW_ONLY", + exportId, + attempt: (last?.attempt ?? 0) + 1, + maxAttempts, + priority: last?.priority ?? 50, + manifest: manifest as unknown as Prisma.InputJsonValue, + }, + }); + + const updated = await applyTransition( + tx, + exportRow, + "GENERATING_PREVIEW", + { + type: "USER", + id: input.actorId, + note: input.note ?? `Regenerate preview (attempt ${job.attempt})`, + }, + { vfxScope, submissionNote } + ); + + return { + export: { id: updated.id, status: updated.status }, + renderJob: { id: job.id, attempt: job.attempt }, + slate: { vfxScope, submissionNote, description: manifest.slate.description }, + }; + }); +} diff --git a/lib/render-pipeline/transitions.ts b/lib/render-pipeline/transitions.ts index dd13c98..0bd2957 100644 --- a/lib/render-pipeline/transitions.ts +++ b/lib/render-pipeline/transitions.ts @@ -24,8 +24,11 @@ export const EXPORT_TRANSITIONS: Record = { VALIDATION_FAILED: ["QUEUED", "SUPERSEDED"], GENERATING_PREVIEW: ["PREVIEW_FAILED", "READY_FOR_QC"], PREVIEW_FAILED: ["GENERATING_PREVIEW", "SUPERSEDED"], - READY_FOR_QC: ["QC_FAILED", "READY_FOR_DELIVERY", "SUPERSEDED"], - QC_FAILED: ["SUPERSEDED"], + // GENERATING_PREVIEW: a slate/burn-in mistake is caught *after* a successful + // preview, so the preview stage must be re-runnable without re-rendering the + // validated EXRs or burning a version number (T11b). + READY_FOR_QC: ["GENERATING_PREVIEW", "QC_FAILED", "READY_FOR_DELIVERY", "SUPERSEDED"], + QC_FAILED: ["GENERATING_PREVIEW", "SUPERSEDED"], READY_FOR_DELIVERY: ["PACKAGED", "SUPERSEDED"], PACKAGED: ["DELIVERED"], DELIVERED: ["ARCHIVED"],