"""
Blender Camera Handlers
This module contains handlers for camera operations in Blender.
Includes aligning camera to view, setting projection, and setting active camera.
"""
import bpy
from math import degrees
def align_active_camera_to_view():
"""Align active camera to current viewport"""
try:
if not bpy.context.scene.camera:
return {"error": "No active camera in scene"}
camera = bpy.context.scene.camera
# Find 3D viewport
area = None
for a in bpy.context.screen.areas:
if a.type == 'VIEW_3D':
area = a
break
if not area:
return {"error": "No 3D viewport found"}
# Align camera to view
with bpy.context.temp_override(area=area):
bpy.ops.view3d.camera_to_view()
return {
"success": True,
"camera_name": camera.name,
"location": list(camera.location),
"rotation": [degrees(r) for r in camera.rotation_euler]
}
except Exception as e:
return {"error": f"Failed to align camera: {str(e)}"}
def set_camera_projection(projection_type, ortho_scale):
"""Set camera projection type"""
try:
if not bpy.context.scene.camera:
return {"error": "No active camera in scene"}
camera = bpy.context.scene.camera.data
if projection_type == 'ORTHO':
camera.type = 'ORTHO'
camera.ortho_scale = ortho_scale
elif projection_type == 'PERSP':
camera.type = 'PERSP'
else:
return {"error": f"Invalid projection type: {projection_type}"}
return {
"success": True,
"camera_name": bpy.context.scene.camera.name
}
except Exception as e:
return {"error": f"Failed to set camera projection: {str(e)}"}
def set_active_camera(camera_name):
"""Set the active scene camera"""
try:
camera_obj = bpy.data.objects.get(camera_name)
if not camera_obj:
return {"error": f"Camera '{camera_name}' not found"}
if camera_obj.type != 'CAMERA':
return {"error": f"Object '{camera_name}' is not a camera"}
bpy.context.scene.camera = camera_obj
return {
"success": True,
"projection_type": camera_obj.data.type
}
except Exception as e:
return {"error": f"Failed to set active camera: {str(e)}"}