"""Render close-up product photographs from the exact interactive model."""
import bpy, os, sys, math
from pathlib import Path
from mathutils import Vector
ROOT=Path(os.environ.get('MEZZO_ROOT',str(Path(__file__).resolve().parent.parent)))
bpy.ops.wm.open_mainfile(filepath=str(ROOT/'modeling/mezzo-studio.blend'))
s=bpy.context.scene;c=s.camera
s.render.threads_mode='FIXED';s.render.threads=8
s.render.resolution_x=1420;s.render.resolution_y=1040;s.cycles.samples=48
floor=bpy.data.materials['Studio / floor finish'].node_tree.nodes.get('Principled BSDF')
floor.inputs['Base Color'].default_value=(.49,.53,.47,1)
def render(name,loc,target,scale):
    c.location=loc;c.rotation_euler=(Vector(target)-c.location).to_track_quat('-Z','Y').to_euler();c.data.ortho_scale=scale
    s.render.filepath=str(ROOT/'public/images'/name)
    bpy.ops.render.render(write_still=True)
s.render.resolution_x=1600;s.render.resolution_y=1200
floor.inputs['Base Color'].default_value=(.023,.031,.024,1)
render('mezzo-studio.png',(39,-51,43),(0,0,2.5),57)
s.render.resolution_x=1420;s.render.resolution_y=1040
floor.inputs['Base Color'].default_value=(.49,.53,.47,1)
if '--expansion-only' not in sys.argv:
    render('mezzo-sculpture.png',(-28,-26,40),(-7,3.5,3),34)
# A low, almost side-on photograph makes the through-cut silhouette explicit.
s.render.resolution_x=1800;s.render.resolution_y=680
floor.inputs['Base Color'].default_value=(.023,.031,.024,1)
render('mezzo-profile.png',(43,-14,10.2),(0,0,3.6),47)
s.render.resolution_x=1800;s.render.resolution_y=750
render('mezzo-connections.png',(-10,47,16),(0,11.3,2.8),37)
s.render.resolution_x=1420;s.render.resolution_y=1040
floor.inputs['Base Color'].default_value=(.49,.53,.47,1)
bpy.data.objects['cartridge_compute'].location.z+=5.8
for side in ('front','rear'):bpy.data.objects['bay_01_door_'+side].rotation_euler.x=0
bpy.data.objects['cartridge_project'].location.x+=4.5
render('mezzo-expansion.png',(40,-30,31),(10.5,4.5,6.0),41)

# Underside photographs: use the same studio with an inverted complete product.
for name in ('cartridge_compute','cartridge_project'):
    ob=bpy.data.objects[name]
    if name=='cartridge_compute':ob.location.z-=5.8
    else:ob.location.x-=4.5
for side,sign in (('front',-1),('rear',1)):bpy.data.objects['bay_01_door_'+side].rotation_euler.x=sign*math.pi/2
root=bpy.data.objects.new('Studio / inverted ALTO',None);s.collection.objects.link(root)
for ob in list(s.objects):
    if ob.parent is None and ob.type not in ('CAMERA','LIGHT') and not ob.name.startswith('Studio'):
        ob.parent=root
root.rotation_euler.x=3.141592653589793;root.location.z=12
s.render.resolution_x=1600;s.render.resolution_y=1200
floor.inputs['Base Color'].default_value=(.023,.031,.024,1)
render('mezzo-underside.png',(27,-30,57),(0,0,10),51)
bpy.data.objects['service_lid'].location.x+=30
bpy.data.objects['service_lid'].rotation_euler.y=-1.0
bpy.data.objects['service_lid'].location.z-=6
bpy.data.objects['lid_clamp'].location.y-=.5
for name in ('heel_left','heel_right'):bpy.data.objects[name].rotation_euler.x=.55
render('mezzo-service.png',(35,-39,68),(10,0,10),73)
