Python wrapper for meshoptimizer library
Maintained. Last release 9 months ago, commits in 20 of the last 26 weeks. Weaker on release cadence and contributor spread.
pip install meshoptimizerMeshoptimizer Python
Python bindings for the meshoptimizer library, which provides algorithms for optimizing 3D meshes for GPU rendering.
Installation
pip install meshoptimizer
Or install from source:
git clone https://github.com/zeux/meshoptimizer.git
cd meshoptimizer/python
pip install -e .
Features
- Vertex cache optimization
- Overdraw optimization
- Vertex fetch optimization
- Mesh simplification
- Vertex/index buffer compression and decompression
- Zip file storage for encoded meshes
- Numpy array compression and storage
- And more...
Usage
Low-level API
The meshoptimizer Python bindings provide a low-level API that directly maps to the C++ functions. All functions accept numpy arrays and have optional parameters that are automatically derived when not provided.
import numpy as np
from meshoptimizer import (
optimize_vertex_cache,
optimize_overdraw,
optimize_vertex_fetch,
simplify,
encode_vertex_buffer,
decode_vertex_buffer,
encode_index_buffer,
decode_index_buffer
)
# Create a simple mesh (a cube)
vertices = np.array([
# positions
[-0.5, -0.5, -0.5],
[0.5, -0.5, -0.5],
[0.5, 0.5, -0.5],
[-0.5, 0.5, -0.5],
[-0.5, -0.5, 0.5],
[0.5, -0.5, 0.5],
[0.5, 0.5, 0.5],
[-0.5, 0.5, 0.5]
], dtype=np.float32)
indices = np.array([
0, 1, 2, 2, 3, 0, # front
1, 5, 6, 6, 2, 1, # right
5, 4, 7, 7, 6, 5, # back
4, 0, 3, 3, 7, 4, # left
3, 2, 6, 6, 7, 3, # top
4, 5, 1, 1, 0, 4 # bottom
], dtype=np.uint32)
# Optimize vertex cache
optimized_indices = np.zeros_like(indices)
optimize_vertex_cache(optimized_indices, indices) # vertex_count is automatically derived
# Optimize overdraw
optimized_indices2 = np.zeros_like(indices)
optimize_overdraw(
optimized_indices2,
optimized_indices,
vertices
) # index_count, vertex_count, and vertex_positions_stride are automatically derived
# Optimize vertex fetch
optimized_vertices = np.zeros_like(vertices)
unique_vertex_count = optimize_vertex_fetch(
optimized_vertices,
optimized_indices2,
vertices
) # index_count, vertex_count, and vertex_size are automatically derived
print(f"Optimized mesh has {unique_vertex_count} unique vertices")
# Simplify the mesh
simplified_indices = np.zeros(len(indices), dtype=np.uint32)
target_index_count = len(indices) // 2 # Keep 50% of triangles
simplified_index_count = simplify(
simplified_indices,
optimized_indices2,
vertices,
target_index_count=target_index_count
) # index_count, vertex_count, and vertex_positions_stride are automatically derived
print(f"Simplified mesh has {simplified_index_count} indices")
# Encode the mesh for efficient transmission
encoded_vertices = encode_vertex_buffer(optimized_vertices[:unique_vertex_count])
encoded_indices = encode_index_buffer(simplified_indices[:simplified_index_count])
print(f"Encoded vertex buffer size: {len(encoded_vertices)} bytes")
print(f"Encoded index buffer size: {len(encoded_indices)} bytes")
# Decode the mesh
decoded_vertices = decode_vertex_buffer(
unique_vertex_count,
vertices.itemsize * vertices.shape[1],
encoded_vertices
)
decoded_indices = decode_index_buffer(
simplified_index_count,
4, # 4 bytes per index (uint32)
encoded_indices
)
# Verify the decoded data
print(f"Decoded vertices shape: {decoded_vertices.shape}")
print(f"Decoded indices shape: {decoded_indices.shape}")