Metadata-Version: 2.4
Name: ply_parser
Version: 3.0.0
Summary: A python module that parses ascii and binary PLY (.ply) (Polygon File Format) files. It includes a class PLYObject for storing PLY file data and a function parse_ply_file() for reading PLY files and creating PLYObject instances and can easily be customized.
Home-page: https://github.com/KrishnaMBhattarai/ply_parser
Author: Krishna Bhattarai
Author-email: Krishna Bhattarai <krishna@kbhattarai.com>
Project-URL: Homepage, https://github.com/KrishnaMBhattarai/ply_parser
Project-URL: Bug Tracker, https://github.com/KrishnaMBhattarai/ply_parser/issues
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Dynamic: author
Dynamic: home-page
Dynamic: requires-python

# ply_parser

**Read PLY (Polygon File Format) files in Python, with zero dependencies.**

`ply_parser` reads ASCII and binary PLY meshes into plain Python lists: vertices, faces, colours and normals. It follows the header, so unusual vertex layouts and extra elements are handled, and malformed files fail with a readable `ValueError` instead of silent garbage.

```bash
pip install ply_parser
```

Requires Python 3.8+.

## Quick start

```python
from ply_parser import parse_ply_file

obj = parse_ply_file("cube.ply")

print(obj.name)            # cube.ply
print(len(obj.vertices))   # 8
print(len(obj.faces))      # 12
print(obj.faces[0])        # [0, 1, 2]
```

## What you get

`parse_ply_file()` returns a `PLYObject` with:

| Attribute  | Contents                                                              |
| ---------- | --------------------------------------------------------------------- |
| `name`     | The file name, or the `name` you passed to `parse_ply()`              |
| `vertices` | `[x, y, z]` for every vertex                                          |
| `faces`    | Vertex indices for each face (any number of vertices per face)        |
| `colors`   | `[r, g, b]` per vertex, as stored in the file. Empty if none          |
| `normals`  | `[nx, ny, nz]` per vertex. Empty if none                              |

Colours keep their file values: a `uchar` colour channel is `0-255`.

## Features

- **ASCII, binary little-endian and big-endian** formats
- **Any header layout**: properties in any order, extra properties and extra elements are skipped
- **Polygons, not just triangles**: faces keep every vertex index
- **Colours and normals** picked up from `red/green/blue`, `r/g/b`, `diffuse_*`, `nx/ny/nz` or `normal_*` properties
- **Strict by default**: header and body must agree, indices must be in range, coordinates must be finite
- **Parse from memory** as well as from disk

## Parsing data you already have

```python
from ply_parser import parse_ply

with open("scan.ply", "rb") as f:
    obj = parse_ply(f.read(), name="scan")
```

`parse_ply()` takes `str` for ASCII data or `bytes` for any format.

For line-based input, `parse_ply_file_data()` returns the older `[vertices, faces, colors]` list:

```python
from ply_parser import parse_ply_file_data

vertices, faces, colors = parse_ply_file_data(lines)
```

## Handling bad files

```python
from ply_parser import parse_ply_file

try:
    obj = parse_ply_file("broken.ply")
except ValueError as err:
    print(err)  # e.g. "PLY: vertex index 9 out of range (only 8 vertices) in face 0"
```

## Development

```bash
git clone https://github.com/KrishnaMBhattarai/ply_parser
cd ply_parser
pip install .
python -m unittest tests/test_ply_parser.py -v
```

## Contributing

Issues and pull requests are welcome, especially sample PLY files that the parser gets wrong. Please include a small file that reproduces the problem.

## Licence

MIT. See [license.txt](license.txt).
