# blender -b -P clipshot2.py -- model.glb outprefix "Clip:frame,..." yaw pitch
import bpy, sys, math, mathutils
a=sys.argv[sys.argv.index("--")+1:]; src,out,spec=a[0],a[1],a[2]; yaw=float(a[3]) if len(a)>3 else 30; pitch=float(a[4]) if len(a)>4 else 32
bpy.ops.wm.read_factory_settings(use_empty=True); bpy.ops.import_scene.gltf(filepath=src)
for o in list(bpy.context.scene.objects):
    if o.type=="MESH" and o.name.startswith("Ico"): bpy.data.objects.remove(o)
arm=[o for o in bpy.context.scene.objects if o.type=="ARMATURE"][0]; me=[o for o in bpy.context.scene.objects if o.type=="MESH"][0]
pts=[me.matrix_world@mathutils.Vector(c) for c in me.bound_box]
zs=[p.z for p in pts]; H=max(zs)-min(zs); ground=min(zs)
sc=bpy.context.scene; sc.render.engine="BLENDER_EEVEE"; sc.render.resolution_x=sc.render.resolution_y=320
w=bpy.data.worlds.new("w"); sc.world=w; w.use_nodes=True; w.node_tree.nodes["Background"].inputs[0].default_value=(0.16,0.17,0.2,1); w.node_tree.nodes["Background"].inputs[1].default_value=1.4
sun=bpy.data.objects.new("sun",bpy.data.lights.new("sun","SUN")); sun.data.energy=2.5; sun.rotation_euler=(math.radians(35),0,math.radians(25)); sc.collection.objects.link(sun)
bpy.ops.mesh.primitive_plane_add(size=H*4, location=(0,0,ground)); gp=bpy.context.active_object
gm=bpy.data.materials.new("g"); gm.use_nodes=True; gm.node_tree.nodes["Principled BSDF"].inputs[0].default_value=(0.3,0.32,0.36,1); gp.data.materials.append(gm)
cam=bpy.data.objects.new("cam",bpy.data.cameras.new("cam")); sc.collection.objects.link(cam); sc.camera=cam; cam.data.type="ORTHO"; cam.data.ortho_scale=H*1.9
y,p=math.radians(yaw),math.radians(pitch); d=mathutils.Vector((math.sin(y)*math.cos(p),-math.cos(y)*math.cos(p),math.sin(p)))
ctr=mathutils.Vector((0,0,ground+H*0.42)); cam.location=ctr+d*H*5; cam.rotation_euler=(-d).to_track_quat("-Z","Y").to_euler()
arm.animation_data_create()
for i,item in enumerate(spec.split(",")):
    name,f=item.split(":"); act=[x for x in bpy.data.actions if x.name==name or x.name.startswith(name+"_")][0]
    arm.animation_data.action=act
    if act.slots: arm.animation_data.action_slot=act.slots[0]
    sc.frame_set(int(f)); sc.render.filepath=f"{out}_{i:02d}.png"; bpy.ops.render.render(write_still=True)
