Check if there's a certain object in a Maya scene Ascii file without opening it

I need to find out how I can get a list of all objects in an Ascii file without opening it. I know how to go through a list to find the name of the object and confirm it exists but I don’t know how to get that list through python.

What is the basic method of doing that?

If you’re using Maya 2020 or earlier you could parse the Maya file with mottosso/maya-scenefile-parser: An Experimental Batch Scene Parser (github.com)

I would prefer to do it without using any add-ons

You can use regex.

Here’s a quick function that takes a path file of a .ma scene and a node to check for its existence:

import re

def object_exists_on_file(path, object_name):

    object_exists = False
    regex = re.compile('^createNode.+\"({0})\";$'.format(object_name))
    
    with open(path, 'r') as file:
        for line in file:
            if regex.match(line) is not None:
                return True

    return False
            
print object_exists_on_file(file_path, "persp")
# True

You may want to use a regex site to check if your regular expression works against the scenes you have.

There’s also some optimization that could be done but hope you get the general idea of it.

1 Like

Thanks, I’m going to take the morning tomorrow to figure this all out, I’m very green on what regex can do but this looks promising. Out of curiosity can this method also find objects that were referenced in the scene?

I just tested it out, and it looks like Nope.
To do something like that, I’m guessing you’d have to look for lines that start with "file -r " (note the space after the “r”), then get the referenced filepath from that line, and then read the referenced file.

1 Like

If you’re having to deal with multiple or nested references then it gets to the point where you might want to just use mayapy.exe (headless Maya) and with the maya.standalone lib, you can use Maya commands as normal to open the file (which will also load references) and query the objects with cmds.ls.

1 Like

I see, thanks everybody this was a big help.