[Python] Maya Python script to import USD wont import Animation Data

Discussão em 'Python' iniciado por Stack, Março 28, 2025.

  1. Stack

    Stack Membro Participativo

    I am currently creating a custom python script for maya 2023 that imports usd files that contain animation.

    The issue im having is my script is importing the contents of the usd without any animation present. When i toggle the function in maya the only script editor feedback I get is

    mayaUsdTranslatorImport_AnimationCB;
    mayaUsdTranslatorImport_AnimationCB;


    for both on and off

    Here is what I have tried so far to turn on the import USD > Animation > Animation Data settings.

    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


    Any help would be greatly appreciated! thank you!

    Continue reading...

Compartilhe esta Página