"""Orthographic top-down render of the built arena GLB, for eyeballing the level layout.

    blender -b --python tools/render_topdown.py -- build/glb/room-for-9.glb .bitmagic/topdown.png

Camera looks along Unity -z... in Blender terms: the GLB imports with Blender y = Unity depth
(negative toward the camera), Blender z = Unity y (map vertical). So the camera sits at large
negative Blender y looking toward +y, with Blender z as image up and Blender x as image right.
"""
import sys, os, bpy, math, mathutils
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from import_config import CFG as _CFG  # noqa: E402
argv = sys.argv[sys.argv.index('--') + 1:] if '--' in sys.argv else []
GLB = argv[0] if argv else _CFG['glb']
OUT = argv[1] if len(argv) > 1 else '.bitmagic/topdown.png'
bpy.ops.wm.read_factory_settings(use_empty=True)
bpy.ops.import_scene.gltf(filepath=GLB)
sc = bpy.context.scene
cam_data = bpy.data.cameras.new('top'); cam_data.type = 'ORTHO'; cam_data.ortho_scale = 150
cam = bpy.data.objects.new('top', cam_data); sc.collection.objects.link(cam)
cam.location = (0, -200, 0)
cam.rotation_euler = (math.radians(90), 0, 0)   # look along +y, z up
sc.camera = cam
sun = bpy.data.lights.new('sun', 'SUN'); sun.energy = 4.0
so = bpy.data.objects.new('sun', sun); sc.collection.objects.link(so)
so.rotation_euler = (math.radians(60), math.radians(20), 0)
sc.render.engine = 'BLENDER_EEVEE_NEXT' if hasattr(bpy.types, 'SceneEEVEE') and 'BLENDER_EEVEE_NEXT' in [e.identifier for e in bpy.types.RenderSettings.bl_rna.properties['engine'].enum_items] else 'BLENDER_EEVEE'
sc.render.resolution_x = 1400; sc.render.resolution_y = 1400
sc.render.filepath = OUT
sc.world = bpy.data.worlds.new('w'); sc.world.use_nodes = True
sc.world.node_tree.nodes['Background'].inputs[0].default_value = (0.02, 0.02, 0.03, 1)
sc.world.node_tree.nodes['Background'].inputs[1].default_value = 1.0
bpy.ops.render.render(write_still=True)
print('TOPDOWN', OUT)
