Remapping the Reference File to a new Directory on load

Hello, sorry to bother everyone here with what might be a simple problem but I’m looking to go through several Maya files and change the reference file path on load without having this pop up every time.

I’m looking to make a python script in Maya that will batch out a bunch of files within a folder that has the exact same missing path issue. The batching part I can do but the action of remapping the path on load has me at a wall. If anyone can help me with this issue I’d be very thankful.

1 Like

Hello!

If you save your Maya files as ASCII (.ma instead of binary .mb) then you can just open them up in the batch as if they were text files, find the reference path you want to replace and replace it with a new string with the new reference path. You just need to be careful to not edit anything else than the string path. This way you avoid opening even Maya so the batch is very quick, I did this in the past and it’s just a few seconds even with hundreds of files.

Also, I’d search for the current reference path in all the lines of the .ma files just in case it appears multiple times, you need to replace all of them.

Hope that helps :slight_smile:

1 Like

The simplest way is only possible if your files are .ma as mentioned. You just batch open the file as text and replace the paths you need.
That said, for security purposes its generally not recommended to use .ma anymore.
For .mb you’ll need to register a callback to change the filepath before the reference is loaded using OpenMaya.MSceneMessage.addCheckFileCallback(OpenMaya.MSceneMessage.kBeforeLoadReferenceCheck, ...)
You register your own command to take the file object to be referenced, remap the paths and then continue with the reference and re-save the file.

E: saving

3 Likes

I didn’t know about this callback on .mb files, thanks! Are the security reasons related to things like the vaccine scriptJob case?

Interesting. I am using .ma files in this case so opening them up as if they were text files certainly is an option. I assume that can be done in Maya like most things or was that a tool you made outside of Maya?

The callbacks are just in Maya’s api (OpenMaya.MSceneMessage) and .mb doesnt contain the callback, its just necessary to do it with Maya unlike .ma.
The steps would be;
Start Maya.
Register Reference path swap callback.
Batch open and save files.

The callback deals with the paths so you just need to open then save the file.

Regarding the security issues for .ma, its just that the vaccine scriptjob is one of many potential issues.

1 Like

Can do it in Maya script editor, or in native python. No special version of libraries required. .ma files can be read and modified as you would any text file with python.

1 Like

Thanks a bunch guys! I’ll be trying these solutions out today then.

1 Like

Sorry for the late reply, I managed to get the tool to work the way I needed it to. I’ll post the work here in case someone else in the future has the same issue.

your_folder_path = r'D:\Test'

old_path = 'S:/something/something/old_file_path/your_reference_file.ma'
new_path = "D:/something/something/more_stuff/your_reference_file.ma"

def remap_the_files(path, old_ref, new_ref):
    list_of_items = os.listdir(path)
    for item in list_of_items:
        if os.path.isdir(os.path.join(path, item)): # Checks to see if this item is a folder (dir)
            remap_the_files(os.path.join(path, item), old_path, new_path)
        else:
            if item.endswith('.ma'):                # Checks to see if this item is a maya ascii file
                print(os.path.join(path, item))
                filepath = os.path.join(path, item)
                with open(filepath, "r") as read_stream:
                    # Get all file contents in a list.
                    fileLines = read_stream.readlines()
                
                    # Use enumerate to keep track of what index we're at.
                    for i, line in enumerate(fileLines):
                        # Check if the line has the old path in it.
                        if old_ref in line:
                            # Replace with the new path and assign the change.
                            fileLines[i] = line.replace(old_ref, new_ref)
                # Completely replace the file with our changes.
                with open(filepath, 'w') as write_stream:
                    # You must pass the contents in here to write it.
                    write_stream.writelines(fileLines)

remap_the_files(your_folder_path, old_path, new_path)
print "Job Done..."

Thanks everyone for the help.

1 Like