Fixing references before opening file

Hi everyone!
I’m trying to fix up broken references to the rig whenever a user opens an animation scene. I’ve tried using script jobs listening for a SceneOpened event, like this

cmds.scriptJob(event=["SceneOpened", "myReferenceFixer"])

That works if you skip through the broken reference dialogs, but I’d like to fix the target file before it’s opened and avoid hitting those broken references to begin with.

Hiya Back!

I found this post awhile back addressing this:

I couldn’t get it to work without some extra lines forcing a few things here and there either.

You’ll have to write supporting code on locating your missing references.

And I hope you have better luck than I did, once I spliced in my own function to search, and replace when one was missing, I couldn’t return back to the default behavior to prompt the user to locate it themselves. My maya api knowledge is practically nill so I couldn’t take this much further myself.

Head’s up, it says OldOpenMaya because I attempted this with 2.0 and had less luck.
PS sorry about bad formatting I’m awful in forums, somehow I can’t keep the initial indention in the code snippet.

import maya.OpenMaya as OldOpenMaya

def fixReferencePaths(retCode, file_object, clientData):
try:
    orig_path = file_object.fullName()
    ref_path = file_object.rawFullName().encode("ascii")
    if not os.path.exists(ref_path):
        # find path logic
        fnd_path = find_file(ref_path, search_root=get_project_path()) #find_file is a custom method
        fnd_path = convert_path_to_maya_path(fnd_path)
        if os.path.exists(fnd_path):
            file_object.setRawFullName(fnd_path)
            OldOpenMaya.MScriptUtil.setBool(retCode, True)
            print('Could not locate: {}\n Used: {}.'.format(ref_path, fnd_path))
            return
    else:
        file_object.fullName = orig_path
        OldOpenMaya.MScriptUtil.setBool(retCode, True)
        return
except:
    message = 'ERROR: Count not run fix reference paths'
    print(message)
    pm.warning(message)
    return

def enableFixReferences():
OldOpenMaya.MSceneMessage.addCheckFileCallback(OldOpenMaya.MSceneMessage.kBeforeCreateReferenceCheck,
                                               fixReferencePaths)

Thanks, I appreciate it but I do know how to change the file reference. I have a script that does this, but only after they hit the dialog:


That’s what I’m trying to avoid, though.

Sorry, maybe this wasn’t clear: I need to be able to execute a script when Maya opens a scene, but before it tries to load file references

Correct.

This is after Maya opens a scene, before it does the reference check.

The event you are trying to find is the kBeforeCreateReferenceCheck. And to my knowledge its only accessible via the API level scripting.

The only other thing I could think of, is write your own open file dialogue ui, so you can do a script level check with a ascii file parser before maya runs the file command.

Oh I see… Also looking closer at the post now that I have time. This is all very helpful! I’ll play with the callback for a bit.

Thank you!

1 Like

There is also the dirmap command, which temporarily remaps references or texture paths, avoiding the missing ref popup. Then once the scene loads, you should be able to finish running your code to replace the paths permanently.

https://help.autodesk.com/cloudhelp/2017/CHS/Maya-Tech-Docs/Commands/dirmap.html

1 Like