[Python] Assigning audio by similar name in Maya's sequencer

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    In the sequencer of Maya I want to select a shot or more and with a dialogue choose where the audio is and assign it by name(each audio and shot have the same name) using python. This code imports audio but when playing the shots there is no audio. If I assign manually the audio it works, so no playback issues

    import maya.cmds as cmds
    import os

    def assign_audio_to_shots():
    # select the directory containing the audio files
    audio_directory = cmds.fileDialog2(dialogStyle=2, fileMode=3, caption="Select Audio Directory")

    if not audio_directory:
    cmds.warning("No directory selected. Exiting.")
    return

    audio_directory = os.path.abspath(audio_directory[0])

    # shots in the sequencer
    shots = cmds.ls(type="shot")

    if not shots:
    cmds.warning("No shots found in the sequencer.")
    return

    print("\n=== Assigning Audio to Shots ===")

    for shot in shots:

    shot_name = cmds.getAttr(f"{shot}.shotName")

    if not shot_name:
    cmds.warning(f"Shot '{shot}' has no valid name. Skipping.")
    continue


    audio_file = os.path.join(audio_directory, f"{shot_name}.wav")


    if not os.path.exists(audio_file):
    cmds.warning(f"Audio file not found for shot '{shot_name}': {audio_file}")
    continue


    existing_audio = cmds.shot(shot, query=True, audio=True)
    if existing_audio:
    cmds.delete(existing_audio)


    sound_node = cmds.createNode("audio", name=f"{shot_name}_audio")
    cmds.setAttr(f"{sound_node}.filename", audio_file, type="string")


    cmds.shot(shot, edit=True, audio=sound_node)


    cmds.refresh()

    print(f"✔ Assigned audio '{audio_file}' to shot '{shot_name}'")

    # Run the function
    assign_audio_to_shots()

    Continue reading...

Compartilhe esta Página