Python bindings for the Open Asset Import Library (ASSIMP) enabling 3D model loading, processing, and export
—
Core functionality for loading 3D models from files or memory and exporting scenes to various formats. PyAssimp supports over 30 file formats and provides flexible loading options with comprehensive post-processing capabilities.
Load 3D models from files on disk with optional post-processing.
def load(filename, file_type=None, processing=postprocess.aiProcess_Triangulate):
"""
Load 3D model from file path.
Parameters:
- filename: str, path to 3D model file
- file_type: str, optional file extension hint
- processing: int, post-processing flags (default: triangulate faces)
Returns:
Scene object containing loaded 3D data
Raises:
AssimpError: If file cannot be loaded or is invalid
"""Usage examples:
import pyassimp
from pyassimp.postprocess import aiProcess_Triangulate, aiProcess_GenNormals
# Basic loading with default triangulation
scene = pyassimp.load("model.obj")
# Loading with custom post-processing
scene = pyassimp.load("model.dae", processing=aiProcess_Triangulate | aiProcess_GenNormals)
# Loading with file type hint (optional)
scene = pyassimp.load("model.dat", file_type="obj")Load 3D models from file objects or memory buffers.
def load(filename, file_type=None, processing=postprocess.aiProcess_Triangulate):
"""
Load 3D model from file object.
Parameters:
- filename: file object with read() method
- file_type: str, required file extension when using file objects
- processing: int, post-processing flags
Returns:
Scene object containing loaded 3D data
Raises:
AssimpError: If file_type is None or file cannot be loaded
"""Usage examples:
import pyassimp
# Loading from file object
with open("model.obj", "rb") as f:
scene = pyassimp.load(f, file_type="obj")
# Loading from BytesIO
from io import BytesIO
data = BytesIO(model_bytes)
scene = pyassimp.load(data, file_type="stl")Export loaded or modified scenes to files in various formats.
def export(scene, filename, file_type=None, processing=postprocess.aiProcess_Triangulate):
"""
Export scene to file.
Parameters:
- scene: Scene object to export
- filename: str, output file path
- file_type: str, export format identifier (e.g., "obj", "dae", "stl")
- processing: int, post-processing flags applied during export
Raises:
AssimpError: If export fails or format is unsupported
"""Usage examples:
import pyassimp
# Load and export to different format
scene = pyassimp.load("input.dae")
pyassimp.export(scene, "output.obj", file_type="obj")
# Export with post-processing
pyassimp.export(scene, "output.stl", file_type="stl",
processing=pyassimp.postprocess.aiProcess_Triangulate)
# Clean up
pyassimp.release(scene)Export scenes to binary blobs in memory for further processing or network transmission.
def export_blob(scene, file_type=None, processing=postprocess.aiProcess_Triangulate):
"""
Export scene to binary blob.
Parameters:
- scene: Scene object to export
- file_type: str, export format identifier
- processing: int, post-processing flags
Returns:
Pointer to ExportDataBlob containing binary export data
Raises:
AssimpError: If export fails
"""Usage examples:
import pyassimp
# Export to memory blob
scene = pyassimp.load("model.obj")
blob_ptr = pyassimp.export_blob(scene, file_type="stl")
# The blob contains the binary data for the exported format
# Use with caution - this returns a ctypes pointerExplicit memory management for loaded scenes.
def release(scene):
"""
Release scene memory allocated by ASSIMP.
Parameters:
- scene: Scene object to release
Note:
Always call this when done with a scene to prevent memory leaks.
The scene object becomes invalid after calling release().
"""
def pythonize_assimp(type, obj, scene):
"""
Make ASSIMP data structures more Python-friendly.
Applies post-processing to convert ASSIMP structures to Python objects
with enhanced accessibility and cross-references.
Parameters:
- type: str, operation type ("MESH", "ADDTRANSFORMATION")
- obj: object to process
- scene: Scene object for context
Returns:
Processed object with Python-friendly structure
"""
def recur_pythonize(node, scene):
"""
Recursively apply pythonization to scene node hierarchy.
Traverses the scene graph and applies pythonize_assimp to all nodes
and their associated data structures.
Parameters:
- node: Node to process recursively
- scene: Scene object for context
"""Usage patterns:
import pyassimp
# Pattern 1: Try/finally
scene = pyassimp.load("model.obj")
try:
# Use scene data
process_scene(scene)
finally:
pyassimp.release(scene)
# Pattern 2: Context manager (custom)
class SceneManager:
def __init__(self, filename, **kwargs):
self.scene = pyassimp.load(filename, **kwargs)
def __enter__(self):
return self.scene
def __exit__(self, exc_type, exc_val, exc_tb):
pyassimp.release(self.scene)
# Usage
with SceneManager("model.obj") as scene:
process_scene(scene)PyAssimp supports loading and exporting numerous 3D file formats:
Common Formats:
CAD and Engineering:
Game Formats:
Others:
Query supported formats programmatically:
def available_formats():
"""
Get list of supported file formats.
Returns:
list: List of supported file format extensions (uppercase strings)
"""Usage example:
from pyassimp.formats import available_formats
# Get list of all supported formats
formats = available_formats()
print("Supported formats:", formats)
# Check if specific format is supported
if "OBJ" in formats:
print("OBJ format is supported")
# Print all formats
for format_name in formats:
print(f"- {format_name}")The complete list includes: CSM, LWS, B3D, COB, PLY, IFC, OFF, SMD, IRRMESH, 3D, DAE, MDL, HMP, TER, WRL, XML, NFF, AC, OBJ, 3DS, STL, IRR, Q3O, Q3D, MS3D, Q3S, ZGL, MD2, X, BLEND, XGL, MD5MESH, MAX, LXO, DXF, BVH, LWO, NDO
Common loading and export errors:
import pyassimp
try:
scene = pyassimp.load("nonexistent.obj")
except pyassimp.AssimpError as e:
print(f"Loading failed: {e}")
try:
# File object without file_type
with open("model.obj", "rb") as f:
scene = pyassimp.load(f) # Missing file_type parameter
except pyassimp.AssimpError as e:
print(f"File type required: {e}")
try:
scene = pyassimp.load("model.obj")
pyassimp.export(scene, "output.xyz", file_type="xyz") # Unsupported format
except pyassimp.AssimpError as e:
print(f"Export failed: {e}")
finally:
pyassimp.release(scene)release() to prevent memory leaks, especially in long-running applicationsInstall with Tessl CLI
npx tessl i tessl/pypi-pyassimp