blender/tests/python/opengl_draw_tests.py
Sybren A. Stüvel b7028617ad Refactor: render tests, change how argparse is used
Some changes to how argparse is used in render tests:

1. Use the common approach of one dash for single-letter options (`-b`)
   and two dashes for longer options (`--blender`). In this commit that
   just means changing single-dashed (`-testdir`) to double-dashed
   (`--testdir`).

2. Remove unnecessary `nargs` arguments. The code was telling `argparse`
   to put CLI arguments into a list of one item, and then had code to
   turn that one-item list into the item itself. I've just removed the
   `nargs` argument altogether, as that just produces the desired
   value without requiring more code.

   I've also removed `nargs="+"` from the handling of the `--blender`
   parameter, as that allowed for multiple occurrences of `--blender
   {path}` but was silently ignoring all of those except the first.

   To ensure that required arguments are present, the code now uses
   `required=True` instead of `nargs`.

3. Add a `description` parameter so that `--help` shows what the
   test script actually does. Also it helps people (like me) who want
   to figure out which blend file is actually being opened by the
   test, without making the test itself more verbose.

No functional changes, except that you now cannot add multiple
`--blender` arguments any more (the CLI invocation will fail). This wasn't
used anywhere I could find, though.

Pull Request: https://projects.blender.org/blender/blender/pulls/131666
2024-12-10 14:52:34 +01:00

75 lines
1.7 KiB
Python

#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2018-2022 Blender Authors
#
# SPDX-License-Identifier: Apache-2.0
import argparse
import os
import sys
def screenshot():
import bpy
output_path = sys.argv[-1]
# Force redraw and take screenshot.
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
bpy.ops.screen.screenshot(filepath=output_path, full=True)
bpy.ops.wm.quit_blender()
# When run from inside Blender, take screenshot and exit.
try:
import bpy
inside_blender = True
except ImportError:
inside_blender = False
if inside_blender:
screenshot()
sys.exit(0)
def get_arguments(filepath, output_filepath):
return [
"--no-window-focus",
"--window-geometry",
"0", "0", "1024", "768",
"-noaudio",
"--factory-startup",
"--enable-autoexec",
"--debug-memory",
"--debug-exit-on-error",
filepath,
"-P",
os.path.realpath(__file__),
"--",
output_filepath + '0001.png']
def create_argparse():
parser = argparse.ArgumentParser(
description="Run test script for each blend file in TESTDIR, comparing the render result with known output."
)
parser.add_argument("--blender", required=True)
parser.add_argument("--testdir", required=True)
parser.add_argument("--outdir", required=True)
parser.add_argument("--oiiotool", required=True)
return parser
def main():
parser = create_argparse()
args = parser.parse_args()
from modules import render_report
report = render_report.Report("OpenGL Draw", args.outdir, args.oiiotool)
ok = report.run(args.testdir, args.blender, get_arguments)
sys.exit(not ok)
if __name__ == "__main__":
main()