File size: 2,658 Bytes
1ef96d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
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
"""Render the final scenes in this directory, without shared project files."""
import argparse
from pathlib import Path
import re
import sys


def main():
    root = Path(__file__).resolve().parent
    scenes = {}
    for path in root.glob("*.blend"):
        match = re.fullmatch(r"([A-F][0-9]{2})_(P[012]_A[NAC])\.blend", path.name)
        if match:
            scenes[match[2]] = path
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--cell", choices=sorted(scenes), default="P1_AA")
    mode = parser.add_mutually_exclusive_group()
    mode.add_argument("--frame", type=int, help="Render one scene frame; default is the first frame")
    mode.add_argument("--animation", action="store_true", help="Render the full scene frame range as PNGs")
    parser.add_argument("--output", type=Path, help="Output directory; relative paths are relative to this case")
    argv = sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else sys.argv[1:]
    args = parser.parse_args(argv)
    if args.cell not in scenes:
        parser.error("The selected .blend is missing from this case directory")

    import bpy
    bpy.ops.wm.open_mainfile(filepath=str(scenes[args.cell]), use_scripts=False)
    scene = bpy.context.scene
    if not scene.camera:
        parser.error("The scene has no active camera")
    frame = scene.frame_start if args.frame is None else args.frame
    if not scene.frame_start <= frame <= scene.frame_end:
        parser.error(f"Frame must be in {scene.frame_start}..{scene.frame_end}")
    output = args.output or Path("renders") / args.cell
    if not output.is_absolute():
        output = root / output
    output = output.resolve()
    output.mkdir(parents=True, exist_ok=True)

    if scene.render.engine == "CYCLES":
        # CPU avoids relying on the original workstation's GPU backend.
        scene.cycles.device = "CPU"
        scene.cycles.denoising_use_gpu = False

    scene.render.image_settings.file_format = "PNG"
    scene.render.use_file_extension = True
    # Override the original workstation's output path without modifying the .blend.
    scene.render.filepath = str(output / "frame_")
    print(f"Rendering {scenes[args.cell].name} to {output}", flush=True)
    if args.animation:
        bpy.ops.render.render(animation=True)
    else:
        # Advance sequentially so unbaked simulation caches reach the selected frame.
        for current in range(scene.frame_start, frame + 1):
            scene.frame_set(current)
        scene.render.filepath = str(output / f"frame_{frame:04d}.png")
        bpy.ops.render.render(write_still=True)


if __name__ == "__main__":
    main()