You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maya 2023自定义Python脚本导入USD文件时无法导入动画数据的求助

Maya 2023自定义Python脚本导入USD文件时无法导入动画数据的求助

大家好,我目前正在为Maya 2023编写一个自定义Python脚本,用于导入包含动画的USD文件,但遇到了一个棘手的问题:脚本能成功导入USD的模型内容,却完全没有带进来动画数据。当我在Maya里手动切换「USD导入 > 动画 > 动画数据」选项时,脚本编辑器里只会输出以下内容:

mayaUsdTranslatorImport_AnimationCB;
mayaUsdTranslatorImport_AnimationCB;

不管我是开启还是关闭这个选项,反馈都是上面这两行,完全没有其他有效信息。

为了让脚本能正确导入动画,我已经尝试了多种方法来设置USD导入的动画选项,下面是我的完整代码:

import os
import maya.cmds as cmds
import maya.mel as mel

def import_usd_with_animation(usd_file):
    """Imports USD file with animation using MEL to ensure animation options are set."""
    if not usd_file or not os.path.exists(usd_file):
        cmds.warning("Invalid USD file path.")
        return False
    
    # Get file basename for namespace
    file_basename = os.path.basename(usd_file).split('.')[0]
    
    # Create a secure string representation of the file path for MEL
    usd_file_mel = usd_file.replace('\\', '/')
    
    # First set the option vars that control USD import behavior
    try:
        # Set required optionVars for animation import
        mel.eval('optionVar -iv "mayaUsdTranslatorImport_AnimationCB" 1;')
        mel.eval('optionVar -iv "mayaUsdTranslator_ReadAnimDataCB" 1;')
        
        # Verify options are set
        anim_cb = mel.eval('optionVar -q "mayaUsdTranslatorImport_AnimationCB";')
        read_anim_cb = mel.eval('optionVar -q "mayaUsdTranslator_ReadAnimDataCB";')
        print(f"Animation checkbox: {anim_cb}, Read Animation Data: {read_anim_cb}")
        
        # Method 1: Use MEL command to ensure animation is imported
        import_cmd = f'''
        // Set import options first
        optionVar -iv "mayaUsdTranslatorImport_AnimationCB" 1;
        optionVar -iv "mayaUsdTranslator_ReadAnimDataCB" 1;
        
        // Create options string with animation enabled
        string $options = "Animation=1;ReadAnimData=1";
        
        // Import the file with options
        file -import -type "USD Import" -options $options -namespace "{file_basename}" "{usd_file_mel}";
        '''
        
        print("\nExecuting import with animation...")
        mel.eval(import_cmd)
        print("USD file imported with animation.")
        
        # Check if import was successful by looking for nodes with the namespace
        imported_nodes = cmds.ls(f"{file_basename}:*")
        if imported_nodes:
            print(f"Found {len(imported_nodes)} imported nodes with namespace {file_basename}")
            return True
        else:
            print("No nodes found with expected namespace, trying alternative method...")
            
            # Method 2: Alternative approach - for Maya 2023 specifically
            # Try a different way to force animation import
            fallback_cmd = f'''
            // Set additional optionVars that might be used in Maya 2023
            optionVar -iv "usdReadAnimations" 1;
            optionVar -iv "mayaUsdImport_ReadAnimData" 1;
            
            // Import with specific options for Maya 2023
            file -import -type "USD Import" -options "Animation=1;ReadAnimData=1;ImportUSDZTextures=1;" -namespace "{file_basename}" "{usd_file_mel}";
            '''
            
            print("Trying fallback method for Maya 2023...")
            mel.eval(fallback_cmd)
            
            # Check again for imported nodes
            imported_nodes = cmds.ls(f"{file_basename}:*")
            if imported_nodes:
                print(f"Fallback method successful. Found {len(imported_nodes)} imported nodes.")
                return True
            else:
                cmds.warning("Import may have failed. No nodes with expected namespace found.")
                return False
            
    except Exception as e:
        cmds.warning(f"Error during USD import: {str(e)}")
        
        # Final fallback: Try using Python commands directly
        try:
            print("Attempting Python-based import as final fallback...")
            cmds.file(
                usd_file,
                i=True,
                type="USD Import",
                options="Animation=1;ReadAnimData=1",
                namespace=file_basename
            )
            return True
        except Exception as e2:
            cmds.warning(f"All import methods failed: {str(e2)}")
            
            # Show instructions for manual import
            cmds.confirmDialog(
                title='Manual USD Import Required',
                message='Automatic import failed. Please try manual import:\n'
                        '1. Go to File > Import...\n'
                        '2. Navigate to and select the USD file\n'
                        '3. Set File Type to "USD Import"\n'
                        '4. Click "Options"\n'
                        '5. Enable "Animation" and "Read Animation Data"\n'
                        '6. Click Import',
                button=['OK'],
                defaultButton='OK'
            )
            return False

我尝试过通过MEL设置相关的optionVar来开启动画导入选项,也试过直接用Python的cmds.file命令,但都没能成功把动画数据导入进来。有没有大佬能帮我排查下问题出在哪里?非常感谢!

备注:内容来源于stack exchange,提问作者Thomas Burton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 20:09:49