Export all Maya render layers as ASS files using Python

You would think exporting all render layers in Maya to Arnold Scene Source (.ass) files could be done without much hassle. So did I. To be fair, any confusion over this matter is a result of Mayas new render setup introduced a few years ago. At the time of this writing, Arnold can only export geometry and settings from the currently active render layer. To conveniently switch render layers for export while maintaining the current scene state for the artist, we can write ourselves a small context manager.

import contextlib
import maya.app.renderSetup.model.renderSetup as renderSetup

def maintained_render_layer():
    previous_rs = renderSetup.instance().getVisibleRenderLayer()

Now we can use another code snippet to conveniently export all render layers to ASS files.

import maya.app.renderSetup.model.renderSetup as renderSetup
import pymel.core as pm

export_options = {} # supply your own arnoldExportAss options here         
with maintained_render_layer():
    for layer in renderSetup.instance().getRenderLayers():
        layer_name, layer_obj = render_layer
        print("Run ASS Export for render layer "
              "'{}' with options: '{}'".format(layer.name(), export_options))

Check out this thread for available arnoldExportAss options.