"""Render a walking loop from the editable Blender source, for art review."""
import bpy
import sys
from pathlib import Path

root = Path(__file__).resolve().parents[2]
source = root / 'tools/character-art/source'
meshy = '--meshy' in sys.argv
frames = root / ('_backups/meshy-cook-review/walk-frames' if meshy else '_backups/intro-cook-review/walk-frames')
frames.mkdir(parents=True, exist_ok=True)
bpy.ops.wm.open_mainfile(filepath=str(source / ('meshy_v2/intro_cook_meshy.blend' if meshy else 'intro_cook.blend')))
rig = bpy.data.objects['IntroCookMeshyRig' if meshy else 'IntroCookRig']
action = bpy.data.actions['Walking']
rig.animation_data.action = action
rig.animation_data.action_slot = action.slots[0]
scene = bpy.context.scene
scene.render.resolution_x = 504
scene.render.resolution_y = 560
scene.cycles.samples = 12
count = 18 if meshy else 13
for index in range(count):
    frame = action.frame_range[0] + (action.frame_range[1] - action.frame_range[0]) * index / count
    scene.frame_set(int(frame), subframe=frame-int(frame))
    scene.render.filepath = str(frames / f'{index:02d}.png')
    bpy.ops.render.render(write_still=True)
print('INTRO_COOK_WALK_PREVIEW', frames)
