#!/usr/bin/python
#
# Google Gemini 3/8/26
#


import bpy
import os
import sys

# 1. Handle Command Line Arguments
argv = sys.argv
if "--" not in argv:
    print("Usage: blender -b file.blend -P script.py -- model.glb")
    sys.exit()

glb_path = argv[argv.index("--") + 1:]
if not glb_path:
    sys.exit()
glb_path = glb_path[0]
base_name = os.path.splitext(os.path.basename(glb_path))[0]

# 2. Preparation: Find the Parent and Clear Old Children
parent = bpy.data.objects.get("ColorpieParent")

# Delete old children of the parent so we don't render multiple donuts on top of each other
if parent:
    for child in parent.children:
        bpy.data.objects.remove(child, do_unlink=True)

# 3. Import the New GLB
bpy.ops.import_scene.gltf(filepath=glb_path)

# 4. Parent New Objects to the Animation "Coin"
# Newly imported objects are automatically 'selected'
imported_objs = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']
for obj in imported_objs:
    if parent:
        obj.parent = parent
        # Optional: Reset local transforms so it aligns perfectly with the parent
        obj.location = (0, 0, 0)

# # 5. Render Settings
# scene = bpy.context.scene
# scene.render.image_settings.file_format = 'WEBP'
# scene.render.image_settings.quality = 95
# scene.render.film_transparent = False # Ensure black background from World nodes

# # 6. Render the "Stills" (Front and Back)
# # Front Face (1 second in)
# scene.frame_set(1)
# scene.render.filepath = os.path.abspath(f"front_{base_name}.webp")
# bpy.ops.render.render(write_still=True)

# # Back Face (Post-flip, e.g., Frame 144)
# scene.frame_set(144)
# scene.render.filepath = os.path.abspath(f"back_{base_name}.webp")
# bpy.ops.render.render(write_still=True)

# # 5. Render Settings (FFMPEG Workaround)
# scene = bpy.context.scene
# scene.render.image_settings.file_format = 'FFMPEG'
# scene.render.ffmpeg.format = 'QUICKTIME' # MOV container
# scene.render.ffmpeg.video_codec = 'PNG'   # Use PNG compression inside the MOV

# 5. Render Settings (Blender 5.0 + Headless Workaround)
scene = bpy.context.scene
scene.render.image_settings.file_format = 'FFMPEG'

# Set the Container to Quicktime (MOV)
scene.render.ffmpeg.format = 'QUICKTIME' 

# In Blender 5.0, use .codec instead of .video_codec
scene.render.ffmpeg.codec = 'PNG' 

# Ensure it captures only the frame we want
scene.render.ffmpeg.constant_rate_factor = 'HIGH'

# # 6. Render the "Stills" 
# # Note: Blender will add frame numbers to these filenames
# # Frame 1 (Front)
# scene.frame_set(1)
# scene.render.filepath = os.path.abspath(f"front_{base_name}")
# bpy.ops.render.render(write_still=True)

# # Frame 144 (Back)
# scene.frame_set(144)
# scene.render.filepath = os.path.abspath(f"back_{base_name}")
# bpy.ops.render.render(write_still=True)

# 6. Render the "Stills" as 1-frame movies
# Frame 1 (Front)
scene.frame_start = 1
scene.frame_end = 1
scene.render.filepath = os.path.abspath(f"front_{base_name}")
bpy.ops.render.render(animation=True)

# Frame 144 (Back)
scene.frame_start = 144
scene.frame_end = 144
scene.render.filepath = os.path.abspath(f"back_{base_name}")
bpy.ops.render.render(animation=True)

# 7. Render Full Video (Only for specific files)
if "full_gamut" in base_name:
    # Set to Video format for the animation
    scene.render.image_settings.file_format = 'FFMPEG'
    scene.render.ffmpeg.format = 'MPEG4'
    scene.render.ffmpeg.codec = 'H264'
    scene.render.filepath = os.path.abspath(f"anim_{base_name}.mp4")
    bpy.ops.render.render(animation=True)

