headless_renderer.py•2.25 kB
import os
import logging
from PIL import Image, ImageDraw
from typing import Dict, Any, Optional
logger = logging.getLogger(__name__)
class HeadlessRenderer:
"""Provides rendering capabilities for OpenSCAD in headless environments."""
def __init__(self, openscad_path: str = "openscad"):
self.openscad_path = openscad_path
self.camera_angles = {
"front": "0,0,0,0,0,0,50",
"top": "0,0,0,90,0,0,50",
"right": "0,0,0,0,0,90,50",
"perspective": "70,0,35,25,0,25,250"
}
def create_placeholder_image(self, output_path: str, model_id: str, view: str = "perspective") -> str:
"""Create a placeholder image with model information."""
try:
# Create a blank image
width, height = 800, 600
image = Image.new('RGB', (width, height), color=(240, 240, 240))
draw = ImageDraw.Draw(image)
# Add text
draw.text((20, 20), f"OpenSCAD Model: {model_id}", fill=(0, 0, 0))
draw.text((20, 60), f"View: {view}", fill=(0, 0, 0))
draw.text((20, 100), "Headless rendering mode", fill=(0, 0, 0))
# Draw a simple 3D shape
draw.polygon([(400, 200), (300, 300), (500, 300)], outline=(0, 0, 0), width=2)
draw.polygon([(400, 200), (500, 300), (500, 400)], outline=(0, 0, 0), width=2)
draw.polygon([(400, 200), (300, 300), (300, 400)], outline=(0, 0, 0), width=2)
draw.rectangle((300, 300, 500, 400), outline=(0, 0, 0), width=2)
# Add note about headless mode
note = "Note: This is a placeholder image. OpenSCAD preview generation"
note2 = "requires an X server or a headless rendering solution."
draw.text((20, 500), note, fill=(150, 0, 0))
draw.text((20, 530), note2, fill=(150, 0, 0))
# Save the image
image.save(output_path)
logger.info(f"Created placeholder image: {output_path}")
return output_path
except Exception as e:
logger.error(f"Error creating placeholder image: {str(e)}")
return output_path