|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script to render LAMMPS dump files to PNG images using OVITO |
| 4 | +""" |
| 5 | + |
| 6 | +import sys |
| 7 | +import os |
| 8 | +from pathlib import Path |
| 9 | +from ovito.io import import_file |
| 10 | +from ovito.vis import Viewport, OSPRayRenderer |
| 11 | + |
| 12 | + |
| 13 | +def render_dump_to_images(dump_file, output_dir=None, width=1920, height=1080, frame_step=1): |
| 14 | + """ |
| 15 | + Load a LAMMPS dump file and render frames to PNG images |
| 16 | + |
| 17 | + Parameters: |
| 18 | + ----------- |
| 19 | + dump_file : str |
| 20 | + Path to the dump file |
| 21 | + output_dir : str, optional |
| 22 | + Output directory for images. If None, creates 'frames' subdirectory |
| 23 | + width : int |
| 24 | + Image width in pixels |
| 25 | + height : int |
| 26 | + Image height in pixels |
| 27 | + frame_step : int |
| 28 | + Render every Nth frame (default: 1 = all frames) |
| 29 | + """ |
| 30 | + |
| 31 | + # Import the dump file |
| 32 | + print(f"Loading dump file: {dump_file}") |
| 33 | + pipeline = import_file(dump_file) |
| 34 | + |
| 35 | + # Add pipeline to scene so it will be rendered |
| 36 | + pipeline.add_to_scene() |
| 37 | + |
| 38 | + # Generate output directory if not provided |
| 39 | + if output_dir is None: |
| 40 | + base_name = Path(dump_file).stem |
| 41 | + output_dir = f"{base_name}_frames" |
| 42 | + |
| 43 | + # Create output directory |
| 44 | + os.makedirs(output_dir, exist_ok=True) |
| 45 | + |
| 46 | + print(f"Output directory: {output_dir}") |
| 47 | + print(f"Number of frames: {pipeline.source.num_frames}") |
| 48 | + print(f"Rendering every {frame_step} frame(s)...") |
| 49 | + |
| 50 | + # Set up viewport for rendering - Left view (XZ plane) |
| 51 | + vp = Viewport() |
| 52 | + vp.type = Viewport.Type.Ortho # Use orthographic projection |
| 53 | + vp.camera_pos = (0, 100, 0) # Position camera along Y-axis |
| 54 | + vp.camera_dir = (0, -1, 0) # Look towards the structure (negative Y) |
| 55 | + vp.fov = 60.0 |
| 56 | + |
| 57 | + # Zoom to fit the scene |
| 58 | + vp.zoom_all() |
| 59 | + |
| 60 | + # Use OSPRay renderer (software-based, works without display) |
| 61 | + renderer = OSPRayRenderer() |
| 62 | + |
| 63 | + # Render frames |
| 64 | + frames_to_render = list(range(0, pipeline.source.num_frames, frame_step)) |
| 65 | + for i, frame_num in enumerate(frames_to_render): |
| 66 | + output_file = os.path.join(output_dir, f"frame_{frame_num:05d}.png") |
| 67 | + |
| 68 | + try: |
| 69 | + vp.render_image( |
| 70 | + filename=output_file, |
| 71 | + size=(width, height), |
| 72 | + frame=frame_num, |
| 73 | + renderer=renderer, |
| 74 | + background=(1, 1, 1), # White background |
| 75 | + alpha=False |
| 76 | + ) |
| 77 | + |
| 78 | + if (i + 1) % 10 == 0 or i == len(frames_to_render) - 1: |
| 79 | + print(f" Rendered {i + 1}/{len(frames_to_render)} frames") |
| 80 | + |
| 81 | + except Exception as e: |
| 82 | + print(f" Error rendering frame {frame_num}: {e}") |
| 83 | + continue |
| 84 | + |
| 85 | + print(f"\nDone! Images saved to: {output_dir}/") |
| 86 | + return output_dir |
| 87 | + |
| 88 | + |
| 89 | +def main(): |
| 90 | + """Main function to handle command line arguments""" |
| 91 | + |
| 92 | + if len(sys.argv) < 2: |
| 93 | + print("Usage: python render_frames.py <dump_file> [output_dir] [width] [height] [frame_step]") |
| 94 | + print("\nExamples:") |
| 95 | + print(" python render_frames.py bend-L10-s50-b3-l1.dump") |
| 96 | + print(" python render_frames.py 10/bend-L10-s50-b3-l1.dump frames_output") |
| 97 | + print(" python render_frames.py dump.dump frames_output 1280 720 5") |
| 98 | + print("\nParameters:") |
| 99 | + print(" frame_step: Render every Nth frame (e.g., 5 = every 5th frame)") |
| 100 | + sys.exit(1) |
| 101 | + |
| 102 | + dump_file = sys.argv[1] |
| 103 | + output_dir = sys.argv[2] if len(sys.argv) > 2 else None |
| 104 | + width = int(sys.argv[3]) if len(sys.argv) > 3 else 1280 |
| 105 | + height = int(sys.argv[4]) if len(sys.argv) > 4 else 720 |
| 106 | + frame_step = int(sys.argv[5]) if len(sys.argv) > 5 else 1 |
| 107 | + |
| 108 | + # Check if dump file exists |
| 109 | + if not Path(dump_file).exists(): |
| 110 | + print(f"Error: Dump file not found: {dump_file}") |
| 111 | + sys.exit(1) |
| 112 | + |
| 113 | + render_dump_to_images(dump_file, output_dir, width, height, frame_step) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + main() |
0 commit comments