|
| 1 | +"""Regression: OCIO Write shows exactly ONE preview per write (run: python tools/test_write_preview_single.py). |
| 2 | +
|
| 3 | +A written sequence got TWO previews on the node (2026-08-15): the flipbook of the real frames, and above/below |
| 4 | +it an H.264 proxy of the same frames. They do not agree - the proxy is 8-bit and carries no colour conversion, |
| 5 | +so it reads darker than the frames it claims to preview - and nothing on the node says which one is the master. |
| 6 | +An artist reading colour off the wrong one is the whole failure this pack exists to prevent. |
| 7 | +
|
| 8 | +Neither preview was wrong on its own; shipping both was. So the rule under test is a COUNT, not a value: |
| 9 | +
|
| 10 | +1. A SEQUENCE ships the flipbook (seq_src + range + fps) and NO proxy. thumb_frame reads the written frames |
| 11 | + with _read_still, the same reader OCIO Read uses, so every still format this branch writes is one the |
| 12 | + flipbook can serve back - the proxy has nothing left to add. |
| 13 | +
|
| 14 | +2. A VIDEO still ships the proxy, and no flipbook. A movie is one file, not a numbered range; /ocio/thumb can |
| 15 | + only hand back its FIRST frame, so a flipbook there would be a still pretending to be a clip. |
| 16 | +
|
| 17 | +3. A SINGLE-FRAME sequence ships neither. One frame is a still: the node's own thumb already shows it, and an |
| 18 | + H.264 clip of one frame is a video that cannot move. |
| 19 | +
|
| 20 | +The counts are read off the real return of OCIOWrite.write(), not from the source text - a preview key that is |
| 21 | +merely PRESENT in the file proves nothing about which branch sets it. |
| 22 | +""" |
| 23 | +import importlib.util |
| 24 | +import os |
| 25 | +import sys |
| 26 | +import tempfile |
| 27 | +import types |
| 28 | + |
| 29 | +os.environ.setdefault("OPENCV_IO_ENABLE_OPENEXR", "1") |
| 30 | + |
| 31 | +_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 32 | + |
| 33 | + |
| 34 | +def _load_io_nodes(tmp): |
| 35 | + fp = types.ModuleType("folder_paths") |
| 36 | + fp.get_output_directory = fp.get_temp_directory = fp.get_input_directory = lambda: tmp |
| 37 | + fp.get_filename_list = lambda *a, **k: [] |
| 38 | + sys.modules.setdefault("folder_paths", fp) |
| 39 | + pkg = types.ModuleType("ocio_pkg") |
| 40 | + pkg.__path__ = [_ROOT] |
| 41 | + sys.modules["ocio_pkg"] = pkg |
| 42 | + for name in ("nodes", "io_nodes"): |
| 43 | + spec = importlib.util.spec_from_file_location(f"ocio_pkg.{name}", os.path.join(_ROOT, f"{name}.py")) |
| 44 | + mod = importlib.util.module_from_spec(spec) |
| 45 | + sys.modules[f"ocio_pkg.{name}"] = mod |
| 46 | + spec.loader.exec_module(mod) |
| 47 | + return sys.modules["ocio_pkg.io_nodes"] |
| 48 | + |
| 49 | + |
| 50 | +def _write(io, frames, **kw): |
| 51 | + import torch |
| 52 | + imgs = torch.zeros((frames, 6, 8, 3)) |
| 53 | + imgs[..., 0], imgs[..., 1], imgs[..., 2] = 0.40, 0.60, 0.10 |
| 54 | + args = dict(profile="none", from_colorspace="sRGB - Display", output_colorspace="ACEScg", |
| 55 | + container="sequence", still_format="exr", video_codec="prores_4444", bit_depth="16f", |
| 56 | + auto_range=False, first_frame=1, last_frame=0, start_number=1001, source_start=1, |
| 57 | + raw_data=False, filename="prev", fps=23.976, images=imgs) |
| 58 | + args.update(kw) |
| 59 | + res = io.OCIOWrite().write(**args) |
| 60 | + return (res or {}).get("ui", {}) or {} |
| 61 | + |
| 62 | + |
| 63 | +def check_sequence_is_flipbook_only(io): |
| 64 | + """A multi-frame sequence: the real frames, and nothing standing beside them.""" |
| 65 | + ui = _write(io, 3, output_folder="$OUTPUT/seq3") |
| 66 | + assert "seq_src" in ui, f"a 3-frame sequence lost its flipbook; ui keys were {sorted(ui)}" |
| 67 | + assert "images" not in ui and "animated" not in ui, ( |
| 68 | + f"TWO previews on one node: the flipbook AND the H.264 proxy; ui keys were {sorted(ui)}") |
| 69 | + # The range has to describe the files that exist, or the flipbook asks /ocio/thumb for frames it will 404 on. |
| 70 | + first, last = int(ui["seq_first"][0]), int(ui["seq_last"][0]) |
| 71 | + assert (first, last) == (1001, 1003), f"flipbook range {first}..{last} does not match the 3 frames written" |
| 72 | + folder = ui["seq_src"][0] |
| 73 | + on_disk = sorted(f for f in os.listdir(folder) if f.endswith(".exr")) |
| 74 | + assert len(on_disk) == 3, f"flipbook points at {folder}, which holds {on_disk}" |
| 75 | + assert float(ui["seq_fps"][0]) > 0, "a flipbook with no rate plays at whatever the browser feels like" |
| 76 | + |
| 77 | + |
| 78 | +def check_video_is_proxy_only(io): |
| 79 | + """A movie: the proxy, and no flipbook - /ocio/thumb cannot scrub a single container file.""" |
| 80 | + ui = _write(io, 3, container="video", video_codec="h264", output_folder="$OUTPUT/mov") |
| 81 | + assert "images" in ui and "animated" in ui, f"a movie lost its playable preview; ui keys were {sorted(ui)}" |
| 82 | + assert "seq_src" not in ui, ( |
| 83 | + f"a movie was handed a frame-range flipbook it cannot serve; ui keys were {sorted(ui)}") |
| 84 | + |
| 85 | + |
| 86 | +def check_single_frame_has_no_clip(io): |
| 87 | + """One frame is a still: its own PNG thumb, and nothing that pretends to move. |
| 88 | +
|
| 89 | + `images` IS expected here and is not the proxy - it is _preview(), a static PNG of the first written frame. |
| 90 | + What must not appear is `animated` (an H.264 clip of one frame) or `seq_src` (a range of one). |
| 91 | + """ |
| 92 | + ui = _write(io, 1, output_folder="$OUTPUT/one") |
| 93 | + assert "seq_src" not in ui, f"a single frame was described as a flipbook; ui keys were {sorted(ui)}" |
| 94 | + assert "animated" not in ui, f"a single frame was given a moving preview; ui keys were {sorted(ui)}" |
| 95 | + assert "images" in ui, f"a single frame lost its still thumb; ui keys were {sorted(ui)}" |
| 96 | + |
| 97 | + |
| 98 | +def main(): |
| 99 | + tmp = tempfile.mkdtemp(prefix="ocio_prev_") |
| 100 | + io = _load_io_nodes(tmp) |
| 101 | + failures = [] |
| 102 | + for fn in (check_sequence_is_flipbook_only, check_video_is_proxy_only, check_single_frame_has_no_clip): |
| 103 | + try: |
| 104 | + fn(io) |
| 105 | + print(f" ok {fn.__name__}") |
| 106 | + except AssertionError as e: |
| 107 | + failures.append(f"{fn.__name__}: {e}") |
| 108 | + print(f" FAIL {fn.__name__}: {e}") |
| 109 | + if failures: |
| 110 | + print(f"\n{len(failures)} failure(s)") |
| 111 | + return 1 |
| 112 | + print("\nOCIO Write shows exactly one preview per container: OK") |
| 113 | + return 0 |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + sys.exit(main()) |
0 commit comments