# -*- coding: utf-8 -*- """ Mz Translator Writer Module Write Maya scene to .mz file format (ZIP compressed, contains data.ma) """ import os import tempfile import zipfile import maya.OpenMaya as OpenMaya # import maya.OpenMayaAnim as OpenMayaAnim def write_scene_to_mz(file_path, options=None): """ Write current Maya scene to .mz file Args: file_path: Output .mz file path options: Export options dictionary Returns: bool: True if successful Raises: Exception: Raised when write fails """ if options is None: options = {} # Get compression level (default is maximum compression) compression_level = options.get('compression', 9) # Create temp directory for data.ma temp_dir = tempfile.mkdtemp(prefix='mz_export_') try: # Temp data.ma file path temp_ma_path = os.path.join(temp_dir, 'data.ma') # Use MFileIO to export as Maya ASCII format if not _export_to_ma(temp_ma_path): raise RuntimeError("Failed to export scene to .ma format") # Create .mz (ZIP) file with zipfile.ZipFile(file_path, 'w', zipfile.ZIP_DEFLATED, compresslevel=compression_level) as zf: # Add data.ma to ZIP zf.write(temp_ma_path, 'data.ma') return True except Exception as e: raise RuntimeError(f"Failed to write .mz file: {str(e)}") finally: # Cleanup temp files _cleanup_temp_dir(temp_dir) def _export_to_ma(file_path): """ Export current scene to Maya ASCII (.ma) format Args: file_path: Output .ma file path Returns: bool: True if successful """ try: # Get current scene name scene_name = OpenMaya.MFileIO.currentFile() # Use MFileIO to export scene # Note: Use file command to export as ASCII format import maya.cmds as cmds # Export current scene as MA format cmds.file(file_path, exportAll=True, type='mayaAscii', force=True) return True except Exception as e: print(f"Export to MA failed: {str(e)}") return False def _cleanup_temp_dir(temp_dir): """ Cleanup temp directory Args: temp_dir: Temp directory path to delete """ import shutil try: if os.path.exists(temp_dir): shutil.rmtree(temp_dir) except Exception: pass def get_export_options(): """ Get export options definition Returns: dict: Options dictionary """ return { 'compression': { 'type': 'int', 'default': 9, 'min': 0, 'max': 9, 'description': 'Compression level (0-9)' }, 'version': { 'type': 'string', 'default': '1.0', 'description': 'MZ format version' } }