# AI ATLAS original geometry experiment. Tested with Blender4.3.2.
# This clears the active scene. Save your work first; use background mode.
# No network, downloads or add-ons. Writes only into a NEW output directory.
# Run: blender --background --python-exit-code 1 --python build_table_comparison.py -- --out table-rebuilt
import argparse
import bpy
import json
from pathlib import Path
import sys
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('--out',type=Path,default=Path('table-rebuilt'))
args=parser.parse_args(sys.argv[sys.argv.index('--')+1:] if '--' in sys.argv else [])
out=args.out.resolve()
if out.exists():raise ValueError('Choose a new output directory; existing work is not overwritten: '+str(out))
out.mkdir(parents=True)
report=[]
for sides in (8,32):
    bpy.ops.wm.read_factory_settings(use_empty=True)
    bpy.context.scene.unit_settings.system='METRIC'
    bpy.context.scene.unit_settings.scale_length=1.0
    bpy.ops.mesh.primitive_cylinder_add(vertices=sides,radius=.30,depth=.06,end_fill_type='NGON',location=(0,0,0))
    model=bpy.context.object;model.name='Table_top_'+str(sides)
    material=bpy.data.materials.new('Same warm paper')
    material.diffuse_color=(.86,.79,.60,1);material.use_nodes=True
    material.node_tree.nodes['Principled BSDF'].inputs['Base Color'].default_value=(.86,.79,.60,1)
    material.node_tree.nodes['Principled BSDF'].inputs['Roughness'].default_value=.65
    model.data.materials.append(material)
    model.data.calc_loop_triangles()
    filename='table-'+str(sides)+'.glb'
    bpy.ops.export_scene.gltf(filepath=str(out/filename),export_format='GLB',use_selection=True)
    report.append({'sides':sides,'radiusM':.30,'thicknessM':.06,'triangles':len(model.data.loop_triangles),
                   'file':filename,'bytes':(out/filename).stat().st_size})
(out/'build-report.json').write_text(json.dumps({'blenderVersion':bpy.app.version_string,'models':report,'changedVariable':'Cylinder sides only; same radius,thickness,material,origin and flat shading.'},indent=2)+'\n')
print('ATLAS_TABLE_COMPARISON',json.dumps(report))
