import bpy, os, math
SRC = "/Users/danicosta/Desktop/Avatar-Platon/3d_pipeline/oc_output/plato_medium.usdz"
EVAL = "/Users/danicosta/Desktop/Avatar-Platon/3d_pipeline/clay_eval"
os.makedirs(EVAL, exist_ok=True)
bpy.ops.wm.read_factory_settings(use_empty=True)
scene = bpy.context.scene; coll = scene.collection
bpy.ops.wm.usd_import(filepath=SRC)
meshes = [o for o in scene.objects if o.type == 'MESH']
for o in scene.objects: o.select_set(False)
for o in meshes: o.select_set(True)
bpy.context.view_layer.objects.active = meshes[0]
if len(meshes) > 1: bpy.ops.object.join()
head = bpy.context.active_object
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='BOUNDS')
head.location = (0,0,0); head.scale = [2.2/max(head.dimensions)]*3
bpy.ops.object.transform_apply(scale=True)
for p in head.data.polygons: p.use_smooth = True
# clay material gris
m = bpy.data.materials.new("clay"); m.use_nodes=True
b=m.node_tree.nodes["Principled BSDF"]
b.inputs["Base Color"].default_value=(0.6,0.58,0.56,1); b.inputs["Roughness"].default_value=0.5
head.data.materials.clear(); head.data.materials.append(m)
# luz uniforme
w=bpy.data.worlds.new("w"); scene.world=w; w.use_nodes=True
w.node_tree.nodes["Background"].inputs["Strength"].default_value=1.2
tgt=bpy.data.objects.new("t",None); coll.objects.link(tgt); tgt.location=(0,0,0)
for loc in [(4,-4,4),(-4,-3,2),(0,4,3)]:
    ld=bpy.data.lights.new("L",'AREA'); ld.energy=500; ld.size=6
    lo=bpy.data.objects.new("L",ld); coll.objects.link(lo); lo.location=loc
    lo.constraints.new('TRACK_TO').target=tgt
cd=bpy.data.cameras.new("c"); cd.lens=55; cam=bpy.data.objects.new("c",cd)
coll.objects.link(cam); cam.constraints.new('TRACK_TO').target=tgt; scene.camera=cam
scene.render.engine='BLENDER_EEVEE_NEXT'; scene.render.resolution_x=480; scene.render.resolution_y=600
try: scene.view_settings.view_transform='Standard'
except Exception: pass
R=4.5
for nm,(x,y,z) in {"front":(0,-R,0.3),"profile":(R,0,0.3),"back":(0,R,0.3),
                   "tq":(-R*0.7,-R*0.7,0.5),"top":(0.01,0.01,R),
                   "top_fwd":(0,-R*0.6,R*0.7)}.items():
    cam.location=(x,y,z); scene.render.filepath=os.path.join(EVAL,f"clay_{nm}.png")
    bpy.ops.render.render(write_still=True); print("rendered",nm)
print("DONE")
