"""Run on a workstation with the licensed Chaos V-Ray App SDK installed.
Example: python vray_worker.py scene.vrscene output.png
This standalone starter is not a hosted portal worker or job-queue connector.
"""
import argparse
from pathlib import Path

def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('scene', type=Path)
    parser.add_argument('output', type=Path)
    parser.add_argument('--check', action='store_true', help='Validate paths without importing or running V-Ray')
    args = parser.parse_args()
    if args.scene.suffix.lower() != '.vrscene' or not args.scene.is_file():
        parser.error('Scene must be an existing .vrscene file')
    if args.output.suffix.lower() != '.png' or not args.output.parent.is_dir():
        parser.error('Output must be a PNG path in an existing directory')
    if args.output.exists():
        parser.error('Output already exists; choose a new filename')
    if args.check:
        print('Paths valid. SDK availability and scene dependencies have not been checked.')
        return
    try:
        import vray
    except ImportError:
        parser.error('Install the licensed V-Ray App SDK and configure PYTHONPATH before rendering')
    with vray.VRayRenderer() as renderer:
        renderer.renderMode = 'production'
        renderer.load(str(args.scene.resolve()))
        renderer.start()
        renderer.waitForRenderEnd()
        image = renderer.getImage()
        if image is None:
            raise RuntimeError('V-Ray returned no image; inspect the SDK render log')
        image.save(str(args.output.resolve()))
    print('Saved', args.output)

if __name__ == '__main__':
    main()
