DracoPy - Draco Mesh Compression Wrapper
DracoPy is a Python wrapper for Google's Draco Mesh Compression Library, enabling efficient encoding and decoding of 3D mesh data. It supports common formats like OBJ and PLY (via in-memory mesh objects). The current version is 2.0.0, with releases occurring infrequently, often coinciding with upstream Draco library updates or significant API overhauls.
Common errors
-
TypeError: expected ndarray, got list
cause Attempting to create a `DracoMesh` or set its attributes with Python lists instead of NumPy arrays after upgrading to version 2.0.0.fixConvert your mesh data (vertices, faces, etc.) to NumPy arrays before passing them to `DracoMesh` or `encode`/`decode`. E.g., `vertices=np.array(my_vertex_list, dtype=np.float32)`. -
AttributeError: module 'dracopy' has no attribute 'decode_file'
cause Using the `decode_file` or `encode_file` helper functions, which were removed in version 2.0.0.fixManually handle file reading/writing. Read the file into bytes, then pass to `dracopy.decode()`. Write the bytes returned by `dracopy.encode()` to a file. See warnings for an example. -
ImportError: DLL load failed while importing _dracopy (Windows) / ImportError: ... undefined symbol: ... (Linux)
cause The underlying C++ Draco library failed to load or compile correctly, often due to missing system dependencies, incompatible architecture, or a broken wheel installation.fixTry reinstalling `dracopy`. If issues persist, ensure your system has C++ build tools if wheels aren't used. Check `pip` output for specific errors. For Linux, ensure `libstdc++` is up to date. Consider creating a new virtual environment. -
ValueError: Missing required mesh data (vertices and faces)
cause `DracoMesh` requires at least `vertices` and `faces` to be provided when initialized.fixEnsure both `vertices` and `faces` NumPy arrays are passed to the `DracoMesh` constructor. E.g., `dracopy.DracoMesh(vertices=my_verts_array, faces=my_faces_array)`.
Warnings
- breaking `DracoMesh` attributes (`vertices`, `faces`, `normals`, `colors`, `uvs`) now strictly require NumPy arrays instead of Python lists.
- breaking The helper functions `dracopy.decode_file` and `dracopy.encode_file` have been removed.
- gotcha Installation can fail if pre-compiled wheels are not available for your specific Python/OS/architecture combination, requiring local compilation.
- deprecated The `vertex_features` and `face_features` parameters in the `DracoMesh` constructor have been removed.
Install
-
pip install dracopy
Imports
- encode
import dracopy.encode
from dracopy import encode
- decode
import dracopy.decode
from dracopy import decode
- DracoMesh
import dracopy.DracoMesh
from dracopy import DracoMesh
Quickstart
import dracopy
import numpy as np
# 1. Create a dummy mesh (a simple triangle)
# Vertices and faces must be NumPy arrays in DracoPy 2.0.0+
vertices = np.array([
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0]
], dtype=np.float32)
faces = np.array([
[0, 1, 2]
], dtype=np.int32)
# Create a DracoMesh object
mesh_to_encode = dracopy.DracoMesh(vertices=vertices, faces=faces)
# 2. Encode the mesh to Draco bytes
print("Encoding mesh...")
draco_bytes = dracopy.encode(mesh_to_encode, compression_level=7)
print(f"Encoded bytes length: {len(draco_bytes)}")
# 3. Decode the Draco bytes back to a DracoMesh
print("Decoding mesh...")
decoded_mesh = dracopy.decode(draco_bytes)
# 4. Access decoded data (still NumPy arrays)
print(f"Decoded vertices shape: {decoded_mesh.vertices.shape}")
print(f"Decoded faces shape: {decoded_mesh.faces.shape}")
# Optional: Verify data equality
assert np.array_equal(vertices, decoded_mesh.vertices)
assert np.array_equal(faces, decoded_mesh.faces)
print("Encoding and decoding successful!")