Slow Load, Fast Load

The bulk of an animator’s time is spent waiting for the file to load[citation needed] because there have too many rigs in their scene, and/or the rigs are too heavy. They can use Selective Preload to only load the rigs they’re working on, and/or swap the rigs out for a cache (if available).

The bulk of an export process’ time is spent waiting for the file to load because there are too many rigs in the scene, and/or the rigs are too heavy. Since the export process is run in batch mode, either in the background or on the farm, it can’t use Selective Preload.

Or can it?

The cmds.file function has two kwargs – buildLoadSettings and loadSettings – that allow the caller to defer loading references. You could use this to write your own Selective Preload dialog that actually sorts the references by namespace, or to write your own selective preload function to run in batch mode.

The cmds.selLoadSettings function interacts with the reference tree generated in memory by cmds.file(path, load=True, buildLoadSettings=True). You can query the referenceNode, fileName, and/or proxy information associated with a reference. You can edit whether or not to defer loading the reference. Given this, it would be possible to have your export process just load the rig it is going to export.

Consider this simple example.

def preload(filepath, preload):
    """Load the given file; defer references not specified in 'preload'."""

    cmds.file(filepath, open=True, buildLoadSettings=True)

    # The first index is the the file itself, so we want to skip it        
    indexes = list(range(1, cmds.selLoadSettings(query=True, numSettings=True)))
    ref_nodes = cmds.selLoadSettings(*indexes, query=True, referenceNode=True)

    for idx, ref_node in zip(indexes, ref_nodes):
        cmds.selLoadSettings(idx, edit=True, deferReference=ref_node not in preload)
    
    cmds.file(filepath, open=True, force=True, loadSettings='implicitLoadSettings')

With the above function, you can reduce the time an export process spends on file IO from the time it takes to load all the rigs to the time it takes to load one or two, depending on the scene complexity. Remember that you need to include all references that affect the one in question. For example, if you’re exporting a prop, and a character is holding the prop, while riding a horse, you need to include all three of the references. Calculating the preload list while taking into account constraints is a topic for another blog post.

Leave a comment