import json import os import subprocess import shutil import argparse def update_robot_json(robot_json_file, camera_position, default_position): """Aktualisiert die cameraPosition und defaultPosition in der robot.json-Datei.""" try: with open(robot_json_file, 'r') as f: data = json.load(f) data['renderingInfo']['cameraPosition'] = camera_position data['renderingInfo']['defaultPosition'] = default_position with open(robot_json_file, 'w') as f: json.dump(data, f, indent=2) except FileNotFoundError: print(f"Fehler: Datei {robot_json_file} nicht gefunden.") return False except json.JSONDecodeError: print(f"Fehler: JSON-Datei {robot_json_file} ist ungültig.") return False return True def run_blender(blender_executable, script_path, log_level): """Führt Blender mit dem angegebenen Skript aus.""" try: command = [ blender_executable, "-b", "--python", script_path, "--log-level", str(log_level) ] subprocess.run(command, check=True, capture_output=True, text=True) return True except subprocess.CalledProcessError as e: print(f"Blender-Skript fehlgeschlagen:\n{e.stderr}") return False def copy_and_rename_file(source_file, destination_dir, new_filename): """Kopiert die erstellte Bilddatei in den Zielordner und benennt sie um.""" destination_path = os.path.join(destination_dir, new_filename) try: shutil.copy2(source_file, destination_path) # copy2 behält Metadaten return True except FileNotFoundError: print(f"Fehler: Quelldatei {source_file} nicht gefunden.") return False except Exception as e: print(f"Fehler beim Kopieren/Umbenennen der Datei: {e}") return False def main(): parser = argparse.ArgumentParser(description="Automatisiert die Roboter-Rendering-Pipeline.") parser.add_argument("robot_json", help="Pfad zur robot.json-Datei.") parser.add_argument("blender_executable", help="Pfad zur Blender-Executable.") parser.add_argument("render_script", help="Pfad zum render_robot.py-Skript.") parser.add_argument("output_dir", help="Zielordner für die gerenderten Bilder.") parser.add_argument("--log_level", type=int, default=2, help="Log-Level für Blender (Standard: 2).") args = parser.parse_args() # Kamerapositions-Dictionary camera_positions = { "a": [-300, -800, 500], "b": [300, -700, 500], "c": [600, -500, 600], "d": [-10, -800, 500], "e": [-500, 300, 1200], "f": [1200, 200, 300] } # Robot-Pose-Dictionary robot_poses = { "3": {"x": 10, "y": 4, "z": 20, "a": 10, "b": 2, "c": 9, "e": 1}, "4": {"x": 40, "y": 48, "z": -30, "a": 30, "b": 23, "c": 9, "e": 8}, "5": {"x": 80, "y": 93, "z": -120, "a": 120, "b": 23, "c": 9, "e": 3}, "6": {"x": 80, "y": 20, "z": 80, "a": -120, "b": 23, "c": 9, "e": 3} } for set_name, camera_position in camera_positions.items(): for pose_name, default_position in robot_poses.items(): # 1. JSON aktualisieren if not update_robot_json(args.robot_json, camera_position, default_position): continue # Gehe zum nächsten Schleifendurchlauf # 2. Blender-Skript ausführen if not run_blender(args.blender_executable, args.render_script, args.log_level): continue # Gehe zum nächsten Schleifendurchlauf # 3. Datei kopieren und umbenennen render_script_dir = os.path.dirname(args.render_script) render_file = os.path.join(render_script_dir, "render.png") new_filename = f"render_set{set_name}_{pose_name}.png" if not copy_and_rename_file(render_file, args.output_dir, new_filename): continue # Gehe zum nächsten Schleifendurchlauf print(f"Rendering für Set {set_name}, Pose {pose_name} erfolgreich abgeschlossen.") if __name__ == "__main__": main()