Files
synthetic-defect-factory/blender/render_animation_video.py
T

62 lines
1.8 KiB
Python

"""
blender/render_animation_video.py
Renders the conveyor simulation animation frames via Blender EEVEE
and compiles them into an MP4 video using OpenCV.
"""
import bpy
import os
import glob
import cv2
def render_frames_and_compile(blend_file="station.blend", out_mp4="renders/animation/conveyor_simulation.mp4"):
abs_blend = os.path.abspath(blend_file)
frames_dir = os.path.abspath("renders/animation/frames")
abs_mp4 = os.path.abspath(out_mp4)
os.makedirs(frames_dir, exist_ok=True)
os.makedirs(os.path.dirname(abs_mp4), exist_ok=True)
bpy.ops.wm.open_mainfile(filepath=abs_blend)
scene = bpy.context.scene
scene.render.engine = 'BLENDER_EEVEE'
scene.render.resolution_x = 960
scene.render.resolution_y = 600
scene.render.image_settings.file_format = 'PNG'
scene.render.filepath = os.path.join(frames_dir, "frame_")
scene.frame_start = 1
scene.frame_end = 120
overview_cam = bpy.data.objects.get("ProductionLineOverview")
if overview_cam:
scene.camera = overview_cam
print(f"Rendering 120 frames with EEVEE to: {frames_dir}...")
bpy.ops.render.render(animation=True)
print("Frames rendered successfully!")
# Compile with OpenCV into MP4
frame_files = sorted(glob.glob(os.path.join(frames_dir, "frame_*.png")))
if not frame_files:
print("No frames found to compile.")
return
first_frame = cv2.imread(frame_files[0])
h, w, _ = first_frame.shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(abs_mp4, fourcc, 24.0, (w, h))
for ff in frame_files:
img = cv2.imread(ff)
out.write(img)
out.release()
print(f"\nMP4 video compiled successfully to: {abs_mp4}")
if __name__ == "__main__":
render_frames_and_compile()