PyObjC ModelIO Framework

12.1 · active · verified Tue Apr 14

pyobjc-framework-modelio provides Python bindings for Apple's ModelIO framework, enabling macOS applications to work with 3D assets, meshes, materials, and other related data. As part of the larger PyObjC project, it typically releases new versions to align with new macOS SDKs, with version 12.1 being the current stable release.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple 3D cube mesh using ModelIO, add it to an MDLAsset, and then export it to an OBJ file. This code is macOS-specific due to its reliance on Apple's ModelIO framework.

import os
from ModelIO import MDLAsset, MDLMesh, MDLMaterial
from Foundation import NSURL

# Note: ModelIO is a macOS-only framework for 3D asset manipulation.
# This code will only run on macOS.

def create_simple_asset():
    # Create a simple mesh (e.g., a cube)
    # newBoxWithDimensions_segments_geometryType_inwardNormals_allocator_ is a class method
    cube_mesh = MDLMesh.newBoxWithDimensions_segments_geometryType_inwardNormals_allocator_(
        (1.0, 1.0, 1.0),    # Dimensions (width, height, depth)
        (1, 1, 1),          # Number of segments along each dimension
        1,                  # MDLGeometryTypeTriangles = 1
        False,              # Inward normals
        None                # Allocator (None for default)
    )

    # Create a basic material
    material = MDLMaterial.alloc().initWithName_scatteringFunction_("MyMaterial", None)

    # Add the material to the mesh
    cube_mesh.addMaterial_(material)

    # Create an asset and add the mesh to it
    asset = MDLAsset.alloc().init()
    asset.addObjects_([cube_mesh])

    # Define a temporary path to save the asset
    temp_dir = '/tmp'
    if not os.path.exists(temp_dir):
        os.makedirs(temp_dir)
    output_path = os.path.join(temp_dir, "simple_cube.obj")
    output_url = NSURL.fileURLWithPath_(output_path)

    # Export the asset to a file (e.g., OBJ format)
    # ModelIO supports various formats, but OBJ is simple.
    # exportAssetToURL_ is an instance method
    asset.exportAssetToURL_(output_url)

    print(f"Successfully created and saved a simple 3D asset to: {output_path}")

if __name__ == "__main__":
    create_simple_asset()

view raw JSON →