pyrndr 0.1.2
Platform-agnostic Python mesh renderer with minimum features
License: MIT

# PyRNDR: Trimesh-based Lightweight Python Mesh Renderer

A lightweight, single-file, platform-independent Python module for quick 2D rendering of meshes. PyRNDR leverages the mesh manipulation API of **[Trimesh](https://github.com/mikedh/trimesh)** for rendering, avoiding the need for OpenGL. This simplifies setup and ensures smooth operation across platforms without platform-specific installation steps. It is designed for fast prototyping tasks such as checking camera poses, intrinsic matrices, and mesh positioning.

## Installation

To install the module, use:

```bash
pip install pyrndr
```

**Tested with**: Python 3.10

## Quick Demo

A working demo is available under [`scripts/demo.py`](scripts/demo.py). Below is a preview of the outputs generated by PyRNDR:

### Output

After running the demo, the rendered images will be saved in the `render_outputs` directory. The table below shows sample outputs:

| File Name               | Description              | Preview                               |
|-------------------------|--------------------------|---------------------------------------|
| `color_image.png`       | Color image              | ![Color Image](https://github.com/ykkawana/pyrndr/blob/main/assets/sample_outputs/color_image.png?raw=true) |
| `depth.png`             | Depth map of the mesh    | ![Depth](https://github.com/ykkawana/pyrndr/blob/main/assets/sample_outputs/depth.png?raw=true) |
| `id_image.png`          | Image with object IDs    | ![ID Image](https://github.com/ykkawana/pyrndr/blob/main/assets/sample_outputs/id_image.png?raw=true) |
| `normal_image.png`      | Surface normals image    | ![Normal Image](https://github.com/ykkawana/pyrndr/blob/main/assets/sample_outputs/normal_image.png?raw=true) |
| `coord_image.png`       | Image with coordinates   | ![Coord Image](https://github.com/ykkawana/pyrndr/blob/main/assets/sample_outputs/coord_image.png?raw=true) |

### Minimal Code Example

Below is a minimal example demonstrating how to render a 3D mesh with PyRNDR:

```python
import numpy as np
import trimesh

size = 256
image_size = (size,) * 2

# Define the intrinsic camera matrix
intrinsic_matrix = np.eye(3)
intrinsic_matrix[[0, 1, 0, 1], [0, 1, 2, 2]] = size / 2

# Define the camera pose
camera_pose = np.eye(4)
camera_pose[[0, 2], [0, 2]] = -1
camera_pose[:3, 3] = np.array([0, 0, -2])

# Load the mesh
mesh = trimesh.load('./assets/obj/spot.obj')

# Render the mesh to obtain color and depth images
image, depth = pyrndr.render(
    mesh=mesh, image_size=image_size,
    intrinsic_matrix=intrinsic_matrix,
    camera_pose=camera_pose,
)
```

### Features

- **Lightweight Codebase:** The entire module is contained in a single file, with minimal dependencies.
- **Platform-Agnostic:** Built with Trimesh, it avoids platform-specific installations such as OpenGL, simplifying setup and ensuring smooth operation across different systems.
- **Basic Rendering:** Suitable for quickly testing 2D rendering, camera poses, and intrinsic matrices.

### Limitations

- **Performance:** PyRNDR renders slower compared to OpenGL-based renderers.
- **Material Support:** Complex materials are not supported; the same material is used for all objects, with basic shading applied using directional light.
- **Rendering Quality:** Not intended for high-quality or photorealistic rendering.

## Contributing

Contributions are welcome. Feel free to open an issue or submit a pull request.