如何在Maya中使用Python实现同帧范围摄影机的多镜头文件规范命名导出
Alright, let's tackle this Maya camera export task with Python. I'll walk you through a practical solution that matches your naming rules and handles multiple shot ranges per camera. Here's how to make it work:
Core Approach
First, let's clarify the key requirements we need to cover:
- Identify target cameras in the Maya scene
- Handle multiple shot time ranges per camera
- Generate filenames following your specified format (
camName_shotName_start-end.fbx) - Export each camera-shot combination as a separate FBX file with the correct timeline range
Full Implementation Code
Here's a ready-to-use script that you can adapt to your scene:
import maya.cmds as cmds import os # -------------------------- # Configuration - Update this section for your scene # -------------------------- # Define cameras and their associated shot ranges: {camera_name: [(shot_name, start_frame, end_frame), ...]} camera_shot_ranges = { "cam1": [("shot1", 10, 35), ("shot2", 10, 35)], "cam2": [("shot1", 34, 64), ("shot2", 34, 64)] } # Set your target export folder (replace with your own path) export_directory = "C:/Maya_Exported_Shots" # -------------------------- # Export Logic # -------------------------- # Create export folder if it doesn't exist if not os.path.exists(export_directory): os.makedirs(export_directory) print(f"Created export directory: {export_directory}") # Iterate over each camera and its shots for camera_name, shot_list in camera_shot_ranges.items(): # Skip if the camera doesn't exist in the scene if not cmds.objExists(camera_name): print(f"⚠️ Warning: Camera '{camera_name}' not found in scene - skipping.") continue # Select the camera to ensure we export only its data (adjust if you need to include other objects) cmds.select(camera_name, replace=True) # Process each shot range for the camera for shot_name, start_frame, end_frame in shot_list: # Validate frame range (prevent invalid exports) if start_frame >= end_frame: print(f"⚠️ Invalid frame range for {camera_name}_{shot_name}: {start_frame}-{end_frame} - skipping.") continue # Generate the formatted filename output_filename = f"{camera_name}_{shot_name}_{start_frame}-{end_frame}.fbx" full_export_path = os.path.join(export_directory, output_filename) # Configure FBX export settings for animation and frame range cmds.FBXExportAnimationOnly(True) # Export only animation data cmds.FBXExportFrameRange(min=start_frame, max=end_frame) # Set the shot's timeline range # Execute the export try: cmds.FBXExport(file=full_export_path, selected=True) print(f"✅ Successfully exported: {full_export_path}") except Exception as export_error: print(f"❌ Failed to export {full_export_path}: {str(export_error)}")
Key Details & Customization Tips
Let's break down what the script does and how you can tweak it:
Configuration Section:
- The
camera_shot_rangesdictionary lets you map each camera to its shot ranges. Update this to match your scene's cameras and shot timings. - Set
export_directoryto your preferred output folder. The script will create this folder if it doesn't exist.
- The
Validation Checks:
- The script checks if the camera exists in the scene to avoid errors.
- It validates that the start frame is less than the end frame to skip invalid shot ranges.
FBX Export Settings:
FBXExportAnimationOnly(True)ensures we only export the camera's animation (not static geometry, unless you modify the selection).FBXExportFrameRangesets the exact timeline range for each shot.selected=Truetells Maya to export only the currently selected object (the camera) — if you need to include other assets (like a character or props), add them to the selection before exporting.
Naming Logic:
- The filename is built using an f-string that strictly follows your requested format:
{camera_name}_{shot_name}_{start_frame}-{end_frame}.fbx.
- The filename is built using an f-string that strictly follows your requested format:
Optional Enhancements
If you want to take this further, here are some useful additions:
- Auto-Detect Shot Ranges: Instead of manually defining
camera_shot_ranges, you can read Maya's timeline markers to automatically pull shot names and frame ranges. - Batch Export Multiple Objects: Modify the selection logic to include groups of objects (e.g., a camera + a linked character rig) for each shot.
- Log File: Write export success/failure details to a text log file for easier tracking of large batches.
内容的提问来源于stack exchange,提问作者mayjackass




