Init Repo

This commit is contained in:
2026-03-26 08:27:38 +08:00
commit 4eb266b623
12 changed files with 22763 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
"""
Mz Translator Core Module
.mz file format processing core module
"""
from .reader import (
read_scene_from_mz,
read_to_temp_file,
get_file_info
)
from .writer import (
write_scene_to_mz,
get_export_options
)
__all__ = [
'read_scene_from_mz',
'read_to_temp_file',
'get_file_info',
'write_scene_to_mz',
'get_export_options'
]
__version__ = '1.0.0'
File diff suppressed because it is too large Load Diff
+161
View File
@@ -0,0 +1,161 @@
# -*- coding: utf-8 -*-
"""
Mz Translator Reader Module
Read Maya scene from .mz file (ZIP compressed, contains data.ma)
"""
import os
import zipfile
import tempfile
import io
import maya.OpenMaya as OpenMaya
def read_scene_from_mz(file_path, buffer=None):
"""
Read Maya scene from .mz file
Args:
file_path: Input .mz file path
buffer: Optional buffer for direct data reading
Returns:
str: data.ma content (if using buffer), or temp file path
Raises:
Exception: Raised when read fails
"""
if not os.path.exists(file_path):
raise FileNotFoundError(f"MZ file not found: {file_path}")
try:
# Open ZIP file
with zipfile.ZipFile(file_path, 'r') as zf:
# Check if contains data.ma
if 'data.ma' not in zf.namelist():
raise ValueError("Invalid .mz file: no data.ma found")
# Read data.ma content
ma_content = zf.read('data.ma')
if buffer is not None:
# Write to provided buffer
if hasattr(buffer, 'write'):
buffer.write(ma_content)
else:
raise ValueError("buffer must be a writable file-like object")
return buffer
else:
# Return content string (for stream reading)
return ma_content
except zipfile.BadZipFile:
raise ValueError("Invalid .mz file: not a valid ZIP file")
except Exception as e:
raise RuntimeError(f"Failed to read .mz file: {str(e)}")
def read_to_temp_file(file_path):
"""
Read from .mz file and write to temp file
Args:
file_path: Input .mz file path
Returns:
str: Temp data.ma file path
Note:
Need to call cleanup_temp_file after use
"""
if not os.path.exists(file_path):
raise FileNotFoundError(f"MZ file not found: {file_path}")
# Create temp directory
temp_dir = tempfile.mkdtemp(prefix='mz_import_')
temp_ma_path = os.path.normpath(os.path.join(temp_dir, 'data.ma'))
try:
# Open ZIP file
with zipfile.ZipFile(file_path, 'r') as zf:
# Check if contains data.ma
if 'data.ma' not in zf.namelist():
raise ValueError("Invalid .mz file: no data.ma found")
# Read and write to temp file
ma_content = zf.read('data.ma')
with open(temp_ma_path, 'wb') as f:
f.write(ma_content)
return temp_ma_path
except Exception as e:
# Cleanup temp files
_cleanup_temp_dir(temp_dir)
raise RuntimeError(f"Failed to extract .mz file: {str(e)}")
def read_to_buffer(buffer):
"""
Read .mz file data.ma content to buffer
Args:
buffer: Writable file object (must have write method)
Returns:
int: Number of bytes read
Note:
buffer should be an IOBase or similar object for Maya's file command
"""
# This is a wrapper for supporting stream reading
# Will be called in MPxFileTranslator.reader()
pass
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_file_info(file_path):
"""
Get .mz file info
Args:
file_path: .mz file path
Returns:
dict: Dictionary containing file info
"""
if not os.path.exists(file_path):
raise FileNotFoundError(f"MZ file not found: {file_path}")
info = {
'file_size': os.path.getsize(file_path),
'files': []
}
try:
with zipfile.ZipFile(file_path, 'r') as zf:
for name in zf.namelist():
info['files'].append({
'name': name,
'size': zf.getinfo(name).file_size,
'compressed_size': zf.getinfo(name).compress_size
})
except Exception as e:
raise ValueError(f"Invalid .mz file: {str(e)}")
return info
File diff suppressed because it is too large Load Diff
+124
View File
@@ -0,0 +1,124 @@
# -*- 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'
}
}